Saturday, August 29, 2009

Brain Dead Mini-Calendar

‹prev | My Chain | next›

After a day trip to Brooklyn that included no less than 8 hours of driving, my brain is not functioning much. Having deployed my latest feature to the beta site, it is time to start work on the next feature. Ordinarily, I would pick up the next incomplete Cucumber scenario. Today, I choose the easiest, the homepage mini-calendar.

For reference, the mini-calendar on the legacy homepage looks like:



The Cucumber feature description includes only a single scenario. That may be all that is necessary as the scenario includes the default page, going back to a month without any meals and then back to a month with meals. This is a simple calendar, and a single scenario seems (at least to my brain tonight) to be sufficient.

The Given steps have already been implemented:



To verify that the mini-calendar is visitable. I start by driving its implementation with the following RSpec example:
  describe "GET /mini/" do
it "should respond OK" do
get "/mini/"
last_response.should be_ok
end
end
That fails:
cstrom@jaynestown:~/repos/eee-code$ spec ./spec/eee_spec.rb 
..........................................F

1)
'/mini GET /mini/ should respond OK' FAILED
expected ok? to return true, got false
./spec/eee_spec.rb:541:
I make it pass with a new Sinatra action:
get %r{/mini/.*} do
""
end
Very quickly, I work my way back out to the Cucumber scenario to mark the "When I visit the mini-calendar" step as complete:
When /^I visit the mini\-calendar$/ do
visit("/mini/")
end
The next step in the scenario is that the default mini-calendar page should be the month with the most recent meal. My "meals/count_by_month" reduced CouchDB view is the perfect tool to get this job done, so the mini action needs to grab it. In RSpec parlance:
    it "should retrieve the most recent month with a meal" do
RestClient.
should_receive(:get).
with(/meals.+count_by_month.+descending=true/).
and_return('{"rows": [{"key":"2009-08","value":3}]}')

get "/mini/"
end
The thing that I am testing here is that CouchDB is being queried for the count_by_month in descending order (i.e. with the most recent month first). The return value needs to be a CouchDB result. To implement that example:
get %r{/mini/.*} do
url = "#{@@db}/_design/meals/_view/count_by_month?group=true\&limit=1\&descending=true"
data = RestClient.get url

""
end
Finally to do something with that query, the following example requires that the month is displayed:
    it "should display the month" do
get "/mini/"
last_response.
should have_selector("h1", :content => "2009-08")
end
And to get that example to pass, I add a h1 tag to the output:
get %r{/mini/.*} do
url = "#{@@db}/_design/meals/_view/count_by_month?group=true\&limit=1\&descending=true"
data = RestClient.get url
@last_month = JSON.parse(data)['rows'].first['key']

"<h1>#{@last_month}</h1>"

end
That is a good stopping point for today. I will pick back up with this feature tomorrow, starting with moving that HTML into a Haml template.

No comments:

Post a Comment