aboutsummaryrefslogtreecommitdiffstats
path: root/spec/arel
diff options
context:
space:
mode:
Diffstat (limited to 'spec/arel')
-rw-r--r--spec/arel/unit/primitives/expression_spec.rb14
-rw-r--r--spec/arel/unit/relations/project_spec.rb27
2 files changed, 24 insertions, 17 deletions
diff --git a/spec/arel/unit/primitives/expression_spec.rb b/spec/arel/unit/primitives/expression_spec.rb
index d398805fe2..4943f4ef33 100644
--- a/spec/arel/unit/primitives/expression_spec.rb
+++ b/spec/arel/unit/primitives/expression_spec.rb
@@ -6,40 +6,40 @@ module Arel
@relation = Table.new(:users)
@attribute = @relation[:id]
end
-
+
describe Expression::Transformations do
before do
@expression = Expression.new(@attribute, "COUNT")
end
-
+
describe '#bind' do
it "manufactures an attribute with a rebound relation and self as the ancestor" do
derived_relation = @relation.where(@relation[:id].eq(1))
@expression.bind(derived_relation).should == Expression.new(@attribute.bind(derived_relation), "COUNT", nil, @expression)
end
-
+
it "returns self if the substituting to the same relation" do
@expression.bind(@relation).should == @expression
end
end
-
+
describe '#as' do
it "manufactures an aliased expression" do
@expression.as(:alias).should == Expression.new(@attribute, "COUNT", :alias, @expression)
end
end
-
+
describe '#to_attribute' do
it "manufactures an attribute with the expression as an ancestor" do
@expression.to_attribute.should == Attribute.new(@expression.relation, @expression.alias, :ancestor => @expression)
end
end
end
-
+
describe '#to_sql' do
it "manufactures sql with the expression and alias" do
Expression.new(@attribute, "COUNT", :alias).to_sql.should == "COUNT(`users`.`id`) AS `alias`"
end
end
end
-end \ No newline at end of file
+end
diff --git a/spec/arel/unit/relations/project_spec.rb b/spec/arel/unit/relations/project_spec.rb
index 93cbe5668a..7f531096f0 100644
--- a/spec/arel/unit/relations/project_spec.rb
+++ b/spec/arel/unit/relations/project_spec.rb
@@ -6,17 +6,17 @@ module Arel
@relation = Table.new(:users)
@attribute = @relation[:id]
end
-
+
describe '#attributes' do
before do
@projection = Project.new(@relation, @attribute)
end
-
+
it "manufactures attributes associated with the projection relation" do
@projection.attributes.should == [@attribute].collect { |a| a.bind(@projection) }
end
end
-
+
describe '#to_sql' do
describe 'when given an attribute' do
it "manufactures sql with a limited select clause" do
@@ -26,19 +26,19 @@ module Arel
")
end
end
-
+
describe 'when given a relation' do
before do
@scalar_relation = Project.new(@relation, @relation[:name])
end
-
+
it "manufactures sql with scalar selects" do
Project.new(@relation, @scalar_relation).to_sql.should be_like("
SELECT (SELECT `users`.`name` FROM `users`) AS `users` FROM `users`
")
end
end
-
+
describe 'when given a string' do
it "passes the string through to the select clause" do
Project.new(@relation, 'asdf').to_sql.should be_like("
@@ -46,7 +46,7 @@ module Arel
")
end
end
-
+
describe 'when given an expression' do
it 'manufactures sql with expressions' do
@relation.project(@attribute.count).to_sql.should be_like("
@@ -54,16 +54,23 @@ module Arel
FROM `users`
")
end
+
+ it 'manufactures sql with distinct expressions' do
+ @relation.project(@attribute.count(true)).to_sql.should be_like("
+ SELECT COUNT(DISTINCT `users`.`id`)
+ FROM `users`
+ ")
+ end
end
end
-
+
describe '#externalizable?' do
describe 'when the projections are attributes' do
it 'returns false' do
Project.new(@relation, @attribute).should_not be_externalizable
end
end
-
+
describe 'when the projections include an aggregation' do
it "obtains" do
Project.new(@relation, @attribute.sum).should be_externalizable
@@ -71,4 +78,4 @@ module Arel
end
end
end
-end \ No newline at end of file
+end