Tuesday, May 12, 2009

No Meals

‹prev | My Chain | next›

Having played a bit with meals, I got to wondering what would happen with the recipe couchdb-lucene index if meals were added to the DB. Rather than wonder about it, I'm going to prove it:
    Scenario: Searching recipes with meals and other things in the DB

Given 5 "Yummy" recipes
And 1 "Yummy" meal
And 1 "About this site" document with the word "Yummy" in it
And a 0.5 second wait to allow the search index to be updated
When I search for "yummy"
Then I should see 5 results
Most of these steps are already implemented, but the meal and "About this site" steps are not. They can be defined as:
Given /^(\d+) "([^\"]*)" meals?$/ do |count, keyword|
date = Date.new(2008, 5, 12)

(1..count.to_i).each do |i|
permalink = "id-#{date.to_s}"

meal = {
:title => "#{keyword} meal #{i}",
:date => date + i,
:serves => i,
:summary => "#{keyword} summary",
:description => "#{keyword} description"
}

RestClient.put "#{@@db}/#{permalink}",
meal.to_json,
:content_type => 'application/json'
end
end

Given /^1 "([^\"]*)" document with the word "([^\"]*)" in it$/ do |arg1, arg2|
permalink = "id-#{arg1.gsub(/\W/, '-')}"

doc = {
:title => arg1,
:content => arg2
}

RestClient.put "#{@@db}/#{permalink}",
doc.to_json,
:content_type => 'application/json'
end
Running the scenario generates a big old ball of fail, as expected. Nothing is preventing these new documents from being added to the couchdb-lucene index, but I do not want them in there. I only want to search recipes.

A simple conditional in the indexing javascipt function will do the trick:
  if (doc['preparations']) {
...
}
The conditional exploits the lack of a prepartions attribute in any document other than recipes. Effectively, I index a document only if it quack likes a recipe.

With that, I have my new scenario completely passing:
cstrom@jaynestown:~/repos/eee-code$ cucumber -n features \
-s "Searching recipes with meals and other things in the DB"
Feature: Search for recipes

So that I can find one recipe among many
As a web user
I want to be able search recipes

Scenario: Searching recipes with meals and other things in the DB
Given 5 "Yummy" recipes
And 1 "Yummy" meal
And 1 "About this site" document with the word "Yummy" in it
And a 0.5 second wait to allow the search index to be updated
When I search for "yummy"
Then I should see 5 results

1 scenario
6 passed steps
Before committing, some older scenarios needed adjustment to ensure that simple recipe documents that they were creating also quacked like a recipe.
(commit)

No comments:

Post a Comment