From 07e133e97ed422436810b7398dcbd780cabea2c3 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 12 Mar 2010 17:02:43 -0800 Subject: Add full stack tests for #take --- spec/shared/relation_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'spec/shared') diff --git a/spec/shared/relation_spec.rb b/spec/shared/relation_spec.rb index c492ca847e..aa03ab6c0a 100644 --- a/spec/shared/relation_spec.rb +++ b/spec/shared/relation_spec.rb @@ -14,6 +14,10 @@ share_examples_for 'A Relation' do raise "@expected needs to have at least 6 items" unless @expected.length >= 6 end + before :each do + @expected = @expected.dup + end + describe "#each" do it "iterates over the rows in any order" do @relation.should have_rows(@expected) @@ -89,4 +93,27 @@ share_examples_for 'A Relation' do it "works" end end + + describe "#take" do + it "returns a relation" do + @relation.take(3).should be_a(Arel::Relation) + end + + it "returns X items from the collection" do + length = @expected.length + + @relation.take(3).each do |resource| + @expected.delete_if { |r| r.tuple == resource.tuple } + end + + @expected.length.should == length - 3 + end + + it "works with ordering" do + expected = @expected.sort_by { |r| [r[@relation[:age]], r[@relation[:id]]] }.map { |r| r[@relation[:id]] } + actual = @relation.order(@relation[:age].asc, @relation[:id].asc).take(3).map { |r| r[@relation[:id]] } + + actual.should == expected[0,3] + end + end end \ No newline at end of file -- cgit v1.2.3