Merge pull request #27 from ZeusWPI/test_coverage

Test coverage
This commit is contained in:
benji 2015-03-04 20:17:08 +01:00
commit bb36147721
15 changed files with 141 additions and 37 deletions

7
.simplecov Normal file
View file

@ -0,0 +1,7 @@
require 'simplecov'
require 'coveralls'
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
SimpleCov.start do
add_filter 'config/'
end

View file

@ -70,3 +70,5 @@ gem 'paper_trail', '~> 4.0.0.beta'
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]
gem 'coveralls', require: false gem 'coveralls', require: false
gem "codeclimate-test-reporter", group: :test, require: nil

View file

@ -71,6 +71,8 @@ GEM
activesupport (>= 3.0) activesupport (>= 3.0)
cocaine (0.5.5) cocaine (0.5.5)
climate_control (>= 0.0.3, < 1.0) 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-rails (4.0.1)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0) railties (>= 4.0.0, < 5.0)
@ -240,6 +242,7 @@ DEPENDENCIES
capistrano (~> 3.1) capistrano (~> 3.1)
capistrano-rails (~> 1.1) capistrano-rails (~> 1.1)
capistrano-rvm capistrano-rvm
codeclimate-test-reporter
coffee-rails (~> 4.0.0) coffee-rails (~> 4.0.0)
coveralls coveralls
devise devise

View file

@ -1,5 +0,0 @@
class UserHomepageConstraint < Struct.new(:role)
def matches?(request)
role == request.env['warden'].user.koelkast?
end
end

View file

@ -5,9 +5,13 @@ Rails.application.routes.draw do
unauthenticated :user do unauthenticated :user do
root to: 'devise/sessions#new' root to: 'devise/sessions#new'
end end
authenticated :user do
root to: 'orders#overview', constraints: UserHomepageConstraint.new(true), as: 'koelkast_root' authenticated :user, ->(u) { u.koelkast? } do
root to: 'users#show', constraints: UserHomepageConstraint.new(false), as: 'user_root' root to: 'orders#overview', as: :koelkast_root
end
authenticated :user, ->(u) { !u.koelkast? } do
root to: 'users#show', as: :user_root
end end
end end

View file

@ -28,8 +28,10 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
user: benji:
nickname: 'Gebruiker' nickname: 'benji'
name: 'Benjamin'
last_name: 'Cousaert'
admin: admin:
nickname: 'admin' nickname: 'admin'

View 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

View 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

View 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

View 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

View file

@ -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

View file

@ -12,10 +12,17 @@
require 'test_helper' require 'test_helper'
class OrderTest < ActiveSupport::TestCase 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 test "order total price is correct" do
o = Order.new assert_equal @order.price, 300
o.order_items.build(product: products(:fanta), count: 1) end
o.order_items.build(product: products(:bueno), count: 2)
assert_equal o.price, 300 test "to_sentence is correct" do
assert_equal @order.to_sentence, "1 Fanta and 2 Kinder Buenos"
end end
end end

View file

@ -18,7 +18,15 @@
require 'test_helper' require 'test_helper'
class ProductTest < ActiveSupport::TestCase class ProductTest < ActiveSupport::TestCase
# test "the truth" do test "price behaves correctly" do
# assert true p = products(:fanta)
# end
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 end

View file

@ -29,7 +29,30 @@
require 'test_helper' require 'test_helper'
class UserTest < ActiveSupport::TestCase class UserTest < ActiveSupport::TestCase
# test "the truth" do def setup
# assert true @user = users(:benji)
# end 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 end

View file

@ -1,3 +1,6 @@
require "codeclimate-test-reporter"
CodeClimate::TestReporter.start
require 'coveralls' require 'coveralls'
Coveralls.wear! Coveralls.wear!