From 22f4ae115a7d7970a86670fcb6839bb49bf17178 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 27 Feb 2024 13:41:03 +1100 Subject: [PATCH] Add missing associations and validations specs --- app/models/spree/variant.rb | 2 +- spec/models/spree/variant_spec.rb | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index 40370c3c20..b2ed204dd6 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -25,7 +25,7 @@ module Spree variants_display_name supplier_name).join('_or_')}_cont".freeze - belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product' + belongs_to :product, -> { with_deleted }, required: true, touch: true, class_name: 'Spree::Product' belongs_to :tax_category, class_name: 'Spree::TaxCategory' belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', optional: false belongs_to :primary_taxon, class_name: 'Spree::Taxon', touch: true, optional: false diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 6a3fac9e13..abfea244dd 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -7,9 +7,29 @@ RSpec.describe Spree::Variant do subject(:variant) { build(:variant) } it { is_expected.to have_many :semantic_links } + it { is_expected.to belong_to(:product).required } it { is_expected.to belong_to(:supplier).required } - it { is_expected.to have_many(:supplier_properties) } - # TODO add test for the other associations + it { is_expected.to have_many(:inventory_units) } + it { is_expected.to have_many(:line_items) } + it { is_expected.to have_many(:stock_items) } + it { is_expected.to have_many(:stock_locations).through(:stock_items) } + it { is_expected.to have_many(:images) } + it { is_expected.to have_one(:default_price) } + it { is_expected.to have_many(:prices) } + it { is_expected.to have_many(:exchange_variants) } + it { is_expected.to have_many(:exchanges).through(:exchange_variants) } + it { is_expected.to have_many(:variant_overrides) } + it { is_expected.to have_many(:inventory_items) } + it { is_expected.to have_many(:supplier_properties).through(:supplier) } + + describe "shipping category" do + it "sets a shipping category if none provided" do + variant = build(:variant, shipping_category: nil) + + expect(variant).to be_valid + expect(variant.shipping_category).to_not be_nil + end + end describe "supplier properties" do subject { create(:variant) }