Created
June 30, 2009 07:56
-
-
Save jgarber/138062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.dirname(__FILE__) + '/spec_helper' | |
class Page | |
def part_with_array_find(name) | |
parts.to_a.find {|p| p.name == name.to_s } | |
end | |
def part_with_sql_find(name) | |
parts.find_by_name name.to_s | |
end | |
end | |
REPEAT = 1000 | |
describe Page, '#part(name)' do | |
dataset :pages | |
shared_examples_for "part finders" do | |
specify "using array find" do | |
REPEAT.times { @page.part_with_array_find("body") } | |
end | |
specify "using SQL find" do | |
REPEAT.times { @page.part_with_sql_find("body") } | |
end | |
specify "using Array#any? + SQL find (existing part method)" do | |
REPEAT.times { @page.part("body") } | |
end | |
end | |
describe "on a small set of parts" do | |
it_should_behave_like "part finders" | |
before(:each) do | |
@page = pages(:first) | |
@page.parts.should_not be_empty | |
@page.part("body").should_not be_nil | |
end | |
end | |
describe "on a large set of parts" do | |
it_should_behave_like "part finders" | |
before(:each) do | |
@page = pages(:first) | |
@page.part("body").should_not be_nil | |
1000.times do |n| | |
@page.parts.create(:name => "part_#{n}", :content => "Lorem ipsum dolor sit amet " * 1000) | |
end | |
@page.parts.count.should == 1001 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment