<commit_before>require "spec_helper"

require 'tempfile'

describe UPS::Connection do
  before do
    Excon.defaults[:mock] = true
  end

  after do
    Excon.stubs.clear
  end

  let(:stub_path) { File.expand_path("../../stubs", __FILE__) }
  let(:server) { UPS::Connection.new(test_mode: true) }
  let(:shipper) { {
    company_name: 'Veeqo Limited',
    phone_number: '01792 123456',
    address_line_1: '11 Wind Street',
    city: 'Swansea',
    state: 'Wales',
    postal_code: 'SA1 1DA',
    country: 'GB',
    shipper_number: ENV['UPS_ACCOUNT_NUMBER']
  } }
  let(:ship_to) { {
    company_name: 'Google Inc.',
    phone_number: '0207 031 3000',
    address_line_1: '1 St Giles High Street',
    city: 'London',
    state: 'England',
    postal_code: 'WC2H 8AG',
    country: 'GB'
  } }
  let(:package) { {
    weight: '0.5',
    unit: 'KGS'
  } }

  context "when setting test mode" do
    subject { UPS::Connection.new(test_mode: true) }

    it "should set the uri to the test url" do
      expect(subject.url).to eql UPS::Connection::TEST_URL
    end
  end

  context "when setting live mode" do
    subject { UPS::Connection.new }

    it "should set the uri to the live url" do
      expect(subject.url).to eql UPS::Connection::LIVE_URL
    end
  end

  context "if requesting rates" do
    before do
      Excon.stub({:method => :post}) do |params|
        case params[:path]
        when UPS::Connection::RATE_PATH
          {body: File.read("#{stub_path}/rates_success.xml"), status: 200}
        end
      end
    end

    subject do
      server.rates do |rate_builder|
        rate_builder.add_access_request ENV['UPS_LICENSE_NUMBER'], ENV['UPS_USER_ID'], ENV['UPS_PASSWORD']
        rate_builder.add_shipper shipper
        rate_builder.add_ship_from shipper
        rate_builder.add_ship_to ship_to
        rate_builder.add_package package
      end
    end

    it "should return rates" do
      expect { subject }.not_to raise_error
      expect(subject.rated_shipments).not_to be_empty
    end
  end

  context "if requesting a shipment" do
    before do
      Excon.stub({:method => :post}) do |params|
        case params[:path]
        when UPS::Connection::SHIP_CONFIRM_PATH
          {body: File.read("#{stub_path}/ship_confirm_success.xml"), status: 200}
        when UPS::Connection::SHIP_ACCEPT_PATH
          {body: File.read("#{stub_path}/ship_accept_success.xml"), status: 200}
        end
      end
    end
<commit_msg>Add spec to validate rated_shipments from mock<commit_after>require "spec_helper"

require 'tempfile'

describe UPS::Connection do
  before do
    Excon.defaults[:mock] = true
  end

  after do
    Excon.stubs.clear
  end

  let(:stub_path) { File.expand_path("../../stubs", __FILE__) }
  let(:server) { UPS::Connection.new(test_mode: true) }
  let(:shipper) { {
    company_name: '<NAME>',
    phone_number: '01792 123456',
    address_line_1: '11 Wind Street',
    city: 'Swansea',
    state: 'Wales',
    postal_code: 'SA1 1DA',
    country: 'GB',
    shipper_number: ENV['UPS_ACCOUNT_NUMBER']
  } }
  let(:ship_to) { {
    company_name: 'Google Inc.',
    phone_number: '0207 031 3000',
    address_line_1: '1 St Giles High Street',
    city: 'London',
    state: 'England',
    postal_code: 'WC2H 8AG',
    country: 'GB'
  } }
  let(:package) { {
    weight: '0.5',
    unit: 'KGS'
  } }

  context "when setting test mode" do
    subject { UPS::Connection.new(test_mode: true) }

    it "should set the uri to the test url" do
      expect(subject.url).to eql UPS::Connection::TEST_URL
    end
  end

  context "when setting live mode" do
    subject { UPS::Connection.new }

    it "should set the uri to the live url" do
      expect(subject.url).to eql UPS::Connection::LIVE_URL
    end
  end

  context "if requesting rates" do
    before do
      Excon.stub({:method => :post}) do |params|
        case params[:path]
        when UPS::Connection::RATE_PATH
          {body: File.read("#{stub_path}/rates_success.xml"), status: 200}
        end
      end
    end

    subject do
      server.rates do |rate_builder|
        rate_builder.add_access_request ENV['UPS_LICENSE_NUMBER'], ENV['UPS_USER_ID'], ENV['UPS_PASSWORD']
        rate_builder.add_shipper shipper
        rate_builder.add_ship_from shipper
        rate_builder.add_ship_to ship_to
        rate_builder.add_package package
      end
    end

    it "should return rates" do
      expect { subject }.not_to raise_error
      expect(subject.rated_shipments).not_to be_empty
      expect(subject.rated_shipments).to eql [
        {:service_code=>"11", :service_name=>"UPS Standard", :total=>"25.03"},
        {:service_code=>"65", :service_name=>"UPS Saver", :total=>"45.82"},
        {:service_code=>"54", :service_name=>"Express Plus", :total=>"82.08"},
        {:service_code=>"07", :service_name=>"Express", :total=>"47.77"}
      ]
    end
  end

  context "if requesting a shipment" do
    before do
      Excon.stub({:method => :post}) do |params|
        case params[:path]
        when UPS::Connection::SHIP_CONFIRM_PATH
          {body: File.read("#{stub_path}/ship_confirm_success.xml"), status: 200}
        when UPS::Connection::SHIP_ACCEPT_PATH
          {body: File.read("#{stub_path}/ship_accept_success.xml"), status: 200}
        end
      end
    end

{'fraction_non_alphanumeric': 0.12702906757266894, 'fraction_numerical': 0.020385050962627407, 'mean_word_length': 2.9515287099179717, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6786304', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1784, 'n_words': 426}