1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/test/test_insert_manager.rb

245 lines
6.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'helper'
module Arel
describe 'insert manager' do
describe 'new' do
it 'takes an engine' do
Arel::InsertManager.new
end
end
2010-08-13 21:28:38 -04:00
describe 'insert' do
it 'can create a Values node' do
manager = Arel::InsertManager.new
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
it 'allows sql literals' do
manager = Arel::InsertManager.new
manager.into Table.new(:users)
manager.values = manager.create_values [Arel.sql('*')], %w{ a }
manager.to_sql.must_be_like %{
INSERT INTO \"users\" VALUES (*)
}
end
2017-05-21 08:12:08 -04:00
it 'works with multiple values' do
table = Table.new(:users)
manager = Arel::InsertManager.new
manager.into table
manager.columns << table[:id]
manager.columns << table[:name]
manager.values = manager.create_values_list([
%w{1 david},
%w{2 kir},
["3", Arel.sql('DEFAULT')],
])
manager.to_sql.must_be_like %{
INSERT INTO \"users\" (\"id\", \"name\") VALUES ('1', 'david'), ('2', 'kir'), ('3', DEFAULT)
}
end
it 'literals in multiple values are not escaped' do
table = Table.new(:users)
manager = Arel::InsertManager.new
manager.into table
manager.columns << table[:name]
manager.values = manager.create_values_list([
[Arel.sql('*')],
[Arel.sql('DEFAULT')],
])
manager.to_sql.must_be_like %{
INSERT INTO \"users\" (\"name\") VALUES (*), (DEFAULT)
}
end
it 'works with multiple single values' do
table = Table.new(:users)
manager = Arel::InsertManager.new
manager.into table
manager.columns << table[:name]
manager.values = manager.create_values_list([
%w{david},
%w{kir},
[Arel.sql('DEFAULT')],
])
manager.to_sql.must_be_like %{
INSERT INTO \"users\" (\"name\") VALUES ('david'), ('kir'), (DEFAULT)
}
end
2010-09-10 21:01:45 -04:00
it "inserts false" do
table = Table.new(:users)
manager = Arel::InsertManager.new
2010-09-14 17:58:34 -04:00
manager.insert [[table[:bool], false]]
manager.to_sql.must_be_like %{
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
2010-08-13 21:55:15 -04:00
manager.insert [[table[:id], nil]]
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
2010-08-13 21:55:15 -04:00
time = Time.now
attribute = table[:created_at]
2010-09-27 14:32:04 -04:00
manager.insert [[attribute, time]]
manager.to_sql.must_be_like %{
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
2010-08-13 21:28:38 -04:00
manager.into table
manager.insert [[table[:id], 1], [table[:name], 'aaron']]
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
2010-08-13 21:28:38 -04:00
manager.insert [[table[:id], 1], [table[:name], 'aaron']]
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 'noop for empty list' do
table = Table.new(:users)
manager = Arel::InsertManager.new
manager.insert [[table[:id], 1]]
2010-08-13 21:28:38 -04:00
manager.insert []
manager.to_sql.must_be_like %{
INSERT INTO "users" ("id") VALUES (1)
}
2010-08-13 21:28:38 -04:00
end
2017-02-28 16:10:40 -05:00
it 'is chainable' do
table = Table.new(:users)
manager = Arel::InsertManager.new
insert_result = manager.insert [[table[:id],1]]
assert_equal manager, insert_result
end
2010-08-13 21:28:38 -04:00
end
describe 'into' do
it 'takes a Table and chains' do
manager = Arel::InsertManager.new
manager.into(Table.new(:users)).must_equal manager
end
it 'converts to sql' do
table = Table.new :users
manager = Arel::InsertManager.new
manager.into table
manager.to_sql.must_be_like %{
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
2010-08-13 18:43:32 -04:00
manager.into table
manager.columns << table[:id]
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
2010-08-13 18:43:32 -04:00
manager.into table
manager.values = Nodes::Values.new [1]
manager.to_sql.must_be_like %{
2010-08-13 18:43:32 -04:00
INSERT INTO "users" VALUES (1)
}
end
2017-05-04 18:14:33 -04:00
it "accepts sql literals" do
table = Table.new :users
manager = Arel::InsertManager.new
manager.into table
manager.values = Arel.sql("DEFAULT VALUES")
manager.to_sql.must_be_like %{
INSERT INTO "users" DEFAULT VALUES
}
end
2010-08-13 18:43:32 -04:00
end
describe "combo" do
it "combines columns and values list in order" do
2010-08-13 18:43:32 -04:00
table = Table.new :users
manager = Arel::InsertManager.new
2010-08-13 18:43:32 -04:00
manager.into table
manager.values = Nodes::Values.new [1, 'aaron']
2010-08-13 18:43:32 -04:00
manager.columns << table[:id]
manager.columns << table[:name]
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
describe "select" do
it "accepts a select query in place of a VALUES clause" do
table = Table.new :users
manager = Arel::InsertManager.new
manager.into table
select = Arel::SelectManager.new
select.project Arel.sql('1')
select.project Arel.sql('"aaron"')
manager.select select
manager.columns << table[:id]
manager.columns << table[:name]
manager.to_sql.must_be_like %{
2014-04-10 13:30:22 -04:00
INSERT INTO "users" ("id", "name") (SELECT 1, "aaron")
}
end
end
end
end