Each sub-resources I add to an API representation introduces an n+1.
I'd like a test to fail until I adjust the eager-loading accordingly.
Seems pragmatic, despite the dangers of testing for performance optimisations.
I've gone with this for the moment:
https://gist.github.com/3606669
The QuerySpy helps assert that increasing the number of records fetched does not increase the number of queries:
it "should not exhibit n+1" do
add_thing
count1 = QuerySpy.count { get "/things" }
add_thing
count2 = QuerySpy.count { get "/things" }
count2.should <= count1
end
The API could be fancier, but it does the job.
I can break eager-loading and see it fail, fix it and see it pass.
— Paul