Permalink
Browse files

Add full stack tests for #take

  • Loading branch information...
1 parent e9c71a8 commit 07e133e97ed422436810b7398dcbd780cabea2c3 Carl Lerche committed Mar 13, 2010
Showing with 29 additions and 1 deletion.
  1. +2 −1 lib/arel/algebra/relations/operations/take.rb
  2. +27 −0 spec/shared/relation_spec.rb
@@ -1,7 +1,8 @@
module Arel
class Take < Compound
attributes :relation, :taken
- deriving :initialize, :==
+ deriving :initialize, :==
+ requires :limiting
def externalizable?
true
@@ -14,6 +14,10 @@
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 @@
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

0 comments on commit 07e133e

Please sign in to comment.