From d9899e8af61b6134b5e0b14a8ce280930bbfda22 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 8 Aug 2023 12:10:05 +0100 Subject: [PATCH] Update more specs --- spec/controllers/api/v0/products_controller_spec.rb | 7 ++++--- spec/controllers/api/v0/variants_controller_spec.rb | 3 ++- .../spree/admin/products_controller_spec.rb | 4 +++- .../spree/admin/variants_controller_spec.rb | 4 +++- .../reports/products_and_inventory_report_spec.rb | 2 +- spec/models/spree/product_spec.rb | 7 +------ spec/models/spree/taxon_spec.rb | 6 +++--- spec/services/products_renderer_spec.rb | 6 ------ spec/system/admin/bulk_product_update_spec.rb | 3 --- spec/system/admin/products_spec.rb | 2 +- spec/system/admin/reports_spec.rb | 12 ++++++------ spec/system/admin/variants_spec.rb | 4 ++++ spec/system/consumer/caching/shops_caching_spec.rb | 5 +++-- 13 files changed, 31 insertions(+), 34 deletions(-) diff --git a/spec/controllers/api/v0/products_controller_spec.rb b/spec/controllers/api/v0/products_controller_spec.rb index af6dbfe032..96f22a9fe8 100644 --- a/spec/controllers/api/v0/products_controller_spec.rb +++ b/spec/controllers/api/v0/products_controller_spec.rb @@ -25,6 +25,7 @@ describe Api::V0::ProductsController, type: :controller do end context "as a normal user" do + let(:taxon) { create(:taxon) } let(:attachment) { fixture_file_upload("thinking-cat.jpg") } before do @@ -34,9 +35,10 @@ describe Api::V0::ProductsController, type: :controller do it "gets a single product" do product.create_image!(attachment:) - product.variants.create!(unit_value: "1", unit_description: "thing", price: 1) + product.variants.create!(unit_value: "1", unit_description: "thing", price: 1, primary_taxon: taxon) product.variants.first.images.create!(attachment:) product.set_property("spree", "rocks") + api_get :show, id: product.to_param expect(all_attributes.all?{ |attr| json_response.keys.include? attr }).to eq(true) @@ -117,8 +119,7 @@ describe Api::V0::ProductsController, type: :controller do expect(response.status).to eq(422) expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.") errors = json_response["errors"] - expect(errors.keys).to match_array(["name", "primary_taxon", "supplier", "variant_unit", - "price"]) + expect(errors.keys).to match_array(["name", "supplier", "variant_unit", "price"]) end it "can update a product" do diff --git a/spec/controllers/api/v0/variants_controller_spec.rb b/spec/controllers/api/v0/variants_controller_spec.rb index 9272b0b522..1a90dd704a 100644 --- a/spec/controllers/api/v0/variants_controller_spec.rb +++ b/spec/controllers/api/v0/variants_controller_spec.rb @@ -127,6 +127,7 @@ describe Api::V0::VariantsController, type: :controller do let(:product) { create(:product) } let(:variant) { product.variants.first } + let(:taxon) { create(:taxon) } let!(:variant2) { create(:variant, product:) } context "deleted variants" do @@ -144,7 +145,7 @@ describe Api::V0::VariantsController, type: :controller do it "can create a new variant" do original_number_of_variants = variant.product.variants.count api_post :create, variant: { sku: "12345", unit_value: "1", - unit_description: "L", price: "1" }, + unit_description: "L", price: "1", primary_taxon_id: taxon.id }, product_id: variant.product.id expect(attributes.all?{ |attr| json_response.include? attr.to_s }).to eq(true) diff --git a/spec/controllers/spree/admin/products_controller_spec.rb b/spec/controllers/spree/admin/products_controller_spec.rb index 2fcb53ae2a..9dbf146f53 100644 --- a/spec/controllers/spree/admin/products_controller_spec.rb +++ b/spec/controllers/spree/admin/products_controller_spec.rb @@ -93,6 +93,7 @@ describe Spree::Admin::ProductsController, type: :controller do variant_unit_name: nil ) end + let!(:taxon) { create(:taxon) } before { controller_login_as_enterprise_user([producer]) } @@ -111,7 +112,8 @@ describe Spree::Admin::ProductsController, type: :controller do "price" => "5.0", "unit_value" => 4, "unit_description" => "", - "display_name" => "name" + "display_name" => "name", + "primary_taxon_id" => taxon.id } ] } diff --git a/spec/controllers/spree/admin/variants_controller_spec.rb b/spec/controllers/spree/admin/variants_controller_spec.rb index b3b05d8afc..7bdfc7bcbe 100644 --- a/spec/controllers/spree/admin/variants_controller_spec.rb +++ b/spec/controllers/spree/admin/variants_controller_spec.rb @@ -11,7 +11,9 @@ module Spree describe "deleted variants" do let(:product) { create(:product, name: 'Product A') } let(:deleted_variant) do - deleted_variant = product.variants.create(unit_value: "2", price: 1) + deleted_variant = product.variants.create( + unit_value: "2", price: 1, primary_taxon: create(:taxon) + ) deleted_variant.delete deleted_variant end diff --git a/spec/lib/reports/products_and_inventory_report_spec.rb b/spec/lib/reports/products_and_inventory_report_spec.rb index f2449f56f0..8096fa46b1 100644 --- a/spec/lib/reports/products_and_inventory_report_spec.rb +++ b/spec/lib/reports/products_and_inventory_report_spec.rb @@ -43,7 +43,7 @@ module Reporting allow(variant).to receive_message_chain(:product, :name).and_return("Product Name") allow(variant).to receive_message_chain(:product, :properties) .and_return [double(name: "property1"), double(name: "property2")] - allow(variant).to receive_message_chain(:product, :primary_taxon). + allow(variant).to receive_message_chain(:primary_taxon). and_return double(name: "taxon1") allow(variant).to receive_message_chain(:product, :group_buy_unit_size).and_return(21) allow(subject).to receive(:query_result).and_return [variant] diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 49fc233147..82bddc66db 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -155,7 +155,6 @@ module Spree describe "associations" do it { is_expected.to belong_to(:supplier).required } - it { is_expected.to belong_to(:primary_taxon).required } end describe "validations and defaults" do @@ -167,10 +166,6 @@ module Spree it { is_expected.to validate_length_of(:name).is_at_most(255) } it { is_expected.to validate_length_of(:sku).is_at_most(255) } - it "requires a primary taxon" do - expect(build(:simple_product, primary_taxon: nil)).not_to be_valid - end - context "unit value" do it "requires a unit value when variant unit is weight" do expect(build(:simple_product, variant_unit: 'weight', variant_unit_name: 'name', @@ -232,7 +227,7 @@ module Spree before do create(:stock_location) - product.primary_taxon = create(:taxon) + product.primary_taxon_id = create(:taxon).id product.supplier = create(:supplier_enterprise) product.name = "Product1" product.variant_unit = "weight" diff --git a/spec/models/spree/taxon_spec.rb b/spec/models/spree/taxon_spec.rb index edbc968585..7f481413df 100644 --- a/spec/models/spree/taxon_spec.rb +++ b/spec/models/spree/taxon_spec.rb @@ -41,11 +41,11 @@ module Spree let!(:taxon1) { create(:taxon) } let!(:taxon2) { create(:taxon) } let!(:product) { create(:simple_product, primary_taxon: taxon1) } + let(:variant) { product.variants.first } - it "is touched when assignment of primary_taxon on a product changes" do + it "is touched when assignment of primary_taxon on a variant changes" do expect do - product.primary_taxon = taxon2 - product.save + variant.update(primary_taxon: taxon2) end.to change { taxon2.reload.updated_at } end end diff --git a/spec/services/products_renderer_spec.rb b/spec/services/products_renderer_spec.rb index e14a6832d0..179233fe2d 100644 --- a/spec/services/products_renderer_spec.rb +++ b/spec/services/products_renderer_spec.rb @@ -162,12 +162,6 @@ describe ProductsRenderer do expect(products_renderer.products_json).to include "998.0" end - it "includes the primary taxon" do - taxon = create(:taxon) - allow_any_instance_of(Spree::Product).to receive(:primary_taxon).and_return taxon - expect(products_renderer.products_json).to include taxon.name - end - it "loads tag_list for variants" do VariantOverride.create(variant:, hub: distributor, tag_list: 'lalala') expect(products_renderer.products_json).to include "[\"lalala\"]" diff --git a/spec/system/admin/bulk_product_update_spec.rb b/spec/system/admin/bulk_product_update_spec.rb index 9475b3a9cb..1f954dc142 100644 --- a/spec/system/admin/bulk_product_update_spec.rb +++ b/spec/system/admin/bulk_product_update_spec.rb @@ -344,7 +344,6 @@ describe ' within "tr#p_#{p.id}" do expect(page).to have_field "product_name", with: p.name expect(page).to have_select "producer_id", selected: s1.name - expect(page).to have_select2 "p#{p.id}_category_id", selected: t2.name expect(page).to have_select "variant_unit_with_scale", selected: "Volume (L)" expect(page).to have_checked_field "inherits_properties" expect(page).to have_field "product_sku", with: p.sku @@ -352,7 +351,6 @@ describe ' fill_in "product_name", with: "Big Bag Of Potatoes" select s2.name, from: 'producer_id' select "Weight (kg)", from: "variant_unit_with_scale" - select2_select t1.name, from: "p#{p.id}_category_id" uncheck "inherits_properties" fill_in "product_sku", with: "NEW SKU" end @@ -365,7 +363,6 @@ describe ' expect(p.supplier).to eq s2 expect(p.variant_unit).to eq "weight" expect(p.variant_unit_scale).to eq 1000 # Kg - expect(p.primary_taxon.permalink).to eq t1.permalink expect(p.inherits_properties).to be false expect(p.sku).to eq "NEW SKU" end diff --git a/spec/system/admin/products_spec.rb b/spec/system/admin/products_spec.rb index 8f497f086f..55e15a3e41 100644 --- a/spec/system/admin/products_spec.rb +++ b/spec/system/admin/products_spec.rb @@ -143,7 +143,7 @@ describe ' expect(product.variants.first.unit_value).to eq(5000) expect(product.variants.first.unit_description).to eq("") expect(product.variant_unit_name).to eq("") - expect(product.primary_taxon_id).to eq(taxon.id) + expect(product.variants.first.primary_taxon_id).to eq(taxon.id) expect(product.variants.first.price.to_s).to eq('19.99') expect(product.on_hand).to eq(5) expect(product.variants.first.tax_category_id).to eq(tax_category.id) diff --git a/spec/system/admin/reports_spec.rb b/spec/system/admin/reports_spec.rb index 472ce6a333..abe26647c7 100644 --- a/spec/system/admin/reports_spec.rb +++ b/spec/system/admin/reports_spec.rb @@ -358,15 +358,15 @@ describe ' let(:taxon) { create(:taxon, name: 'Taxon Name') } let(:product1) { create(:simple_product, name: "Product Name", price: 100, supplier:, - primary_taxon: taxon) + primary_taxon_id: taxon.id) } let(:product2) { create(:simple_product, name: "Product 2", price: 99.0, variant_unit: 'weight', variant_unit_scale: 1, unit_value: '100', supplier:, - primary_taxon: taxon, sku: "product_sku") + primary_taxon_id: taxon.id, sku: "product_sku") } let(:variant1) { product1.variants.first } - let(:variant2) { create(:variant, product: product1, price: 80.0) } + let(:variant2) { create(:variant, product: product1, price: 80.0, primary_taxon: taxon) } let(:variant3) { product2.variants.first } before do @@ -396,17 +396,17 @@ describe ' expect(page).to have_table_row [product1.supplier.name, product1.supplier.address.city, "Product Name", product1.properties.map(&:presentation).join(", "), - product1.primary_taxon.name, "1g", "100.0", + taxon.name, "1g", "100.0", "none", "", "sku1", "No", "10"] expect(page).to have_table_row [product1.supplier.name, product1.supplier.address.city, "Product Name", product1.properties.map(&:presentation).join(", "), - product1.primary_taxon.name, "1g", "80.0", + taxon.name, "1g", "80.0", "none", "", "sku2", "No", "20"] expect(page).to have_table_row [product2.supplier.name, product1.supplier.address.city, "Product 2", product1.properties.map(&:presentation).join(", "), - product2.primary_taxon.name, "100g", "99.0", + taxon.name, "100g", "99.0", "none", "", "product_sku", "No", "9"] end diff --git a/spec/system/admin/variants_spec.rb b/spec/system/admin/variants_spec.rb index e8487959c3..754670b542 100644 --- a/spec/system/admin/variants_spec.rb +++ b/spec/system/admin/variants_spec.rb @@ -9,6 +9,8 @@ describe ' include AuthenticationHelper include WebHelper + let!(:taxon) { create(:taxon) } + describe "new variant" do it "creating a new variant" do # Given a product with a unit-related option type @@ -21,6 +23,7 @@ describe ' fill_in 'unit_value_human', with: '1' fill_in 'variant_unit_description', with: 'foo' + select taxon.name, from: "variant_primary_taxon_id" click_button 'Create' # Then the variant should have been created @@ -61,6 +64,7 @@ describe ' # Expect variant_weight to accept 3 decimal places fill_in 'variant_weight', with: '1.234' fill_in 'unit_value_human', with: 1 + select taxon.name, from: "variant_primary_taxon_id" click_button 'Create' # Then the variant should have been created diff --git a/spec/system/consumer/caching/shops_caching_spec.rb b/spec/system/consumer/caching/shops_caching_spec.rb index 7c84c6c2f8..b44a8f77bc 100644 --- a/spec/system/consumer/caching/shops_caching_spec.rb +++ b/spec/system/consumer/caching/shops_caching_spec.rb @@ -53,8 +53,9 @@ describe "Shops caching", caching: true do let!(:property) { create(:property, presentation: "Cached Property") } let!(:property2) { create(:property, presentation: "New Property") } let!(:product) { - create(:product, primary_taxon: taxon, properties: [property]) + create(:product, primary_taxon_id: taxon.id, properties: [property]) } + let(:variant) { product.variants.first } let(:exchange) { order_cycle.exchanges.to_enterprises(distributor).outgoing.first } let(:test_domain) { @@ -92,7 +93,7 @@ describe "Shops caching", caching: true do expect(page).to have_content taxon.name expect(page).to have_content property.presentation - product.update_attribute(:primary_taxon, taxon2) + variant.update_attribute(:primary_taxon, taxon2) product.update_attribute(:properties, [property2]) visit enterprise_shop_path(distributor)