From 6771d2e7bef58cf6b1f7b39d46790fd9abfd2d10 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Sat, 8 Apr 2023 07:19:51 +0100 Subject: [PATCH] test enterprise fees creation with tax category inheritance flag --- ...category_from_per_order_enterprise_fees.rb | 6 +-- spec/models/enterprise_fee_spec.rb | 49 +++++++++++++++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/db/migrate/20230316034707_disable_inherits_tax_category_from_per_order_enterprise_fees.rb b/db/migrate/20230316034707_disable_inherits_tax_category_from_per_order_enterprise_fees.rb index 4771fe826b..07b1e8bf21 100644 --- a/db/migrate/20230316034707_disable_inherits_tax_category_from_per_order_enterprise_fees.rb +++ b/db/migrate/20230316034707_disable_inherits_tax_category_from_per_order_enterprise_fees.rb @@ -2,10 +2,10 @@ class DisableInheritsTaxCategoryFromPerOrderEnterpriseFees < ActiveRecord::Migration[6.1] class EnterpriseFee < ApplicationRecord - has_one :calculator, as: :calculable, class_name: "Spree::Calculator", dependent: :destroy + has_many :calculator, class_name: "Spree::Calculator", foreign_key: :calculable_id end - class Spree + module Spree class Calculator < ApplicationRecord self.table_name = 'spree_calculators' end @@ -18,7 +18,7 @@ class DisableInheritsTaxCategoryFromPerOrderEnterpriseFees < ActiveRecord::Migra end def calculators - Spree::Calculator.where(type: per_order_calculators) + Spree::Calculator.where(type: per_order_calculators, calculable_type: 'EnterpriseFee') end def per_order_calculators diff --git a/spec/models/enterprise_fee_spec.rb b/spec/models/enterprise_fee_spec.rb index 6d579a42dd..ab6f5eda53 100644 --- a/spec/models/enterprise_fee_spec.rb +++ b/spec/models/enterprise_fee_spec.rb @@ -10,17 +10,46 @@ describe EnterpriseFee do describe "validations" do it { is_expected.to validate_presence_of(:name) } - it "requires a per-item calculator to inherit tax" do - subject = build( - :enterprise_fee, - inherits_tax_category: true, - calculator: Calculator::FlatRate.new - ) + describe "requires a per-item calculator to inherit tax" do + let(:per_order_calculators){ + [ + Calculator::FlatRate, + Calculator::FlexiRate, + Calculator::PriceSack, + ] + } - expect(subject.save).to eq false - expect(subject.errors.full_messages.first).to eq( - "Inheriting the tax categeory requires a per-item calculator." - ) + let(:per_item_calculators){ + [ + Calculator::PerItem, + Calculator::FlatPercentPerItem + ] + } + + it "is valid when inheriting tax and using a per-item calculator" do + per_item_calculators.each do |calculator| + subject = build( + :enterprise_fee, + inherits_tax_category: true, + calculator: calculator.new + ) + expect(subject.save).to eq true + end + end + + it "is invalid when inheriting tax and using a per-order calculator" do + per_order_calculators.each do |calculator| + subject = build( + :enterprise_fee, + inherits_tax_category: true, + calculator: calculator.new + ) + expect(subject.save).to eq false + expect(subject.errors.full_messages.first).to eq( + "Inheriting the tax categeory requires a per-item calculator." + ) + end + end end end