Merge branch 'rspec'
This commit is contained in:
commit
5ba36e431f
54 changed files with 815 additions and 326 deletions
3
.rspec
Normal file
3
.rspec
Normal file
|
@ -0,0 +1,3 @@
|
|||
--color
|
||||
--require spec_helper
|
||||
--require rails_helper
|
|
@ -1,5 +1,7 @@
|
|||
language: ruby
|
||||
sudo: false
|
||||
language: ruby
|
||||
rvm:
|
||||
- 2.2.0
|
||||
|
||||
before_script:
|
||||
- "RAILS_ENV=test bundle exec rake db:create"
|
||||
|
|
15
Gemfile
15
Gemfile
|
@ -37,7 +37,10 @@ end
|
|||
group :test do
|
||||
gem 'capybara'
|
||||
gem 'launchy'
|
||||
gem "codeclimate-test-reporter", require: nil
|
||||
gem 'codeclimate-test-reporter', require: nil
|
||||
gem 'rspec-rails'
|
||||
gem 'factory_girl_rails'
|
||||
gem 'faker', '1.4.2'
|
||||
end
|
||||
|
||||
group :development do
|
||||
|
@ -65,9 +68,6 @@ end
|
|||
# Airbrake
|
||||
gem 'airbrake'
|
||||
|
||||
# Generate seed data
|
||||
gem 'faker', '1.4.2'
|
||||
|
||||
# Debug stuff
|
||||
gem 'byebug'
|
||||
|
||||
|
@ -99,3 +99,10 @@ gem 'identicon'
|
|||
|
||||
# Slack
|
||||
gem 'tarumi'
|
||||
|
||||
# Run stuff in the background
|
||||
gem 'daemons'
|
||||
gem 'delayed_job', '~> 4.0'
|
||||
gem 'delayed_job_active_record'
|
||||
|
||||
gem 'httparty'
|
||||
|
|
37
Gemfile.lock
37
Gemfile.lock
|
@ -105,7 +105,13 @@ GEM
|
|||
simplecov (~> 0.9.1)
|
||||
term-ansicolor (~> 1.3)
|
||||
thor (~> 0.19.1)
|
||||
daemons (1.2.3)
|
||||
debugger-linecache (1.2.0)
|
||||
delayed_job (4.0.6)
|
||||
activesupport (>= 3.0, < 5.0)
|
||||
delayed_job_active_record (4.0.3)
|
||||
activerecord (>= 3.0, < 5.0)
|
||||
delayed_job (>= 3.0, < 4.1)
|
||||
devise (3.4.1)
|
||||
bcrypt (~> 3.0)
|
||||
orm_adapter (~> 0.1)
|
||||
|
@ -113,9 +119,15 @@ GEM
|
|||
responders
|
||||
thread_safe (~> 0.1)
|
||||
warden (~> 1.2.3)
|
||||
diff-lcs (1.2.5)
|
||||
docile (1.1.5)
|
||||
erubis (2.7.0)
|
||||
execjs (2.3.0)
|
||||
factory_girl (4.5.0)
|
||||
activesupport (>= 3.0.0)
|
||||
factory_girl_rails (4.5.0)
|
||||
factory_girl (~> 4.5.0)
|
||||
railties (>= 3.0.0)
|
||||
faker (1.4.2)
|
||||
i18n (~> 0.5)
|
||||
faraday (0.9.1)
|
||||
|
@ -238,6 +250,23 @@ GEM
|
|||
ffi (~> 1.9)
|
||||
mime-types (>= 1.16, < 3.0)
|
||||
netrc (~> 0.7)
|
||||
rspec-core (3.3.2)
|
||||
rspec-support (~> 3.3.0)
|
||||
rspec-expectations (3.3.1)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.3.0)
|
||||
rspec-mocks (3.3.2)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.3.0)
|
||||
rspec-rails (3.3.3)
|
||||
actionpack (>= 3.0, < 4.3)
|
||||
activesupport (>= 3.0, < 4.3)
|
||||
railties (>= 3.0, < 4.3)
|
||||
rspec-core (~> 3.3.0)
|
||||
rspec-expectations (~> 3.3.0)
|
||||
rspec-mocks (~> 3.3.0)
|
||||
rspec-support (~> 3.3.0)
|
||||
rspec-support (3.3.0)
|
||||
sass (3.2.19)
|
||||
sass-rails (4.0.5)
|
||||
railties (>= 4.0.0, < 5.0)
|
||||
|
@ -314,8 +343,13 @@ DEPENDENCIES
|
|||
codeclimate-test-reporter
|
||||
coffee-rails (~> 4.0.0)
|
||||
coveralls
|
||||
daemons
|
||||
delayed_job (~> 4.0)
|
||||
delayed_job_active_record
|
||||
devise
|
||||
factory_girl_rails
|
||||
faker (= 1.4.2)
|
||||
httparty
|
||||
identicon
|
||||
jbuilder (~> 2.0)
|
||||
jquery-rails
|
||||
|
@ -328,6 +362,7 @@ DEPENDENCIES
|
|||
pry-rails
|
||||
rails (= 4.2)
|
||||
responders (~> 2.0)
|
||||
rspec-rails
|
||||
sass-rails (~> 4.0.3)
|
||||
sdoc (~> 0.4.0)
|
||||
spring
|
||||
|
@ -339,4 +374,4 @@ DEPENDENCIES
|
|||
will_paginate (= 3.0.7)
|
||||
|
||||
BUNDLED WITH
|
||||
1.10.5
|
||||
1.10.6
|
||||
|
|
|
@ -3,7 +3,7 @@ class OrdersController < ApplicationController
|
|||
include ApplicationHelper
|
||||
|
||||
load_resource :user
|
||||
load_and_authorize_resource :order, through: :user, shallow: true
|
||||
load_and_authorize_resource :order, through: :user, shallow: true, except: :destroy
|
||||
|
||||
def new
|
||||
products = (@user.products.for_sale.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc") | Product.for_sale)
|
||||
|
@ -21,12 +21,12 @@ class OrdersController < ApplicationController
|
|||
end
|
||||
|
||||
def destroy
|
||||
order = Order.find(params[:id])
|
||||
if order.created_at > 5.minutes.ago
|
||||
order.cancel
|
||||
order = Order.unscoped.find(params[:id])
|
||||
authorize! :destroy, order
|
||||
if order.cancel
|
||||
flash[:success] = "Order has been removed."
|
||||
else
|
||||
flash[:error] = "This order has been placed too long ago, it can't be removed. Please contact a sysadmin."
|
||||
flash[:error] = "Something went wrong. Perhaps this order was already cancelled, or it has been place too long ago."
|
||||
end
|
||||
redirect_to root_path
|
||||
end
|
||||
|
|
|
@ -33,7 +33,7 @@ class UsersController < ApplicationController
|
|||
end
|
||||
|
||||
def edit_dagschotel
|
||||
@user = User.find(params[:user_id])
|
||||
@user = User.find(params[:id])
|
||||
authorize! :update_dagschotel, @user
|
||||
@dagschotel = @user.dagschotel
|
||||
|
||||
|
@ -42,7 +42,7 @@ class UsersController < ApplicationController
|
|||
end
|
||||
|
||||
def update_dagschotel
|
||||
user = User.find(params[:user_id])
|
||||
user = User.find(params[:id])
|
||||
authorize! :update_dagschotel, user
|
||||
|
||||
user.dagschotel = Product.find(params[:product_id])
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
# cancelled :boolean default("f")
|
||||
#
|
||||
|
||||
require 'httparty'
|
||||
class Order < ActiveRecord::Base
|
||||
include ActionView::Helpers::TextHelper
|
||||
|
||||
|
@ -17,6 +18,8 @@ class Order < ActiveRecord::Base
|
|||
has_many :order_items, dependent: :destroy
|
||||
has_many :products, through: :order_items
|
||||
|
||||
after_create :tab_api_created
|
||||
|
||||
default_scope -> { where(cancelled: false) }
|
||||
|
||||
validates :user, presence: true
|
||||
|
@ -36,11 +39,28 @@ class Order < ActiveRecord::Base
|
|||
write_attribute(:price_cents, price_cents)
|
||||
end
|
||||
|
||||
def tab_api_created
|
||||
body = { transaction: { debtor: user.uid, cents: price_cents, message: to_sentence } }
|
||||
headers = { "Authorization" => "Token token=LNJxGqkM39O21gcJJq6BLQ==" }
|
||||
HTTParty.post("https://zeus.ugent.be/tab/transactions", body: body, headers: headers )
|
||||
end
|
||||
handle_asynchronously :tab_api_created
|
||||
|
||||
def tab_api_cancelled
|
||||
body = { transaction: { creditor: user.uid, cents: price_cents, message: "Order cancelled" } }
|
||||
headers = { "Authorization" => "Token token=LNJxGqkM39O21gcJJq6BLQ==" }
|
||||
HTTParty.post("https://zeus.ugent.be/tab/transactions", body: body, headers: headers )
|
||||
end
|
||||
handle_asynchronously :tab_api_cancelled
|
||||
|
||||
def cancel
|
||||
return if self.cancelled
|
||||
return false if cancelled || created_at < 5.minutes.ago
|
||||
|
||||
User.decrement_counter(:orders_count, user.id)
|
||||
update_attribute(:cancelled, true)
|
||||
self.order_items.each(&:cancel)
|
||||
tab_api_cancelled
|
||||
true
|
||||
end
|
||||
|
||||
def to_sentence
|
||||
|
|
|
@ -13,7 +13,7 @@ class OrderItem < ActiveRecord::Base
|
|||
belongs_to :product
|
||||
|
||||
validates :product, presence: true
|
||||
validates :count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :count, presence: true, numericality: { only_integer: true, greater_than: 0 }
|
||||
|
||||
after_create :remove_from_stock
|
||||
|
||||
|
|
|
@ -13,7 +13,8 @@
|
|||
# avatar_updated_at :datetime
|
||||
# category :integer default("0")
|
||||
# stock :integer default("0"), not null
|
||||
# calories :integer default("0") // expressed in kcal
|
||||
# calories :integer
|
||||
# deleted :boolean default("f")
|
||||
#
|
||||
|
||||
class Product < ActiveRecord::Base
|
||||
|
@ -24,7 +25,7 @@ class Product < ActiveRecord::Base
|
|||
|
||||
validates :name, presence: true
|
||||
validates :price_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :stock, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :stock, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :calories, numericality: { only_integer: true, allow_nil: true, greater_than_or_equal_to: 0 }
|
||||
validates_attachment :avatar,
|
||||
presence: true,
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
# Table name: users
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# debt_cents :integer default("0"), not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# remember_created_at :datetime
|
||||
|
@ -22,7 +21,8 @@
|
|||
# koelkast :boolean default("f")
|
||||
# provider :string
|
||||
# uid :string
|
||||
# encrypted_password :string
|
||||
# encrypted_password :string default(""), not null
|
||||
# private :boolean default("f")
|
||||
#
|
||||
|
||||
require 'identicon'
|
||||
|
|
|
@ -22,9 +22,9 @@
|
|||
<% if @user.dagschotel %>
|
||||
Huidige dagschotel
|
||||
<%= image_tag @user.dagschotel.avatar %>
|
||||
<%= link_to "Change dagschotel", user_edit_dagschotel_path(@user), class: "btn btn-default btn-block" %>
|
||||
<%= link_to "Change dagschotel", dit_dagschotel_user_path(@user), class: "btn btn-default btn-block" %>
|
||||
<% else %>
|
||||
<%= link_to "Set dagschotel", user_edit_dagschotel_path(@user), class: "btn btn-default btn-block" %>
|
||||
<%= link_to "Set dagschotel", edit_dagschotel_user_path(@user), class: "btn btn-default btn-block" %>
|
||||
<% end %>
|
||||
</li>
|
||||
</ul>
|
||||
|
|
5
bin/delayed_job
Executable file
5
bin/delayed_job
Executable file
|
@ -0,0 +1,5 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
|
||||
require 'delayed/command'
|
||||
Delayed::Command.new(ARGV).daemonize
|
|
@ -20,5 +20,6 @@ module Tab002
|
|||
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
||||
# config.i18n.default_locale = :de
|
||||
config.active_record.raise_in_transactional_callbacks = true
|
||||
config.active_job.queue_adapter = :delayed_job
|
||||
end
|
||||
end
|
||||
|
|
|
@ -45,6 +45,7 @@ namespace :passenger do
|
|||
execute "touch #{current_path}/tmp/restart.txt"
|
||||
end
|
||||
end
|
||||
invoke 'delayed_job:restart'
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -20,9 +20,11 @@ Rails.application.routes.draw do
|
|||
|
||||
resources :users do
|
||||
resources :orders
|
||||
get 'quickpay' => 'orders#quickpay'
|
||||
get 'dagschotel/edit' => 'users#edit_dagschotel', as: 'edit_dagschotel'
|
||||
get 'dagschotel/:product_id' => 'users#update_dagschotel', as: 'dagschotel'
|
||||
member do
|
||||
get 'quickpay' => 'orders#quickpay'
|
||||
get 'dagschotel/edit' => 'users#edit_dagschotel', as: 'edit_dagschotel'
|
||||
get 'dagschotel/:product_id' => 'users#update_dagschotel', as: 'dagschotel'
|
||||
end
|
||||
end
|
||||
|
||||
resources :products
|
||||
|
|
22
db/migrate/20150911195029_create_delayed_jobs.rb
Normal file
22
db/migrate/20150911195029_create_delayed_jobs.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
class CreateDelayedJobs < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :delayed_jobs, force: true do |table|
|
||||
table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue
|
||||
table.integer :attempts, default: 0, null: false # Provides for retries, but still fail eventually.
|
||||
table.text :handler, null: false # YAML-encoded string of the object that will do work
|
||||
table.text :last_error # reason for last failure (See Note below)
|
||||
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
|
||||
table.datetime :locked_at # Set when a client is working on this object
|
||||
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
|
||||
table.string :locked_by # Who is working on this object (if locked)
|
||||
table.string :queue # The name of the queue this job is in
|
||||
table.timestamps null: true
|
||||
end
|
||||
|
||||
add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority"
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :delayed_jobs
|
||||
end
|
||||
end
|
18
db/schema.rb
18
db/schema.rb
|
@ -11,7 +11,23 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20150827155036) do
|
||||
ActiveRecord::Schema.define(version: 20150911195029) do
|
||||
|
||||
create_table "delayed_jobs", force: :cascade do |t|
|
||||
t.integer "priority", default: 0, null: false
|
||||
t.integer "attempts", default: 0, null: false
|
||||
t.text "handler", null: false
|
||||
t.text "last_error"
|
||||
t.datetime "run_at"
|
||||
t.datetime "locked_at"
|
||||
t.datetime "failed_at"
|
||||
t.string "locked_by"
|
||||
t.string "queue"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority"
|
||||
|
||||
create_table "order_items", force: :cascade do |t|
|
||||
t.integer "order_id"
|
||||
|
|
44
lib/capistrano/tasks/delayed_job.cap
Normal file
44
lib/capistrano/tasks/delayed_job.cap
Normal file
|
@ -0,0 +1,44 @@
|
|||
namespace :delayed_job do
|
||||
|
||||
def args
|
||||
fetch(:delayed_job_args, "")
|
||||
end
|
||||
|
||||
def delayed_job_roles
|
||||
fetch(:delayed_job_server_role, :app)
|
||||
end
|
||||
|
||||
desc 'Stop the delayed_job process'
|
||||
task :stop do
|
||||
on roles(delayed_job_roles) do
|
||||
within release_path do
|
||||
with rails_env: fetch(:rails_env) do
|
||||
execute :ruby, 'bin/delayed_job', :stop
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Start the delayed_job process'
|
||||
task :start do
|
||||
on roles(delayed_job_roles) do
|
||||
within release_path do
|
||||
with rails_env: fetch(:rails_env) do
|
||||
execute :ruby, 'bin/delayed_job', args, :start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Restart the delayed_job process'
|
||||
task :restart do
|
||||
on roles(delayed_job_roles) do
|
||||
within release_path do
|
||||
with rails_env: fetch(:rails_env) do
|
||||
execute :ruby, 'bin/delayed_job', args, :restart
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
97
spec/controllers/products_controller_spec.rb
Normal file
97
spec/controllers/products_controller_spec.rb
Normal file
|
@ -0,0 +1,97 @@
|
|||
describe ProductsController, type: :controller do
|
||||
before :each do
|
||||
@admin = create :admin
|
||||
sign_in @admin
|
||||
end
|
||||
|
||||
describe 'GET new' do
|
||||
it 'should render the form' do
|
||||
get :new
|
||||
expect(response).to render_template(:new)
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST create' do
|
||||
context 'successfull' do
|
||||
it 'should create a product' do
|
||||
expect{
|
||||
post :create, product: attributes_for(:product)
|
||||
}.to change{Product.count}.by(1)
|
||||
end
|
||||
|
||||
it 'should redirect to index page' do
|
||||
post :create, product: attributes_for(:product)
|
||||
expect(response).to redirect_to action: :index
|
||||
end
|
||||
end
|
||||
|
||||
context 'failed' do
|
||||
it 'should not create a product' do
|
||||
expect{
|
||||
post :create, product: attributes_for(:invalid_product)
|
||||
}.to_not change{Product.count}
|
||||
end
|
||||
|
||||
it 'should render form' do
|
||||
post :create, product: attributes_for(:invalid_product)
|
||||
expect(response).to render_template(:new)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET index' do
|
||||
it 'should load all the products' do
|
||||
product = create :product
|
||||
get :index
|
||||
expect(assigns :products).to eq([product])
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET edit' do
|
||||
before :each do
|
||||
@product = create :product
|
||||
end
|
||||
|
||||
it 'should render the correct form' do
|
||||
get :edit, id: @product
|
||||
expect(response).to render_template(:edit)
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
|
||||
it 'should load the correct product' do
|
||||
get :edit, id: @product
|
||||
expect(assigns :product).to eq(@product)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT update' do
|
||||
before :each do
|
||||
@product = create :product
|
||||
end
|
||||
|
||||
it 'loads right product' do
|
||||
put :edit, id: @product, product: attributes_for(:product)
|
||||
expect(assigns :product).to eq(@product)
|
||||
end
|
||||
|
||||
context 'successful' do
|
||||
it 'should update attributes' do
|
||||
attributes = attributes_for(:product)
|
||||
attributes.merge(price: (attributes[:price_cents] / 100))
|
||||
attributes.delete(:price_cents)
|
||||
put :update, id: @product, product: attributes
|
||||
new_attributes = @product.reload.attributes.symbolize_keys.slice(*attributes.keys)
|
||||
expect(new_attributes).to eq(attributes.except(:avatar))
|
||||
end
|
||||
end
|
||||
|
||||
context 'failed' do
|
||||
it 'should not update attributes' do
|
||||
old_attributes = @product.reload.attributes
|
||||
put :update, id: @product, product: attributes_for(:invalid_product)
|
||||
expect(@product.reload.attributes).to eq(old_attributes)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
84
spec/controllers/users_controller_spec.rb
Normal file
84
spec/controllers/users_controller_spec.rb
Normal file
|
@ -0,0 +1,84 @@
|
|||
require 'identicon'
|
||||
require 'faker'
|
||||
|
||||
describe UsersController, type: :controller do
|
||||
before :each do
|
||||
@user = create :user
|
||||
sign_in @user
|
||||
end
|
||||
|
||||
describe 'GET show' do
|
||||
before :each do
|
||||
get :show, id: @user
|
||||
end
|
||||
|
||||
it 'should be successful' do
|
||||
expect(response).to render_template(:show)
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
|
||||
it 'should load the correct user' do
|
||||
expect(assigns(:user)).to eq(@user)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET edit' do
|
||||
before :each do
|
||||
get :edit, id: @user
|
||||
end
|
||||
|
||||
it 'should render the form' do
|
||||
expect(response).to render_template(:edit)
|
||||
end
|
||||
|
||||
it 'should load the correct user' do
|
||||
expect(assigns(:user)).to eq(@user)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT update' do
|
||||
it 'should load the correct user' do
|
||||
put :update, id: @user, user: attributes_for(:user)
|
||||
expect(assigns(:user)).to eq(@user)
|
||||
end
|
||||
|
||||
context 'successful' do
|
||||
it 'should update attributes' do
|
||||
new_private = !(@user.private)
|
||||
put :update, id: @user, user: { private: new_private }
|
||||
expect(@user.reload.private).to be new_private
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET index' do
|
||||
before :each do
|
||||
get :index
|
||||
end
|
||||
|
||||
it 'should load an array of all users' do
|
||||
expect(assigns(:users)).to eq([@user])
|
||||
end
|
||||
|
||||
it 'should render the correct template' do
|
||||
expect(response).to render_template(:index)
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET edit_dagschotel' do
|
||||
it 'should render the page' do
|
||||
get :edit_dagschotel, id: @user
|
||||
expect(response).to render_template(:edit_dagschotel)
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET update_dagschotel' do
|
||||
it 'should update the dagschotel' do
|
||||
product = create :product
|
||||
get :update_dagschotel, id: @user, product_id: product
|
||||
expect(@user.reload.dagschotel).to eq(product)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,4 +8,10 @@
|
|||
# count :integer default("0")
|
||||
#
|
||||
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
FactoryGirl.define do
|
||||
factory :order_item do
|
||||
order
|
||||
association :product, factory: :product
|
||||
count { 1 + rand(5) }
|
||||
end
|
||||
end
|
|
@ -10,4 +10,16 @@
|
|||
# cancelled :boolean default("f")
|
||||
#
|
||||
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
require 'faker'
|
||||
|
||||
FactoryGirl.define do
|
||||
factory :order do
|
||||
user
|
||||
transient do
|
||||
products_count 1
|
||||
end
|
||||
before(:create) do |order, evaluator|
|
||||
order.order_items << create_list(:order_item, evaluator.products_count, order: order)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,20 +13,23 @@
|
|||
# avatar_updated_at :datetime
|
||||
# category :integer default("0")
|
||||
# stock :integer default("0"), not null
|
||||
# calories :integer
|
||||
# deleted :boolean default("f")
|
||||
#
|
||||
|
||||
require 'test_helper'
|
||||
require 'faker'
|
||||
require 'identicon'
|
||||
|
||||
class ProductTest < ActiveSupport::TestCase
|
||||
test "price behaves correctly" do
|
||||
p = products(:fanta)
|
||||
FactoryGirl.define do
|
||||
factory :product do
|
||||
name { Faker::Name.name }
|
||||
price_cents { rand 120 }
|
||||
stock { 30 + rand(30) }
|
||||
calories { rand 20 }
|
||||
avatar { Identicon.data_url_for name }
|
||||
|
||||
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
|
||||
factory :invalid_product do
|
||||
name nil
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,7 +3,6 @@
|
|||
# Table name: users
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# debt_cents :integer default("0"), not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# remember_created_at :datetime
|
||||
|
@ -22,22 +21,24 @@
|
|||
# koelkast :boolean default("f")
|
||||
# provider :string
|
||||
# uid :string
|
||||
# encrypted_password :string
|
||||
# encrypted_password :string default(""), not null
|
||||
# private :boolean default("f")
|
||||
#
|
||||
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
require 'faker'
|
||||
require 'identicon'
|
||||
|
||||
benji:
|
||||
uid: benji
|
||||
dagschotel_id: 1
|
||||
FactoryGirl.define do
|
||||
factory :user do
|
||||
uid { Faker::Name.name }
|
||||
avatar { Identicon.data_url_for uid }
|
||||
|
||||
iasoon:
|
||||
uid: iasoon
|
||||
factory :admin do
|
||||
admin true
|
||||
end
|
||||
|
||||
admin:
|
||||
uid: admin
|
||||
admin: 1
|
||||
|
||||
koelkast:
|
||||
uid: koelkast
|
||||
koelkast: 1
|
||||
factory :koelkast do
|
||||
koelkast true
|
||||
end
|
||||
end
|
||||
end
|
41
spec/models/ability_spec.rb
Normal file
41
spec/models/ability_spec.rb
Normal file
|
@ -0,0 +1,41 @@
|
|||
require 'cancan/matchers'
|
||||
|
||||
describe User do
|
||||
describe 'abilities' do
|
||||
subject(:ability){ Ability.new(user) }
|
||||
let(:user) { nil}
|
||||
|
||||
describe 'as admin' do
|
||||
let(:user) { create :admin }
|
||||
|
||||
it{ should be_able_to(:manage, Product.new) }
|
||||
it{ should be_able_to(:manage, Order.new) }
|
||||
it{ should be_able_to(:manage, Stock.new) }
|
||||
it{ should be_able_to(:manage, User.new) }
|
||||
end
|
||||
|
||||
describe 'as normal user' do
|
||||
let(:user) { create :user }
|
||||
|
||||
it{ should be_able_to(:read, Product.new) }
|
||||
it{ should_not be_able_to(:manage, Product.new) }
|
||||
|
||||
it{ should be_able_to(:manage, Order.new(user: user)) }
|
||||
it{ should_not be_able_to(:manage, Order.new) }
|
||||
|
||||
it{ should_not be_able_to(:manage, Stock.new) }
|
||||
|
||||
it{ should be_able_to(:manage, user) }
|
||||
it{ should_not be_able_to(:manage, User.new) }
|
||||
end
|
||||
|
||||
describe 'as koelkast' do
|
||||
let(:user) { create :koelkast }
|
||||
|
||||
it{ should_not be_able_to(:manage, Product.new) }
|
||||
it{ should be_able_to(:manage, Order.new) }
|
||||
it{ should_not be_able_to(:manage, Stock.new) }
|
||||
it{ should_not be_able_to(:manage, User.new) }
|
||||
end
|
||||
end
|
||||
end
|
61
spec/models/order_item_spec.rb
Normal file
61
spec/models/order_item_spec.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: order_items
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# order_id :integer
|
||||
# product_id :integer not null
|
||||
# count :integer default("0")
|
||||
#
|
||||
|
||||
describe OrderItem do
|
||||
it 'has a valid factory' do
|
||||
order_item = create :order_item
|
||||
expect(order_item).to be_valid
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
before :each do
|
||||
@order_item = create :order_item
|
||||
end
|
||||
|
||||
it 'product should be present' do
|
||||
@order_item.product = nil
|
||||
expect(@order_item).to_not be_valid
|
||||
end
|
||||
|
||||
describe 'count' do
|
||||
it 'should be present' do
|
||||
@order_item.count = nil
|
||||
expect(@order_item).to_not be_valid
|
||||
end
|
||||
|
||||
it 'should be positive' do
|
||||
@order_item.count = -5
|
||||
expect(@order_item).to_not be_valid
|
||||
end
|
||||
|
||||
it 'should not be 0' do
|
||||
@order_item.count = 0
|
||||
expect(@order_item).to_not be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'product stock' do
|
||||
before :each do
|
||||
@product = create :product
|
||||
@count = rand 10
|
||||
@order_item = build :order_item, product: @product, count: @count
|
||||
end
|
||||
|
||||
it 'should decrement on create' do
|
||||
expect{@order_item.save}.to change{@product.stock}.by(-@count)
|
||||
end
|
||||
|
||||
it 'should increment on cancel' do
|
||||
@order_item.save
|
||||
expect{@order_item.cancel}.to change{@product.stock}.by(@count)
|
||||
end
|
||||
end
|
||||
end
|
60
spec/models/order_spec.rb
Normal file
60
spec/models/order_spec.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: orders
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# user_id :integer
|
||||
# price_cents :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# cancelled :boolean default("f")
|
||||
#
|
||||
|
||||
describe Order do
|
||||
before :each do
|
||||
@user = create :user
|
||||
@order = create :order, user: @user
|
||||
end
|
||||
|
||||
it 'has a valid factory' do
|
||||
expect(@order).to be_valid
|
||||
end
|
||||
|
||||
describe 'cancelling' do
|
||||
it 'should cancel the order' do
|
||||
@order.cancel
|
||||
expect(@order.cancelled).to be true
|
||||
end
|
||||
|
||||
it 'should not happen twice' do
|
||||
@order.cancel
|
||||
expect(@order.cancel).to be false
|
||||
end
|
||||
|
||||
it 'should not work on old orders' do
|
||||
order = create :order, created_at: 3.days.ago
|
||||
expect(order.cancel).to be false
|
||||
end
|
||||
|
||||
it 'should change the orders_count' do
|
||||
expect{@order.cancel}.to change{@user.reload.orders_count}.by(-1)
|
||||
end
|
||||
|
||||
it 'should cancel the orderitems' do
|
||||
expect(@order.order_items.first).to receive(:cancel)
|
||||
@order.cancel
|
||||
end
|
||||
end
|
||||
|
||||
describe 'price' do
|
||||
it 'should be calculated from order_items' do
|
||||
@order = build :order, products_count: 0
|
||||
sum = (create_list :product, 1 + rand(10)).map do |p|
|
||||
create(:order_item, order: @order, product: p, count: 1 + rand(5)) do |oi|
|
||||
@order.order_items << oi
|
||||
end
|
||||
end.map{ |oi| oi.count * oi.product.price_cents }.sum
|
||||
expect(@order.price_cents).to eq(sum)
|
||||
end
|
||||
end
|
||||
end
|
78
spec/models/product_spec.rb
Normal file
78
spec/models/product_spec.rb
Normal file
|
@ -0,0 +1,78 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: products
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# name :string not null
|
||||
# price_cents :integer default("0"), not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# avatar_file_name :string
|
||||
# avatar_content_type :string
|
||||
# avatar_file_size :integer
|
||||
# avatar_updated_at :datetime
|
||||
# category :integer default("0")
|
||||
# stock :integer default("0"), not null
|
||||
# calories :integer
|
||||
# deleted :boolean default("f")
|
||||
#
|
||||
|
||||
describe Product do
|
||||
before :each do
|
||||
@product = create :product
|
||||
end
|
||||
|
||||
it 'has a valid factory' do
|
||||
expect(@product).to be_valid
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
it 'name should be present' do
|
||||
@product.name = ''
|
||||
expect(@product).to_not be_valid
|
||||
end
|
||||
|
||||
describe 'price' do
|
||||
it 'should be positive' do
|
||||
@product.price = -5
|
||||
expect(@product).to_not be_valid
|
||||
end
|
||||
|
||||
it 'should be saved correctly' do
|
||||
@product.price = 1.20
|
||||
@product.save
|
||||
expect(@product.reload.price).to eq(1.20)
|
||||
expect(@product.reload.price_cents).to eq(120)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'stock' do
|
||||
it 'should be present' do
|
||||
@product.stock = nil
|
||||
expect(@product).to_not be_valid
|
||||
end
|
||||
|
||||
it 'should be positive' do
|
||||
@product.stock = -5
|
||||
expect(@product).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe 'calories' do
|
||||
it 'should not be present' do
|
||||
@product.calories = nil
|
||||
expect(@product).to be_valid
|
||||
end
|
||||
|
||||
it 'should be positive' do
|
||||
@product.calories = -5
|
||||
expect(@product).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it 'avatar should be present' do
|
||||
@product.avatar = nil
|
||||
expect(@product).to_not be_valid
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,7 +3,6 @@
|
|||
# Table name: users
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# debt_cents :integer default("0"), not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# remember_created_at :datetime
|
||||
|
@ -22,17 +21,16 @@
|
|||
# koelkast :boolean default("f")
|
||||
# provider :string
|
||||
# uid :string
|
||||
# encrypted_password :string
|
||||
# encrypted_password :string default(""), not null
|
||||
# private :boolean default("f")
|
||||
#
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class UserTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@user = users(:benji)
|
||||
describe User do
|
||||
before :each do
|
||||
@user = create :user
|
||||
end
|
||||
|
||||
test "to_param" do
|
||||
assert_equal @user.to_param, "#{@user.id}-benji"
|
||||
it 'has a valid factory' do
|
||||
expect(@user).to be_valid
|
||||
end
|
||||
end
|
52
spec/rails_helper.rb
Normal file
52
spec/rails_helper.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
||||
ENV['RAILS_ENV'] ||= 'test'
|
||||
require File.expand_path('../../config/environment', __FILE__)
|
||||
# Prevent database truncation if the environment is production
|
||||
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
||||
require 'spec_helper'
|
||||
require 'rspec/rails'
|
||||
# Add additional requires below this line. Rails is not loaded until this point!
|
||||
|
||||
# Requires supporting ruby files with custom matchers and macros, etc, in
|
||||
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
|
||||
# run as spec files by default. This means that files in spec/support that end
|
||||
# in _spec.rb will both be required and run as specs, causing the specs to be
|
||||
# run twice. It is recommended that you do not name files matching this glob to
|
||||
# end with _spec.rb. You can configure this pattern with the --pattern
|
||||
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
|
||||
#
|
||||
# The following line is provided for convenience purposes. It has the downside
|
||||
# of increasing the boot-up time by auto-requiring all files in the support
|
||||
# directory. Alternatively, in the individual `*_spec.rb` files, manually
|
||||
# require only the support files necessary.
|
||||
#
|
||||
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
|
||||
|
||||
# Checks for pending migrations before tests are run.
|
||||
# If you are not using ActiveRecord, you can remove this line.
|
||||
ActiveRecord::Migration.maintain_test_schema!
|
||||
|
||||
RSpec.configure do |config|
|
||||
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
||||
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
||||
|
||||
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
||||
# examples within a transaction, remove the following line or assign false
|
||||
# instead of true.
|
||||
config.use_transactional_fixtures = true
|
||||
|
||||
# RSpec Rails can automatically mix in different behaviours to your tests
|
||||
# based on their file location, for example enabling you to call `get` and
|
||||
# `post` in specs under `spec/controllers`.
|
||||
#
|
||||
# You can disable this behaviour by removing the line below, and instead
|
||||
# explicitly tag your specs with their type, e.g.:
|
||||
#
|
||||
# RSpec.describe UsersController, :type => :controller do
|
||||
# # ...
|
||||
# end
|
||||
#
|
||||
# The different available types are documented in the features, such as in
|
||||
# https://relishapp.com/rspec/rspec-rails/docs
|
||||
config.infer_spec_type_from_file_location!
|
||||
end
|
101
spec/spec_helper.rb
Normal file
101
spec/spec_helper.rb
Normal file
|
@ -0,0 +1,101 @@
|
|||
require 'coveralls'
|
||||
Coveralls.wear!
|
||||
|
||||
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
|
||||
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
||||
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
||||
# this file to always be loaded, without a need to explicitly require it in any
|
||||
# files.
|
||||
#
|
||||
# Given that it is always loaded, you are encouraged to keep this file as
|
||||
# light-weight as possible. Requiring heavyweight dependencies from this file
|
||||
# will add to the boot time of your test suite on EVERY test run, even for an
|
||||
# individual file that may not need all of that loaded. Instead, consider making
|
||||
# a separate helper file that requires the additional dependencies and performs
|
||||
# the additional setup, and require it from the spec files that actually need
|
||||
# it.
|
||||
#
|
||||
# The `.rspec` file also contains a few flags that are not defaults but that
|
||||
# users commonly want.
|
||||
#
|
||||
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
||||
require 'factory_girl'
|
||||
require 'devise'
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.include FactoryGirl::Syntax::Methods
|
||||
config.include Devise::TestHelpers, type: :controller
|
||||
|
||||
# rspec-expectations config goes here. You can use an alternate
|
||||
# assertion/expectation library such as wrong or the stdlib/minitest
|
||||
# assertions if you prefer.
|
||||
config.expect_with :rspec do |expectations|
|
||||
# This option will default to `true` in RSpec 4. It makes the `description`
|
||||
# and `failure_message` of custom matchers include text for helper methods
|
||||
# defined using `chain`, e.g.:
|
||||
# be_bigger_than(2).and_smaller_than(4).description
|
||||
# # => "be bigger than 2 and smaller than 4"
|
||||
# ...rather than:
|
||||
# # => "be bigger than 2"
|
||||
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
||||
end
|
||||
|
||||
# rspec-mocks config goes here. You can use an alternate test double
|
||||
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
||||
config.mock_with :rspec do |mocks|
|
||||
# Prevents you from mocking or stubbing a method that does not exist on
|
||||
# a real object. This is generally recommended, and will default to
|
||||
# `true` in RSpec 4.
|
||||
mocks.verify_partial_doubles = true
|
||||
end
|
||||
|
||||
# The settings below are suggested to provide a good initial experience
|
||||
# with RSpec, but feel free to customize to your heart's content.
|
||||
=begin
|
||||
# These two settings work together to allow you to limit a spec run
|
||||
# to individual examples or groups you care about by tagging them with
|
||||
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
||||
# get run.
|
||||
config.filter_run :focus
|
||||
config.run_all_when_everything_filtered = true
|
||||
|
||||
# Allows RSpec to persist some state between runs in order to support
|
||||
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
||||
# you configure your source control system to ignore this file.
|
||||
config.example_status_persistence_file_path = "spec/examples.txt"
|
||||
|
||||
# Limits the available syntax to the non-monkey patched syntax that is
|
||||
# recommended. For more details, see:
|
||||
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
||||
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
||||
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
||||
config.disable_monkey_patching!
|
||||
|
||||
# Many RSpec users commonly either run the entire suite or an individual
|
||||
# file, and it's useful to allow more verbose output when running an
|
||||
# individual spec file.
|
||||
if config.files_to_run.one?
|
||||
# Use the documentation formatter for detailed output,
|
||||
# unless a formatter has already been configured
|
||||
# (e.g. via a command-line flag).
|
||||
config.default_formatter = 'doc'
|
||||
end
|
||||
|
||||
# Print the 10 slowest examples and example groups at the
|
||||
# end of the spec run, to help surface which specs are running
|
||||
# particularly slow.
|
||||
config.profile_examples = 10
|
||||
|
||||
# Run specs in random order to surface order dependencies. If you find an
|
||||
# order dependency and want to debug it, you can fix the order by providing
|
||||
# the seed, which is printed after each run.
|
||||
# --seed 1234
|
||||
config.order = :random
|
||||
|
||||
# Seed global randomization in this process using the `--seed` CLI option.
|
||||
# Setting this allows you to use `--seed` to deterministically reproduce
|
||||
# test failures related to randomization by passing the same `--seed` value
|
||||
# as the one that triggered the failure.
|
||||
Kernel.srand config.seed
|
||||
=end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class AdminsControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class CallbacksControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class OrdersControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -1,4 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class ProductsControllerTest < ActionController::TestCase
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class SessionsControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class StockControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -1,4 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class UsersControllerTest < ActionController::TestCase
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class WelcomeControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
0
test/fixtures/.keep
vendored
0
test/fixtures/.keep
vendored
42
test/fixtures/products.yml
vendored
42
test/fixtures/products.yml
vendored
|
@ -1,42 +0,0 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: products
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# name :string not null
|
||||
# price_cents :integer default("0"), not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# avatar_file_name :string
|
||||
# avatar_content_type :string
|
||||
# avatar_file_size :integer
|
||||
# avatar_updated_at :datetime
|
||||
# category :integer default("0")
|
||||
# stock :integer default("0"), not null
|
||||
#
|
||||
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
fanta:
|
||||
id: 1
|
||||
name: 'Fanta'
|
||||
price_cents: 60
|
||||
stock: 100
|
||||
|
||||
cola:
|
||||
id: 2
|
||||
name: 'Cola'
|
||||
price_cents: 60
|
||||
stock: 0
|
||||
|
||||
mate:
|
||||
id: 3
|
||||
name: 'Club Mate'
|
||||
price_cents: 120
|
||||
stock: 100
|
||||
|
||||
bueno:
|
||||
id: 4
|
||||
name: 'Kinder Bueno'
|
||||
stock: 50
|
||||
price_cents: 120
|
|
@ -1,19 +0,0 @@
|
|||
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
|
|
@ -1,39 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class OrderIntegrationTest < ActionDispatch::IntegrationTest
|
||||
def setup
|
||||
Product.all.each do |product|
|
||||
product.avatar = File.new('public/seeds/products/1.jpg', 'r')
|
||||
product.save
|
||||
end
|
||||
|
||||
sign_in users(:koelkast)
|
||||
end
|
||||
|
||||
# test 'orders are saved for the right user' do
|
||||
# visit new_user_order_path(users(:benji))
|
||||
|
||||
# assert page.has_content? 'Order for benji'
|
||||
|
||||
# assert_difference "User.find(users(:benji).id).debt_cents", 240 do
|
||||
# fill_in 'order_order_items_attributes_2_count', with: 2
|
||||
# click_button "Order!"
|
||||
# end
|
||||
# end
|
||||
|
||||
# test 'quickpay' do
|
||||
# assert_difference "User.find(users(:benji).id).debt_cents", User.find(users(:benji).id).dagschotel.price_cents do
|
||||
# visit user_quickpay_path(users(:benji))
|
||||
# assert page.has_content? 'Success!'
|
||||
# end
|
||||
# end
|
||||
|
||||
# test 'cancelling quickpay' do
|
||||
# visit user_quickpay_path(users(:benji))
|
||||
|
||||
# assert_difference "User.find(users(:benji).id).debt_cents", -User.find(users(:benji).id).dagschotel.price_cents do
|
||||
# click_link 'Undo'
|
||||
# assert page.has_content? 'Success!'
|
||||
# end
|
||||
# end
|
||||
end
|
|
@ -1,20 +0,0 @@
|
|||
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
|
|
@ -1,16 +0,0 @@
|
|||
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.cancel
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
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,29 +0,0 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: orders
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# user_id :integer
|
||||
# price_cents :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# cancelled :boolean default("f")
|
||||
#
|
||||
|
||||
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
|
||||
assert_equal @order.price, 3.00
|
||||
end
|
||||
|
||||
test "to_sentence is correct" do
|
||||
assert_equal @order.to_sentence, "1 Fanta and 2 Kinder Buenos"
|
||||
end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class StockEntryTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -1,30 +0,0 @@
|
|||
require "codeclimate-test-reporter"
|
||||
CodeClimate::TestReporter.start
|
||||
|
||||
require 'coveralls'
|
||||
Coveralls.wear!
|
||||
|
||||
ENV['RAILS_ENV'] ||= 'test'
|
||||
require File.expand_path('../../config/environment', __FILE__)
|
||||
require 'rails/test_help'
|
||||
|
||||
require 'capybara/rails'
|
||||
|
||||
class ActiveSupport::TestCase
|
||||
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
|
||||
fixtures :all
|
||||
|
||||
# Add more helper methods to be used by all tests here...
|
||||
end
|
||||
|
||||
class ActionDispatch::IntegrationTest
|
||||
include Warden::Test::Helpers
|
||||
Warden.test_mode!
|
||||
|
||||
def sign_in(user)
|
||||
login_as user, scope: :user
|
||||
end
|
||||
|
||||
# Make the Capybara DSL available in all integration tests
|
||||
include Capybara::DSL
|
||||
end
|
Loading…
Reference in a new issue