-
-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathInsertTests.cs
308 lines (248 loc) · 10.7 KB
/
InsertTests.cs
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using SqlKata.Compilers;
using SqlKata.Tests.Infrastructure;
using Xunit;
namespace SqlKata.Tests
{
public class InsertTests : TestSupport
{
private class Account
{
public Account(string name, string currency = null, string created_at = null, string color = null)
{
this.name = name ?? throw new ArgumentNullException(nameof(name));
this.Currency = currency;
this.color = color;
}
public string name { get; set; }
[Column("currency_id")]
public string Currency { get; set; }
[Ignore]
public string color { get; set; }
}
[Fact]
public void InsertObject()
{
var query = new Query("Table")
.AsInsert(
new
{
Name = "The User",
Age = new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc),
});
var c = Compile(query);
Assert.Equal(
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
c[EngineCodes.Firebird]);
}
[Fact]
public void InsertFromSubQueryWithCte()
{
var query = new Query("expensive_cars")
.With("old_cards", new Query("all_cars").Where("year", "<", 2000))
.AsInsert(
new[] { "name", "model", "year" },
new Query("old_cars").Where("price", ">", 100).ForPage(2, 10));
var c = Compile(query);
Assert.Equal(
"WITH [old_cards] AS (SELECT * FROM [all_cars] WHERE [year] < 2000)\nINSERT INTO [expensive_cars] ([name], [model], [year]) SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS [row_num] FROM [old_cars] WHERE [price] > 100) AS [results_wrapper] WHERE [row_num] BETWEEN 11 AND 20",
c[EngineCodes.SqlServer]);
Assert.Equal(
"WITH `old_cards` AS (SELECT * FROM `all_cars` WHERE `year` < 2000)\nINSERT INTO `expensive_cars` (`name`, `model`, `year`) SELECT * FROM `old_cars` WHERE `price` > 100 LIMIT 10 OFFSET 10",
c[EngineCodes.MySql]);
Assert.Equal(
"WITH \"old_cards\" AS (SELECT * FROM \"all_cars\" WHERE \"year\" < 2000)\nINSERT INTO \"expensive_cars\" (\"name\", \"model\", \"year\") SELECT * FROM \"old_cars\" WHERE \"price\" > 100 LIMIT 10 OFFSET 10",
c[EngineCodes.PostgreSql]);
}
[Fact]
public void InsertMultiRecords()
{
var query = new Query("expensive_cars")
.AsInsert(
new[] { "name", "brand", "year" },
new[]
{
new object[] { "Chiron", "Bugatti", null },
new object[] { "Huayra", "Pagani", 2012 },
new object[] { "Reventon roadster", "Lamborghini", 2009 }
});
var c = Compile(query);
Assert.Equal(
"INSERT INTO [expensive_cars] ([name], [brand], [year]) VALUES ('Chiron', 'Bugatti', NULL), ('Huayra', 'Pagani', 2012), ('Reventon roadster', 'Lamborghini', 2009)",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"EXPENSIVE_CARS\" (\"NAME\", \"BRAND\", \"YEAR\") SELECT 'Chiron', 'Bugatti', NULL FROM RDB$DATABASE UNION ALL SELECT 'Huayra', 'Pagani', 2012 FROM RDB$DATABASE UNION ALL SELECT 'Reventon roadster', 'Lamborghini', 2009 FROM RDB$DATABASE",
c[EngineCodes.Firebird]);
}
[Fact]
public void InsertWithNullValues()
{
var query = new Query("Books")
.AsInsert(
new[] { "Id", "Author", "ISBN", "Date" },
new object[] { 1, "Author 1", "123456", null });
var c = Compile(query);
Assert.Equal("INSERT INTO [Books] ([Id], [Author], [ISBN], [Date]) VALUES (1, 'Author 1', '123456', NULL)",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"BOOKS\" (\"ID\", \"AUTHOR\", \"ISBN\", \"DATE\") VALUES (1, 'Author 1', '123456', NULL)",
c[EngineCodes.Firebird]);
}
[Fact]
public void InsertWithEmptyString()
{
var query = new Query("Books")
.AsInsert(
new[] { "Id", "Author", "ISBN", "Description" },
new object[] { 1, "Author 1", "123456", "" });
var c = Compile(query);
Assert.Equal(
"INSERT INTO [Books] ([Id], [Author], [ISBN], [Description]) VALUES (1, 'Author 1', '123456', '')",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"BOOKS\" (\"ID\", \"AUTHOR\", \"ISBN\", \"DESCRIPTION\") VALUES (1, 'Author 1', '123456', '')",
c[EngineCodes.Firebird]);
}
[Fact]
public void InsertWithByteArray()
{
var fauxImagebytes = new byte[] { 0x1, 0x3, 0x3, 0x7 };
var query = new Query("Books")
.AsInsert(
new[] { "Id", "CoverImageBytes" },
new object[]
{
1,
fauxImagebytes
});
var c = Compilers.Compile(query);
Assert.All(c.Values, a => Assert.Equal(2, a.NamedBindings.Count));
var exemplar = c[EngineCodes.SqlServer];
Assert.Equal("INSERT INTO [Books] ([Id], [CoverImageBytes]) VALUES (?, ?)", exemplar.RawSql);
Assert.Equal("INSERT INTO [Books] ([Id], [CoverImageBytes]) VALUES (@p0, @p1)", exemplar.Sql);
}
[Fact]
public void InsertWithIgnoreAndColumnProperties()
{
var account = new Account(name: $"popular", color: $"blue", currency: "US");
var query = new Query("Account").AsInsert(account);
var c = Compile(query);
Assert.Equal(
"INSERT INTO [Account] ([name], [currency_id]) VALUES ('popular', 'US')",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"ACCOUNT\" (\"NAME\", \"CURRENCY_ID\") VALUES ('popular', 'US')",
c[EngineCodes.Firebird]);
}
[Fact]
public void InsertFromRaw()
{
var query = new Query()
.FromRaw("Table.With.Dots")
.AsInsert(
new
{
Name = "The User",
Age = new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc),
});
var c = Compile(query);
Assert.Equal(
"INSERT INTO Table.With.Dots ([Name], [Age]) VALUES ('The User', '2018-01-01')",
c[EngineCodes.SqlServer]);
}
[Fact]
public void InsertFromQueryShouldFail()
{
var query = new Query()
.From(new Query("InnerTable"))
.AsInsert(
new
{
Name = "The User",
Age = new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc),
});
Assert.Throws<InvalidOperationException>(() =>
{
Compile(query);
});
}
[Fact]
public void InsertKeyValuePairs()
{
var dictionaryUser = new Dictionary<string, object>
{
{ "Name", "The User" },
{ "Age", new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc) },
}
.ToArray();
var query = new Query("Table")
.AsInsert(dictionaryUser);
var c = Compile(query);
Assert.Equal(
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
c[EngineCodes.Firebird]);
}
[Fact]
public void InsertDictionary()
{
var dictionaryUser = new Dictionary<string, object> {
{ "Name", "The User" },
{ "Age", new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc) },
};
var query = new Query("Table")
.AsInsert(dictionaryUser);
var c = Compile(query);
Assert.Equal(
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
c[EngineCodes.Firebird]);
}
[Fact]
public void InsertReadOnlyDictionary()
{
var dictionaryUser = new ReadOnlyDictionary<string, object>(
new Dictionary<string, object>
{
{ "Name", "The User" },
{ "Age", new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc) },
});
var query = new Query("Table")
.AsInsert(dictionaryUser);
var c = Compile(query);
Assert.Equal(
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
c[EngineCodes.Firebird]);
}
[Fact]
public void InsertExpandoObject()
{
dynamic expandoUser = new ExpandoObject();
expandoUser.Name = "The User";
expandoUser.Age = new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var query = new Query("Table")
.AsInsert(expandoUser);
var c = Compile(query);
Assert.Equal(
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
c[EngineCodes.SqlServer]);
Assert.Equal(
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
c[EngineCodes.Firebird]);
}
}
}