Friday, December 31, 2010

Rails4 - Iteration B2 Product controller tests fail.

The book solution demos adding an @update product not conflicting with the default fixtures :one and :two, which by default have the same title. (after adding uniqueness test on title, these fail).

It also works to fix the test data, just update the products.yml file and provide a unique title for :one and :two

one:
title: MyString One
description: MyText
image_url: MyString
price: 9.99

two:
title: MyString Two
description: MyText
image_url: MyString
price: 9.99

This allows the update test to pass no problem.

Note: you do still need to provide a unique title in should create product (where using :one as a template). Simple as adding one line into the method:
#after validation added, now required to provide a unique title.
@product.title="a unique test title"
#this is the part that takes the form input..
post :create, :product => @product.attributes

Thursday, December 30, 2010

Rails4: Iteration B2 - generic def for new default valid products

Building on example for "def new_product(image_url)" pg. 101

#a generic method to build new test produce with valid data
#but also allow insertion of any attributes that need to be over-ridden
#takes a hash of attribute values, checks for each and populates default as needed.
#don't add this to real Product, 'cause production code shouldn't provide defaults.
def new_valid_product(data)
Product.new(
:title => data.has_key?(:title) ? data[:title] : "A Book Title",
:description => data.has_key?(:description) ? data[:description] : "ipso lorum... a default description",
:price => data.has_key?(:price) ? data[:price] : 123.45,
:image_url => data.has_key?(:image_url) ? data[:image_url] : "test.gif"
)
end

#a quick test to warn me if any rules change that invalidate the default values.
test "default product attributes are valid" do
product = new_valid_product({})
assert !product.errors[:title].any?, "default title in error"
assert !product.errors[:description].any?, "default_description in error"
assert !product.errors[:price].any?, "default_price in error"
assert !product.errors[:image_url].any?, "default image_url in error"
end

#a quick test to prove to myself that the method accepts input data & uses it correctly.
test "default product attributes can be changed" do
product = new_valid_product({:title => "T", :description => "D", :price => 1, :image_url => "I"})
assert_equal "T", product.title
assert_equal "D", product.description
assert_equal 1, product.price
assert_equal "I", product.image_url
end

Note: My guess it that this kind of method is not really useful, Ruby has fixtures, my suspicion is that in time I'll find it much easier to define and get a fixture, then change a value or two, rather than write this kind of utility method for each model unit test class.

Tuesday, December 28, 2010

Rails4 - curl to download directory content

thanks to: http://www.linuxquestions.org/questions/programming-9/using-curl-to-dl-files-from-http-sites-with-wildcard-379067/ for the initial curl script line and to http://www.linuxquestions.org/questions/programming-9/extract-substring-using-sed-and-regular-expressions-regexp-702074/ for help extracting the filename from html.

for file in `curl http://media.pragprog.com/titles/rails4/code/depot_b/public/images/ | perl -wlne 'print $1 if
/href="(.*[gif|png|jpg])">/'`;do curl -o ${file} http://media.pragprog.com/titles/rails4/code/depot_b/public/images/${file};done

*Note: not happy with having to specify the file types, but the page HTML contains other links like Download, Parent etc so href=.* litters the results with junk files.


*Note would have been easier with wget, but for some reason darwin-ports for leopard DMG was unavailable.
should I get darwin-ports installed try this instead.

run from depot/public/images directory
wget -rkp -np -nH --cut-dirs=1 http://media.pragprog.com/titles/rails4/code/depot_b/public/images

@see http://psung.blogspot.com/2008/06/using-wget-or-curl-to-download-web.html