Skip to content

Commit

Permalink
increase test coverage
Browse files Browse the repository at this point in the history
Co-authored-by: viehlieb <pf@pragma-shift.net>
co-authored-by: Tobias Kneuker <tk@pragma-shift.net>
  • Loading branch information
3 people committed Oct 31, 2022
1 parent 427561e commit ad03caf
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 9 deletions.
8 changes: 8 additions & 0 deletions 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
23 changes: 23 additions & 0 deletions spec/integration/home_controller_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
41 changes: 35 additions & 6 deletions spec/integration/supplier_spec.rb
Expand Up @@ -2,16 +2,14 @@

feature 'supplier' do
let(:supplier) { create :supplier }
let(:user) { create :user, groups: [create(:workgroup, role_suppliers: true)] }

describe 'create new' do
let(:user) { create :user, groups: [create(:workgroup, role_suppliers: true)] }

before { login user }
before { login user }

describe 'create new' do
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
Expand All @@ -28,4 +26,35 @@
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
32 changes: 32 additions & 0 deletions spec/models/article_spec.rb
Expand Up @@ -15,6 +15,38 @@
expect(article).to be_deleted
end

describe 'german legacy 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' 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
Expand Down
23 changes: 23 additions & 0 deletions 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
17 changes: 15 additions & 2 deletions spec/models/group_order_article_spec.rb
Expand Up @@ -40,13 +40,22 @@
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)
Expand All @@ -55,9 +64,13 @@

it 'can calculate the result for the distribution strategy "no automatic distribution"' do
FoodsoftConfig[:distribution_strategy] = FoodsoftConfig::DistributionStrategy::NO_AUTOMATIC_DISTRIBUTION

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
32 changes: 31 additions & 1 deletion spec/models/supplier_spec.rb
Expand Up @@ -3,6 +3,36 @@
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
Expand All @@ -26,7 +56,7 @@
let!(:updated_article) do
updated_shared_article.build_new_article(supplier).tap do |article|
article.article_category = create :article_category
article.origin = "FubarX1"
article.origin = 'FubarX1'
article.shared_updated_on = 1.day.ago
article.save!
end
Expand Down

0 comments on commit ad03caf

Please sign in to comment.