Saturday, April 4, 2009

Trying to Specify Images

‹prev | My Chain | next›

I figured out how to display images in the last link of my chain. Now I just need to specify how I want it to work:
    describe 'GET /recipes/:permalink/:image' do
it "should retrieve the image" do
jpeg = File.open('spec/fixtures/sample.jpeg').read
pending
end

it "should return 404 for a non-existent image" do
pending
end
end
The two cases I need to specify are: if the image is there, show it and if the image is not there, then return a 404 / File not found error. Simple, right? When I run the spec however, I get my one previously passing spec, one pending spec and one big old failure:
./spec/eee_spec.rb:44

1)
RestClient::RequestFailed in 'eee a CouchDB recipe GET /recipes/:permalink/:image should return 404 for a non-existent image'
HTTP status code 409
/home/cstrom/.gem/ruby/1.8/gems/rest-client-0.9.2/lib/restclient/request.rb:144:in `process_result'
/home/cstrom/.gem/ruby/1.8/gems/rest-client-0.9.2/lib/restclient/request.rb:106:in `transmit'
/home/cstrom/.gem/ruby/1.8/gems/rest-client-0.9.2/lib/restclient/request.rb:103:in `transmit'
/home/cstrom/.gem/ruby/1.8/gems/rest-client-0.9.2/lib/restclient/request.rb:36:in `execute_inner'
/home/cstrom/.gem/ruby/1.8/gems/rest-client-0.9.2/lib/restclient/request.rb:28:in `execute'
/home/cstrom/.gem/ruby/1.8/gems/rest-client-0.9.2/lib/restclient/request.rb:12:in `execute'
/home/cstrom/.gem/ruby/1.8/gems/rest-client-0.9.2/lib/restclient.rb:65:in `put'
./spec/eee_spec.rb:20:
./spec/eee_spec.rb:3:

Finished in 0.07537 seconds

3 examples, 1 failure, 1 pending
The 409 HTTP error is a conflict. The conflict in this case is occurring in the before(:each) block that creates the recipe in CouchDB. In the new describe block, there are two cases (image exists / image does not exist). The recipe is created for the first example (even though it is pending), but it is not rolled back afterwards—this is CouchDB, not a relational database.

To resolve this issue, I need to manually rollback the newly created CouchDB record after each example. Fortunately, RSpec has the after(:each) block for exactly this purpose:
    after(:each) do
data = RestClient.get "#{@@db}/#{@permalink}"
recipe = JSON.parse(data)

RestClient.delete "#{@@db}/#{@permalink}?rev=#{recipe['_rev']}"
end
For a delete, CouchDB requires that the revision number needs to be supplied, which is the reason for the GET and the parse in the after(:each) block.

When I run the specification again, I get one passing example and two pending, just as I expected:
cstrom@jaynestown:~/repos/eee-code$ ruby ./spec/eee_spec.rb 
.**

Pending:

eee a CouchDB recipe GET /recipes/:permalink/:image should retrieve the image (TODO)
./spec/eee_spec.rb:44

eee a CouchDB recipe GET /recipes/:permalink/:image should return 404 for a non-existent image (TODO)
./spec/eee_spec.rb:48

Finished in 0.060699 seconds

3 examples, 0 failures, 2 pending
Unfortunate that I needed to do a bit of yak shaving tonight, but I am better positioned for future specifications. It is also good to learn how to delete CouchDB records via REST.

No comments:

Post a Comment