Skip to content

Commit

Permalink
Merge pull request #6079 from arku/perf/test-prof-setup
Browse files Browse the repository at this point in the history
Set up test_prof gem
  • Loading branch information
sauloperez authored Oct 6, 2020
2 parents 76def3a + 4858f5f commit 427d1aa
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 38 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ end

group :test do
gem 'simplecov', require: false
gem 'test-prof'
gem 'webmock'
# See spec/spec_helper.rb for instructions
# gem 'perftools.rb'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ GEM
stringex (1.5.1)
stripe (5.25.0)
temple (0.8.2)
test-prof (0.7.5)
test-unit (3.3.6)
power_assert
thor (0.20.3)
Expand Down Expand Up @@ -811,6 +812,7 @@ DEPENDENCIES
state_machine (= 1.2.0)
stringex (~> 1.5.1)
stripe
test-prof
test-unit (~> 3.3)
timecop
truncate_html (= 0.9.2)
Expand Down
4 changes: 2 additions & 2 deletions spec/factories/enterprise_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
promo_image {}
end

owner { FactoryBot.create :user }
owner factory: :user
sequence(:name) { |n| "Enterprise #{n}" }
sells 'any'
description 'enterprise'
long_description '<p>Hello, world!</p><p>This is a paragraph.</p>'
address { FactoryBot.create(:address) }
address

after(:create) do |enterprise, proxy|
proxy.users.each do |user|
Expand Down
8 changes: 4 additions & 4 deletions spec/factories/tag_rule_factory.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
FactoryBot.define do
factory :filter_order_cycles_tag_rule, class: TagRule::FilterOrderCycles do
enterprise { FactoryBot.create :distributor_enterprise }
enterprise factory: :distributor_enterprise
end

factory :filter_shipping_methods_tag_rule, class: TagRule::FilterShippingMethods do
enterprise { FactoryBot.create :distributor_enterprise }
enterprise factory: :distributor_enterprise
end

factory :filter_products_tag_rule, class: TagRule::FilterProducts do
enterprise { FactoryBot.create :distributor_enterprise }
enterprise factory: :distributor_enterprise
end

factory :filter_payment_methods_tag_rule, class: TagRule::FilterPaymentMethods do
enterprise { FactoryBot.create :distributor_enterprise }
enterprise factory: :distributor_enterprise
end

factory :tag_rule, class: TagRule::DiscountOrder do
Expand Down
9 changes: 5 additions & 4 deletions spec/models/order_cycle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
end

describe "checking status" do
let(:oc) { create(:simple_order_cycle) }
let(:oc) { build_stubbed(:simple_order_cycle) }

it "reports status when an order cycle is upcoming" do
Timecop.freeze(oc.orders_open_at - 1.second) do
Expand Down Expand Up @@ -319,7 +319,8 @@
end

it "reports status when an order cycle is undated" do
oc.update!(orders_open_at: nil, orders_close_at: nil)
oc.orders_open_at = nil
oc.orders_close_at = nil

expect(oc).to be_undated
expect(oc).not_to be_dated
Expand All @@ -329,7 +330,7 @@
end

it "reports status when an order cycle is partially dated - opening time only" do
oc.update!(orders_close_at: nil)
oc.orders_close_at = nil

expect(oc).to be_undated
expect(oc).not_to be_dated
Expand All @@ -339,7 +340,7 @@
end

it "reports status when an order cycle is partially dated - closing time only" do
oc.update!(orders_open_at: nil)
oc.orders_open_at = nil

expect(oc).to be_undated
expect(oc).not_to be_dated
Expand Down
6 changes: 3 additions & 3 deletions spec/models/spree/classification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

module Spree
describe Classification do
let!(:product) { create(:simple_product) }
let!(:taxon) { create(:taxon) }
let(:classification) { create(:classification, taxon: taxon, product: product) }
let(:product) { build_stubbed(:simple_product) }
let(:taxon) { build_stubbed(:taxon) }

it "won't destroy if classification is the primary taxon" do
classification = Classification.new(taxon: taxon, product: product)
product.primary_taxon = taxon
expect(classification.destroy).to be false
expect(classification.errors.messages[:base]).to eq(["Taxon #{taxon.name} is the primary taxon of #{product.name} and cannot be deleted"])
Expand Down
8 changes: 3 additions & 5 deletions spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe Spree::Order do
describe Spree::Order::Checkout do
let(:order) { Spree::Order.new }

context "with default state machine" do
Expand All @@ -15,8 +15,6 @@

it "has the following transitions" do
transitions.each do |transition|
puts transition.keys.first
puts transition.values.first
transition = Spree::Order.find_transition(from: transition.keys.first,
to: transition.values.first)
expect(transition).to_not be_nil
Expand Down Expand Up @@ -302,12 +300,12 @@ class SubclassedOrder < Spree::Order
end

describe 'event :restart_checkout' do
let(:order) { create(:order) }
let(:order) { build_stubbed(:order) }

context 'when the order is not complete' do
before { allow(order).to receive(:completed?) { false } }

it 'does transition to cart state' do
it 'transitions to cart state' do
expect(order.state).to eq('cart')
end
end
Expand Down
20 changes: 15 additions & 5 deletions spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
end

it "does nothing when the line item is not found" do
p = create(:simple_product)
p = build_stubbed(:simple_product)
subject.set_variant_attributes(p.master, { 'max_quantity' => '3' }.with_indifferent_access)
end
end
Expand Down Expand Up @@ -823,10 +823,14 @@ def advance_to_delivery_state(order)
end

describe '#restart_checkout!' do
let(:order) { build(:order, line_items: [build(:line_item)]) }

context 'when the order is complete' do
before { order.completed_at = Time.zone.now }
let(:order) do
build_stubbed(
:order,
completed_at: Time.zone.now,
line_items: [build_stubbed(:line_item)]
)
end

it 'raises' do
expect { order.restart_checkout! }
Expand All @@ -835,7 +839,13 @@ def advance_to_delivery_state(order)
end

context 'when the is not complete' do
before { order.completed_at = nil }
let(:order) do
build(
:order,
completed_at: nil,
line_items: [build(:line_item)]
)
end

it 'transitions to :cart state' do
order.restart_checkout!
Expand Down
2 changes: 1 addition & 1 deletion spec/models/tag_rule/filter_order_cycles_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe TagRule::FilterOrderCycles, type: :model do
let!(:tag_rule) { create(:filter_order_cycles_tag_rule) }
let!(:tag_rule) { build_stubbed(:filter_order_cycles_tag_rule) }

describe "determining whether tags match for a given exchange" do
context "when the exchange is nil" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/tag_rule/filter_payment_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe TagRule::FilterPaymentMethods, type: :model do
let!(:tag_rule) { create(:filter_payment_methods_tag_rule) }
let!(:tag_rule) { build_stubbed(:filter_payment_methods_tag_rule) }

describe "determining whether tags match for a given payment method" do
context "when the payment method is nil" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/tag_rule/filter_products_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe TagRule::FilterProducts, type: :model do
let!(:tag_rule) { create(:filter_products_tag_rule) }
let!(:tag_rule) { build_stubbed(:filter_products_tag_rule) }

describe "determining whether tags match for a given variant" do
context "when the variant is nil" do
Expand Down
4 changes: 2 additions & 2 deletions spec/models/tag_rule/filter_shipping_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe TagRule::FilterShippingMethods, type: :model do
let!(:tag_rule) { create(:filter_shipping_methods_tag_rule) }
let!(:tag_rule) { build_stubbed(:filter_shipping_methods_tag_rule) }

describe "determining whether tags match for a given shipping method" do
context "when the shipping method is nil" do
Expand All @@ -11,7 +11,7 @@
end

context "when the shipping method is not nil" do
let(:shipping_method) { create(:shipping_method, tag_list: ["member", "local", "volunteer"]) }
let(:shipping_method) { build_stubbed(:shipping_method, tag_list: ["member", "local", "volunteer"]) }

context "when the rule has no preferred shipping method tags specified" do
before { allow(tag_rule).to receive(:preferred_shipping_method_tags) { "" } }
Expand Down
68 changes: 58 additions & 10 deletions spec/models/variant_override_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@

describe "validation" do
describe "ensuring that on_demand and count_on_hand are compatible" do
let(:variant_override) {
build(:variant_override, hub: hub, variant: variant,
on_demand: on_demand, count_on_hand: count_on_hand)
}
let(:variant_override) do
build_stubbed(
:variant_override,
hub: build_stubbed(:distributor_enterprise),
variant: build_stubbed(:variant),
on_demand: on_demand,
count_on_hand: count_on_hand
)
end

context "when using producer stock settings" do
let(:on_demand) { nil }
Expand All @@ -62,7 +67,7 @@
let(:count_on_hand) { 1 }

it "is invalid" do
expect(variant_override.save).to be_falsey
expect(variant_override).not_to be_valid
error_message = I18n.t("using_producer_stock_settings_but_count_on_hand_set",
scope: [i18n_scope_for_error, "count_on_hand"])
expect(variant_override.errors[:count_on_hand]).to eq([error_message])
Expand Down Expand Up @@ -139,15 +144,30 @@
end

describe "with price" do
let(:variant_override) { build_stubbed(:variant_override, variant: variant, hub: hub, price: 12.34) }
let(:variant_override) do
build_stubbed(
:variant_override,
variant: build_stubbed(:variant),
hub: build_stubbed(:distributor_enterprise),
price: 12.34
)
end

it "returns the numeric price" do
expect(variant_override.price).to eq(12.34)
end
end

describe "with nil count on hand" do
let(:variant_override) { build_stubbed(:variant_override, variant: variant, hub: hub, count_on_hand: nil, on_demand: true) }
let(:variant_override) do
build_stubbed(
:variant_override,
variant: build_stubbed(:variant),
hub: build_stubbed(:distributor_enterprise),
count_on_hand: nil,
on_demand: true
)
end

describe "stock_overridden?" do
it "returns false" do
Expand All @@ -164,7 +184,14 @@
end

describe "with count on hand" do
let(:variant_override) { create(:variant_override, variant: variant, hub: hub, count_on_hand: 12) }
let(:variant_override) do
build_stubbed(
:variant_override,
variant: build_stubbed(:variant),
hub: build_stubbed(:distributor_enterprise),
count_on_hand: 12
)
end

it "returns the numeric count on hand" do
expect(variant_override.count_on_hand).to eq(12)
Expand All @@ -177,6 +204,15 @@
end

describe "move_stock!" do
let(:variant_override) do
create(
:variant_override,
variant: variant,
hub: hub,
count_on_hand: 12
)
end

it "does nothing for quantity zero" do
variant_override.move_stock!(0)
expect(variant_override.reload.count_on_hand).to eq(12)
Expand All @@ -196,12 +232,24 @@

describe "checking default stock value is present" do
it "returns true when a default stock level has been set" do
vo = build_stubbed(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 20)
vo = build_stubbed(
:variant_override,
variant: build_stubbed(:variant),
hub: build_stubbed(:distributor_enterprise),
count_on_hand: 12,
default_stock: 20
)
expect(vo.default_stock?).to be true
end

it "returns false when the override has no default stock level" do
vo = build_stubbed(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: nil)
vo = build_stubbed(
:variant_override,
variant: build_stubbed(:variant),
hub: build_stubbed(:distributor_enterprise),
count_on_hand: 12,
default_stock: nil
)
expect(vo.default_stock?).to be false
end
end
Expand Down

0 comments on commit 427d1aa

Please sign in to comment.