2010-10-18 19:54:50 -04:00
|
|
|
require 'helper'
|
2010-08-13 18:30:22 -04:00
|
|
|
|
|
|
|
module Arel
|
|
|
|
describe 'insert manager' do
|
|
|
|
describe 'new' do
|
|
|
|
it 'takes an engine' do
|
|
|
|
Arel::InsertManager.new Table.engine
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-13 21:28:38 -04:00
|
|
|
describe 'insert' do
|
2011-03-22 00:31:45 -04:00
|
|
|
it 'can create a Values node' do
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
values = manager.create_values %w{ a b }, %w{ c d }
|
|
|
|
|
|
|
|
assert_kind_of Arel::Nodes::Values, values
|
|
|
|
assert_equal %w{ a b }, values.left
|
|
|
|
assert_equal %w{ c d }, values.right
|
|
|
|
end
|
|
|
|
|
2011-03-22 00:35:23 -04:00
|
|
|
it 'allows sql literals' do
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.values = manager.create_values [Arel.sql('*')], %w{ a }
|
|
|
|
manager.to_sql.must_be_like %{
|
|
|
|
INSERT INTO NULL VALUES (*)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-09-10 21:01:45 -04:00
|
|
|
it "inserts false" do
|
|
|
|
table = Table.new(:users)
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
2010-09-14 17:58:34 -04:00
|
|
|
|
2010-11-05 17:05:10 -04:00
|
|
|
manager.insert [[table[:bool], false]]
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-11-05 17:05:10 -04:00
|
|
|
INSERT INTO "users" ("bool") VALUES ('f')
|
2010-09-10 21:01:45 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-13 21:55:15 -04:00
|
|
|
it "inserts null" do
|
|
|
|
table = Table.new(:users)
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.insert [[table[:id], nil]]
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-08-13 21:55:15 -04:00
|
|
|
INSERT INTO "users" ("id") VALUES (NULL)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "inserts time" do
|
|
|
|
table = Table.new(:users)
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
|
|
|
|
time = Time.now
|
2010-11-05 17:05:10 -04:00
|
|
|
attribute = table[:created_at]
|
2010-09-27 14:32:04 -04:00
|
|
|
|
|
|
|
manager.insert [[attribute, time]]
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-11-05 17:05:10 -04:00
|
|
|
INSERT INTO "users" ("created_at") VALUES (#{Table.engine.connection.quote time})
|
2010-08-13 21:55:15 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-13 21:28:38 -04:00
|
|
|
it 'takes a list of lists' do
|
|
|
|
table = Table.new(:users)
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.into table
|
|
|
|
manager.insert [[table[:id], 1], [table[:name], 'aaron']]
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-08-13 21:55:15 -04:00
|
|
|
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
|
2010-08-13 21:28:38 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults the table' do
|
|
|
|
table = Table.new(:users)
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.insert [[table[:id], 1], [table[:name], 'aaron']]
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-08-13 21:55:15 -04:00
|
|
|
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
|
2010-08-13 21:28:38 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes an empty list' do
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.insert []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-13 18:30:22 -04:00
|
|
|
describe 'into' do
|
|
|
|
it 'takes an engine' do
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.into(Table.new(:users)).must_equal manager
|
2010-08-13 18:30:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to sql' do
|
|
|
|
table = Table.new :users
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.into table
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-08-13 18:30:22 -04:00
|
|
|
INSERT INTO "users"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2010-08-13 18:43:32 -04:00
|
|
|
|
|
|
|
describe 'columns' do
|
|
|
|
it "converts to sql" do
|
|
|
|
table = Table.new :users
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.into table
|
|
|
|
manager.columns << table[:id]
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-08-13 21:55:15 -04:00
|
|
|
INSERT INTO "users" ("id")
|
2010-08-13 18:43:32 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "values" do
|
|
|
|
it "converts to sql" do
|
|
|
|
table = Table.new :users
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.into table
|
|
|
|
|
2010-09-09 18:20:25 -04:00
|
|
|
manager.values = Nodes::Values.new [1]
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-08-13 18:43:32 -04:00
|
|
|
INSERT INTO "users" VALUES (1)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "combo" do
|
|
|
|
it "puts shit together" do
|
|
|
|
table = Table.new :users
|
|
|
|
manager = Arel::InsertManager.new Table.engine
|
|
|
|
manager.into table
|
|
|
|
|
2010-09-09 18:20:25 -04:00
|
|
|
manager.values = Nodes::Values.new [1, 'aaron']
|
2010-08-13 18:43:32 -04:00
|
|
|
manager.columns << table[:id]
|
|
|
|
manager.columns << table[:name]
|
2010-10-18 18:41:21 -04:00
|
|
|
manager.to_sql.must_be_like %{
|
2010-08-13 21:55:15 -04:00
|
|
|
INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
|
2010-08-13 18:43:32 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2010-08-13 18:30:22 -04:00
|
|
|
end
|
|
|
|
end
|