daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Mar 8th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require File.dirname(__FILE__) + '/spec_helper'
  2.  
  3. class Page
  4.   def part_with_array_find(name)
  5.     parts.to_a.find {|p| p.name == name.to_s }
  6.   end
  7.   def part_with_sql_find(name)
  8.     parts.find_by_name name.to_s
  9.   end
  10. end
  11.  
  12. REPEAT = 1000
  13.  
  14. describe Page, '#part(name)' do
  15.   dataset :pages
  16.  
  17.   shared_examples_for "part finders" do
  18.     specify "using array find" do
  19.       REPEAT.times { @page.part_with_array_find("body") }
  20.     end
  21.     specify "using SQL find" do
  22.       REPEAT.times { @page.part_with_sql_find("body") }
  23.     end
  24.     specify "using Array#any? + SQL find (existing part method)" do
  25.       REPEAT.times { @page.part("body") }
  26.     end
  27.   end
  28.  
  29.   describe "on a small set of parts" do
  30.     it_should_behave_like "part finders"
  31.    
  32.     before(:each) do
  33.       @page = pages(:first)
  34.       @page.parts.should_not be_empty
  35.       @page.part("body").should_not be_nil
  36.     end
  37.   end
  38.  
  39.   describe "on a large set of parts" do
  40.     it_should_behave_like "part finders"
  41.    
  42.     before(:each) do
  43.       @page = pages(:first)
  44.       @page.part("body").should_not be_nil
  45.       1000.times do |n|
  46.         @page.parts.create(:name => "part_#{n}", :content => "Lorem ipsum dolor sit amet " * 1000)
  47.       end
  48.       @page.parts.count.should == 1001
  49.     end
  50.   end
  51.  
  52. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top