Thursday, October 18, 2012

DDD summary book

For those lazy or just struggling to find enough time for reading the full version of the DDD book find out this prune summary. The book is definitely worth reading for software engineers and architects. It describes the basics of modeling and how to model business processes with business goals in the first place. I consider it to be on the same shelf as Martin Fowler's book PEAA.

Wednesday, October 17, 2012

Tiny cucumber test issue

I was working on a project the other day, and out of the blue there was a failing test which was not failing before.

The cucumber test reads as follows:

Scenario: Index action (No filtering)
Given the following persons with time_submitted exist:

id email name time_submitted
1 person1@example.com person1 1347978729
2 person2@example.com person2 1346802773
When I visit "/index.json"
Then the JSON response should have 2 persons
And the JSON response at row 0:time_since_last_update should be approximately 3 week(s) ago
And the JSON response at row 1:time_since_last_update should be approximately 1 month(s) ago

The issue here is that we rely on the hardcoded value of the time submitted. And when we run out of the timeframe window the test starts failing.
To resolve the issue we need to come up with some dynamically evaluated value. Certainly, my initial attempt was to update the number with 2.weeks.ago. The test failed one more time claiming that the object is a string. That made me poke around the documentation a little bit. And what I came up is method send which allows you to execute method by name.

And so after refactoring my test input table looks so:

Scenario: Index action (No filtering)
Given the following persons with time_submitted exist:
id email name time_submitted period
1 person1@example.com person1 3 weeks
2 person2@example.com person2 1 months
When I visit "/index.json"
Then the JSON response should have 2 persons
And the JSON response at row 0:time_since_last_update should be approximately 3 week(s) ago
And the JSON response at row 1:time_since_last_update should be approximately 1 month(s) ago

and the code behind looks like
Given /the following persons with time_submitted exist:/ do |table|
  table.hashes.each do |hash|
    value = PersonAttributeRecord.new
    period = hash['period']
    number = hash['time_submitted'].to_i

// Here comes the magic
    value.submitted = number.send(period).ago

    Person.create ({:id => hash['id'], :email => hash['email'], :name => hash['name'], :time_submitted_db => value})
  end
end

So we parse the number from the table. And execute either months or weeks method (or whatever method we need) on it. And then subtract it from the current date.
That's it. Sometimes those tiny little tricks help a lot!
PS: Thanks to Kory Kraft who put a lot of effort into this little thing as well