<commit_before>require 'spec_helper'

module ChannelAdvisor
  describe Order do
    describe ".new" do
      let(:attrs) do
        {
          :number_of_matches=>"2",
          :order_time_gmt=> DateTime.parse("2012-05-16T15:44:27+00:00 ((2456064j,56667s,683000000n),+0s,2299161j)"),
          :last_update_date=> DateTime.parse("2012-05-16T15:44:27+00:00 ((2456064j,56667s,723000000n),+0s,2299161j)"),
          :total_order_amount=>"44.9200",
          :order_state=>"Active",
          :date_cancelled_gmt=>DateTime.new(2012,05,17),
          :order_id=>"14161613",
          :client_order_identifier=>"14161613",
          :seller_order_id=>"EFGH5678",
          :reseller_id=>"999999",
          :buyer_email_address=>"<EMAIL>",
          :email_opt_in=>false,
          :flag_description=>nil,
          :custom_value_list=>{},
          :buyer_ip_address=>"192.168.127.12",
          :transaction_notes=>"Example notes",
          :"@xmlns:q1"=>"http://api.channeladvisor.com/datacontracts/orders",
          :"@xsi:type"=>"q1:OrderResponseDetailComplete"
        }
      end

      subject { Order.new(attrs) }

      its(:created_at)            { should == attrs[:order_time_gmt]          }
      its(:updated_at)            { should == attrs[:last_update_date]        }
      its(:total)                 { should == attrs[:total_order_amount].to_f }
      its(:state)                 { should == attrs[:order_state]             }
      its(:cancelled_on)          { should == attrs[:date_cancelled_gmt]      }
      its(:id)                    { should == attrs[:order_id].to_i           }
      its(:client_id)             { should == attrs[:client_order_identifier] }
      its(:seller_id)             { should == attrs[:seller_order_id]         }
      its(:reseller_id)           { should == attrs[:reseller_id]             }
      its(:buyer_email)           { should == attrs[:buyer_email_address]     }
      its(:email_opt_in)          { should == attrs[:email_opt_in]            }
      its(:flag_description)      { should == attrs[:flag_description]        }
      its(:custom_values)         { should == attrs[:custom_value_list]       }
      its(:buyer_ip_address)      { should == attrs[:buyer_ip_address]        }
      its(:transaction_notes)     { should == attrs[:transaction_notes]       }

      context "with shipping info" do
        let(:attrs) do
          {
            :shipping_info=> {
              :shipping_instructions => "do some stuff",
              :estimated_ship_date => DateTime.new(2012,05,18),
              :delivery_date => DateTime.new(2012,05,21),
              :shipment_list => {
                :shipment => {:shipping_carrier => "UPS", :shipping_class => "GND", :tracking_number => "1234567890"}
              }
            }
          }
        end

        its(:shipping_instructions) { should == attrs[:shipping_info][:shipping_instructions]   }
        its(:estimated_ship_date)   { should == attrs[:shipping_info][:estimated_ship_date]     }
        its(:delivery_date)         { should == attrs[:shipping_info][:delivery_date]           }

        context "with one shipment" do
          before(:each) do
            stub.proxy(Shipment).new
            @order = Order.new(attrs)
          end

          it "creates a new shipment object from the attributes hash" do
            shipment = attrs[:shipping_info][:shipment_list][:shipment]
            Shipment.should have_received.new(shipment)
          end

          it "creates a @shipments collection with one shipment" do
            @order.shipments.should have(1).shipment
            @order.shipments.first.should be_a Shipment
          end
        end

        context "with two shipments" do
          before(:each) do
            @shipments = attrs[:shipping_info][:shipment_list] = {
              :shipment => [
                {:shipping_carrier => "FedEx", :shipping_class => "Home Delivery", :tracking_number => "999999"},
                {:shipping_carrier => "UPS", :shipping_class => "Ground", :tracking_number => "1234567"}
              ]
            }
            stub.proxy(Shipment).new
            @order = Order.new(attrs)
          end

          it "creates a @shipments collection with two shipments" do
            @order.shipments.should have(2).shipments
            @order.shipments.each do |shipment|
              shipment.should be_a Shipment
            end
          end

          it "creates a new shipment object for each shipment in the attribute hash" do
            @shipments[:shipment].each do |shipment|
              Shipment.should have_received.new(shipment)
            end
          end
        end
      end


      context "with order_status in the attributes hash" do
        let(:attrs) do
          {:order_status => {}}
        end

        its(:status) { should be_an OrderStatus }

        it "sets @status to an OrderStatus object" do
          stub(OrderStatus).new
          Order.new(attrs)
          OrderStatus.should have_received.new(attrs[:order_status])
        end
      end

      context "with payment_info in the attributes hash" do
        let(:attrs) do
          {:payment_info => {}}
        end

        its(:payment) { should be_a Payment }

        it "sets @payment to a Payment object" do
          stub(Payment).new
          Order.new(attrs)
          Payment.should have_received.new(attrs[:payment_info])
        end
      end


      context "with billing_address in the attributes hash" do
        let(:attrs) do
          {:billing_info => {:billing_stuff => nil}}
        end

        its(:billing_address) { should be_an Address }

        it "sets @billing_address to an Address object" do
          stub(Address).new
          order = Order.new(attrs)
          Address.should have_received.new(attrs[:billing_info])
        end
      end

      context "with shipping_address in the attributes hash" do
        let(:attrs) do
          {:shipping_info => {:shipping_stuff => nil}}
        end

        its(:shipping_address) { should be_an Address }

        it "sets @shipping_address to an Address object" do
          stub(Address).new
          order = Order.new(attrs)
          Address.should have_received.new(attrs[:shipping_info])
        end
      end

      context "with shopping_cart in the attributes hash" do
        let(:attrs) do
          {:shopping_cart => {}}
        end

        its(:shopping_cart) { should be_a ShoppingCart }

        it "sets @shopping_cart to a ShoppingCart object" do
          stub(ShoppingCart).new
          Order.new(attrs)
          ShoppingCart.should have_received.new(attrs[:shopping_cart])
        end
      end
    end

    describe "#items_ship_cost" do
      before(:each) do
        @order = Order.new
        @item = Object.new
        stub(@item).shipping_cost { 1.50 }
        stub(@order).shopping_cart.stub!.items { items }
      end

      context "with one item" do
        let(:items) { [@item] }

        it "returns the shipping cost of the order item" do
          @order.items_ship_cost.should == 1.50
        end
      end

      context "with two items" do
        let(:items) { [@item, @item] }

        it "returns the cumulative shipping cost of all order items" do
          @order.items_ship_cost.should == 3.00
        end
      end
    end

    describe "#invoice_ship_cost" do
      before(:each) do
        @order = Order.new
        invoice = Object.new
        stub(invoice).type { "Shipping" }
        stub(invoice).unit_price { unit_price }
        stub(@order).shopping_cart.stub!.invoices { [invoice] }
      end

      context "when shipping cost is 0" do
        let(:unit_price) { 0.00 }

        it "returns nil" do
          @order.invoice_ship_cost.should == nil
        end
      end

      context "when shipping cost is not 0" do<commit_msg>Add Spec for Item with Shipping Cost of Zero<commit_after>require 'spec_helper'

module ChannelAdvisor
  describe Order do
    describe ".new" do
      let(:attrs) do
        {
          :number_of_matches=>"2",
          :order_time_gmt=> DateTime.parse("2012-05-16T15:44:27+00:00 ((2456064j,56667s,683000000n),+0s,2299161j)"),
          :last_update_date=> DateTime.parse("2012-05-16T15:44:27+00:00 ((2456064j,56667s,723000000n),+0s,2299161j)"),
          :total_order_amount=>"44.9200",
          :order_state=>"Active",
          :date_cancelled_gmt=>DateTime.new(2012,05,17),
          :order_id=>"14161613",
          :client_order_identifier=>"14161613",
          :seller_order_id=>"EFGH5678",
          :reseller_id=>"999999",
          :buyer_email_address=>"<EMAIL>",
          :email_opt_in=>false,
          :flag_description=>nil,
          :custom_value_list=>{},
          :buyer_ip_address=>"192.168.127.12",
          :transaction_notes=>"Example notes",
          :"@xmlns:q1"=>"http://api.channeladvisor.com/datacontracts/orders",
          :"@xsi:type"=>"q1:OrderResponseDetailComplete"
        }
      end

      subject { Order.new(attrs) }

      its(:created_at)            { should == attrs[:order_time_gmt]          }
      its(:updated_at)            { should == attrs[:last_update_date]        }
      its(:total)                 { should == attrs[:total_order_amount].to_f }
      its(:state)                 { should == attrs[:order_state]             }
      its(:cancelled_on)          { should == attrs[:date_cancelled_gmt]      }
      its(:id)                    { should == attrs[:order_id].to_i           }
      its(:client_id)             { should == attrs[:client_order_identifier] }
      its(:seller_id)             { should == attrs[:seller_order_id]         }
      its(:reseller_id)           { should == attrs[:reseller_id]             }
      its(:buyer_email)           { should == attrs[:buyer_email_address]     }
      its(:email_opt_in)          { should == attrs[:email_opt_in]            }
      its(:flag_description)      { should == attrs[:flag_description]        }
      its(:custom_values)         { should == attrs[:custom_value_list]       }
      its(:buyer_ip_address)      { should == attrs[:buyer_ip_address]        }
      its(:transaction_notes)     { should == attrs[:transaction_notes]       }

      context "with shipping info" do
        let(:attrs) do
          {
            :shipping_info=> {
              :shipping_instructions => "do some stuff",
              :estimated_ship_date => DateTime.new(2012,05,18),
              :delivery_date => DateTime.new(2012,05,21),
              :shipment_list => {
                :shipment => {:shipping_carrier => "UPS", :shipping_class => "GND", :tracking_number => "1234567890"}
              }
            }
          }
        end

        its(:shipping_instructions) { should == attrs[:shipping_info][:shipping_instructions]   }
        its(:estimated_ship_date)   { should == attrs[:shipping_info][:estimated_ship_date]     }
        its(:delivery_date)         { should == attrs[:shipping_info][:delivery_date]           }

        context "with one shipment" do
          before(:each) do
            stub.proxy(Shipment).new
            @order = Order.new(attrs)
          end

          it "creates a new shipment object from the attributes hash" do
            shipment = attrs[:shipping_info][:shipment_list][:shipment]
            Shipment.should have_received.new(shipment)
          end

          it "creates a @shipments collection with one shipment" do
            @order.shipments.should have(1).shipment
            @order.shipments.first.should be_a Shipment
          end
        end

        context "with two shipments" do
          before(:each) do
            @shipments = attrs[:shipping_info][:shipment_list] = {
              :shipment => [
                {:shipping_carrier => "FedEx", :shipping_class => "Home Delivery", :tracking_number => "999999"},
                {:shipping_carrier => "UPS", :shipping_class => "Ground", :tracking_number => "1234567"}
              ]
            }
            stub.proxy(Shipment).new
            @order = Order.new(attrs)
          end

          it "creates a @shipments collection with two shipments" do
            @order.shipments.should have(2).shipments
            @order.shipments.each do |shipment|
              shipment.should be_a Shipment
            end
          end

          it "creates a new shipment object for each shipment in the attribute hash" do
            @shipments[:shipment].each do |shipment|
              Shipment.should have_received.new(shipment)
            end
          end
        end
      end


      context "with order_status in the attributes hash" do
        let(:attrs) do
          {:order_status => {}}
        end

        its(:status) { should be_an OrderStatus }

        it "sets @status to an OrderStatus object" do
          stub(OrderStatus).new
          Order.new(attrs)
          OrderStatus.should have_received.new(attrs[:order_status])
        end
      end

      context "with payment_info in the attributes hash" do
        let(:attrs) do
          {:payment_info => {}}
        end

        its(:payment) { should be_a Payment }

        it "sets @payment to a Payment object" do
          stub(Payment).new
          Order.new(attrs)
          Payment.should have_received.new(attrs[:payment_info])
        end
      end


      context "with billing_address in the attributes hash" do
        let(:attrs) do
          {:billing_info => {:billing_stuff => nil}}
        end

        its(:billing_address) { should be_an Address }

        it "sets @billing_address to an Address object" do
          stub(Address).new
          order = Order.new(attrs)
          Address.should have_received.new(attrs[:billing_info])
        end
      end

      context "with shipping_address in the attributes hash" do
        let(:attrs) do
          {:shipping_info => {:shipping_stuff => nil}}
        end

        its(:shipping_address) { should be_an Address }

        it "sets @shipping_address to an Address object" do
          stub(Address).new
          order = Order.new(attrs)
          Address.should have_received.new(attrs[:shipping_info])
        end
      end

      context "with shopping_cart in the attributes hash" do
        let(:attrs) do
          {:shopping_cart => {}}
        end

        its(:shopping_cart) { should be_a ShoppingCart }

        it "sets @shopping_cart to a ShoppingCart object" do
          stub(ShoppingCart).new
          Order.new(attrs)
          ShoppingCart.should have_received.new(attrs[:shopping_cart])
        end
      end
    end

    describe "#items_ship_cost" do
      let(:items) { [@item] }

      before(:each) do
        @order = Order.new
        @item = Object.new
        stub(@item).shipping_cost { 1.50 }
        stub(@order).shopping_cart.stub!.items { items }
      end

      context "with one item" do
        it "returns the shipping cost of the order item" do
          @order.items_ship_cost.should == 1.50
        end

        context "when item shipping cost is 0.00" do
          it "returns 0.00" do
            stub(@item).shipping_cost { 0.00 }
            @order.items_ship_cost.should == 0.00
          end
        end
      end

      context "with two items" do
        let(:items) { [@item, @item] }

        it "returns the cumulative shipping cost of all order items" do
          @order.items_ship_cost.should == 3.00
        end
      end
    end

    describe "#invoice_ship_cost" do
      before(:each) do
        @order = Order.new
        invoice = Object.new
        stub(invoice).type { "Shipping" }
        stub(invoice).unit_price { unit_price }
        stub(@order).shopping_cart.stub!.invoices { [invoice] }
      end

      context "when shipping cost is 0" do
        let(:unit_price) { 0.00 }

        it "returns nil" do
          @order.invoice_ship_cost.should == nil
        end
      end

      context "when shipping cost is not 0" do
{'fraction_non_alphanumeric': 0.10982876497522423, 'fraction_numerical': 0.026218403060904474, 'mean_word_length': 1.9449575175471001, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, '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': '114208', 'n_tokens_mistral': 5008, 'n_tokens_neox': 4688, 'n_words': 997}