diff options
Diffstat (limited to 'spec/active_relation')
-rw-r--r-- | spec/active_relation/unit/predicates/binary_spec.rb | 14 | ||||
-rw-r--r-- | spec/active_relation/unit/primitives/attribute_spec.rb | 34 | ||||
-rw-r--r-- | spec/active_relation/unit/primitives/expression_spec.rb | 2 | ||||
-rw-r--r-- | spec/active_relation/unit/primitives/value_spec.rb | 28 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/grouping_spec.rb | 33 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/join_spec.rb | 33 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/order_spec.rb | 14 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/projection_spec.rb | 37 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/range_spec.rb | 35 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/relation_spec.rb | 97 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/rename_spec.rb | 64 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/selection_spec.rb | 20 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/skip_spec.rb | 20 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/table_spec.rb | 10 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/take_spec.rb | 20 |
15 files changed, 221 insertions, 240 deletions
diff --git a/spec/active_relation/unit/predicates/binary_spec.rb b/spec/active_relation/unit/predicates/binary_spec.rb index 5dd5e5599a..56c568b078 100644 --- a/spec/active_relation/unit/predicates/binary_spec.rb +++ b/spec/active_relation/unit/predicates/binary_spec.rb @@ -57,20 +57,6 @@ module ActiveRelation end end - describe '#qualify' do - it "descends" do - ConcreteBinary.new(@attribute1, @attribute2).qualify \ - .should == ConcreteBinary.new(@attribute1, @attribute2).descend(&:qualify) - end - end - - describe '#descend' do - it "distributes a block over the predicates and attributes" do - ConcreteBinary.new(@attribute1, @attribute2).descend(&:qualify). \ - should == ConcreteBinary.new(@attribute1.qualify, @attribute2.qualify) - end - end - describe '#bind' do before do @another_relation = Table.new(:photos) diff --git a/spec/active_relation/unit/primitives/attribute_spec.rb b/spec/active_relation/unit/primitives/attribute_spec.rb index fbbcbaef37..471ae91b5b 100644 --- a/spec/active_relation/unit/primitives/attribute_spec.rb +++ b/spec/active_relation/unit/primitives/attribute_spec.rb @@ -21,16 +21,10 @@ module ActiveRelation end it "returns self if the substituting to the same relation" do - @attribute.should == @attribute + @attribute.bind(@relation).should == @attribute end end - describe '#qualify' do - it "manufactures an attribute aliased with that attribute's qualified name" do - @attribute.qualify.should == Attribute.new(@attribute.relation, @attribute.name, :alias => @attribute.qualified_name, :ancestor => @attribute) - end - end - describe '#to_attribute' do it "returns self" do @attribute.to_attribute.should == @attribute @@ -40,16 +34,13 @@ module ActiveRelation describe '#column' do it "returns the corresponding column in the relation" do - pending "damn mock based tests are too easy" - stub(@relation).column_for(@attribute) { 'bruisers' } - @attribute.column.should == 'bruisers' + @attribute.column.should == @relation.column_for(@attribute) end end describe '#qualified_name' do it "manufactures an attribute name prefixed with the relation's name" do - stub(@relation).prefix_for(anything) { 'bruisers' } - Attribute.new(@relation, :id).qualified_name.should == 'bruisers.id' + @attribute.qualified_name.should == "#{@relation.prefix_for(@attribute)}.id" end end @@ -61,6 +52,7 @@ module ActiveRelation describe Attribute::Congruence do describe '=~' do + it "obtains if the attributes are identical" do Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name) end @@ -80,13 +72,21 @@ module ActiveRelation end describe '#to_sql' do - it "" do - pending "this test is not sufficiently resilient" + describe 'for a simple attribute' do + it "manufactures sql with an alias" do + @attribute.to_sql.should be_like("`users`.`id`") + end end - it "manufactures sql with an alias" do - stub(@relation).prefix_for(anything) { 'bruisers' } - Attribute.new(@relation, :name, :alias => :alias).to_sql.should be_like("`bruisers`.`name`") + describe 'for an attribute in a join relation where the source relation is aliased' do + before do + another_relation = Table.new(:photos) + @join_with_alias = @relation.as(:alias).join(another_relation).on(@relation[:id].eq(another_relation[:user_id])) + end + + it "manufactures sql with an alias" do + @join_with_alias[@attribute].to_sql.should be_like("`alias`.`id`") + end end end diff --git a/spec/active_relation/unit/primitives/expression_spec.rb b/spec/active_relation/unit/primitives/expression_spec.rb index dda35157b0..c15b073ba3 100644 --- a/spec/active_relation/unit/primitives/expression_spec.rb +++ b/spec/active_relation/unit/primitives/expression_spec.rb @@ -14,7 +14,7 @@ module ActiveRelation describe '#bind' do it "manufactures an attribute with a rebound relation and self as the ancestor" do - derived_relation = @relation.select(@relation[:id] == 1) + derived_relation = @relation.select(@relation[:id].eq(1)) @expression.bind(derived_relation).should == Expression.new(@attribute.bind(derived_relation), "COUNT", nil, @expression) end diff --git a/spec/active_relation/unit/primitives/value_spec.rb b/spec/active_relation/unit/primitives/value_spec.rb new file mode 100644 index 0000000000..ab632ef987 --- /dev/null +++ b/spec/active_relation/unit/primitives/value_spec.rb @@ -0,0 +1,28 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') + +module ActiveRelation + describe Value do + before do + @relation = Table.new(:users) + end + + describe '#to_sql' do + it "appropriately quotes the value" do + Value.new(1, @relation).to_sql.should be_like('1') + Value.new('asdf', @relation).to_sql.should be_like("'asdf'") + end + end + + describe '#format' do + it "returns the sql of the provided object" do + Value.new(1, @relation).format(@relation[:id]).should == @relation[:id].to_sql + end + end + + describe '#bind' do + it "manufactures a new value whose relation is the provided relation" do + Value.new(1, @relation).bind(another_relation = Table.new(:photos)).should == Value.new(1, another_relation) + end + end + end +end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/grouping_spec.rb b/spec/active_relation/unit/relations/grouping_spec.rb new file mode 100644 index 0000000000..4b5badbb8b --- /dev/null +++ b/spec/active_relation/unit/relations/grouping_spec.rb @@ -0,0 +1,33 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') + +module ActiveRelation + describe Grouping do + before do + @relation = Table.new(:users) + @attribute = @relation[:id] + end + + describe '#to_sql' do + describe 'when given a predicate' do + it "manufactures sql with where clause conditions" do + Grouping.new(@relation, @attribute).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + GROUP BY `users`.`id` + ") + end + end + + describe 'when given a string' do + it "passes the string through to the where clause" do + pending 'it should not quote asdf' + Grouping.new(@relation, 'asdf').to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + GROUP BY asdf + ") + end + end + end + end +end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/join_spec.rb b/spec/active_relation/unit/relations/join_spec.rb index 423e513be4..c1a13cee6e 100644 --- a/spec/active_relation/unit/relations/join_spec.rb +++ b/spec/active_relation/unit/relations/join_spec.rb @@ -32,20 +32,6 @@ module ActiveRelation end end - describe '#qualify' do - it 'descends' do - Join.new("INNER JOIN", @relation1, @relation2, @predicate).qualify. \ - should == Join.new("INNER JOIN", @relation1, @relation2, @predicate).descend(&:qualify) - end - end - - describe '#descend' do - it 'distributes over the relations and predicates' do - Join.new("INNER JOIN", @relation1, @relation2, @predicate).qualify. \ - should == Join.new("INNER JOIN", @relation1.qualify, @relation2.qualify, @predicate.qualify) - end - end - describe '#prefix_for' do describe 'when the joined relations are simple' do it "returns the name of the relation containing the attribute" do @@ -110,10 +96,9 @@ module ActiveRelation describe 'when joining aggregated relations' do before do - @aggregation = @relation2 \ - .aggregate(@relation2[:user_id], @relation2[:id].count) \ - .group(@relation2[:user_id]) \ - .rename(@relation2[:id].count, :cnt) \ + @aggregation = @relation2 \ + .group(@relation2[:user_id]) \ + .project(@relation2[:user_id], @relation2[:id].count.as(:cnt)) \ .as('photo_count') end @@ -159,6 +144,18 @@ module ActiveRelation end end + describe 'when joining aliased relations' do + it 'aliases the table and attributes properly' do + aliased_relation = @relation1.as(:alias) + @relation1.join(aliased_relation).on(@relation1[:id].eq(aliased_relation[:id])).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name`, `alias`.`id`, `alias`.`name` + FROM `users` + INNER JOIN `users` AS `alias` + ON `users`.`id` = `alias`.`id` + ") + end + end + describe 'when joining with a string' do it "passes the string through to the where clause" do Join.new("INNER JOIN asdf ON fdsa", @relation1).to_sql.should be_like(" diff --git a/spec/active_relation/unit/relations/order_spec.rb b/spec/active_relation/unit/relations/order_spec.rb index db322fe00b..e8a2f4c227 100644 --- a/spec/active_relation/unit/relations/order_spec.rb +++ b/spec/active_relation/unit/relations/order_spec.rb @@ -18,20 +18,6 @@ module ActiveRelation end end - describe '#qualify' do - it "descends" do - Order.new(@relation, @attribute).qualify. \ - should == Order.new(@relation, @attribute).descend(&:qualify) - end - end - - describe '#descend' do - it "distributes a block over the relation and attributes" do - Order.new(@relation, @attribute).descend(&:qualify). \ - should == Order.new(@relation.descend(&:qualify), @attribute.qualify) - end - end - describe '#to_sql' do describe "when given an attribute" do it "manufactures sql with an order clause populated by the attribute" do diff --git a/spec/active_relation/unit/relations/projection_spec.rb b/spec/active_relation/unit/relations/projection_spec.rb index f58564840b..5936cbc45f 100644 --- a/spec/active_relation/unit/relations/projection_spec.rb +++ b/spec/active_relation/unit/relations/projection_spec.rb @@ -30,20 +30,6 @@ module ActiveRelation end end - describe '#qualify' do - it "descends" do - Projection.new(@relation, @attribute).qualify. \ - should == Projection.new(@relation, @attribute).descend(&:qualify) - end - end - - describe '#descend' do - it "distributes a block over the relation and attributes" do - Projection.new(@relation, @attribute).descend(&:qualify). \ - should == Projection.new(@relation.descend(&:qualify), @attribute.qualify) - end - end - describe '#to_sql' do describe 'when given an attribute' do it "manufactures sql with a limited select clause" do @@ -67,16 +53,29 @@ module ActiveRelation end describe 'when given a string' do - before do - @string = "asdf" - end - it "passes the string through to the select clause" do - Projection.new(@relation, @string).to_sql.should be_like(" + Projection.new(@relation, 'asdf').to_sql.should be_like(" SELECT asdf FROM `users` ") end end end + + describe Projection::Externalizable do + describe '#aggregation?' do + describe 'when the projections are attributes' do + it 'returns false' do + Projection.new(@relation, @attribute).should_not be_aggregation + end + end + + describe 'when the projections include an aggregation' do + it "obtains" do + Projection.new(@relation, @attribute.sum).should be_aggregation + end + end + end + + end end end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/range_spec.rb b/spec/active_relation/unit/relations/range_spec.rb deleted file mode 100644 index a0207c7342..0000000000 --- a/spec/active_relation/unit/relations/range_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module ActiveRelation - describe Range do - before do - @relation = Table.new(:users) - @range = 4..9 - end - - describe '#qualify' do - it "descends" do - Range.new(@relation, @range).qualify.should == Range.new(@relation, @range).descend(&:qualify) - end - end - - describe '#descend' do - it "distributes a block over the relation" do - Range.new(@relation, @range).descend(&:qualify).should == Range.new(@relation.descend(&:qualify), @range) - end - end - - describe '#to_sql' do - it "manufactures sql with limit and offset" do - range_size = @range.last - @range.first + 1 - range_start = @range.first - Range.new(@relation, @range).to_s.should be_like(" - SELECT `users`.`id`, `users`.`name` - FROM `users` - LIMIT #{range_size} - OFFSET #{range_start} - ") - end - end - end -end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/relation_spec.rb b/spec/active_relation/unit/relations/relation_spec.rb index 70bf87271a..c40974e136 100644 --- a/spec/active_relation/unit/relations/relation_spec.rb +++ b/spec/active_relation/unit/relations/relation_spec.rb @@ -9,12 +9,6 @@ module ActiveRelation end describe '[]' do - describe 'when given a', Range do - it "manufactures a range relation when given a range" do - @relation[1..2].should == Range.new(@relation, 1..2) - end - end - describe 'when given an', Attribute do it "return the attribute congruent to the provided attribute" do @relation[@attribute1].should == @attribute1 @@ -63,6 +57,12 @@ module ActiveRelation @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) end end + + describe "when given something blank" do + it "returns self" do + @relation.join.should == @relation + end + end end describe '#outer_join' do @@ -78,17 +78,23 @@ module ActiveRelation @relation.project(@attribute1, @attribute2). \ should == Projection.new(@relation, @attribute1, @attribute2) end + + describe "when given blank attributes" do + it "returns self" do + @relation.project.should == @relation + end + end end describe '#as' do it "manufactures an alias relation" do @relation.as(:paul).should == Alias.new(@relation, :paul) end - end - - describe '#rename' do - it "manufactures a rename relation" do - @relation.rename(@attribute1, :users).should == Rename.new(@relation, @attribute1 => :users) + + describe 'when given a blank alias' do + it 'returns self' do + @relation.as.should == @relation + end end end @@ -104,34 +110,59 @@ module ActiveRelation it "accepts arbitrary strings" do @relation.select("arbitrary").should == Selection.new(@relation, "arbitrary") end + + describe 'when given a blank predicate' do + it 'returns self' do + @relation.select.should == @relation + end + end end describe '#order' do it "manufactures an order relation" do @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2) end + + describe 'when given a blank ordering' do + it 'returns self' do + @relation.order.should == @relation + end + end end - describe '#call' do - it 'executes a select_all on the connection' do - mock(connection = Object.new).select_all(@relation.to_sql) - @relation.call(connection) + describe '#take' do + it "manufactures a take relation" do + @relation.take(5).should == Take.new(@relation, 5) + end + + describe 'when given a blank number of items' do + it 'returns self' do + @relation.take.should == @relation + end end end - - describe '#aggregate' do - before do - @expression1 = @attribute1.sum - @expression2 = @attribute2.sum + describe '#skip' do + it "manufactures a skip relation" do + @relation.skip(4).should == Skip.new(@relation, 4) end + describe 'when given a blank number of items' do + it 'returns self' do + @relation.skip.should == @relation + end + end + end + + describe '#group' do it 'manufactures a group relation' do - @relation.aggregate(@expression1, @expression2).group(@attribute1, @attribute2). \ - should == Aggregation.new(@relation, - :expressions => [@expression1, @expression2], - :groupings => [@attribute1, @attribute2] - ) + @relation.group(@attribute1, @attribute2).should == Grouping.new(@relation, @attribute1, @attribute2) + end + + describe 'when given blank groupings' do + it 'returns self' do + @relation.group.should == @relation + end end end @@ -168,12 +199,16 @@ module ActiveRelation end describe Relation::Enumerable do - it "is enumerable" do - pending "I don't like this mock-based test" - data = [1,2,3] - mock.instance_of(Session).read(anything) { data } - @relation.collect.should == data - @relation.first.should == data.first + it "implements enumerable" do + @relation.collect.should == @relation.session.read(@relation) + @relation.first.should == @relation.session.read(@relation).first + end + end + + describe '#call' do + it 'executes a select_all on the connection' do + mock(connection = Object.new).select_all(@relation.to_sql) + @relation.call(connection) end end end diff --git a/spec/active_relation/unit/relations/rename_spec.rb b/spec/active_relation/unit/relations/rename_spec.rb deleted file mode 100644 index 9a63c4fc80..0000000000 --- a/spec/active_relation/unit/relations/rename_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') - -module ActiveRelation - describe Rename do - before do - @relation = Table.new(:users) - end - - describe '#initialize' do - it "manufactures nested rename relations if multiple renames are provided" do - Rename.new(@relation, @relation[:id] => :humpty, @relation[:name] => :dumpty). \ - should == Rename.new(Rename.new(@relation, @relation[:name] => :dumpty), @relation[:id] => :humpty) - end - end - - describe '==' do - before do - @another_relation = Table.new(:photos) - end - - it "obtains if the relation, attribute, and rename are identical" do - Rename.new(@relation, @relation[:id] => :humpty).should == Rename.new(@relation, @relation[:id] => :humpty) - Rename.new(@relation, @relation[:id] => :humpty).should_not == Rename.new(@relation, @relation[:id] => :dumpty) - Rename.new(@relation, @relation[:id] => :humpty).should_not == Rename.new(@another_relation, @relation[:id] => :humpty) - end - end - - describe '#attributes' do - before do - @renamed_relation = Rename.new(@relation, @relation[:id] => :schmid) - end - - it "manufactures a list of attributes with the renamed attribute renameed" do - @renamed_relation.attributes.should include(@relation[:id].as(:schmid).bind(@renamed_relation)) - @renamed_relation.attributes.should_not include(@relation[:id].bind(@renamed_relation)) - @renamed_relation.attributes.should include(@relation[:name].bind(@renamed_relation)) - @renamed_relation.should have(@relation.attributes.size).attributes - end - end - - describe '#qualify' do - it "descends" do - Rename.new(@relation, @relation[:id] => :schmid).qualify. \ - should == Rename.new(@relation, @relation[:id] => :schmid).descend(&:qualify) - end - end - - describe '#descend' do - it "distributes a block over the relation and renames" do - Rename.new(@relation, @relation[:id] => :schmid).descend(&:qualify). \ - should == Rename.new(@relation.descend(&:qualify), @relation[:id].qualify => :schmid) - end - end - - describe '#to_sql' do - it 'manufactures sql renaming the attribute' do - Rename.new(@relation, @relation[:id] => :schmid).to_sql.should be_like(" - SELECT `users`.`id` AS 'schmid', `users`.`name` - FROM `users` - ") - end - end - end -end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/selection_spec.rb b/spec/active_relation/unit/relations/selection_spec.rb index 20833de58d..d87e075f98 100644 --- a/spec/active_relation/unit/relations/selection_spec.rb +++ b/spec/active_relation/unit/relations/selection_spec.rb @@ -17,25 +17,7 @@ module ActiveRelation should == Selection.new(Selection.new(@relation, @another_predicate), @predicate) end end - - describe '#qualify' do - it "descends" do - Selection.new(@relation, @predicate).qualify. \ - should == Selection.new(@relation, @predicate).descend(&:qualify) - end - end - - describe '#descend' do - before do - @selection = Selection.new(@relation, @predicate) - end - - it "distributes a block over the relation and predicates" do - @selection.descend(&:qualify). \ - should == Selection.new(@selection.relation.descend(&:qualify), @selection.predicate.qualify) - end - end - + describe '#to_sql' do describe 'when given a predicate' do it "manufactures sql with where clause conditions" do diff --git a/spec/active_relation/unit/relations/skip_spec.rb b/spec/active_relation/unit/relations/skip_spec.rb new file mode 100644 index 0000000000..219bfdd80e --- /dev/null +++ b/spec/active_relation/unit/relations/skip_spec.rb @@ -0,0 +1,20 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') + +module ActiveRelation + describe Skip do + before do + @relation = Table.new(:users) + @skipped = 4 + end + + describe '#to_sql' do + it "manufactures sql with limit and offset" do + Skip.new(@relation, @skipped).to_s.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + OFFSET #{@skipped} + ") + end + end + end +end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/table_spec.rb b/spec/active_relation/unit/relations/table_spec.rb index 6b562cff89..2751d9cb63 100644 --- a/spec/active_relation/unit/relations/table_spec.rb +++ b/spec/active_relation/unit/relations/table_spec.rb @@ -46,8 +46,8 @@ module ActiveRelation end describe '#column_for' do - it "" do - @relation[:id].column.should == @relation.columns.detect { |c| c.name == 'id' } + it "returns the column corresponding to the attribute" do + @relation.column_for(@relation[:id]).should == @relation.columns.detect { |c| c.name == 'id' } end end @@ -74,12 +74,6 @@ module ActiveRelation end end - describe '#qualify' do - it 'manufactures a rename relation with all attribute names qualified' do - @relation.qualify.should == Rename.new(@relation, @relation[:name] => 'users.name', @relation[:id] => 'users.id') - end - end - describe 'hashing' do it "implements hash equality" do Table.new(:users).should hash_the_same_as(Table.new(:users)) diff --git a/spec/active_relation/unit/relations/take_spec.rb b/spec/active_relation/unit/relations/take_spec.rb new file mode 100644 index 0000000000..8c13732258 --- /dev/null +++ b/spec/active_relation/unit/relations/take_spec.rb @@ -0,0 +1,20 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') + +module ActiveRelation + describe Take do + before do + @relation = Table.new(:users) + @taken = 4 + end + + describe '#to_sql' do + it "manufactures sql with limit and offset" do + Take.new(@relation, @taken).to_s.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + LIMIT #{@taken} + ") + end + end + end +end
\ No newline at end of file |