foodsoft/spec/models/article_spec.rb

115 lines
4 KiB
Ruby
Raw Normal View History

2013-09-18 12:44:41 +02:00
require_relative '../spec_helper'
describe Article do
2013-09-18 12:44:41 +02:00
let(:supplier) { create :supplier }
let(:article) { create :article, supplier: supplier }
it 'has a unique name' do
article2 = FactoryGirl.build :article, supplier: supplier, name: article.name
2013-07-24 22:46:25 +02:00
expect(article2).to be_invalid
end
it 'computes the gross price correctly' do
article.deposit = 0
article.tax = 12
2013-07-24 22:46:25 +02:00
expect(article.gross_price).to eq((article.price * 1.12).round(2))
article.deposit = 1.20
2013-07-24 22:46:25 +02:00
expect(article.gross_price).to eq(((article.price + 1.20) * 1.12).round(2))
end
it 'gross price >= net price' do
2013-07-24 22:46:25 +02:00
expect(article.gross_price).to be >= article.price
end
it 'fc-price >= gross price' do
if article.gross_price > 0
expect(article.fc_price).to be > article.gross_price
else
expect(article.fc_price).to be >= article.gross_price
end
end
it 'knows when it is deleted' do
2013-07-24 22:46:25 +02:00
expect(supplier.deleted?).to be_false
supplier.mark_as_deleted
2013-07-24 22:46:25 +02:00
expect(supplier.deleted?).to be_true
end
it 'keeps a price history' do
2013-07-25 00:01:58 +02:00
expect(article.article_prices.all.map(&:price)).to eq([article.price])
oldprice = article.price
2013-07-25 00:01:58 +02:00
sleep 1 # so that the new price really has a later creation time
article.price += 1
article.save!
2013-07-25 00:01:58 +02:00
expect(article.article_prices.all.map(&:price)).to eq([article.price, oldprice])
end
2013-09-18 22:27:53 +02:00
it 'is not in an open order by default' do
expect(article.in_open_order).to be_nil
end
it 'is knows its open order' do
order = create :order, supplier: supplier, article_ids: [article.id]
expect(article.in_open_order).to eq(order)
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
2013-11-13 23:22:26 +01:00
expect(article.price).to be_within(1e-3).of(shared_article.price/5)
end
it 'does not synchronise when it has no order number' do
article.update_attributes :order_number => nil
expect(supplier.sync_all).to eq [[], []]
end
end
end