From 0afd0ce02b5236c7e227eb6569dc5eeb23893988 Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Tue, 15 Nov 2016 20:18:41 +0100 Subject: [PATCH] Iteration B2. --- app/models/product.rb | 2 +- test/fixtures/products.yml | 9 ++++++ test/models/product_test.rb | 69 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/app/models/product.rb b/app/models/product.rb index 85bd632..e8b913e 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -1,7 +1,7 @@ class Product < ApplicationRecord validates :title, :description, :image_url, :price, presence: true validates :price, numericality: {greater_than_or_equal_to: 0.01} - validates :title, uniqueness: true + validates :title, uniqueness: true, length: { minimum: 5, maximum: 30 } validates :image_url, allow_blank: true, format: { with: %r{\.(gif|jpg|png)\Z}i, message: 'must be URL for GIF, JPG or PNG image.' diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index 4abfe6e..a447b94 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -11,3 +11,12 @@ two: description: MyText image_url: MyString price: 9.99 + +ruby: + title: Programming Ruby 1.9 + description: + Ruby is the fastes growing and most exciting dynamic language out + there. If you need to get working programs delivered fast, you should + add Ruby to your toolbox. + price: 49.50 + image_url: ruby.png diff --git a/test/models/product_test.rb b/test/models/product_test.rb index 211cdd0..d70cdf8 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -1,7 +1,76 @@ require 'test_helper' class ProductTest < ActiveSupport::TestCase + # Rails is already doing this! (convention over configuration!) + fixtures :products # 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[:image_url].any? + assert product.errors[:price].any? + end + + test "product price be positive" do + product = Product.new(title: 'My Book Title', + description: 'My Description.', + image_url: 'image.jpg') + + product.price = -1 + assert product.invalid? + assert_equal ['must be greater than or equal to 0.01'], + product.errors[:price] + + product.price = 0 + assert product.invalid? + assert_equal ['must be greater than or equal to 0.01'], + product.errors[:price] + + product.price = 1 + assert product.valid? + end + + def new_product(image_url) + product = Product.new(title: 'My Book Title', + description: 'My Description.', + image_url: image_url, + price: 1) + end + + test "image url" do + ok = %w{ fred.jpg hans.png paul.Png peter.JPG + http://a.b.c.d/images/foo.gif } + bad =%w{ fred.doc hans.gif/more } + + ok.each { |name| assert new_product(name).valid? "#{name} shouldn't be invalid" } + + bad.each do |name| + assert new_product(name).invalid? "#{name} shouldn't be valid" + end + end + + test "product is not valid without a unique title - i18n" do + product = Product.new(title: products(:ruby).title, + description: 'Foo.', + price: 1, + image_url: "foo.jpg") + assert product.invalid? + assert_equal [I18n.translate('errors.messages.taken')], + product.errors[:title] + end + + test "product is not valid without minimum 5 characters in title" do + product = Product.new(title: 'Shrt', + description: 'A short work.', + price: 0.99, + image_url: 'short.jpg') + assert product.invalid? + assert_equal ['is too short (minimum is 5 characters)'], + product.errors[:title] + end end