jgarber (owner)

Revisions

gist: 138062 Download_button fork
public
Public Clone URL: git://gist.github.com/138062.git
page_part_finder_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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