Add unit tests

This commit is contained in:
Joseph Johansen
2024-08-07 17:38:02 +01:00
committed by Konrad
parent b73e529bfc
commit 1b8e256e8a
5 changed files with 204 additions and 66 deletions

View File

@@ -10,8 +10,6 @@ module InjectionHelper
def inject_enterprises(enterprises = nil)
enterprises ||= default_enterprise_query
# make sure the query is performed now so it is only performed once
enterprises = enterprises.to_a
inject_json_array(
"enterprises",
enterprises,
@@ -56,8 +54,7 @@ module InjectionHelper
relatives_including_self.
activated.
includes(:properties, address: [:state, :country], supplied_products: :properties).
all.
to_a
all
inject_json_array "enterprises",
enterprises_and_relatives,
@@ -66,7 +63,7 @@ module InjectionHelper
end
def inject_group_enterprises(group)
enterprises = group.enterprises.activated.visible.all.to_a
enterprises = group.enterprises.activated.visible.all
inject_json_array(
"enterprises",
enterprises,
@@ -97,11 +94,11 @@ module InjectionHelper
end
def inject_taxons
inject_json_array "taxons", Spree::Taxon.all.to_a, Api::TaxonSerializer
inject_json_array "taxons", Spree::Taxon.all, Api::TaxonSerializer
end
def inject_properties
inject_json_array "properties", Spree::Property.all.to_a, Api::PropertySerializer
inject_json_array "properties", Spree::Property.all, Api::PropertySerializer
end
def inject_currency_config

View File

@@ -16,39 +16,60 @@ RSpec.describe InjectionHelper, type: :helper do
}
let!(:d2o1) { create(:completed_order_with_totals, distributor: distributor2, user_id: user.id) }
let(:sm) { create(:shipping_method) }
let(:pm) { create(:payment_method) }
let(:distributor) {
create(:distributor_enterprise, shipping_methods: [sm], payment_methods: [pm])
}
let(:order) { create(:order, distributor:) }
before do
allow_any_instance_of(EnterprisesHelper).to receive(:current_distributor).and_return distributor
allow_any_instance_of(EnterprisesHelper).to receive(:current_order).and_return order
end
it "will inject via AMS" do
expect(helper.inject_json_array("test", [enterprise],
Api::IdSerializer)).to match /#{enterprise.id}/
end
it "injects enterprises" do
expect(helper.inject_enterprises).to match enterprise.name
expect(helper.inject_enterprises).to match enterprise.facebook
describe "#inject_enterprises" do
it "injects enterprises" do
expect(helper.inject_enterprises).to match enterprise.name
expect(helper.inject_enterprises).to match enterprise.facebook
end
it "only injects activated enterprises" do
inactive_enterprise = create(:enterprise, sells: 'unspecified')
expect(helper.inject_enterprises).not_to match inactive_enterprise.name
end
end
it "only injects activated enterprises" do
inactive_enterprise = create(:enterprise, sells: 'unspecified')
expect(helper.inject_enterprises).not_to match inactive_enterprise.name
describe "#inject_enterprise_and_relatives" do
let(:child) { create :distributor_enterprise }
let!(:relationship) { create :enterprise_relationship, parent: distributor, child: }
it "injects the current distributor and its relatives" do
expect(helper.inject_enterprise_and_relatives).to match distributor.name
expect(helper.inject_enterprise_and_relatives).to match child.name
end
end
it "injects shipping_methods" do
sm = create(:shipping_method)
current_distributor = create(:distributor_enterprise, shipping_methods: [sm])
order = create(:order, distributor: current_distributor)
allow(helper).to receive(:current_order) { order }
allow(helper).to receive(:spree_current_user) { nil }
describe "#inject_group_enterprises" do
let(:group) { create :enterprise_group, enterprises: [enterprise] }
it "injects an enterprise group's enterprises" do
expect(helper.inject_group_enterprises(group)).to match enterprise.name
end
end
it "injects payment methods" do
pm = create(:payment_method)
current_distributor = create(:distributor_enterprise, payment_methods: [pm])
order = create(:order, distributor: current_distributor)
allow(helper).to receive(:current_order) { order }
allow(helper).to receive(:spree_current_user) { nil }
describe "#inject_current_hub" do
it "injects the current distributor" do
expect(helper.inject_current_hub).to match distributor.name
end
end
it "injects current order" do
allow(helper).to receive(:current_order).and_return order = create(:order)
expect(helper.inject_current_order).to match order.id.to_s
end

View File

@@ -0,0 +1,47 @@
# frozen_string_literal: true
require 'spec_helper'
require 'open_food_network/enterprise_injection_data'
RSpec.describe OpenFoodNetwork::EnterpriseInjectionData do
let(:enterprise1) { create :distributor_enterprise, with_payment_and_shipping: true }
let(:enterprise2) { create :distributor_enterprise, with_payment_and_shipping: true }
let(:enterprise3) { create :distributor_enterprise, with_payment_and_shipping: true }
let(:enterprise4) { create :distributor_enterprise, with_payment_and_shipping: true }
before do
[enterprise1, enterprise2, enterprise3].each do |ent|
create :open_order_cycle, distributors: [ent]
end
end
let!(:closed_oc) { create :closed_order_cycle, coordinator: enterprise4 }
context "when scoped to specific enterprises" do
let(:subject) {
described_class.new([enterprise1.id, enterprise2.id])
}
describe "#active_distributor_ids" do
it "should include enterprise1.id and enterprise2.id" do
ids = subject.active_distributor_ids
expect(ids).to include enterprise1.id
expect(ids).to include enterprise2.id
expect(ids).not_to include enterprise3.id
end
end
end
context "when unscoped to specific enterprises" do
let(:subject) { described_class.new }
describe "#active_distributor_ids" do
it "should include all enterprise ids" do
ids = subject.active_distributor_ids
expect(ids).to include enterprise1.id
expect(ids).to include enterprise2.id
expect(ids).to include enterprise3.id
end
end
end
end

View File

@@ -423,6 +423,25 @@ RSpec.describe OrderCycle do
it "returns the earliest closing time" do
expect(OrderCycle.earliest_closing_times[e2.id].round).to eq(time2.round)
end
context "when scoped by distributors" do
it "returns times for the given distributors" do
expect(OrderCycle.earliest_closing_times([e1.id])).to have_key e1.id
expect(OrderCycle.earliest_closing_times([e2.id])).to have_key e2.id
end
it "doesn't return times for other distributors" do
expect(OrderCycle.earliest_closing_times([e1.id])).not_to have_key e2.id
expect(OrderCycle.earliest_closing_times([e2.id])).not_to have_key e1.id
end
end
context "when not scoped by distributors" do
it "returns times for all distributors" do
expect(OrderCycle.earliest_closing_times).to have_key e1.id
expect(OrderCycle.earliest_closing_times).to have_key e2.id
end
end
end
describe "finding all line items sold by to a user by a given shop" do

View File

@@ -2,54 +2,108 @@
require 'spec_helper'
module Spree
RSpec.describe Taxon do
let(:taxon) { Spree::Taxon.new(name: "Ruby on Rails") }
RSpec.describe Spree::Taxon do
let(:taxon) { described_class.new(name: "Ruby on Rails") }
let(:e) { create(:supplier_enterprise) }
let(:t1) { create(:taxon) }
let(:t2) { create(:taxon) }
let(:e) { create(:supplier_enterprise) }
let(:e2) { create(:supplier_enterprise) }
let(:t1) { create(:taxon) }
let(:t2) { create(:taxon) }
describe "finding all supplied taxons" do
let!(:p1) {
create(:simple_product, primary_taxon_id: t1.id, supplier_id: e.id)
}
describe ".supplied_taxons" do
let!(:p1) {
create(:simple_product, primary_taxon_id: t1.id, supplier_id: e.id)
}
let!(:p2) {
create(:simple_product, primary_taxon_id: t2.id, supplier_id: e2.id)
}
context "when scoped to specific enterprises" do
it "finds taxons" do
expect(Taxon.supplied_taxons).to eq(e.id => Set.new([t1.id]))
expect(described_class.supplied_taxons([e.id])).to eq(e.id => Set.new([t1.id]))
expect(described_class.supplied_taxons([e2.id])).to eq(e2.id => Set.new([t2.id]))
expect(described_class.supplied_taxons([e.id, e2.id])).to eq(
e.id => Set.new([t1.id]),
e2.id => Set.new([t2.id])
)
end
end
describe "finding distributed taxons" do
let!(:oc_open) {
create(:open_order_cycle, distributors: [e], variants: [p_open.variants.first])
}
let!(:oc_closed) {
create(:closed_order_cycle, distributors: [e], variants: [p_closed.variants.first])
}
let!(:p_open) { create(:simple_product, primary_taxon: t1) }
let!(:p_closed) { create(:simple_product, primary_taxon: t2) }
it "finds all distributed taxons" do
expect(Taxon.distributed_taxons(:all)).to eq(e.id => Set.new([t1.id, t2.id]))
end
it "finds currently distributed taxons" do
expect(Taxon.distributed_taxons(:current)).to eq(e.id => Set.new([t1.id]))
end
end
describe "touches" do
let!(:taxon1) { create(:taxon) }
let!(:taxon2) { create(:taxon) }
let!(:product) { create(:simple_product, primary_taxon_id: taxon1.id) }
let(:variant) { product.variants.first }
it "is touched when assignment of primary_taxon on a variant changes" do
expect do
variant.update(primary_taxon: taxon2)
end.to change { taxon2.reload.updated_at }
context "when not scoped to specific enterprises" do
it "finds taxons" do
expect(described_class.supplied_taxons).to eq(
e.id => Set.new([t1.id]),
e2.id => Set.new([t2.id])
)
end
end
end
describe ".distributed_taxons" do
before do
[e, e2].each do |ent|
p_open = create(:simple_product, primary_taxon: t1)
p_closed = create(:simple_product, primary_taxon: t2)
create(:open_order_cycle, distributors: [ent], variants: [p_open.variants.first])
create(:closed_order_cycle, distributors: [ent], variants: [p_closed.variants.first])
end
end
context "when scoped to specific enterprises" do
it "finds all distributed taxons" do
expect(described_class.distributed_taxons(:all, [e.id])).to eq(
e.id => Set.new([t1.id, t2.id])
)
expect(described_class.distributed_taxons(:all, [e2.id])).to eq(
e2.id => Set.new([t1.id, t2.id])
)
expect(described_class.distributed_taxons(:all, [e.id, e2.id])).to eq(
e.id => Set.new([t1.id, t2.id]),
e2.id => Set.new([t1.id, t2.id]),
)
end
it "finds currently distributed taxons" do
expect(described_class.distributed_taxons(:current, [e.id])).to eq(
e.id => Set.new([t1.id])
)
expect(described_class.distributed_taxons(:current, [e2.id])).to eq(
e2.id => Set.new([t1.id])
)
expect(described_class.distributed_taxons(:current, [e.id, e2.id])).to eq(
e.id => Set.new([t1.id]),
e2.id => Set.new([t1.id]),
)
end
end
context "when not scoped to specific enterprises" do
it "finds all distributed taxons" do
expect(described_class.distributed_taxons(:all)).to eq(
e.id => Set.new([t1.id, t2.id]),
e2.id => Set.new([t1.id, t2.id]),
)
end
it "finds currently distributed taxons" do
expect(described_class.distributed_taxons(:current)).to eq(
e.id => Set.new([t1.id]),
e2.id => Set.new([t1.id]),
)
end
end
end
describe "touches" do
let!(:taxon1) { create(:taxon) }
let!(:taxon2) { create(:taxon) }
let!(:product) { create(:simple_product, primary_taxon_id: taxon1.id) }
let(:variant) { product.variants.first }
it "is touched when assignment of primary_taxon on a variant changes" do
expect do
variant.update(primary_taxon: taxon2)
end.to change { taxon2.reload.updated_at }
end
end
end