Ph values

Stephan on testing, software (good and bad), Ruby and the world at large

Archive for the ‘testing’ Category

Ruby, Sequel and Trees

Posted by Stephan on 28. July 2009

While working on some tree structure a couple of unit tests failed, when creating some form of summary of said tree structure. First of all here’s a condensed form of the code:

require 'sequel'

DB = Sequel.sqlite
DB.create_table :items do
  primary_key :id
  String :name
  String :foo, :default => 'NOT SET'
  Integer :item_id
end

class Item < Sequel::Model
  one_to_many :items
  def summary
    if items.empty?
      return [ { self.name => self.foo } ]
    else
      items.inject( [] ){ | r, sub_res | r << sub_res.summary }
    end
  end
end

r1 = Item.create :name => 'R1'
r2 = Item.create :name => 'R2'

r1.add_item r2
r2.foo = 'Ding'
#r2.save

[ r1, r2 ].each{ |i|
  puts "Item    : #{ i.id }"
  puts "Direct Foo: #{ i.foo.inspect }"
  puts "Summary   : #{ i.summary.inspect }"
  puts
}

Notice, that I’ve commented out saving r2. The output is:

Item    : 1
Direct Foo: "NOT SET"
Summary   : [[{"R2"=>"NOT SET"}]]

Item    : 2
Direct Foo: "Ding"
Summary   : [{"R2"=>"Ding"}]

What I didn’t expect – and why the unit tests failed – is the ‘NOT SET’ in the r1 summary.

One way to end up with what I originally expected is to save r2. Another way is to set r2.foo before adding r1 to r1:

r2.foo = 'Ding'
r1.add_item r2

How ever I’m still not sure whether this is intended behaviour and why it should behave like this. (If it is, I’d like to know why.)
What do you think?

Posted in Ruby, testing, Uncategorized | Tagged: , , | Leave a Comment »

Pasting Code Is OK

Posted by Stephan on 10. October 2008

While reading about the DRY principle (for “Don’t repeat yourself”) and the evil of copy-and-paste coding (again), I started thinking what to do instead. Actually, what to do is more or less obvious: Put the code into a place where its accessible to be reused — a method, may be in a new or existing module or class.

So, whenever I feel the ‘need’ to copy code, I now think about cutting it, creating a new method and calling that. Apart form avoiding duplication, the code is now testable immediately by calling the method (instead of getting the surrounding code exercised). And the methods using the code gets shorter.

In the end, it boils down to pasting code being perfectly OK, it’s the copying that causes the trouble.

Posted in method call, Programming, testing, Uncategorized | Tagged: , | Leave a Comment »

RejectConf in Berlin 2007

Posted by Stephan on 19. September 2007

During the RejectConf 2007 in Berlin yesterday evening I talked about testing, contraints and the relationship between testers and developers. The talk is available as a PDF file at “Test, Test, Test”.

Thank you to all the other presenters and the Berlin Ruby User Group for providing the location etc.

Posted in RailsConfEurope, RailsConfEurope07, testing | Leave a Comment »

Berlin – Day 3 / RailsConf Europe part 1 – Tutorials

Posted by Stephan on 17. September 2007

The morning started with me finding out that breakfast at my hotel is served from 8 to 11 — while I had to leave before 8. Anyway there’s so many places in Berlin to get a great breakfast. So I had it on the way to the conference.

The 1st tutorial I atended was Trotter Cashion’s “Refactoring”. that was a good introduction as well as a few great tips for tools to help refactoring:

  • heckle: ‘Bebugs’ your code automatically and will tell you what you didn’t cover in you test cases in quite a few cases. It dynamically changes you code by turning ifs to unlesses, ‘==’ to ‘!=’ etc. You get the idea. It then checks whether you unit/tests are failing, i.e. wether they would allow you to find this particular fault. Which they likely won’t in so many cases.
  • grep: Well, it’s grep.
  • rcov: Another tool to tell you about test coverage. This one generates HMTL reports which show which code is and isn’t executed when running your test cases. Very useful a lot faster than heckle, at the price of being less rigousus.
  • glark: A new way for grepping.

After than lunch time revealed that this time lunch is a lot better than at last year’s RailsConf in London. In fact is was excellent. Fresh fish, salads, lettuce, desserts. Options – more than one indeed – for vegetarians.
A minor issue is, just like in London last year, the wireless network: With about 300 attendees around, the network is really really slow.

Now, I’m attending the GIS tutorial (while typing this post). Anyway the accompanying script seems to be a good starting point.

And my RejectConf talk still isn’t done…

Posted in Programming, RailsConfEurope, RailsConfEurope07, testing | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.