1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/spec/arel/attributes/attribute_spec.rb

121 lines
3.5 KiB
Ruby
Raw Normal View History

2010-08-12 18:22:26 -04:00
require 'spec_helper'
module Arel
module Attributes
describe 'attribute' do
2010-09-10 14:39:50 -04:00
describe '#gteq' do
it 'should create a GreaterThanOrEqual node' do
relation = Table.new(:users)
relation[:id].gteq(10).should be_kind_of Nodes::GreaterThanOrEqual
end
it 'should generate >= in sql' do
relation = Table.new(:users)
mgr = relation.project relation[:id]
mgr.where relation[:id].gteq(10)
mgr.to_sql.should be_like %{
SELECT "users"."id" FROM "users" WHERE "users"."id" >= 10
}
end
end
2010-09-08 19:23:15 -04:00
describe '#average' do
it 'should create a AVG node' do
relation = Table.new(:users)
relation[:id].average.should be_kind_of Nodes::Avg
end
# FIXME: backwards compat. Is this really necessary?
it 'should set the alias to "avg_id"' do
relation = Table.new(:users)
mgr = relation.project relation[:id].average
mgr.to_sql.should be_like %{
SELECT AVG("users"."id") AS avg_id
FROM "users"
}
end
end
2010-09-08 18:08:00 -04:00
describe '#maximum' do
it 'should create a MAX node' do
relation = Table.new(:users)
relation[:id].maximum.should be_kind_of Nodes::Max
end
# FIXME: backwards compat. Is this really necessary?
it 'should set the alias to "max_id"' do
relation = Table.new(:users)
mgr = relation.project relation[:id].maximum
mgr.to_sql.should be_like %{
SELECT MAX("users"."id") AS max_id
FROM "users"
}
end
end
2010-09-08 17:36:10 -04:00
describe '#sum' do
it 'should create a SUM node' do
relation = Table.new(:users)
relation[:id].sum.should be_kind_of Nodes::Sum
end
# FIXME: backwards compat. Is this really necessary?
it 'should set the alias to "sum_id"' do
relation = Table.new(:users)
mgr = relation.project relation[:id].sum
mgr.to_sql.should be_like %{
SELECT SUM("users"."id") AS sum_id
FROM "users"
}
end
end
2010-09-07 19:37:11 -04:00
describe '#count' do
it 'should return a count node' do
relation = Table.new(:users)
relation[:id].count.should be_kind_of Nodes::Count
end
2010-09-07 19:45:25 -04:00
it 'should take a distinct param' do
relation = Table.new(:users)
count = relation[:id].count(nil)
count.should be_kind_of Nodes::Count
count.distinct.should be_nil
end
2010-09-07 19:37:11 -04:00
end
2010-08-12 18:22:26 -04:00
describe '#eq' do
it 'should return an equality node' do
attribute = Attribute.new nil, nil, nil
equality = attribute.eq 1
2010-08-16 17:43:18 -04:00
check equality.left.should == attribute
check equality.right.should == 1
2010-08-12 18:22:26 -04:00
equality.should be_kind_of Nodes::Equality
end
end
2010-08-16 17:43:18 -04:00
describe '#in' do
it 'can be constructed with a list' do
end
it 'should return an in node' do
attribute = Attribute.new nil, nil, nil
node = Nodes::In.new attribute, [1,2,3]
check node.left.should == attribute
check node.right.should == [1, 2, 3]
end
end
2010-08-12 18:22:26 -04:00
end
2010-08-12 18:40:58 -04:00
describe 'equality' do
describe '#to_sql' do
it 'should produce sql' do
table = Table.new :users
condition = table['id'].eq 1
condition.to_sql.should == '"users"."id" = 1'
end
end
end
2010-08-12 18:22:26 -04:00
end
end