commit
bb36147721
15 changed files with 141 additions and 37 deletions
7
.simplecov
Normal file
7
.simplecov
Normal file
|
@ -0,0 +1,7 @@
|
|||
require 'simplecov'
|
||||
require 'coveralls'
|
||||
|
||||
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
||||
SimpleCov.start do
|
||||
add_filter 'config/'
|
||||
end
|
2
Gemfile
2
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
class UserHomepageConstraint < Struct.new(:role)
|
||||
def matches?(request)
|
||||
role == request.env['warden'].user.koelkast?
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
||||
|
|
6
test/fixtures/users.yml
vendored
6
test/fixtures/users.yml
vendored
|
@ -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'
|
||||
|
|
19
test/integration/generate_order_items_test.rb
Normal file
19
test/integration/generate_order_items_test.rb
Normal file
|
@ -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
|
20
test/integration/product_attributes_test.rb
Normal file
20
test/integration/product_attributes_test.rb
Normal file
|
@ -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
|
16
test/integration/stock_test.rb
Normal file
16
test/integration/stock_test.rb
Normal file
|
@ -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
|
12
test/integration/stock_validator_test.rb
Normal file
12
test/integration/stock_validator_test.rb
Normal file
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
require "codeclimate-test-reporter"
|
||||
CodeClimate::TestReporter.start
|
||||
|
||||
require 'coveralls'
|
||||
Coveralls.wear!
|
||||
|
||||
|
|
Loading…
Reference in a new issue