<commit_before>require 'spec_helper'

module Spree
  describe OrderPopulator do
    let(:order) { double(:order, id: 123) }
    let(:currency) { double(:currency) }
    let(:params) { {} }
    let(:distributor) { double(:distributor) }
    let(:order_cycle) { double(:order_cycle) }
    let(:op) { OrderPopulator.new(order, currency) }

    context "end-to-end" do
      let(:order) { create(:order, distributor: distributor, order_cycle: order_cycle) }
      let(:distributor) { create(:distributor_enterprise) }
      let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], variants: [v]) }
      let(:op) { OrderPopulator.new(order, nil) }
      let(:v) { create(:variant) }

      describe "populate" do
        it "adds a variant" do
          op.populate({variants: {v.id.to_s => {quantity: '1', max_quantity: '2'}}}, true)
          li = order.find_line_item_by_variant(v)
          li.should be
          li.quantity.should == 1
          li.max_quantity.should == 2
        end

        it "updates a variant's quantity and max quantity" do
          order.add_variant v, 1, 2

          op.populate({variants: {v.id.to_s => {quantity: '2', max_quantity: '3'}}}, true)
          li = order.find_line_item_by_variant(v)
          li.should be
          li.quantity.should == 2
          li.max_quantity.should == 3
        end

        it "removes a variant" do
          order.add_variant v, 1, 2

          op.populate({variants: {}}, true)
          order.line_items(:reload)
          li = order.find_line_item_by_variant(v)
          li.should_not be
        end
      end
    end

    describe "populate" do
      before do
        op.should_receive(:distributor_and_order_cycle).
          and_return([distributor, order_cycle])
      end

      it "checks that distribution can supply all products in the cart" do
        op.should_receive(:distribution_can_supply_products_in_cart).
          with(distributor, order_cycle).and_return(false)

        op.populate(params).should be_false
        op.errors.to_a.should == ["That distributor or order cycle can't supply all the products in your cart. Please choose another."]
      end

      it "locks the order" do
        op.stub(:distribution_can_supply_products_in_cart).and_return(true)
        order.should_receive(:with_lock)
        op.populate(params, true)
      end

      it "attempts cart add with max_quantity" do
        op.stub(:distribution_can_supply_products_in_cart).and_return true
        params = {variants: {"1" => {quantity: 1, max_quantity: 2}}}
        order.stub(:with_lock).and_yield
        op.stub(:varies_from_cart) { true }
        op.should_receive(:attempt_cart_add).with("1", 1, 2).and_return true
        op.populate(params, true)
      end
    end
<commit_msg>Fix broken spec<commit_after>require 'spec_helper'

module Spree
  describe OrderPopulator do
    let(:order) { double(:order, id: 123) }
    let(:currency) { double(:currency) }
    let(:params) { {} }
    let(:distributor) { double(:distributor) }
    let(:order_cycle) { double(:order_cycle) }
    let(:op) { OrderPopulator.new(order, currency) }

    context "end-to-end" do
      let(:order) { create(:order, distributor: distributor, order_cycle: order_cycle) }
      let(:distributor) { create(:distributor_enterprise) }
      let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], variants: [v]) }
      let(:op) { OrderPopulator.new(order, nil) }
      let(:v) { create(:variant) }

      describe "populate" do
        it "adds a variant" do
          op.populate({variants: {v.id.to_s => {quantity: '1', max_quantity: '2'}}}, true)
          li = order.find_line_item_by_variant(v)
          li.should be
          li.quantity.should == 1
          li.max_quantity.should == 2
        end

        it "updates a variant's quantity and max quantity" do
          order.add_variant v, 1, 2

          op.populate({variants: {v.id.to_s => {quantity: '2', max_quantity: '3'}}}, true)
          li = order.find_line_item_by_variant(v)
          li.should be
          li.quantity.should == 2
          li.max_quantity.should == 3
        end

        it "removes a variant" do
          order.add_variant v, 1, 2

          op.populate({variants: {}}, true)
          order.line_items(:reload)
          li = order.find_line_item_by_variant(v)
          li.should_not be
        end
      end
    end

    describe "populate" do
      before do
        op.should_receive(:distributor_and_order_cycle).
          and_return([distributor, order_cycle])
      end

      it "checks that distribution can supply all products in the cart" do
        op.should_receive(:distribution_can_supply_products_in_cart).
          with(distributor, order_cycle).and_return(false)

        op.populate(params).should be_false
        op.errors.to_a.should == ["That distributor or order cycle can't supply all the products in your cart. Please choose another."]
      end

      it "locks the order" do
        op.stub(:distribution_can_supply_products_in_cart).and_return(true)
        order.should_receive(:with_lock)
        op.populate(params, true)
      end

      it "attempts cart add with max_quantity" do
        op.stub(:distribution_can_supply_products_in_cart).and_return true
        params = {variants: {"1" => {quantity: 1, max_quantity: 2}}}
        order.stub(:with_lock).and_yield
        op.stub(:varies_from_cart) { true }
        op.stub(:variants_removed) { [] }
        op.should_receive(:attempt_cart_add).with("1", 1, 2).and_return true
        op.populate(params, true)
      end
    end

{'fraction_non_alphanumeric': 0.11617961511047754, 'fraction_numerical': 0.007483962936564505, 'mean_word_length': 2.640077821011673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2354455', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1732, 'n_words': 395}