diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e449d97d..b7e21eab 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -405,6 +405,7 @@ RSpec/Capybara/FeatureMethods: - "spec/integration/articles_spec.rb" - "spec/integration/balancing_spec.rb" - "spec/integration/config_spec.rb" + - "spec/integration/home_spec.rb" - "spec/integration/login_spec.rb" - "spec/integration/order_spec.rb" - "spec/integration/product_distribution_example_spec.rb" diff --git a/spec/factories/delivery.rb b/spec/factories/delivery.rb new file mode 100644 index 00000000..5d27d870 --- /dev/null +++ b/spec/factories/delivery.rb @@ -0,0 +1,8 @@ +require 'factory_bot' + +FactoryBot.define do + factory :delivery do + supplier { create :supplier } + date { Time.now } + end +end diff --git a/spec/integration/home_spec.rb b/spec/integration/home_spec.rb new file mode 100644 index 00000000..313b9afe --- /dev/null +++ b/spec/integration/home_spec.rb @@ -0,0 +1,23 @@ +require_relative '../spec_helper' + +feature 'my profile page' do + let(:user) { create :user } + + before { login user } + + describe 'my profile' do + before { visit my_profile_path } + + it 'is accessible' do + expect(page).to have_field 'user_first_name' + expect(find_field('user_first_name').value).to eq user.first_name + end + + it 'updates first name' do + fill_in 'user_first_name', with: 'foo' + click_button I18n.t('ui.save') + expect(User.find(user.id).first_name).to eq 'foo' + expect(page).to have_selector '.alert-success' + end + end +end diff --git a/spec/integration/supplier_spec.rb b/spec/integration/supplier_spec.rb index ea015d06..178892b8 100644 --- a/spec/integration/supplier_spec.rb +++ b/spec/integration/supplier_spec.rb @@ -2,16 +2,14 @@ require_relative '../spec_helper' feature 'supplier' do let(:supplier) { create :supplier } + let(:user) { create :user, :role_suppliers } + + before { login user } describe 'create new' do - let(:user) { create :user, groups: [create(:workgroup, role_suppliers: true)] } - - before { login user } - it 'can be created' do create :supplier_category - visit suppliers_path - click_on I18n.t('suppliers.index.action_new') + visit new_supplier_path supplier = build :supplier within('#new_supplier') do fill_in 'supplier_name', :with => supplier.name @@ -28,4 +26,35 @@ feature 'supplier' do expect(page).to have_content(supplier.name) end end + + describe 'existing', js: true do + it 'can be shown' do + supplier + visit suppliers_path + click_link supplier.name + expect(page).to have_content(supplier.address) + expect(page).to have_content(supplier.phone) + expect(page).to have_content(supplier.email) + end + + it 'can be updated' do + new_supplier = build :supplier + supplier + visit edit_supplier_path(id: supplier.id) + fill_in 'supplier_name', with: new_supplier.name + find('input[type="submit"]').click + expect(supplier.reload.name).to eq new_supplier.name + end + + it 'can be destroyed' do + supplier + visit suppliers_path + expect(page).to have_content(supplier.name) + accept_confirm do + click_link I18n.t('ui.delete') + end + expect(page).not_to have_content(supplier.name) + expect(supplier.reload.deleted?).to be true + end + end end diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 77c3aea0..519f5b64 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -15,6 +15,38 @@ describe Article do expect(article).to be_deleted end + describe 'convert units' do + it 'returns nil when equal' do + expect(article.convert_units(article)).to be_nil + end + + it 'returns false when invalid unit' do + article1 = build :article, supplier: supplier, unit: 'invalid' + expect(article.convert_units(article1)).to be false + end + + it 'converts from ST to KI (german foodcoops legacy)' do + article1 = build :article, supplier: supplier, unit: 'ST' + article2 = build :article, supplier: supplier, name: 'banana 10-12 St', price: 12.34, unit: 'KI' + new_price, new_unit_quantity = article1.convert_units(article2) + expect(new_unit_quantity).to eq 10 + expect(new_price).to eq 1.23 + end + + it 'converts from g to kg' do + article1 = build :article, supplier: supplier, unit: 'kg' + article2 = build :article, supplier: supplier, unit: 'g', price: 0.12, unit_quantity: 1500 + new_price, new_unit_quantity = article1.convert_units(article2) + expect(new_unit_quantity).to eq 1.5 + expect(new_price).to eq 120 + end + end + + it 'computes changed article attributes' do + article2 = build :article, supplier: supplier, name: 'banana' + expect(article.unequal_attributes(article2)[:name]).to eq 'banana' + end + it 'computes the gross price correctly' do article.deposit = 0 article.tax = 12 diff --git a/spec/models/delivery_spec.rb b/spec/models/delivery_spec.rb new file mode 100644 index 00000000..b48449ab --- /dev/null +++ b/spec/models/delivery_spec.rb @@ -0,0 +1,23 @@ +require_relative '../spec_helper' + +describe Delivery do + let(:delivery) { create :delivery } + let(:stock_article) { create :stock_article, price: 3 } + + it 'creates new stock_changes' do + delivery.new_stock_changes = ([ + { + quantity: 1, + stock_article: stock_article + }, + { + quantity: 2, + stock_article: stock_article + } + ]) + + expect(delivery.stock_changes.last[:stock_article_id]).to be stock_article.id + expect(delivery.includes_article?(stock_article)).to be true + expect(delivery.sum(:net)).to eq 9 + end +end diff --git a/spec/models/group_order_article_spec.rb b/spec/models/group_order_article_spec.rb index ddb9158a..42178a6c 100644 --- a/spec/models/group_order_article_spec.rb +++ b/spec/models/group_order_article_spec.rb @@ -40,13 +40,22 @@ describe GroupOrderArticle do goa.update_quantities(0, 0) expect(GroupOrderArticle.exists?(goa.id)).to be false end + + it 'updates quantity and tolerance' do + goa.update_quantities(2, 2) + goa.update_quantities(1, 1) + expect(goa.quantity).to eq(1) + expect(goa.tolerance).to eq(1) + goa.update_quantities(1, 2) + expect(goa.tolerance).to eq(2) + end end describe 'distribution strategy' do let(:article) { create :article, supplier: order.supplier, unit_quantity: 1 } let(:oa) { order.order_articles.create(:article => article) } let(:goa) { create :group_order_article, group_order: go, order_article: oa } - let!(:goaq) { create :group_order_article_quantity, group_order_article: goa, quantity: 4 } + let!(:goaq) { create :group_order_article_quantity, group_order_article: goa, quantity: 4, tolerance: 6 } it 'can calculate the result for the distribution strategy "first order first serve"' do res = goa.calculate_result(2) @@ -59,5 +68,10 @@ describe GroupOrderArticle do res = goa.calculate_result(2) expect(res).to eq(quantity: 4, tolerance: 0, total: 4) end + + it 'determines tolerance correctly' do + res = goa.calculate_result(6) + expect(res).to eq(quantity: 4, tolerance: 2, total: 6) + end end end diff --git a/spec/models/supplier_spec.rb b/spec/models/supplier_spec.rb index 72e870ed..70ba6def 100644 --- a/spec/models/supplier_spec.rb +++ b/spec/models/supplier_spec.rb @@ -3,6 +3,36 @@ require_relative '../spec_helper' describe Supplier do let(:supplier) { create :supplier } + context 'syncs from file' do + it 'imports and updates articles' do + article1 = create(:article, supplier: supplier, order_number: 177813, unit: '250 g', price: 0.1) + article2 = create(:article, supplier: supplier, order_number: 12345) + supplier.articles = [article1, article2] + options = { filename: 'foodsoft_file_01.csv' } + options[:outlist_absent] = true + options[:convert_units] = true + updated_article_pairs, outlisted_articles, new_articles = supplier.sync_from_file(Rails.root.join('spec/fixtures/foodsoft_file_01.csv'), options) + expect(new_articles.length).to be > 0 + expect(updated_article_pairs.first[1][:name]).to eq 'Tomaten' + expect(outlisted_articles.first).to eq article2 + end + end + + it 'return correct tolerance' do + supplier = create :supplier, articles: create_list(:article, 1, unit_quantity: 1) + expect(supplier.has_tolerance?).to be false + supplier2 = create :supplier, articles: create_list(:article, 1, unit_quantity: 2) + expect(supplier2.has_tolerance?).to be true + end + + it 'deletes the supplier and its articles' do + supplier = create :supplier, article_count: 3 + supplier.articles.each { |a| allow(a).to receive(:mark_as_deleted) } + supplier.mark_as_deleted + supplier.articles.each { |a| expect(a).to have_received(:mark_as_deleted) } + expect(supplier.deleted?).to be true + end + it 'has a unique name' do supplier2 = build :supplier, name: supplier.name expect(supplier2).to be_invalid