1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/test/nodes/test_sql_literal.rb
Ernie Miller 6e638bba59 Add equality to ALL THE THINGS (that matter)
People are often trying to use ARel nodes inside ActiveRecord, and when
they do so, lots of things can break, because ActiveRecord relies on
Array#uniq and sometimes hash key equality to handle values that end up
in wheres, havings, etc. By implementing equality for all the nodes, we
should hopefully be able to prevent any nodes (even nodes containing
other nodes) from failing an equality check they should otherwise pass,
and alleviate many of these errors.

Fixes #130
2012-08-18 22:33:25 -04:00

61 lines
1.7 KiB
Ruby

require 'helper'
module Arel
module Nodes
describe 'sql literal' do
before do
@visitor = Visitors::ToSql.new Table.engine.connection_pool
end
describe 'sql' do
it 'makes a sql literal node' do
sql = Arel.sql 'foo'
sql.must_be_kind_of Arel::Nodes::SqlLiteral
end
end
describe 'count' do
it 'makes a count node' do
node = SqlLiteral.new('*').count
@visitor.accept(node).must_be_like %{ COUNT(*) }
end
it 'makes a distinct node' do
node = SqlLiteral.new('*').count true
@visitor.accept(node).must_be_like %{ COUNT(DISTINCT *) }
end
end
describe 'equality' do
it 'makes an equality node' do
node = SqlLiteral.new('foo').eq(1)
@visitor.accept(node).must_be_like %{ foo = 1 }
end
it 'is equal with equal contents' do
array = [SqlLiteral.new('foo'), SqlLiteral.new('foo')]
assert_equal 1, array.uniq.size
end
it 'is not equal with different contents' do
array = [SqlLiteral.new('foo'), SqlLiteral.new('bar')]
assert_equal 2, array.uniq.size
end
end
describe 'grouped "or" equality' do
it 'makes a grouping node with an or node' do
node = SqlLiteral.new('foo').eq_any([1,2])
@visitor.accept(node).must_be_like %{ (foo = 1 OR foo = 2) }
end
end
describe 'grouped "and" equality' do
it 'makes a grouping node with an or node' do
node = SqlLiteral.new('foo').eq_all([1,2])
@visitor.accept(node).must_be_like %{ (foo = 1 AND foo = 2) }
end
end
end
end
end