Skip to content

Commit

Permalink
refactor: lvl 2 difficulty replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticspoon committed Mar 25, 2024
1 parent f056d90 commit 9b29eff
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 272 deletions.
31 changes: 18 additions & 13 deletions spec/models/adjustment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
# user_id :bigint
#

RSpec.describe Adjustment, type: :model do
RSpec.describe Adjustment, type: :model, seed_items: false do
it_behaves_like "itemizable"
# This mixes feature specs with model specs... idealy we do not want to do this
# it_behaves_like "pagination"
#
let(:organization) { create(:organization, skip_items: true) }

context "Validations >" do
it "must belong to an organization" do
Expand All @@ -26,9 +28,9 @@
end

it "allows you to remove all the inventory that exists in the storage location" do
storage_location1 = create(:storage_location, organization: @organization)
storage_location1 = create(:storage_location)
item1 = create(:item)
TestInventory.create_inventory(@organization, {
TestInventory.create_inventory(storage_location1.organization, {
storage_location1.id => {
item1.id => 10
}
Expand All @@ -38,36 +40,39 @@
end

context "Scopes >" do
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }

it "`at_location` can filter out adjustments to a specific location" do
adj1 = create(:adjustment)
create(:adjustment)
expect(Adjustment.at_location(adj1.storage_location_id).size).to eq(1)
end

it "`by_user` can filter out adjustments to a specific user" do
adj1 = create(:adjustment, user_id: @user.id)
create(:adjustment, user_id: @organization_admin.id)
adj1 = create(:adjustment, user_id: user.id)
create(:adjustment, user_id: organization_admin.id)
expect(Adjustment.by_user(adj1.user_id).size).to eq(1)
end
end

context "Methods >" do
it "`self.storage_locations_adjusted_for` returns only storage_locations that are used in adjustments for one org" do
storage_location1 = create(:storage_location, organization: @organization)
storage_location2 = create(:storage_location, organization: @organization)
create(:storage_location, organization: @organization)
storage_location4 = create(:storage_location, organization: create(:organization))
create(:adjustment, storage_location: storage_location1, organization: @organization)
create(:adjustment, storage_location: storage_location2, organization: @organization)
storage_location1 = create(:storage_location, organization: organization)
storage_location2 = create(:storage_location, organization: organization)
create(:storage_location, organization: organization)
storage_location4 = create(:storage_location, organization: create(:organization, skip_items: true))
create(:adjustment, storage_location: storage_location1, organization: organization)
create(:adjustment, storage_location: storage_location2, organization: organization)
create(:adjustment, storage_location: storage_location4, organization: storage_location4.organization)
expect(Adjustment.storage_locations_adjusted_for(@organization).to_a).to match_array([storage_location1, storage_location2])
expect(Adjustment.storage_locations_adjusted_for(organization).to_a).to match_array([storage_location1, storage_location2])
end

describe "split_difference" do
it "returns two adjustment objects" do
item = create(:item)
storage_location = create(:storage_location, :with_items, item: item, item_quantity: 10)
TestInventory.create_inventory(@organization, {
TestInventory.create_inventory(organization, {
storage_location.id => {
create(:item).id => 10
}
Expand Down
43 changes: 24 additions & 19 deletions spec/models/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@

require 'rails_helper'

RSpec.describe Audit, type: :model do
RSpec.describe Audit, type: :model, seed_items: false do
let(:organization) { create(:organization, skip_items: true) }

it_behaves_like "itemizable"

context "Validations >" do
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }

it "must belong to an organization" do
expect(build(:audit, storage_location: create(:storage_location), organization_id: nil)).not_to be_valid
expect(build(:audit, organization: @organization)).to be_valid
expect(build(:audit, organization: organization)).to be_valid
end

it "must belong to an organization admin of its organization" do
expect(build(:audit, storage_location: create(:storage_location, organization: @organization), user: @user, organization: @organization)).not_to be_valid
expect(build(:audit, storage_location: create(:storage_location, organization: @organization), user: @organization_admin, organization: @organization)).to be_valid
expect(build(:audit, storage_location: create(:storage_location, organization: organization), user: user, organization: organization)).not_to be_valid
expect(build(:audit, storage_location: create(:storage_location, organization: organization), user: organization_admin, organization: organization)).to be_valid
end

it "can not have line items that has quantity as negative integer" do
Expand Down Expand Up @@ -101,28 +106,28 @@

context "Methods >" do
it "`self.storage_locations_audited_for` returns only storage_locations that are used for one org" do
storage_location1 = create(:storage_location, organization: @organization)
storage_location2 = create(:storage_location, organization: @organization)
create(:storage_location, organization: @organization)
storage_location4 = create(:storage_location, organization: create(:organization))
create(:audit, storage_location: storage_location1, organization: @organization)
create(:audit, storage_location: storage_location2, organization: @organization)
storage_location1 = create(:storage_location, organization: organization)
storage_location2 = create(:storage_location, organization: organization)
create(:storage_location, organization: organization)
storage_location4 = create(:storage_location, organization: create(:organization, skip_items: true))
create(:audit, storage_location: storage_location1, organization: organization)
create(:audit, storage_location: storage_location2, organization: organization)
create(:audit, storage_location: storage_location4, organization: storage_location4.organization)
expect(Audit.storage_locations_audited_for(@organization).to_a).to match_array([storage_location1, storage_location2])
expect(Audit.storage_locations_audited_for(organization).to_a).to match_array([storage_location1, storage_location2])
end

it "`self.finalized_since?` returns true iff some finalized audit occurred after itemizable created_at that shares item for location(s)" do
storage_location1 = create(:storage_location, :with_items, item_quantity: 10, organization: @organization)
storage_location2 = create(:storage_location, :with_items, item_quantity: 10, organization: @organization)
storage_location3 = create(:storage_location, :with_items, item_quantity: 10, organization: @organization)
storage_location4 = create(:storage_location, organization: @organization)
storage_location5 = create(:storage_location, :with_items, item_quantity: 10, organization: @organization)
storage_location1 = create(:storage_location, :with_items, item_quantity: 10, organization: organization)
storage_location2 = create(:storage_location, :with_items, item_quantity: 10, organization: organization)
storage_location3 = create(:storage_location, :with_items, item_quantity: 10, organization: organization)
storage_location4 = create(:storage_location, organization: organization)
storage_location5 = create(:storage_location, :with_items, item_quantity: 10, organization: organization)

create(:audit, storage_location: storage_location2, status: "finalized", line_items_attributes: [{item_id: storage_location2.items.first.id, quantity: 10}])

xfer1 = create(:transfer, :with_items, item_quantity: 5, item: storage_location1.items.first, from: storage_location1, to: storage_location2, organization: @organization)
xfer2 = create(:transfer, :with_items, item_quantity: 5, item: storage_location1.items.first, from: storage_location1, to: storage_location3, organization: @organization)
xfer3 = create(:transfer, :with_items, item_quantity: 10, item: storage_location2.items.first, from: storage_location2, to: storage_location3, organization: @organization)
xfer1 = create(:transfer, :with_items, item_quantity: 5, item: storage_location1.items.first, from: storage_location1, to: storage_location2, organization: organization)
xfer2 = create(:transfer, :with_items, item_quantity: 5, item: storage_location1.items.first, from: storage_location1, to: storage_location3, organization: organization)
xfer3 = create(:transfer, :with_items, item_quantity: 10, item: storage_location2.items.first, from: storage_location2, to: storage_location3, organization: organization)

create(:audit, storage_location: storage_location1, status: "finalized", line_items_attributes: [{item_id: storage_location1.items.first.id, quantity: 5}])
create(:audit, storage_location: storage_location3, status: "finalized", line_items_attributes: [{item_id: storage_location3.items.first.id, quantity: 10}])
Expand Down
42 changes: 20 additions & 22 deletions spec/models/barcode_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
end
end

RSpec.describe BarcodeItem, type: :model do
RSpec.describe BarcodeItem, type: :model, seed_items: false do
let(:organization) { create(:organization, skip_items: true) }

context "Barcodes of BaseItems ('Global')" do
let(:base_item) { create(:base_item) }
let(:global_barcode_item) { create(:global_barcode_item, barcodeable: base_item) }
Expand Down Expand Up @@ -94,8 +96,7 @@
context "validations >" do
it "is valid with or without an organization" do
expect(build(:global_barcode_item, organization: nil)).to be_valid
org = Organization.try(:first) || create(:organization)
expect(build(:global_barcode_item, organization: org)).to be_valid
expect(build(:global_barcode_item, organization: organization)).to be_valid
end

it "enforces uniqueness in the global scope" do
Expand All @@ -104,7 +105,7 @@
end

it "allows multiple barcodes to point at the same base item" do
base_item = BaseItem.first
base_item = create(:base_item)
create(:global_barcode_item, barcodeable: base_item)
expect(build(:global_barcode_item, barcodeable: base_item)).to be_valid
end
Expand All @@ -114,7 +115,7 @@
end

context "Organization barcodes" do
let(:item) { create(:item) }
let(:item) { create(:item, name: "First Item") }
let(:barcode_item) { create(:barcode_item, barcodeable: item) }

it "updates a counter in Item whenever it tracks a new barcode" do
Expand All @@ -134,14 +135,15 @@
context "scopes >" do
it "->for_csv_export will accept an organization and provide all barcodes for that org" do
barcode_item
create(:barcode_item, organization: create(:organization))
create(:barcode_item, organization: create(:organization, skip_items: true))
results = BarcodeItem.for_csv_export(barcode_item.organization)
expect(results).to eq([barcode_item])
end

it "#by_item_partner_key returns barcodes that match the partner key" do
i1 = create(:item, base_item: BaseItem.first)
i2 = create(:item, base_item: BaseItem.last)
bases = create_list(:base_item, 2)
i1 = create(:item, name: "Item 1", base_item: bases.first)
i2 = create(:item, name: "Item 2", base_item: bases.last)
b1 = create(:barcode_item, barcodeable: i1)
create(:barcode_item, barcodeable: i2)
expect(BarcodeItem.by_item_partner_key(i1.partner_key).first).to eq(b1)
Expand All @@ -156,11 +158,11 @@

context "when searching for a barcode where there is a global and local with the same value" do
let!(:base_item) { create(:base_item, partner_key: "foo", name: "base item") }
let!(:item) { create(:item, partner_key: "foo", name: "custom item", organization: @organization) }
let!(:item) { create(:item, partner_key: "foo", name: "custom item", organization: organization) }
let!(:other_item) { create(:item, partner_key: "foo", name: "other item", organization: create(:organization, skip_items: true)) }

let!(:global) { create(:global_barcode_item, value: "DEADBEEF", barcodeable: base_item) }
let!(:local) { create(:barcode_item, value: "DEADBEEF", barcodeable: item, organization: @organization) }
let!(:local) { create(:barcode_item, value: "DEADBEEF", barcodeable: item, organization: organization) }
let!(:other_local) { create(:barcode_item, value: "DEADBEEF", barcodeable: other_item, organization: other_item.organization) }

it "favors the local barcode" do
Expand All @@ -170,31 +172,27 @@
end

context "validations >" do
it "is valid only with an organization" do
expect(build(:barcode_item, organization: nil)).not_to be_valid
org = Organization.try(:first) || create(:organization)
expect(build(:barcode_item, organization: org)).to be_valid
end
it { should validate_presence_of(:organization) }

it "does not enforces value uniqueness across organizations" do
barcode = create(:barcode_item, value: "DEADBEEF", organization: @organization)
barcode = create(:barcode_item, value: "DEADBEEF", organization: organization)
expect(build(:barcode_item, value: barcode.value, organization: create(:organization, skip_items: true))).to be_valid
end

it "enforces value uniqueness within the organization" do
barcode = create(:barcode_item, value: "DEADBEEF", organization: @organization)
expect(build(:barcode_item, value: barcode.value, organization: @organization)).not_to be_valid
barcode = create(:barcode_item, value: "DEADBEEF", organization: organization)
expect(build(:barcode_item, value: barcode.value, organization: organization)).not_to be_valid
end

it "does not enforce value uniqueness compared with the global scope" do
barcode = create(:global_barcode_item, value: "DEADBEEF")
expect(build(:barcode_item, value: barcode.value, organization: @organization)).to be_valid
expect(build(:barcode_item, value: barcode.value, organization: organization)).to be_valid
end

it "allows multiple barcodes to point at the same item" do
item = create(:item, organization: @organization)
create(:barcode_item, organization: @organization, barcodeable: item)
expect(build(:barcode_item, organization: @organization, barcodeable: item)).to be_valid
item = create(:item, organization: organization)
create(:barcode_item, organization: organization, barcodeable: item)
expect(build(:barcode_item, organization: organization, barcodeable: item)).to be_valid
end

include_examples "common barcode tests", :barcode_item
Expand Down
62 changes: 42 additions & 20 deletions spec/models/base_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,56 @@

require "rails_helper"

RSpec.describe BaseItem, type: :model do
describe "Validations >" do
it "is invalid without a name" do
expect(build(:base_item, name: nil)).not_to be_valid
end
RSpec.describe BaseItem, type: :model, seed_items: false do
let(:organization) { create(:organization, skip_items: true) }

it "is invalid without a unique name" do
f = create(:base_item)
expect(build(:base_item, name: f.name)).not_to be_valid
end

it "is invalid without a partner key" do
expect(build(:base_item, partner_key: nil)).not_to be_valid
end

it "is invalid without a uniqueness key" do
f = create(:base_item)
expect(build(:base_item, partner_key: f.partner_key)).not_to be_valid
end
describe "Validations >" do
it { should validate_presence_of(:name) }
it { should validate_uniqueness_of(:name) }
it { should validate_presence_of(:partner_key) }
it { should validate_uniqueness_of(:partner_key) }
end

describe "Associations >" do
it "keeps count of its associated items" do
c = BaseItem.first
c = create(:base_item, name: "Base", item_count: 0) # BUG: should this default to 0?
expect { create_list(:item, 2, base_item: c) }.to change { c.item_count }.by(2)
end
end

describe "Methods >" do
describe '.seed_items' do
let(:fake_base_items_data) do
{
'category1' => [
{'name' => 'Item1', 'key' => 'key1'},
{'name' => 'Item2', 'key' => 'key2'}
],
'category2' => [
{'name' => 'Item3', 'key' => 'key3'}
]
}
end

before do
allow(File).to receive(:read).with(Rails.root.join("db", "base_items.json")).and_return('')
allow(JSON).to receive(:parse).and_return(fake_base_items_data)
end

it 'creates base items' do
expect {
described_class.seed_items
}.to change(BaseItem, :count).by(3)
end

it 'creates base items with correct attributes' do
described_class.seed_items

expect(BaseItem.exists?(name: 'Item1', category: 'category1', partner_key: 'key1')).to be_truthy
expect(BaseItem.exists?(name: 'Item2', category: 'category1', partner_key: 'key2')).to be_truthy
expect(BaseItem.exists?(name: 'Item3', category: 'category2', partner_key: 'key3')).to be_truthy
end
end
end

describe "Filtering >" do
Expand All @@ -58,7 +79,8 @@

describe "->by_partner_key" do
it "shows the Base Items by partner_key" do
expect(BaseItem.by_partner_key(BaseItem.first.partner_key).size).to eq(1)
base_item = create(:base_item)
expect(BaseItem.by_partner_key(base_item.partner_key).size).to eq(1)
expect(BaseItem.by_partner_key("random_string").size).to eq(0)
end
end
Expand Down
Loading

0 comments on commit 9b29eff

Please sign in to comment.