diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-16 22:55:06 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-16 22:55:06 -0800 |
commit | b47ac80a17d7a74e1b5deac0e63a72960a4da453 (patch) | |
tree | de13e27979148c8a39c396298cc789348d4acb2e /spec | |
parent | b5a2057fcd5dfcac3682a565f2ba15281f5dcbb2 (diff) | |
download | rails-b47ac80a17d7a74e1b5deac0e63a72960a4da453.tar.gz rails-b47ac80a17d7a74e1b5deac0e63a72960a4da453.tar.bz2 rails-b47ac80a17d7a74e1b5deac0e63a72960a4da453.zip |
adding grouping functionality; added some dummy code ("Schmoin") for experimenting with aggregate joins. need to resolve the ambiguity in the #as operator between (SELECT * FROM foo AS bar) vs. (SELECT * FROM foo) AS bar
Diffstat (limited to 'spec')
-rw-r--r-- | spec/active_relation/primitives/aggregation_spec.rb | 15 | ||||
-rw-r--r-- | spec/active_relation/primitives/attribute_spec.rb | 6 | ||||
-rw-r--r-- | spec/active_relation/relations/group_spec.rb | 29 | ||||
-rw-r--r-- | spec/active_relation/relations/relation_spec.rb | 8 | ||||
-rw-r--r-- | spec/active_relation/relations/schmoin_spec.rb | 24 | ||||
-rw-r--r-- | spec/spec_helper.rb | 2 |
6 files changed, 78 insertions, 6 deletions
diff --git a/spec/active_relation/primitives/aggregation_spec.rb b/spec/active_relation/primitives/aggregation_spec.rb index bdfa152bca..fbd846ffed 100644 --- a/spec/active_relation/primitives/aggregation_spec.rb +++ b/spec/active_relation/primitives/aggregation_spec.rb @@ -9,10 +9,17 @@ module ActiveRelation describe '==' do it 'obtains if the attribute and function sql are identical' do - @relation1[:id].sum.should == @relation1[:id].sum - @relation1[:id].sum.should_not == @relation1[:name].sum - @relation1[:id].sum.should_not == @relation1[:name].average - @relation1[:id].sum.should_not == @relation2[:id].sum + Aggregation.new(@relation1[:id], "SUM").should == Aggregation.new(@relation1[:id], "SUM") + Aggregation.new(@relation1[:id], "SUM").should_not == Aggregation.new(@relation1[:name], "SUM") + Aggregation.new(@relation1[:id], "SUM").should_not == Aggregation.new(@relation1[:name], "SUM") + Aggregation.new(@relation1[:id], "SUM").should_not == Aggregation.new(@relation2[:id], "SUM") + end + end + + describe '#substitute' do + it "distributes over the attribute" do + Aggregation.new(@relation1[:id], "SUM").substitute(@relation2). \ + should == Aggregation.new(@relation1[:id].substitute(@relation2), "SUM") end end diff --git a/spec/active_relation/primitives/attribute_spec.rb b/spec/active_relation/primitives/attribute_spec.rb index 6078da08f3..543217b9cf 100644 --- a/spec/active_relation/primitives/attribute_spec.rb +++ b/spec/active_relation/primitives/attribute_spec.rb @@ -12,6 +12,12 @@ module ActiveRelation @relation1[:id].as(:alias).should == Attribute.new(@relation1, :id, :alias) end end + + describe '#substitute' do + it "manufactures an attribute with the relation substituted" do + @relation1[:id].substitute(@relation2).should == Attribute.new(@relation2, :id) + end + end describe '#qualified_name' do it "manufactures an attribute name prefixed with the relation's name" do diff --git a/spec/active_relation/relations/group_spec.rb b/spec/active_relation/relations/group_spec.rb new file mode 100644 index 0000000000..256d7dde45 --- /dev/null +++ b/spec/active_relation/relations/group_spec.rb @@ -0,0 +1,29 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') + +module ActiveRelation + describe Group do + before do + @relation1 = Table.new(:foo) + @relation2 = Table.new(:bar) + @attribute1 = @relation1[:id] + @attribute2 = @relation2[:id] + end + + describe '#qualify' do + it "distributes over the relation and attributes" do + Group.new(@relation1, @attribute1).qualify. \ + should == Group.new(@relation1.qualify, @attribute1.qualify) + end + end + + describe '#to_sql' do + it "manufactures sql with an order clause" do + Group.new(@relation1, @attribute1).to_sql.should be_like(""" + SELECT `foo`.`name`, `foo`.`id` + FROM `foo` + GROUP BY `foo`.`id` + """) + end + end + end +end diff --git a/spec/active_relation/relations/relation_spec.rb b/spec/active_relation/relations/relation_spec.rb index 72b7d14389..1b4ab785b7 100644 --- a/spec/active_relation/relations/relation_spec.rb +++ b/spec/active_relation/relations/relation_spec.rb @@ -82,7 +82,13 @@ module ActiveRelation describe '#order' do it "manufactures an order relation" do - @relation1.order(@attribute1, @attribute2).should be_kind_of(Order) + @relation1.order(@attribute1, @attribute2).should == Order.new(@relation1, @attribute1, @attribute2) + end + end + + describe '#group' do + it 'manufactures a group relation' do + @relation1.group(@attribute1).should == Group.new(@relation1, @attribute1) end end end diff --git a/spec/active_relation/relations/schmoin_spec.rb b/spec/active_relation/relations/schmoin_spec.rb new file mode 100644 index 0000000000..14e583afab --- /dev/null +++ b/spec/active_relation/relations/schmoin_spec.rb @@ -0,0 +1,24 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') + +module ActiveRelation + describe Schmoin do + before do + @relation = Table.new(:users) + photos = Table.new(:photos) + @aggregate_relation = photos.project(photos[:user_id], photos[:id].count).group(photos[:user_id]).as(:photo_count) + @predicate = Equality.new(@aggregate_relation[:user_id], @relation[:id]) + end + + describe '#to_sql' do + it 'manufactures sql joining the two tables on the predicate, merging the selects' do + pending + Schmoin.new("INNER JOIN", @relation, @aggregate_relation, @predicate).to_sql.should be_like(""" + SELECT `users`.`name` + FROM `users` + INNER JOIN (SELECT `photos`.`user_id`, count(`photos`.`id`) FROM `photos`) AS `photo_count` + ON `photo_count`.`user_id` = `users`.`id` + """) + end + end + end +end
\ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ab9a14f3bd..8f5d76370c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,7 +9,7 @@ ActiveRecord::Base.configurations = { 'sql_algebra_test' => { :adapter => 'mysql', :username => 'root', - :password => '', + :password => 'password', :encoding => 'utf8', :database => 'sql_algebra_test', }, |