Skip to content

Commit

Permalink
Added more tests for models
Browse files Browse the repository at this point in the history
  • Loading branch information
chugh97 committed Mar 1, 2014
1 parent b583341 commit a207b04
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Ignore bundler config.
/.bundle

.idea/*
projectFilesBackup/*

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ group :doc do
end

group :development, :test do
gem 'pry'
gem 'rspec-rails'
gem 'shoulda-matchers'
gem 'shoulda'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GEM
atomic (1.1.14)
bcrypt-ruby (3.1.2)
builder (3.1.4)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -68,6 +69,7 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.2)
mime-types (1.25)
mini_portile (0.5.1)
minitest (4.7.5)
Expand All @@ -93,6 +95,10 @@ GEM
omniauth-paypal (1.2.1)
omniauth-oauth2 (~> 1.1.0)
polyglot (0.3.3)
pry (0.9.12.6)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
Expand Down Expand Up @@ -138,6 +144,7 @@ GEM
shoulda-context (1.1.6)
shoulda-matchers (2.5.0)
activesupport (>= 3.0.0)
slop (3.4.7)
sprockets (2.10.0)
hike (~> 1.2)
multi_json (~> 1.0)
Expand Down Expand Up @@ -172,6 +179,7 @@ DEPENDENCIES
jquery-rails
mysql2
omniauth-paypal
pry
rails (= 4.0.1)
rspec-rails
sass-rails (~> 4.0.0)
Expand Down
9 changes: 4 additions & 5 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
class Cart < ActiveRecord::Base
belongs_to :product #foreign_key product_id
belongs_to :product #foreign_key product_id

def self.total(session_id)
self.where('session_id = ?', session_id).sum('unit_price * quantity')
end

def self.items_in_cart(session_id)
cart_json = self.where('session_id = ?', session_id).as_json

cart_json.collect do |line_item|
product = line_item['product']

{
:name => product['name'],
:number => product['description'],
:description => product['description'],
:quantity => line_item['quantity'],
:amount => line_item['unit_price'] * 100
:amount => line_item['unit_price'] * line_item['quantity']
}
end

end

def as_json(options={ })
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20140228233434_change_column_in_carts_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeColumnInCartsTable < ActiveRecord::Migration
def change
change_column :carts, :unit_price, :decimal, {:precision => 8, :scale =>2}
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131209231704) do
ActiveRecord::Schema.define(version: 20140228233434) do

create_table "address_types", force: true do |t|
t.string "description"
Expand All @@ -31,7 +31,7 @@
t.string "session_id"
t.integer "product_id"
t.integer "quantity"
t.decimal "unit_price", precision: 10, scale: 0
t.decimal "unit_price", precision: 8, scale: 2
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "purchased_at"
Expand Down
18 changes: 18 additions & 0 deletions on.collect do |line_item|
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

From: /home/shaleenchugh/development/eshop/app/models/cart.rb @ line 10 Cart.items_in_cart:

8: def self.items_in_cart(session_id)
9: cart_json = self.where('session_id = ?', session_id).as_json
=> 10: binding.pry
11: cart_json.collect do |line_item|
12: product = line_item['product']
13: {
14: :name => product['name'],
15: :number => product['description'],
16: :quantity => line_item['quantity'],
17: :amount => line_item['unit_price'] * 100
18: }
19: end
20:
21: end

6 changes: 6 additions & 0 deletions spec/models/address_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Address do
it { should belong_to :user }
it { should belong_to :address_type }
end
5 changes: 5 additions & 0 deletions spec/models/address_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe AddressType do
it { should have_many :addresses }
end
25 changes: 25 additions & 0 deletions spec/models/cart_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Cart do
before :each do
cart_item = Cart.new :product_id => 18, :unit_price => 6.50, :quantity => 7, :session_id => 999
cart_item2 = Cart.new :product_id => 29, :unit_price => 12.50, :quantity => 10, :session_id => 999
cart_item.save
cart_item2.save
end
it { should belong_to :product }

it "should get the right total" do
expect(described_class.total(999)).to eq(170.50)
end

it "should have the correct items" do
described_class.any_instance.stub(:as_json).and_return('product' => {'name' => 'Soap','description' => "Some desc" },
'quantity' => 10, 'unit_price' => 5
)

result = [{:name=>"Soap", :description=>"Some desc", :quantity=>10, :amount=>50}, {:name=>"Soap", :description=>"Some desc", :quantity=>10, :amount=>50}]
expect(described_class.items_in_cart(999)).to eq(result)
end

end
5 changes: 5 additions & 0 deletions spec/models/category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Category do
it { should have_many :products }
end
34 changes: 16 additions & 18 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe User do

it { should have_many :orders }
it { should have_many :addresses }
it { should have_many :phones }

=begin
decribe "callbacks" do
it "should call hash_password before a save" do
model = stub_model(User)
model.stub(:valid?).and_return(true)
model.should_receive(:hash_password)
model.save
end
describe "callbacks" do
it "should call hash_password before a save" do
model = stub_model(User)
model.stub(:valid?).and_return(true)
model.should_receive(:hash_password)
model.save
end

# test your method works...
it "should set hash the password" do
model = stub_model(User, :encrypted_password => "donotreveal").as_new_record
Digest::SHA1.stub(:hexdigest).and_return('other')
model.send :hash_password
model.encrypted_password.should_not == "donotreveal"
end
it "should set hash the password" do
model = stub_model(User, :encrypted_password => "donotreveal").as_new_record
Digest::SHA1.stub(:hexdigest).and_return('other')
model.send :hash_password
model.encrypted_password.should_not == "donotreveal"
end
end
end
end=end

0 comments on commit a207b04

Please sign in to comment.