From 1c21e115102026379194cfe7cad46ecc110b2d9f Mon Sep 17 00:00:00 2001 From: ajugo Date: Fri, 15 Apr 2022 12:23:47 +0200 Subject: [PATCH] Stock management test [SCI-6420] (#3946) * Initial test for stock management * Repository stock values adding [does not work] SCI-6420 * Fix typos SCI-6420 * Fix test [SCI-6420] * Test changes [SCI-6402] * Remove locking test [SCI-6420] * Remove lock [SCI-6420] * Remove serializer [SCI-6420] * Fix stock test and add ledger creation on consume [SCI-6420] --- .../repository_stock_value_controller.rb | 60 +++++++++++++++++++ spec/factories/repository_ledger_record.rb | 10 ++++ spec/models/my_module_repository_row_spec.rb | 21 +++++++ spec/models/repository_cell_spec.rb | 5 ++ spec/models/repository_ledger_record_spec.rb | 35 +++++++++++ .../models/repository_stock_unit_item_spec.rb | 32 ++++++++++ spec/models/repository_stock_value_spec.rb | 56 +++++++++++++++++ 7 files changed, 219 insertions(+) create mode 100644 spec/controllers/repository_stock_value_controller.rb create mode 100644 spec/factories/repository_ledger_record.rb create mode 100644 spec/models/repository_ledger_record_spec.rb create mode 100644 spec/models/repository_stock_unit_item_spec.rb create mode 100644 spec/models/repository_stock_value_spec.rb diff --git a/spec/controllers/repository_stock_value_controller.rb b/spec/controllers/repository_stock_value_controller.rb new file mode 100644 index 000000000..d505f82f8 --- /dev/null +++ b/spec/controllers/repository_stock_value_controller.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe RepositoryStockValuesController, type: :controller do + before :all do + ApplicationSettings.instance.update( + values: ApplicationSettings.instance.values.merge({"stock_management_enabled" => true}) + ) + end + + login_user + let!(:user) { controller.current_user } + let!(:team) { create :team, created_by: user } + let!(:user_team) { create :user_team, :admin, team: team, user: user } + let!(:repository) { create :repository, team: team, created_by: user } + let(:repository_column) do + create :repository_column, :stock_type, created_by: user, repository: repository + end + + let!(:repository_row) do + create :repository_row, repository: repository, + created_by: user, + last_modified_by: user + end + + let!(:repository_stock_unit_item) {create :repository_stock_unit_item, created_by: user, + last_modified_by: user, + repository_column: repository_column} + + describe 'create' do + let(:params) do { + repository_stock_value: { + amount: 10, + unit_item_id: repository_stock_unit_item.id, + comment: 'test', + low_stock_threshold: '' + }, + operator: 'set', + change_amount: 0, + repository_id: repository.id, + id: repository_row.id + } + end + + let(:action) { post :create_or_update, params: params, format: :json } + let(:action1) { post :create_or_update, params: params, format: :json } + + it 'Create stock value' do + expect { action }.to change(RepositoryLedgerRecord, :count).by(1) + end + + it 'Ledger immutability' do + action + expect { action1 } + .to (change(RepositoryLedgerRecord, :count).by(1) + .and(change(RepositoryStockValue, :count).by(0))) + end + end +end diff --git a/spec/factories/repository_ledger_record.rb b/spec/factories/repository_ledger_record.rb new file mode 100644 index 000000000..8fa5d4a2e --- /dev/null +++ b/spec/factories/repository_ledger_record.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :repository_ledger_record do + repository_stock_value + association :reference, factory: :repository_row + user + end +end + diff --git a/spec/models/my_module_repository_row_spec.rb b/spec/models/my_module_repository_row_spec.rb index 5b794d864..79c6aac08 100644 --- a/spec/models/my_module_repository_row_spec.rb +++ b/spec/models/my_module_repository_row_spec.rb @@ -5,6 +5,16 @@ require 'rails_helper' describe MyModuleRepositoryRow, type: :model do let(:my_module_repository_row) { build :mm_repository_row } + let(:user) { create :user } + let(:repository_stock_value) { create :repository_stock_value } + + let(:repository_row) { create :repository_row, repository_stock_value: repository_stock_value } + let(:my_module_repository_row_stock) { create :mm_repository_row, + repository_row: repository_row, + assigned_by: user, + last_modified_by: user + } + it 'is valid' do expect(my_module_repository_row).to be_valid end @@ -16,6 +26,8 @@ describe MyModuleRepositoryRow, type: :model do describe 'Database table' do it { should have_db_column :id } it { should have_db_column :repository_row_id } + it { should have_db_column :repository_stock_unit_item_id } + it { should have_db_column :stock_consumption } it { should have_db_column :my_module_id } it { should have_db_column :assigned_by_id } it { should have_db_column :created_at } @@ -24,7 +36,16 @@ describe MyModuleRepositoryRow, type: :model do describe 'Relations' do it { should belong_to(:my_module) } + it { should belong_to(:repository_stock_unit_item).optional } it { should belong_to(:assigned_by).class_name('User').optional } it { should belong_to(:repository_row) } end + + describe 'Repository ledger record' do + it 'Ledger creation on changed consumption' do + my_module_repository_row_stock.stock_consumption = 10.0 + expect { my_module_repository_row_stock.save! } + .to change(RepositoryLedgerRecord, :count).by(1) + end + end end diff --git a/spec/models/repository_cell_spec.rb b/spec/models/repository_cell_spec.rb index 8ba01fc42..f89ed0621 100644 --- a/spec/models/repository_cell_spec.rb +++ b/spec/models/repository_cell_spec.rb @@ -10,6 +10,7 @@ describe RepositoryCell, type: :model do let(:repository_cell_a) { build :repository_cell, :asset_value } let(:repository_cell_s) { build :repository_cell, :status_value } let(:repository_cell_d_r) { build :repository_cell, :date_time_range_value } + let(:repository_cell_s_v) { build :repository_cell, :stock_value } context 'when do not have value' do it 'is not valid' do @@ -41,6 +42,10 @@ describe RepositoryCell, type: :model do it 'is valid for date time range value' do expect(repository_cell_d_r).to be_valid end + + it 'is valid for stock value' do + expect(repository_cell_s_v).to be_valid + end end it 'should be of class RepositoryCell' do diff --git a/spec/models/repository_ledger_record_spec.rb b/spec/models/repository_ledger_record_spec.rb new file mode 100644 index 000000000..1ab16846f --- /dev/null +++ b/spec/models/repository_ledger_record_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe RepositoryLedgerRecord, type: :model do + let(:repository_ledger_record) { build :repository_ledger_record } + let(:repository_stock_value_new) { build :repository_stock_value } + + it 'is valid' do + expect(repository_ledger_record).to be_valid + end + + it 'should be of class RepositoryLedgerRecord' do + expect(subject.class).to eq RepositoryLedgerRecord + end + + describe 'Database table' do + it { should have_db_column :id } + it { should have_db_column :repository_stock_value_id } + it { should have_db_column :reference_id } + it { should have_db_column :reference_type } + it { should have_db_column :amount } + it { should have_db_column :balance } + it { should have_db_column :user_id } + it { should have_db_column :comment } + it { should have_db_column :created_at } + it { should have_db_column :updated_at } + end + + describe 'Associations' do + it { should belong_to(:repository_stock_value).optional } + it { should belong_to(:reference) } + it { should belong_to(:user) } + end +end diff --git a/spec/models/repository_stock_unit_item_spec.rb b/spec/models/repository_stock_unit_item_spec.rb new file mode 100644 index 000000000..4387afc1e --- /dev/null +++ b/spec/models/repository_stock_unit_item_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe RepositoryStockUnitItem, type: :model do + let(:repository_stock_unit_item) { build :repository_stock_unit_item } + + it 'is valid' do + expect(repository_stock_unit_item).to be_valid + end + + it 'should be of class RepositoryStockUnitItem' do + expect(subject.class).to eq RepositoryStockUnitItem + end + + describe 'Database table' do + it { should have_db_column :id } + it { should have_db_column :data } + it { should have_db_column :repository_column_id } + it { should have_db_column :created_by_id } + it { should have_db_column :last_modified_by_id } + it { should have_db_column :created_at } + it { should have_db_column :updated_at } + end + + describe 'Associations' do + it { should belong_to(:repository_column) } + it { should belong_to(:created_by) } + it { should belong_to(:last_modified_by) } + end + +end diff --git a/spec/models/repository_stock_value_spec.rb b/spec/models/repository_stock_value_spec.rb new file mode 100644 index 000000000..39b1d25e7 --- /dev/null +++ b/spec/models/repository_stock_value_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe RepositoryStockValue, type: :model do + let(:repository_stock_value) { build :repository_stock_value } + let(:repository) { build :repository } + let(:user) { build :user } + + + it 'is valid' do + expect(repository_stock_value).to be_valid + end + + it 'should be of class RepositoryStockValue' do + expect(subject.class).to eq RepositoryStockValue + end + + describe 'Database table' do + it { should have_db_column :id } + it { should have_db_column :amount } + it { should have_db_column :repository_stock_unit_item_id } + it { should have_db_column :type } + it { should have_db_column :last_modified_by_id } + it { should have_db_column :created_by_id } + it { should have_db_column :created_at } + it { should have_db_column :updated_at } + it { should have_db_column :low_stock_threshold } + end + + describe 'Associations' do + it { is_expected.to belong_to(:repository_stock_unit_item).optional } + it { is_expected.to belong_to(:created_by).optional } + it { is_expected.to belong_to(:last_modified_by).optional } + end + + + describe 'Saving stock value' do + let(:repository_stock_value1) { build :repository_stock_value } + it 'Save stock value' do + expect { repository_stock_value.save }.to change(RepositoryStockValue, :count).by(1) + end + + it 'Updating stock value' do + repository_stock_value.save + expect { repository_stock_value.update_data!({amount: 10, low_stock_threshold:''}, user) } + .to change(RepositoryStockValue, :count).by(0) + end + + it 'Updating stock value with ledger' do + repository_stock_value.save + expect { repository_stock_value.update_stock_with_ledger!(10, repository, "") } + .to (change(RepositoryLedgerRecord, :count).by(1)) + end + end +end