From e5870b5b4128eb4e32d278193178d1f5e6ed6a62 Mon Sep 17 00:00:00 2001 From: viehlieb Date: Thu, 3 Nov 2022 16:44:42 +0100 Subject: [PATCH] fix: fix test for rails upgrade --- spec/controllers/articles_controller_spec.rb | 64 +++++++++++--------- spec/models/supplier_spec.rb | 34 +++++++++++ 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/spec/controllers/articles_controller_spec.rb b/spec/controllers/articles_controller_spec.rb index ab14e30ed..dae89c70b 100644 --- a/spec/controllers/articles_controller_spec.rb +++ b/spec/controllers/articles_controller_spec.rb @@ -4,14 +4,15 @@ describe ArticlesController, type: :controller do let(:user) { create :user, :role_article_meta } - let(:article_category_a) { create :article_category, name: 'AAAA' } - let(:article_category_b) { create :article_category, name: 'BBBB' } - let(:article_category_c) { create :article_category, name: 'CCCC' } - let(:article_a) { create :article, name: 'AAAA', note: 'CCC', unit: '750 g', article_category: article_category_b, availability: false } - let(:article_b) { create :article, name: 'BBBB', note: 'AAA', unit: '500 g', article_category: article_category_a, availability: true } - let(:article_c) { create :article, name: 'CCCC', note: 'BBB', unit: '250 g', article_category: article_category_c, availability: true } - - let(:supplier) { create :supplier, articles: [article_a, article_b, article_c] } + let(:article_category_a) { create :article_category, name: "AAAA" } + let(:article_category_b) { create :article_category, name: "BBBB" } + let(:article_category_c) { create :article_category, name: "CCCC" } + let(:supplier) { create :supplier} + let(:article_a) { create :article, name: 'AAAA', note: "ZZZZ", unit: '750 g', article_category: article_category_b, availability: false, supplier_id: supplier.id } + let(:article_b) { create :article, name: 'BBBB', note: "XXXX", unit: '500 g', article_category: article_category_a, availability: true, supplier_id: supplier.id } + let(:article_c) { create :article, name: 'CCCC', note: "YYYY", unit: '250 g', article_category: article_category_c, availability: true, supplier_id: supplier.id } + let(:article_no_supplier) { create :article, name: 'no_supplier', note: "no_supplier", unit: '100 g', article_category: article_category_b, availability: true } + let(:order) { create :order } let(:order2) { create :order } @@ -28,6 +29,13 @@ def post_with_supplier(action, params: {}, xhr: false, format: nil) before { login user } describe 'GET index' do + before do + supplier + article_a + article_b + article_c + supplier.reload + end it 'assigns sorting on articles' do sortings = [ ['name', [article_a, article_b, article_c]], @@ -128,7 +136,7 @@ def post_with_supplier(action, params: {}, xhr: false, format: nil) end describe '#update_selected' do - let(:order_article) { create :order_article, order: order, article: article_c } + let(:order_article) { create :order_article, order: order, article: article_no_supplier } before do order_article @@ -167,11 +175,11 @@ def post_with_supplier(action, params: {}, xhr: false, format: nil) end it 'fails deletion if one article is in open order' do - get_with_supplier :update_selected, params: { selected_articles: [article_a.id, article_c.id], selected_action: 'destroy' } + get_with_supplier :update_selected, params: { selected_articles: [article_a.id, article_no_supplier.id], selected_action: 'destroy' } article_a.reload - article_c.reload + article_no_supplier.reload expect(article_a).not_to be_deleted - expect(article_c).not_to be_deleted + expect(article_no_supplier).not_to be_deleted expect(response).to have_http_status(:redirect) end end @@ -206,14 +214,14 @@ def post_with_supplier(action, params: {}, xhr: false, format: nil) end describe '#destroy' do - let(:order_article) { create :order_article, order: order, article: article_c } + let(:order_article) { create :order_article, order: order, article: article_no_supplier } before do order_article end it 'does not delete article if order open' do - get_with_supplier :destroy, params: { id: article_c.id }, xhr: true + get_with_supplier :destroy, params: { id: article_no_supplier.id }, xhr: true expect(assigns(:article)).not_to be_deleted expect(response).to have_http_status(:success) expect(response).to render_template('articles/destroy') @@ -228,13 +236,13 @@ def post_with_supplier(action, params: {}, xhr: false, format: nil) end describe '#update_synchronized' do - let(:order_article) { create :order_article, order: order, article: article_c } + let(:order_article) { create :order_article, order: order, article: article_no_supplier } before do order_article article_a article_b - article_c + article_no_supplier end it 'deletes articles' do @@ -274,27 +282,27 @@ def post_with_supplier(action, params: {}, xhr: false, format: nil) end it 'does not delete articles in open order' do - get_with_supplier :update_synchronized, params: { outlisted_articles: { article_c.id => article_c } } - article_c.reload - expect(article_c).not_to be_deleted + get_with_supplier :update_synchronized, params: { outlisted_articles: { article_no_supplier.id => article_no_supplier } } + article_no_supplier.reload + expect(article_no_supplier).not_to be_deleted expect(response).to have_http_status(:success) end it 'assigns updated article_pairs on error' do get_with_supplier :update_synchronized, params: { - articles: { article_a.id => { name: 'DDDD' } }, - outlisted_articles: { article_c.id => article_c } + articles: { article_a.id => { name: 'EEEE' } }, + outlisted_articles: { article_no_supplier.id => article_no_supplier } } - expect(assigns(:updated_article_pairs).first).to eq([article_a, { name: 'DDDD' }]) - article_c.reload - expect(article_c).not_to be_deleted + expect(assigns(:updated_article_pairs).first).to eq([article_a, { name: 'EEEE' }]) + article_no_supplier.reload + expect(article_no_supplier).not_to be_deleted expect(response).to have_http_status(:success) end it 'updates articles in open order' do - get_with_supplier :update_synchronized, params: { articles: { article_c.id => { name: 'DDDD' } } } - article_c.reload - expect(article_c.name).to eq 'DDDD' + get_with_supplier :update_synchronized, params: { articles: { article_no_supplier.id => { name: 'EEEE' } } } + article_no_supplier.reload + expect(article_no_supplier.name).to eq 'EEEE' expect(response).to have_http_status(:redirect) end end @@ -304,7 +312,7 @@ def post_with_supplier(action, params: {}, xhr: false, format: nil) let(:shared_article) { create :shared_article, name: 'shared' } let(:article_s) { create :article, name: 'SSSS', note: 'AAAA', unit: '250 g', article_category: article_category_a, availability: false } - let(:supplier_with_shared) { create :supplier, articles: [article_s], shared_supplier: shared_supplier } + let(:supplier_with_shared) { create :supplier, shared_supplier: shared_supplier } it 'renders view with articles' do get_with_defaults :shared, params: { supplier_id: supplier_with_shared.id, name_cont_all_joined: 'shared' }, xhr: true diff --git a/spec/models/supplier_spec.rb b/spec/models/supplier_spec.rb index 72e870ed7..6bcc6e7ba 100644 --- a/spec/models/supplier_spec.rb +++ b/spec/models/supplier_spec.rb @@ -3,6 +3,40 @@ 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, supplier_id: supplier.id) + supplier.reload + expect(supplier.has_tolerance?).to be false + supplier2 = create :supplier + articles = create_list(:article, 1, unit_quantity: 2, supplier_id: supplier2.id) + supplier.reload + 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