Skip to content

Commit

Permalink
On #8: Done with sync specs
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed May 24, 2024
1 parent bec3039 commit d217b95
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
10 changes: 5 additions & 5 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ def parse_upload
options = { filename: uploaded_file.original_filename }
options[:outlist_absent] = (params[:articles]['outlist_absent'] == '1')
options[:convert_units] = (params[:articles]['convert_units'] == '1')
@updated_article_pairs, @outlisted_articles, @new_articles = @supplier.sync_from_file uploaded_file.tempfile,
options
@updated_article_pairs, @outlisted_articles, @new_articles, import_data = @supplier.sync_from_file(uploaded_file.tempfile,
options)

@articles = @updated_article_pairs.pluck(0) + @new_articles
load_article_units

if @updated_article_pairs.empty? && @outlisted_articles.empty? && @new_articles.empty?
redirect_to supplier_articles_path(@supplier),
notice: I18n.t('articles.controller.parse_upload.notice')
notice: I18n.t('articles.controller.parse_upload.notice', count: import_data[:articles].length)
end
@ignored_article_count = 0
rescue StandardError => e
Expand All @@ -272,8 +272,8 @@ def parse_upload
# sync all articles with the external database
# renders a form with articles, which should be updated
def sync
@updated_article_pairs, @outlisted_articles, @new_articles = @supplier.sync_from_remote
redirect_to(supplier_articles_path(@supplier), notice: I18n.t('articles.controller.parse_upload.notice')) if @updated_article_pairs.empty? && @outlisted_articles.empty? && @new_articles.empty?
@updated_article_pairs, @outlisted_articles, @new_articles, import_data = @supplier.sync_from_remote
redirect_to(supplier_articles_path(@supplier), notice: I18n.t('articles.controller.parse_upload.notice', count: import_data[:articles].length)) if @updated_article_pairs.empty? && @outlisted_articles.empty? && @new_articles.empty?
@ignored_article_count = 0
load_article_units((@new_articles + @updated_article_pairs.map(&:first)).map(&:current_article_units).flatten.uniq)
rescue StandardError => e
Expand Down
19 changes: 0 additions & 19 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,6 @@ def shared_article(supplier = self.supplier)
end
end

# this method checks, if the shared_article has been changed
# unequal attributes will returned in array
# if only the timestamps differ and the attributes are equal,
# false will returned and self.shared_updated_on will be updated
def shared_article_changed?(supplier = self.supplier)
# skip early if the timestamp hasn't changed
shared_article = self.shared_article(supplier)
return if shared_article.nil? || shared_updated_on == shared_article.updated_on

attrs = unequal_attributes(shared_article)
if attrs.empty?
# when attributes not changed, update timestamp of article
update_attribute(:shared_updated_on, shared_article.updated_on)
false
else
attrs
end
end

# Return article attributes that were changed (incl. unit conversion)
# @param new_article [Article] New article to update self
# @option options [Boolean] :convert_units Omit or set to +true+ to keep current unit and recompute unit quantity and price.
Expand Down
4 changes: 2 additions & 2 deletions app/models/supplier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def self.ransackable_associations(_auth_object = nil)
# @option options [Boolean] :convert_units Omit or set to +true+ to keep current units, recomputing unit quantity and price.
def sync_from_file(file, options = {})
data = FoodsoftFile.parse(file, options)
parse_import_data({ articles: data }, options)
parse_import_data({ articles: data }, options) + [data]
end

def read_from_remote(search_params = {})
Expand All @@ -63,7 +63,7 @@ def read_from_remote(search_params = {})

def sync_from_remote(options = {})
data = read_from_remote(options[:search_params])
parse_import_data(data, options)
parse_import_data(data, options) + [data]
end

def deleted?
Expand Down
4 changes: 3 additions & 1 deletion spec/factories/supplier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
after :create do |supplier, evaluator|
article_count = evaluator.article_count
article_count = rand(1..99) if article_count == true
create_list(:article, article_count, supplier: supplier)
article_count.times do |index|
create(:article, supplier: supplier, order_number: index.to_s)
end
end
end

Expand Down
25 changes: 20 additions & 5 deletions spec/integration/articles_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require_relative '../spec_helper'
require_relative '../support/active_record_helper'

feature ArticlesController do
let(:user) { create(:user, groups: [create(:workgroup, role_article_meta: true)]) }
let(:remote_supplier) { create(:supplier, external_uuid: 'TestUUID', article_count: 10) }
let(:supplier) { create(:supplier) }
let!(:article_unit) { create(:article_unit, unit: 'XPK') }
let!(:article_category) { create(:article_category) }
Expand Down Expand Up @@ -85,6 +85,8 @@
end

describe ':sync', :js do
let(:remote_supplier) { create(:supplier, external_uuid: 'TestUUID', article_count: 10) }

before do
supplier.update(
supplier_remote_source: api_v1_shared_supplier_articles_url(remote_supplier.external_uuid,
Expand All @@ -93,12 +95,9 @@
port: Capybara.current_session.server.port),
shared_sync_method: 'all_available'
)
remote_supplier.articles.each_with_index do |article, index|
article.latest_article_version.update(order_number: index.to_s)
end
end

it 'synchronises articles from external suppliers' do
it 'imports articles from external suppliers' do
visit supplier_articles_path(supplier_id: supplier.id)
click_on I18n.t('articles.index.ext_db.sync')
expect(page).to have_css('.sync-table tbody tr', count: 10)
Expand All @@ -110,6 +109,22 @@
click_on I18n.t('articles.sync.submit')
expect(page).to have_css('.just-updated.article', count: 10)
end

it 'synchronizes articles updated in external supplier' do
clone_supplier_articles(remote_supplier, supplier)

first_remote_article_version = remote_supplier.articles.first.latest_article_version
first_remote_article_version.name = 'Changed'
first_remote_article_version.save

visit supplier_articles_path(supplier_id: supplier.id)
click_on I18n.t('articles.index.ext_db.sync')
expect(page).to have_css '.sync-table tbody tr',
count: 2 # 1 row for original + 1 row for changed version

click_on I18n.t('articles.sync.submit')
expect(page).to have_css('.just-updated.article', count: 1)
end
end

describe ':upload' do
Expand Down
19 changes: 19 additions & 0 deletions spec/support/active_record_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module ActiveRecordHelper
def clone_supplier_articles(from_supplier, to_supplier)
from_supplier.articles.each do |article|
article_duplicate = article.duplicate_including_latest_version_and_ratios
article_duplicate.supplier_id = to_supplier.id

article = Article.create(supplier_id: to_supplier.id)
article.attributes = { latest_article_version_attributes: article_duplicate.latest_article_version.attributes.merge(article_unit_ratios_attributes: article_duplicate.latest_article_version.article_unit_ratios.map(&:attributes)) }
article.save

# act as if the article always looked that way:
article.update(created_at: 1.year.ago, updated_at: 1.year.ago)
end
end
end

RSpec.configure do |config|
config.include ActiveRecordHelper
end

0 comments on commit d217b95

Please sign in to comment.