aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/arel/insert_manager_test.rb
blob: 79b85742ee185fb7c233b43068faa0737d2f0960 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# frozen_string_literal: true

require_relative "helper"

module Arel
  class InsertManagerTest < Arel::Spec
    describe "new" do
      it "takes an engine" do
        Arel::InsertManager.new
      end
    end

    describe "insert" do
      it "can create a ValuesList node" do
        manager = Arel::InsertManager.new
        values  = manager.create_values_list([%w{ a b }, %w{ c d }])

        assert_kind_of Arel::Nodes::ValuesList, values
        assert_equal [%w{ a b }, %w{ c d }], values.rows
      end

      it "allows sql literals" do
        manager = Arel::InsertManager.new
        manager.into Table.new(:users)
        manager.values = manager.create_values([Arel.sql("*")])
        manager.to_sql.must_be_like %{
          INSERT INTO \"users\" VALUES (*)
        }
      end

      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

      it "inserts false" do
        table = Table.new(:users)
        manager = Arel::InsertManager.new

        manager.insert [[table[:bool], false]]
        manager.to_sql.must_be_like %{
          INSERT INTO "users" ("bool") VALUES ('f')
        }
      end

      it "inserts null" do
        table = Table.new(:users)
        manager = Arel::InsertManager.new
        manager.insert [[table[:id], nil]]
        manager.to_sql.must_be_like %{
          INSERT INTO "users" ("id") VALUES (NULL)
        }
      end

      it "inserts time" do
        table = Table.new(:users)
        manager = Arel::InsertManager.new

        time = Time.now
        attribute = table[:created_at]

        manager.insert [[attribute, time]]
        manager.to_sql.must_be_like %{
          INSERT INTO "users" ("created_at") VALUES (#{Table.engine.connection.quote time})
        }
      end

      it "takes a list of lists" do
        table = Table.new(:users)
        manager = Arel::InsertManager.new
        manager.into table
        manager.insert [[table[:id], 1], [table[:name], "aaron"]]
        manager.to_sql.must_be_like %{
          INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
        }
      end

      it "defaults the table" do
        table = Table.new(:users)
        manager = Arel::InsertManager.new
        manager.insert [[table[:id], 1], [table[:name], "aaron"]]
        manager.to_sql.must_be_like %{
          INSERT INTO "users" ("id", "name") VALUES (1, 'aaron')
        }
      end

      it "noop for empty list" do
        table = Table.new(:users)
        manager = Arel::InsertManager.new
        manager.insert [[table[:id], 1]]
        manager.insert []
        manager.to_sql.must_be_like %{
          INSERT INTO "users" ("id") VALUES (1)
        }
      end

      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
    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

    describe "columns" do
      it "converts to sql" do
        table   = Table.new :users
        manager = Arel::InsertManager.new
        manager.into table
        manager.columns << table[:id]
        manager.to_sql.must_be_like %{
          INSERT INTO "users" ("id")
        }
      end
    end

    describe "values" do
      it "converts to sql" do
        table   = Table.new :users
        manager = Arel::InsertManager.new
        manager.into table

        manager.values = Nodes::ValuesList.new([[1], [2]])
        manager.to_sql.must_be_like %{
          INSERT INTO "users" VALUES (1), (2)
        }
      end

      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
    end

    describe "combo" do
      it "combines columns and values list in order" do
        table   = Table.new :users
        manager = Arel::InsertManager.new
        manager.into table

        manager.values = Nodes::ValuesList.new([[1, "aaron"], [2, "david"]])
        manager.columns << table[:id]
        manager.columns << table[:name]
        manager.to_sql.must_be_like %{
          INSERT INTO "users" ("id", "name") VALUES (1, 'aaron'), (2, 'david')
        }
      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 %{
          INSERT INTO "users" ("id", "name") (SELECT 1, "aaron")
        }
      end
    end
  end
end