2013-09-18 12:44:41 +02:00
|
|
|
require_relative '../spec_helper'
|
2013-07-14 02:49:40 +02:00
|
|
|
|
|
|
|
describe Supplier do
|
2013-09-18 12:44:41 +02:00
|
|
|
let(:supplier) { create :supplier }
|
2013-07-14 02:49:40 +02:00
|
|
|
|
2022-10-31 16:36:06 +01:00
|
|
|
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
|
2023-01-31 15:25:36 +01:00
|
|
|
updated_article_pairs, outlisted_articles, new_articles = supplier.sync_from_file(Rails.root.join('spec/fixtures/foodsoft_file_01.csv'), "foodsoft", options)
|
2022-10-31 16:36:06 +01:00
|
|
|
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
|
2022-11-28 11:30:38 +01:00
|
|
|
supplier = create :supplier
|
|
|
|
articles = create_list(:article, 1, unit_quantity: 1, supplier_id: supplier.id)
|
|
|
|
supplier.reload
|
2022-10-31 16:36:06 +01:00
|
|
|
expect(supplier.has_tolerance?).to be false
|
2022-11-28 11:30:38 +01:00
|
|
|
supplier2 = create :supplier
|
|
|
|
articles = create_list(:article, 1, unit_quantity: 2, supplier_id: supplier2.id)
|
|
|
|
supplier.reload
|
2022-10-31 16:36:06 +01:00
|
|
|
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
|
|
|
|
|
2013-07-14 02:49:40 +02:00
|
|
|
it 'has a unique name' do
|
2013-09-18 12:44:41 +02:00
|
|
|
supplier2 = build :supplier, name: supplier.name
|
2013-07-24 22:46:25 +02:00
|
|
|
expect(supplier2).to be_invalid
|
2013-07-14 02:49:40 +02:00
|
|
|
end
|
|
|
|
|
2013-07-15 00:17:07 +02:00
|
|
|
it 'has valid articles' do
|
2013-09-18 12:44:41 +02:00
|
|
|
supplier = create :supplier, article_count: true
|
2021-03-01 15:27:26 +01:00
|
|
|
supplier.articles.each { |a| expect(a).to be_valid }
|
2013-07-15 00:17:07 +02:00
|
|
|
end
|
|
|
|
|
2019-03-23 18:31:22 +01:00
|
|
|
context 'connected to a shared supplier' do
|
|
|
|
let(:shared_sync_method) { nil }
|
|
|
|
let(:shared_supplier) { create :shared_supplier }
|
|
|
|
let(:supplier) { create :supplier, shared_supplier: shared_supplier, shared_sync_method: shared_sync_method }
|
|
|
|
|
|
|
|
let!(:synced_shared_article) { create :shared_article, shared_supplier: shared_supplier }
|
|
|
|
let!(:updated_shared_article) { create :shared_article, shared_supplier: shared_supplier }
|
|
|
|
let!(:new_shared_article) { create :shared_article, shared_supplier: shared_supplier }
|
|
|
|
|
|
|
|
let!(:removed_article) { create :article, supplier: supplier, order_number: '10001-ABC' }
|
|
|
|
let!(:updated_article) do
|
|
|
|
updated_shared_article.build_new_article(supplier).tap do |article|
|
|
|
|
article.article_category = create :article_category
|
|
|
|
article.origin = "FubarX1"
|
|
|
|
article.shared_updated_on = 1.day.ago
|
|
|
|
article.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
let!(:synced_article) do
|
|
|
|
synced_shared_article.build_new_article(supplier).tap do |article|
|
|
|
|
article.article_category = create :article_category
|
|
|
|
article.shared_updated_on = 1.day.ago
|
|
|
|
article.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with sync method import' do
|
|
|
|
let(:shared_sync_method) { 'import' }
|
|
|
|
|
|
|
|
it 'returns the expected articles' do
|
|
|
|
updated_article_pairs, outlisted_articles, new_articles = supplier.sync_all
|
|
|
|
|
|
|
|
expect(updated_article_pairs).to_not be_empty
|
|
|
|
expect(updated_article_pairs[0][0].id).to eq updated_article.id
|
|
|
|
expect(updated_article_pairs[0][1].keys).to include :origin
|
|
|
|
|
|
|
|
expect(outlisted_articles).to eq [removed_article]
|
|
|
|
|
|
|
|
expect(new_articles).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with sync method all_available' do
|
|
|
|
let(:shared_sync_method) { 'all_available' }
|
|
|
|
|
|
|
|
it 'returns the expected articles' do
|
|
|
|
updated_article_pairs, outlisted_articles, new_articles = supplier.sync_all
|
|
|
|
|
|
|
|
expect(updated_article_pairs).to_not be_empty
|
|
|
|
expect(updated_article_pairs[0][0].id).to eq updated_article.id
|
|
|
|
expect(updated_article_pairs[0][1].keys).to include :origin
|
|
|
|
|
|
|
|
expect(outlisted_articles).to eq [removed_article]
|
|
|
|
|
|
|
|
expect(new_articles).to_not be_empty
|
|
|
|
expect(new_articles[0].order_number).to eq new_shared_article.number
|
|
|
|
expect(new_articles[0].availability?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-24 22:14:10 +01:00
|
|
|
context 'with sync method all_unavailable' do
|
|
|
|
let(:shared_sync_method) { 'all_unavailable' }
|
|
|
|
|
|
|
|
it 'returns the expected articles' do
|
|
|
|
updated_article_pairs, outlisted_articles, new_articles = supplier.sync_all
|
|
|
|
|
|
|
|
expect(updated_article_pairs).to_not be_empty
|
|
|
|
expect(updated_article_pairs[0][0].id).to eq updated_article.id
|
|
|
|
expect(updated_article_pairs[0][1].keys).to include :origin
|
|
|
|
|
|
|
|
expect(outlisted_articles).to eq [removed_article]
|
|
|
|
|
|
|
|
expect(new_articles).to_not be_empty
|
|
|
|
expect(new_articles[0].order_number).to eq new_shared_article.number
|
|
|
|
expect(new_articles[0].availability?).to be false
|
|
|
|
end
|
|
|
|
end
|
2019-03-23 18:31:22 +01:00
|
|
|
end
|
2013-07-14 02:49:40 +02:00
|
|
|
end
|