aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/attributes/test_attribute.rb16
-rw-r--r--test/nodes/test_not.rb11
-rw-r--r--test/support/fake_record.rb13
-rw-r--r--test/test_select_manager.rb62
-rw-r--r--test/visitors/test_depth_first.rb2
-rw-r--r--test/visitors/test_mssql.rb9
-rw-r--r--test/visitors/test_mysql.rb15
-rw-r--r--test/visitors/test_postgres.rb17
-rw-r--r--test/visitors/test_to_sql.rb6
9 files changed, 129 insertions, 22 deletions
diff --git a/test/attributes/test_attribute.rb b/test/attributes/test_attribute.rb
index df7dc69621..352774071a 100644
--- a/test/attributes/test_attribute.rb
+++ b/test/attributes/test_attribute.rb
@@ -366,6 +366,14 @@ module Arel
SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 OR "users"."id" = 2)
}
end
+
+ it 'should not eat input' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ values = [1,2]
+ mgr.where relation[:id].eq_any(values)
+ values.must_equal [1,2]
+ end
end
describe '#eq_all' do
@@ -382,6 +390,14 @@ module Arel
SELECT "users"."id" FROM "users" WHERE ("users"."id" = 1 AND "users"."id" = 2)
}
end
+
+ it 'should not eat input' do
+ relation = Table.new(:users)
+ mgr = relation.project relation[:id]
+ values = [1,2]
+ mgr.where relation[:id].eq_all(values)
+ values.must_equal [1,2]
+ end
end
describe '#matches' do
diff --git a/test/nodes/test_not.rb b/test/nodes/test_not.rb
index d02a9bad74..c5bb0088c8 100644
--- a/test/nodes/test_not.rb
+++ b/test/nodes/test_not.rb
@@ -6,13 +6,10 @@ module Arel
describe '#not' do
it 'makes a NOT node' do
attr = Table.new(:users)[:id]
- left = attr.eq(10)
- right = attr.eq(11)
- node = left.or right
- node.expr.left.must_equal left
- node.expr.right.must_equal right
-
- node.or(right).not
+ expr = attr.eq(10)
+ node = expr.not
+ node.must_be_kind_of Not
+ node.expr.must_equal expr
end
end
end
diff --git a/test/support/fake_record.rb b/test/support/fake_record.rb
index cdea690a7b..a0fa79d519 100644
--- a/test/support/fake_record.rb
+++ b/test/support/fake_record.rb
@@ -3,7 +3,7 @@ module FakeRecord
end
class Connection
- attr_reader :tables
+ attr_reader :tables, :columns_hash
def initialize
@tables = %w{ users photos developers products}
@@ -19,6 +19,9 @@ module FakeRecord
Column.new('price', :decimal)
]
}
+ @columns_hash = {
+ 'users' => Hash[@columns['users'].map { |x| [x.name, x] }]
+ }
@primary_keys = {
'users' => 'id'
}
@@ -79,6 +82,14 @@ module FakeRecord
def with_connection
yield connection
end
+
+ def table_exists? name
+ connection.tables.include? name.to_s
+ end
+
+ def columns_hash
+ connection.columns_hash
+ end
end
class Base
diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb
index f457c55f40..2fe43aa982 100644
--- a/test/test_select_manager.rb
+++ b/test/test_select_manager.rb
@@ -30,6 +30,10 @@ module Arel
@engine.connection.columns table, message
end
+ def columns_hash
+ @engine.connection.columns_hash
+ end
+
def table_exists? name
@engine.connection.table_exists? name
end
@@ -122,6 +126,24 @@ module Arel
mgr.to_sql.must_be_like %{ SELECT FROM "users" HAVING foo AND bar }
end
end
+
+ describe 'on' do
+ it 'converts to sqlliterals' do
+ table = Table.new :users
+ right = table.alias
+ mgr = table.from table
+ mgr.join(right).on("omg")
+ mgr.to_sql.must_be_like %{ SELECT FROM "users" INNER JOIN "users" "users_2" ON omg }
+ end
+
+ it 'converts to sqlliterals' do
+ table = Table.new :users
+ right = table.alias
+ mgr = table.from table
+ mgr.join(right).on("omg", "123")
+ mgr.to_sql.must_be_like %{ SELECT FROM "users" INNER JOIN "users" "users_2" ON omg AND 123 }
+ end
+ end
end
describe 'clone' do
@@ -158,6 +180,32 @@ module Arel
end
end
+ describe 'offset' do
+ it 'should add an offset' do
+ table = Table.new :users
+ mgr = table.from table
+ mgr.offset = 10
+ mgr.to_sql.must_be_like %{ SELECT FROM "users" OFFSET 10 }
+ end
+
+ it 'should remove an offset' do
+ table = Table.new :users
+ mgr = table.from table
+ mgr.offset = 10
+ mgr.to_sql.must_be_like %{ SELECT FROM "users" OFFSET 10 }
+
+ mgr.offset = nil
+ mgr.to_sql.must_be_like %{ SELECT FROM "users" }
+ end
+
+ it 'should return the offset' do
+ table = Table.new :users
+ mgr = table.from table
+ mgr.offset = 10
+ assert_equal 10, mgr.offset
+ end
+ end
+
describe 'exists' do
it 'should create an exists clause' do
table = Table.new(:users)
@@ -288,9 +336,7 @@ module Arel
as_statement = Arel::Nodes::As.new replies, union
manager = Arel::SelectManager.new Table.engine
- manager.from replies
- manager.with :recursive, as_statement
- manager.project Arel.star
+ manager.with(:recursive, as_statement).from(replies).project(Arel.star)
sql = manager.to_sql
sql.must_be_like %{
@@ -302,7 +348,6 @@ module Arel
SELECT * FROM "replies"
}
end
-
end
describe 'ast' do
@@ -724,6 +769,15 @@ module Arel
manager = Arel::SelectManager.new Table.engine
manager.take(1).must_equal manager
end
+
+ it 'removes LIMIT when nil is passed' do
+ manager = Arel::SelectManager.new Table.engine
+ manager.limit = 10
+ assert_match('LIMIT', manager.to_sql)
+
+ manager.limit = nil
+ refute_match('LIMIT', manager.to_sql)
+ end
end
describe 'where' do
diff --git a/test/visitors/test_depth_first.rb b/test/visitors/test_depth_first.rb
index 6d74926743..06e7bba9fb 100644
--- a/test/visitors/test_depth_first.rb
+++ b/test/visitors/test_depth_first.rb
@@ -63,7 +63,7 @@ module Arel
end
def test_lock
- lock = Nodes::Lock.new
+ lock = Nodes::Lock.new true
@visitor.accept lock
assert_equal [lock], @collector.calls
end
diff --git a/test/visitors/test_mssql.rb b/test/visitors/test_mssql.rb
index b658e46828..ccaea395fe 100644
--- a/test/visitors/test_mssql.rb
+++ b/test/visitors/test_mssql.rb
@@ -13,6 +13,15 @@ module Arel
sql = @visitor.accept(stmt)
sql.must_be_like "SELECT TOP 1"
end
+
+ it 'uses TOP in updates with a limit' do
+ stmt = Nodes::UpdateStatement.new
+ stmt.limit = Nodes::Limit.new(1)
+ stmt.key = 'id'
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "UPDATE NULL WHERE 'id' IN (SELECT TOP 1 'id' )"
+ end
+
end
end
end
diff --git a/test/visitors/test_mysql.rb b/test/visitors/test_mysql.rb
index c3b79ca667..3c15c218b2 100644
--- a/test/visitors/test_mysql.rb
+++ b/test/visitors/test_mysql.rb
@@ -29,11 +29,16 @@ module Arel
sql.must_be_like "SELECT FROM DUAL"
end
- it 'uses FOR UPDATE when locking' do
- stmt = Nodes::SelectStatement.new
- stmt.lock = Nodes::Lock.new
- sql = @visitor.accept(stmt)
- sql.must_be_like "SELECT FROM DUAL FOR UPDATE"
+ describe 'locking' do
+ it 'defaults to FOR UPDATE when locking' do
+ node = Nodes::Lock.new(Arel.sql('FOR UPDATE'))
+ @visitor.accept(node).must_be_like "FOR UPDATE"
+ end
+
+ it 'allows a custom string to be used as a lock' do
+ node = Nodes::Lock.new(Arel.sql('LOCK IN SHARE MODE'))
+ @visitor.accept(node).must_be_like "LOCK IN SHARE MODE"
+ end
end
end
end
diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb
index 8d3f19aa6e..74446c23ba 100644
--- a/test/visitors/test_postgres.rb
+++ b/test/visitors/test_postgres.rb
@@ -7,10 +7,19 @@ module Arel
@visitor = PostgreSQL.new Table.engine
end
- it 'should produce a lock value' do
- @visitor.accept(Nodes::Lock.new).must_be_like %{
- FOR UPDATE
- }
+ describe 'locking' do
+ it 'defaults to FOR UPDATE' do
+ @visitor.accept(Nodes::Lock.new(Arel.sql('FOR UPDATE'))).must_be_like %{
+ FOR UPDATE
+ }
+ end
+
+ it 'allows a custom string to be used as a lock' do
+ node = Nodes::Lock.new(Arel.sql('FOR SHARE'))
+ @visitor.accept(node).must_be_like %{
+ FOR SHARE
+ }
+ end
end
it "should escape LIMIT" do
diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb
index 82ad17a82c..3af316037a 100644
--- a/test/visitors/test_to_sql.rb
+++ b/test/visitors/test_to_sql.rb
@@ -20,6 +20,12 @@ module Arel
assert_equal 'omg(*)', @visitor.accept(function)
end
+ it 'should chain predications on named functions' do
+ function = Nodes::NamedFunction.new('omg', [Arel.star])
+ sql = @visitor.accept(function.eq(2))
+ sql.must_be_like %{ omg(*) = 2 }
+ end
+
it 'works with lists' do
function = Nodes::NamedFunction.new('omg', [Arel.star, Arel.star])
assert_equal 'omg(*, *)', @visitor.accept(function)