add unit tests for shared database synchronisation
This commit is contained in:
parent
a5a29aff5c
commit
ac6b00d4f5
2 changed files with 83 additions and 0 deletions
|
@ -52,4 +52,58 @@ describe Article do
|
||||||
order = create :order, supplier: supplier, article_ids: [article.id]
|
order = create :order, supplier: supplier, article_ids: [article.id]
|
||||||
expect(article.in_open_order).to eq(order)
|
expect(article.in_open_order).to eq(order)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
it 'has no shared article by default' do
|
||||||
|
expect(article.shared_article).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'connected to a shared database', :type => :feature do
|
||||||
|
let(:shared_supplier) { create(:supplier) }
|
||||||
|
let(:shared_article) { create :article, supplier: shared_supplier, order_number: Faker::Lorem.characters(rand(1..12)) }
|
||||||
|
let(:supplier) { create :supplier, shared_supplier_id: shared_supplier.id }
|
||||||
|
let(:article) { create :article, supplier: supplier, order_number: shared_article.order_number }
|
||||||
|
|
||||||
|
it 'can be found in the shared database' do
|
||||||
|
expect(article.shared_article).to_not be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'can find updates' do
|
||||||
|
changed = article.shared_article_changed?
|
||||||
|
expect(changed).to_not be_false
|
||||||
|
expect(changed.length).to be > 1
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'can be synchronised' do
|
||||||
|
# TODO move article sync from supplier to article
|
||||||
|
article # need to reference for it to exist when syncing
|
||||||
|
updated_article = supplier.sync_all[0].select{|s| s[0].id==article.id}.first[0]
|
||||||
|
article.update_attributes updated_article.attributes.reject{|k,v| k=='id' or k=='type'}
|
||||||
|
expect(article.name).to eq(shared_article.name)
|
||||||
|
# now synchronising shouldn't change anything anymore
|
||||||
|
expect(article.shared_article_changed?).to be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not need to synchronise an imported article' do
|
||||||
|
article = SharedArticle.find(shared_article.id).build_new_article(supplier)
|
||||||
|
expect(article.shared_article_changed?).to be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'adapts to foodcoop units when synchronising' do
|
||||||
|
shared_article.unit = '1kg'
|
||||||
|
shared_article.unit_quantity = 1
|
||||||
|
shared_article.save!
|
||||||
|
article = SharedArticle.find(shared_article.id).build_new_article(supplier)
|
||||||
|
article.article_category = create :article_category
|
||||||
|
article.unit = '200g'
|
||||||
|
article.shared_updated_on -= 1 # to make update do something
|
||||||
|
article.save!
|
||||||
|
# TODO get sync functionality in article
|
||||||
|
updated_article = supplier.sync_all[0].select{|s| s[0].id==article.id}.first[0]
|
||||||
|
article.update_attributes! updated_article.attributes.reject{|k,v| k=='id' or k=='type'}
|
||||||
|
expect(article.unit).to eq '200g'
|
||||||
|
expect(article.unit_quantity).to eq 5
|
||||||
|
expect(article.price).to be_close(shared_article.price/5, 1e-3)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
29
spec/support/shared_database.rb
Normal file
29
spec/support/shared_database.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# http://stackoverflow.com/questions/8774227
|
||||||
|
# http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite
|
||||||
|
class ActiveRecord::Base
|
||||||
|
mattr_accessor :shared_connection
|
||||||
|
@@shared_connection = nil
|
||||||
|
|
||||||
|
def self.connection
|
||||||
|
@@shared_connection || retrieve_connection
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# Forces all threads to share the same connection. This works on
|
||||||
|
# Capybara because it starts the web server in a thread.
|
||||||
|
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
|
||||||
|
|
||||||
|
ActiveSupport.on_load(:after_initialize) do
|
||||||
|
# We simulate the shared database by pointing to our own database.
|
||||||
|
# This allows running tests without additional database setup.
|
||||||
|
# But take care when designing tests using the shared database.
|
||||||
|
SharedSupplier.establish_connection Rails.env
|
||||||
|
SharedArticle.establish_connection Rails.env
|
||||||
|
# hack for different structure of shared database
|
||||||
|
SharedArticle.class_eval do
|
||||||
|
alias_attribute :number, :order_number
|
||||||
|
alias_attribute :updated_on, :updated_at
|
||||||
|
def self.find_by_number(n)
|
||||||
|
find_by_order_number(n)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue