Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
healthkxy committed Mar 4, 2012
1 parent ec98504 commit 64f39fe
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 7 deletions.
47 changes: 47 additions & 0 deletions app/assets/stylesheets/products.css.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
// Place all the styles related to the Products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.products {
table {
border-collapse: collapse;
}

table tr td {
padding: 5px;
vertical-align: top;
}

.list_image {
width: 60px;
height: 70px;
}

.list_description {
width: 60%;

dl {
margin: 0;
}

dt {
color: #244;
font-weight: bold;
font-size: larger;
}

dd {
margin: 0;
}
}

.list_actions {
font-size: x-small;
text-align: right;
padding-left: 1em;
}

.list_line_even {
background: #e0f8f8;
}

.list_line_odd {
background: #f8b0f8;
}
}
7 changes: 7 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
class Product < ActiveRecord::Base
validates :title, :description, :image_url, :presence => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :title, :uniqueness => true
validates :image_url, :format => {
:with => %r{\.(gif|jpg|png)$}i,
:message => 'must be a URL for GIF, JPG or PNG image.'
}
end
3 changes: 1 addition & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>

<body class='<%= controller.controller_name %>'>
<%= yield %>

</body>
Expand Down
10 changes: 8 additions & 2 deletions test/functional/products_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
class ProductsControllerTest < ActionController::TestCase
setup do
@product = products(:one)
@update = {
title: 'Lorem Ipsum',
description: 'Wibbles are fun!',
image_url: 'lorem.jpg',
price: 19.95
}
end

test "should get index" do
Expand All @@ -18,7 +24,7 @@ class ProductsControllerTest < ActionController::TestCase

test "should create product" do
assert_difference('Product.count') do
post :create, product: @product.attributes
post :create, product: @update
end

assert_redirected_to product_path(assigns(:product))
Expand All @@ -35,7 +41,7 @@ class ProductsControllerTest < ActionController::TestCase
end

test "should update product" do
put :update, id: @product, product: @product.attributes
put :update, id: @product, product: @update
assert_redirected_to product_path(assigns(:product))
end

Expand Down
Binary file added test/unit/.product_test.rb.swp
Binary file not shown.
50 changes: 47 additions & 3 deletions test/unit/product_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
require 'test_helper'

class ProductTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "product attributes must not be empty" do
product = Product.new
assert product.invalid?
assert product.errors[:title].any?
assert product.errors[:description].any?
assert product.errors[:price].any?
assert product.errors[:image_url].any?
end

test "product price must be positive" do
product = Product.new(title: "My Book Title",
description: "yyy",
image_url: "zzz.jpg")
product.price = -1
assert product.invalid?
assert_equal "must be greater than or equal to 0.01",
product.errors[:price].join('; ')

product.price = 0
assert product.invalid?
assert_equal "must be greater than or equal to 0.01",
product.errors[:price].join('; ')

product.price = 1
assert product.valid?
end

def new_product(image_url)
Product.new(title: "My Book Title",
description: "yyy",
price: 1,
image_url: image_url)
end

test "image url" do
ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg
http://a.b.c/x/y/z/fred.gif }
bad = %w{ fred.doc fred.gif/more fred.gif.more }

ok.each do |name|
assert new_product(name).valid?, "#{name} shouldn't be invalid"
end

bad.each do |name|
assert new_product(name).invalid?, "#{name} shouldn't be valid"
end
end
end

0 comments on commit 64f39fe

Please sign in to comment.