<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) }

    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 "empties the order if override is true" do
        op.stub(:distribution_can_supply_products_in_cart).and_return true
        order.stub(:with_lock).and_yield
        order.should_receive(:empty!)
        op.populate(params, true)
      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
        order.should_receive(:empty!)
        params = {variants: {"1" => {quantity: 1, max_quantity: 2}}} 
        order.stub(:with_lock).and_yield
        op.should_receive(:attempt_cart_add).with("1", 1, 2).and_return true
        op.populate(params, true)
      end
    end

    describe "attempt_cart_add" do
      it "performs additional validations" do
        variant = double(:variant)
        quantity = 123
        Spree::Variant.stub(:find).and_return(variant)

        op.should_receive(:check_stock_levels).with(variant, quantity).and_return(true)
        op.should_receive(:check_order_cycle_provided_for).with(variant).and_return(true)
        op.should_receive(:check_variant_available_under_distribution).with(variant).
          and_return(true)
        order.should_receive(:add_variant).with(variant, quantity, nil, currency)

        op.attempt_cart_add(333, quantity.to_s)
      end
    end


    describe "validations" do<commit_msg>Stub scope_to_hub in spec that wasn't expecting it<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) }

    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 "empties the order if override is true" do
        op.stub(:distribution_can_supply_products_in_cart).and_return true
        order.stub(:with_lock).and_yield
        order.should_receive(:empty!)
        op.populate(params, true)
      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
        order.should_receive(:empty!)
        params = {variants: {"1" => {quantity: 1, max_quantity: 2}}} 
        order.stub(:with_lock).and_yield
        op.should_receive(:attempt_cart_add).with("1", 1, 2).and_return true
        op.populate(params, true)
      end
    end

    describe "attempt_cart_add" do
      it "performs additional validations" do
        variant = double(:variant)
        variant.stub(:scope_to_hub)
        quantity = 123
        Spree::Variant.stub(:find).and_return(variant)

        op.should_receive(:check_stock_levels).with(variant, quantity).and_return(true)
        op.should_receive(:check_order_cycle_provided_for).with(variant).and_return(true)
        op.should_receive(:check_variant_available_under_distribution).with(variant).
          and_return(true)
        order.should_receive(:add_variant).with(variant, quantity, nil, currency)

        op.attempt_cart_add(333, quantity.to_s)
      end
    end


    describe "validations" do
{'fraction_non_alphanumeric': 0.10184996950599716, 'fraction_numerical': 0.006098800569221387, 'mean_word_length': 3.2051282051282053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2754023', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1460, 'n_words': 299}