From df2d56eb3ad8bf71abf89b746f6f784cf447d3c8 Mon Sep 17 00:00:00 2001 From: Donvittorio Date: Tue, 3 Mar 2015 16:56:33 +0100 Subject: [PATCH 01/10] Updated Coveralls badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c35713c..d7370a0 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ Tab === [![Code Climate](https://codeclimate.com/github/ZeusWPI/Tab/badges/gpa.svg)](https://codeclimate.com/github/ZeusWPI/Tab) [![Travis CI](https://travis-ci.org/ZeusWPI/Tab.svg)](https://travis-ci.org/ZeusWPI/Tab) -[![Coverage Status](https://coveralls.io/repos/ZeusWPI/Tab/badge.svg)](https://coveralls.io/r/ZeusWPI/Tab) +[![Coverage Status](https://coveralls.io/repos/ZeusWPI/Tab/badge.svg?branch=master)](https://coveralls.io/r/ZeusWPI/Tab?branch=master) Yes. We have to drink. But we also have to pay. This combines both. From 95987e48fefddd3539eb5780eb2fa86c75cf1c9a Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 3 Mar 2015 17:41:53 +0100 Subject: [PATCH 02/10] Add tests for orders and products --- test/models/order_product_test.rb | 17 ----------------- test/models/order_test.rb | 15 +++++++++++---- test/models/product_test.rb | 14 +++++++++++--- 3 files changed, 22 insertions(+), 24 deletions(-) delete mode 100644 test/models/order_product_test.rb diff --git a/test/models/order_product_test.rb b/test/models/order_product_test.rb deleted file mode 100644 index a6600ed..0000000 --- a/test/models/order_product_test.rb +++ /dev/null @@ -1,17 +0,0 @@ -# == Schema Information -# -# Table name: order_products -# -# id :integer not null, primary key -# order_id :integer -# product_id :integer -# count :integer default(1) -# - -require 'test_helper' - -class OrderProductTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/order_test.rb b/test/models/order_test.rb index c3b34ef..ef55eff 100644 --- a/test/models/order_test.rb +++ b/test/models/order_test.rb @@ -12,10 +12,17 @@ require 'test_helper' class OrderTest < ActiveSupport::TestCase + def setup + @order = Order.new + @order.order_items.build(product: products(:fanta), count: 1) + @order.order_items.build(product: products(:bueno), count: 2) + end + test "order total price is correct" do - o = Order.new - o.order_items.build(product: products(:fanta), count: 1) - o.order_items.build(product: products(:bueno), count: 2) - assert_equal o.price, 300 + assert_equal @order.price, 300 + end + + test "to_sentence is correct" do + assert_equal @order.to_sentence, "1 Fanta and 2 Kinder Buenos" end end diff --git a/test/models/product_test.rb b/test/models/product_test.rb index 2063a77..51e08b0 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -18,7 +18,15 @@ require 'test_helper' class ProductTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "price behaves correctly" do + p = products(:fanta) + + assert_equal p.price_cents, 60 + assert_equal p.price, 0.6 + + p.price = 1.3 + + assert_equal p.price, 1.3 + assert_equal p.price_cents, 130 + end end From d72f691442d80cedbc43b24694e1029fa4f18798 Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 3 Mar 2015 18:16:05 +0100 Subject: [PATCH 03/10] Finish user tests --- test/fixtures/users.yml | 6 ++++-- test/models/user_test.rb | 29 ++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index ec4bcc5..fdb1fa1 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -28,8 +28,10 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -user: - nickname: 'Gebruiker' +benji: + nickname: 'benji' + name: 'Benjamin' + last_name: 'Cousaert' admin: nickname: 'admin' diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 78b357b..25c63b8 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -29,7 +29,30 @@ require 'test_helper' class UserTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + def setup + @user = users(:benji) + end + + test "full name" do + assert_equal @user.full_name, "Benjamin Cousaert" + end + + test "balance behaves correctly" do + assert_equal @user.balance_cents, 0 + assert_equal @user.balance, 0 + + @user.balance = 1.3 + + assert_equal @user.balance, 1.3 + assert_equal @user.balance_cents, 130 + end + + test "to_param" do + assert_equal @user.to_param, "#{@user.id}-benji" + end + + test "devise validatable methods" do + assert_not @user.email_required? + assert_not @user.email_changed? + end end From 72cfaadfe345c3bd5e4bc8ed9f95310497b00c9f Mon Sep 17 00:00:00 2001 From: Tleilaxu Date: Tue, 3 Mar 2015 19:45:13 +0100 Subject: [PATCH 04/10] Fixed pixel --- app/assets/stylesheets/orders.css.scss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/stylesheets/orders.css.scss b/app/assets/stylesheets/orders.css.scss index 6c64fa2..55dd93c 100644 --- a/app/assets/stylesheets/orders.css.scss +++ b/app/assets/stylesheets/orders.css.scss @@ -8,7 +8,7 @@ } .form-control.big-form-field { - height: 66px; + height: 65px; text-align: center; } From ea93a4c456048cb44b6f0a73fb1a765d5c23edaf Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 3 Mar 2015 20:11:50 +0100 Subject: [PATCH 05/10] Remove ugly constraint --- Gemfile | 2 ++ Gemfile.lock | 3 +++ app/constraints/user_homepage_constraint.rb | 5 ----- config/routes.rb | 10 +++++++--- test/test_helper.rb | 3 +++ 5 files changed, 15 insertions(+), 8 deletions(-) delete mode 100644 app/constraints/user_homepage_constraint.rb diff --git a/Gemfile b/Gemfile index 8a6238b..c62e729 100644 --- a/Gemfile +++ b/Gemfile @@ -70,3 +70,5 @@ gem 'paper_trail', '~> 4.0.0.beta' gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] gem 'coveralls', require: false + +gem "codeclimate-test-reporter", group: :test, require: nil diff --git a/Gemfile.lock b/Gemfile.lock index 15c5db6..22c975d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,6 +71,8 @@ GEM activesupport (>= 3.0) cocaine (0.5.5) climate_control (>= 0.0.3, < 1.0) + codeclimate-test-reporter (0.4.7) + simplecov (>= 0.7.1, < 1.0.0) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -240,6 +242,7 @@ DEPENDENCIES capistrano (~> 3.1) capistrano-rails (~> 1.1) capistrano-rvm + codeclimate-test-reporter coffee-rails (~> 4.0.0) coveralls devise diff --git a/app/constraints/user_homepage_constraint.rb b/app/constraints/user_homepage_constraint.rb deleted file mode 100644 index ce78952..0000000 --- a/app/constraints/user_homepage_constraint.rb +++ /dev/null @@ -1,5 +0,0 @@ -class UserHomepageConstraint < Struct.new(:role) - def matches?(request) - role == request.env['warden'].user.koelkast? - end -end diff --git a/config/routes.rb b/config/routes.rb index 2f5a7ab..4bfcba3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,9 +5,13 @@ Rails.application.routes.draw do unauthenticated :user do root to: 'devise/sessions#new' end - authenticated :user do - root to: 'orders#overview', constraints: UserHomepageConstraint.new(true), as: 'koelkast_root' - root to: 'users#show', constraints: UserHomepageConstraint.new(false), as: 'user_root' + + authenticated :user, ->(u) { u.koelkast? } do + root to: 'orders#overview', as: :koelkast_root + end + + authenticated :user, ->(u) { !u.koelkast? } do + root to: 'users#show', as: :user_root end end diff --git a/test/test_helper.rb b/test/test_helper.rb index cbd1a50..cf9b45e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,6 @@ +require "codeclimate-test-reporter" +CodeClimate::TestReporter.start + require 'coveralls' Coveralls.wear! From ee0239447e75925085d9fbbc002c63ca57b72e75 Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 4 Mar 2015 09:50:55 +0100 Subject: [PATCH 06/10] Add simplecov config to exclude config files from coveralls --- .simplecov | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .simplecov diff --git a/.simplecov b/.simplecov new file mode 100644 index 0000000..2dad006 --- /dev/null +++ b/.simplecov @@ -0,0 +1,7 @@ +require 'simplecov' +require 'coveralls' + +SimpleCov.formatter = Coveralls::SimpleCov::Formatter +SimpleCov.start do + add_filter 'config/' +end From 51ba90d70aafdb1972e6793be27c93cc9b76498f Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 4 Mar 2015 10:25:03 +0100 Subject: [PATCH 07/10] Add test for stock changes --- test/integration/stock_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/integration/stock_test.rb diff --git a/test/integration/stock_test.rb b/test/integration/stock_test.rb new file mode 100644 index 0000000..bfdc6df --- /dev/null +++ b/test/integration/stock_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' + +class StockTest < ActiveSupport::TestCase + test "creating and deleting orders updates stock of products" do + order = users(:benji).orders.build + order.order_items.build(product: products(:fanta), count: 2) + + assert_difference "products(:fanta).stock", -2 do + order.save(validate: false) + end + + assert_difference "products(:fanta).stock", +2 do + order.destroy + end + end +end From 41140e0784e2112a5c639dea2421a81486e4efe8 Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 4 Mar 2015 17:14:44 +0100 Subject: [PATCH 08/10] Add test for g_order_items in order --- test/integration/generate_order_items_test.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/integration/generate_order_items_test.rb diff --git a/test/integration/generate_order_items_test.rb b/test/integration/generate_order_items_test.rb new file mode 100644 index 0000000..80e6e48 --- /dev/null +++ b/test/integration/generate_order_items_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class GenerateOrderItemsTest < ActiveSupport::TestCase + test "g_order_items works" do + order = Order.new + products = Product.all.where("stock > 0") + size = products.size + + order.order_items.build(product: products(:fanta), count: 150) + order.order_items.build(product: products(:mate), count: 50) + order.g_order_items(products) + + assert_equal order.order_items.size, size + assert_equal order.order_items.select { |oi| oi.product == products(:fanta) }.first.count, products(:fanta).stock + assert_equal order.order_items.select { |oi| oi.product == products(:cola) }.size, 0 + assert_equal order.order_items.select { |oi| oi.product == products(:mate) }.first.count, 50 + assert_equal order.order_items.select { |oi| oi.product == products(:bueno) }.first.count, 0 + end +end From 41e8c113ed3afef92f1fe38e6f224a5634a24618 Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 4 Mar 2015 18:18:47 +0100 Subject: [PATCH 09/10] Stock validator test --- test/integration/stock_validator_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/integration/stock_validator_test.rb diff --git a/test/integration/stock_validator_test.rb b/test/integration/stock_validator_test.rb new file mode 100644 index 0000000..338111a --- /dev/null +++ b/test/integration/stock_validator_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' + +class StockValidatorTest < ActiveSupport::TestCase + test "form gives error when product out of stock" do + order = users(:benji).orders.build + order.order_items.build(product: products(:cola), count: 10) + + order.save + + assert_includes order.errors[:base], "There is not enough stock for your order of the following products: Cola" + end +end From 88c780b14e3eefa3d59bde1dae0f2f0c66d7afd2 Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 4 Mar 2015 19:59:07 +0100 Subject: [PATCH 10/10] Final test --- test/integration/product_attributes_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/integration/product_attributes_test.rb diff --git a/test/integration/product_attributes_test.rb b/test/integration/product_attributes_test.rb new file mode 100644 index 0000000..ec7ccfa --- /dev/null +++ b/test/integration/product_attributes_test.rb @@ -0,0 +1,20 @@ +require 'test_helper' + +class ProductAttributesTest < ActiveSupport::TestCase + test "product_attributes are read correctly" do + params = { + order_items_attributes: [ + { + count: "5", + product_attributes: { + id: products(:fanta).id + } + } + ] + } + + o = Order.create(params) + + assert_equal o.order_items.first.product, products(:fanta) + end +end