diff --git a/Gemfile b/Gemfile index 4f60ef24..8405d992 100644 --- a/Gemfile +++ b/Gemfile @@ -69,4 +69,5 @@ end group :development, :test do gem 'rspec-rails' gem 'factory_girl_rails', '~> 4.0' + gem 'faker' end diff --git a/Gemfile.lock b/Gemfile.lock index b4bfaa25..27e48df4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -94,6 +94,8 @@ GEM factory_girl_rails (4.2.1) factory_girl (~> 4.2.0) railties (>= 3.0.0) + faker (1.1.2) + i18n (~> 0.5) haml (3.1.7) haml-rails (0.3.5) actionpack (>= 3.1, < 4.1) @@ -287,6 +289,7 @@ DEPENDENCIES daemons exception_notification factory_girl_rails (~> 4.0) + faker haml-rails inherited_resources jquery-rails diff --git a/spec/factories/article.rb b/spec/factories/article.rb new file mode 100644 index 00000000..04144b24 --- /dev/null +++ b/spec/factories/article.rb @@ -0,0 +1,20 @@ +require 'factory_girl' + +FactoryGirl.define do + + factory :article do + name { Faker::Lorem.words(rand(2..5)).join(' ') } + unit { Faker::Unit.unit } + price { rand(2600) / 100 } + tax { [6, 21].sample } + deposit { rand(10) < 8 ? 0 : [0.0, 0.80, 1.20, 12.00].sample } + unit_quantity { rand(5) < 3 ? 1 : rand(1..20) } + #supplier_id + article_category { FactoryGirl.create :article_category } + end + + factory :article_category do + name { Faker::Lorem.characters(rand(2..20)) } + end + +end diff --git a/spec/factories/supplier.rb b/spec/factories/supplier.rb new file mode 100644 index 00000000..0833721f --- /dev/null +++ b/spec/factories/supplier.rb @@ -0,0 +1,19 @@ +require 'factory_girl' + +FactoryGirl.define do + + factory :supplier do + name { Faker::Company.name } + phone { Faker::PhoneNumber.phone_number } + address { Faker::Address.street_address } + + ignore do + article_count 0 + end + + after :create do |supplier, evaluator| + FactoryGirl.create_list :article, evaluator.article_count, supplier: supplier + end + end + +end diff --git a/spec/factories/user.rb b/spec/factories/user.rb index 1161efd6..0453d3e1 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -4,8 +4,8 @@ FactoryGirl.define do factory :user do sequence(:nick) { |n| "user#{n}"} - first_name 'John' - email { "#{nick}@foodcoop.test" } + first_name { Faker::Name.first_name } + email { Faker::Internet.email } password { new_random_password } factory :admin do diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb new file mode 100644 index 00000000..5df322a3 --- /dev/null +++ b/spec/models/article_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +describe Article do + let(:supplier) { FactoryGirl.create :supplier } + let(:article) { FactoryGirl.create :article, supplier: supplier } + + it 'has a unique name' do + article2 = FactoryGirl.build :article, supplier: supplier, name: article.name + article2.should_not be_valid + end + + it 'computes the gross price correctly' do + article.deposit = 0 + article.tax = 12 + article.gross_price.should == (article.price * 1.12).round(2) + article.deposit = 1.20 + article.gross_price.should == ((article.price + 1.20) * 1.12).round(2) + end + + it 'gross price >= net price' do + article.gross_price.should >= article.price + end + + it 'fc-price > gross price' do + article.fc_price.should > article.gross_price + end + + it 'knows when it is deleted' do + supplier.deleted?.should be_false + supplier.mark_as_deleted + supplier.deleted?.should be_true + end + + it 'keeps a price history' do + article.article_prices.count.should == 1 + oldprice = article.price + article.price += 1 + article.save! + article.article_prices.count.should == 2 + article.article_prices[0].price.should == article.price + article.article_prices[-1].price.should == oldprice + end + +end diff --git a/spec/models/supplier_spec.rb b/spec/models/supplier_spec.rb new file mode 100644 index 00000000..28dff73f --- /dev/null +++ b/spec/models/supplier_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Supplier do + let(:supplier) { FactoryGirl.create :supplier } + + it 'has a unique name' do + supplier2 = FactoryGirl.build :supplier, name: supplier.name + supplier2.should_not be_valid + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d2cbea7d..5bf80082 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -36,3 +36,13 @@ RSpec.configure do |config| # --seed 1234 config.order = "random" end + +module Faker + class Unit + class << self + def unit + ['kg', '1L', '100ml', 'piece', 'bunch', '500g'].sample + end + end + end +end