<commit_before>require 'spec_helper'

describe Spree::Admin::ShippingMethodsController, type: :controller do
  include AuthenticationWorkflow

  describe "#update" do
    let(:params) {
      {
        id: shipping_method.id,
        shipping_method: {
          calculator_attributes: {
            id: shipping_method.calculator.id
          }
        }
      }
    }

    describe "flat rate calculator details" do
      let(:shipping_method) { create(:shipping_method_with, :flat_rate) }

      it "updates preferred_amount and preferred_currency" do
        login_as_admin
        params[:shipping_method][:calculator_attributes][:preferred_amount] = 123
        params[:shipping_method][:calculator_attributes][:preferred_currency] = "EUR"

        spree_post :update, params

        expect(shipping_method.reload.calculator.preferred_amount).to eq 123
        expect(shipping_method.reload.calculator.preferred_currency).to eq "EUR"
      end
    end
  end

  describe "#delete" do
    describe "shipping method not referenced by order" do
      let(:shipping_method) { create(:shipping_method) }

      scenario "is soft deleted" do
        login_as_admin
        expect(shipping_method.deleted_at).to be_nil

        spree_delete :destroy, "id" => shipping_method.id

        expect(shipping_method.reload.deleted_at).not_to be_nil
      end
    end

    describe "shipping method referenced by order" do
      let(:order) { create(:order_with_line_items) }

      scenario "is not soft deleted" do
        login_as_admin
        expect(order.shipping_method.deleted_at).to be_nil

        spree_delete :destroy, "id" => order.shipping_method.id

        expect(order.shipping_method.reload.deleted_at).to be_nil
      end
    end
  end
end
<commit_msg>Add more specs to ship method controller<commit_after>require 'spec_helper'

describe Spree::Admin::ShippingMethodsController, type: :controller do
  include AuthenticationWorkflow

  describe "#update" do
    let(:shipping_method) { create(:shipping_method) }
    let(:params) {
      {
        id: shipping_method.id,
        shipping_method: {
          calculator_attributes: {
            id: shipping_method.calculator.id
          }
        }
      }
    }

    before { login_as_admin }

    it "updates preferred_amount and preferred_currency of a FlatRate calculator" do
      shipping_method.calculator = create(:calculator_flat_rate, calculable: shipping_method)
      params[:shipping_method][:calculator_attributes][:preferred_amount] = 123
      params[:shipping_method][:calculator_attributes][:preferred_currency] = "EUR"

      spree_post :update, params

      expect(shipping_method.reload.calculator.preferred_amount).to eq 123
      expect(shipping_method.reload.calculator.preferred_currency).to eq "EUR"
    end

    it "updates preferred_per_kg of a Weight calculator" do
      shipping_method.calculator = create(:weight_calculator, calculable: shipping_method)
      params[:shipping_method][:calculator_attributes][:preferred_per_kg] = 10

      spree_post :update, params

      expect(shipping_method.reload.calculator.preferred_per_kg).to eq 10
    end

    it "updates preferred_flat_percent of a FlatPercentPerItem calculator" do
      shipping_method.calculator = Calculator::FlatPercentPerItem.new(preferred_flat_percent: 20,

                                                                      calculable: shipping_method)
      params[:shipping_method][:calculator_attributes][:preferred_flat_percent] = 30

      spree_post :update, params

      expect(shipping_method.reload.calculator.preferred_flat_percent).to eq 30
    end

    it "updates details of a FlexiRate calculator" do
      shipping_method.calculator = Spree::Calculator::FlexiRate.new(calculable: shipping_method)
      params[:shipping_method][:calculator_attributes][:preferred_first_item] = 10
      params[:shipping_method][:calculator_attributes][:preferred_additional_item] = 20
      params[:shipping_method][:calculator_attributes][:preferred_max_items] = 30

      spree_post :update, params

      expect(shipping_method.reload.calculator.preferred_first_item).to eq 10
      expect(shipping_method.reload.calculator.preferred_additional_item).to eq 20
      expect(shipping_method.reload.calculator.preferred_max_items).to eq 30
    end

    it "updates details of a PriceSack calculator" do
      shipping_method.calculator = Spree::Calculator::PriceSack.new(calculable: shipping_method)
      params[:shipping_method][:calculator_attributes][:preferred_minimal_amount] = 10
      params[:shipping_method][:calculator_attributes][:preferred_normal_amount] = 20
      params[:shipping_method][:calculator_attributes][:preferred_discount_amount] = 30

      spree_post :update, params

      expect(shipping_method.reload.calculator.preferred_minimal_amount).to eq 10
      expect(shipping_method.reload.calculator.preferred_normal_amount).to eq 20
      expect(shipping_method.reload.calculator.preferred_discount_amount).to eq 30
    end
  end

  describe "#delete" do
    describe "shipping method not referenced by order" do
      let(:shipping_method) { create(:shipping_method) }

      scenario "is soft deleted" do
        login_as_admin
        expect(shipping_method.deleted_at).to be_nil

        spree_delete :destroy, "id" => shipping_method.id

        expect(shipping_method.reload.deleted_at).not_to be_nil
      end
    end

    describe "shipping method referenced by order" do
      let(:order) { create(:order_with_line_items) }

      scenario "is not soft deleted" do
        login_as_admin
        expect(order.shipping_method.deleted_at).to be_nil

        spree_delete :destroy, "id" => order.shipping_method.id

        expect(order.shipping_method.reload.deleted_at).to be_nil
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08293186510667584, 'fraction_numerical': 0.007914659325533379, 'mean_word_length': 3.5167055167055166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5563035', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1695, 'n_words': 326}