-
-
Notifications
You must be signed in to change notification settings - Fork 593
/
Copy pathEFCoreBulkTestAtypical.cs
398 lines (363 loc) · 15 KB
/
EFCoreBulkTestAtypical.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using EFCore.BulkExtensions.SqlAdapters;
using Microsoft.EntityFrameworkCore;
using NetTopologySuite.Geometries;
using Xunit;
namespace EFCore.BulkExtensions.Tests
{
public class EFCoreBulkTestAtypical
{
protected int EntitiesNumber => 1000;
[Theory]
[InlineData(DbServer.SqlServer)]
[InlineData(DbServer.Sqlite)] // Does NOT have Computed Columns
private void ComputedAndDefaultValuesTest(DbServer databaseType)
{
ContextUtil.DbServer = databaseType;
using (var context = new TestContext(ContextUtil.GetOptions()))
{
context.Truncate<Document>();
var entities = new List<Document>();
for (int i = 1; i <= EntitiesNumber; i++)
{
var entity = new Document
{
Content = "Info " + i
};
if (databaseType == DbServer.Sqlite)
{
entity.DocumentId = Guid.NewGuid();
entity.ContentLength = entity.Content.Length;
entity.IsActive = true;
}
entities.Add(entity);
}
context.BulkInsert(entities, bulkAction => bulkAction.SetOutputIdentity = true); // example of setting BulkConfig with Action argument
}
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = context.Documents.ToList();
Assert.Equal(EntitiesNumber, entities.Count());
var firstDocument = entities[0];
Assert.NotEqual(Guid.Empty, firstDocument.DocumentId);
Assert.Equal(firstDocument.Content.Length, firstDocument.ContentLength);
Assert.Equal(true, firstDocument.IsActive);
}
}
[Theory]
[InlineData(DbServer.SqlServer)]
//[InlineData(DbServer.Sqlite)] // No TimeStamp column type but can be set with DefaultValueSql: "CURRENT_TIMESTAMP" as it is in OnModelCreating() method.
private void TimeStampTest(DbServer databaseType)
{
ContextUtil.DbServer = databaseType;
using (var context = new TestContext(ContextUtil.GetOptions()))
{
context.Truncate<File>();
var entities = new List<File>();
for (int i = 1; i <= EntitiesNumber; i++)
{
var entity = new File
{
Data = "Some data " + i
};
entities.Add(entity);
}
context.BulkInsert(entities, bulkAction => bulkAction.SetOutputIdentity = true); // example of setting BulkConfig with Action argument
// For testing concurrency conflict (UPDATE changes RowVersion which is TimeStamp column)
context.Database.ExecuteSqlRaw("UPDATE dbo.[File] SET Data = 'Some data 1 PRE CHANGE' WHERE [Id] = 1;");
var entitiesToUpdate = entities.Take(10).ToList();
foreach (var entityToUpdate in entitiesToUpdate)
{
entityToUpdate.Data += " UPDATED";
}
using (var transaction = context.Database.BeginTransaction())
{
var bulkConfig = new BulkConfig { SetOutputIdentity = true, DoNotUpdateIfTimeStampChanged = true };
context.BulkUpdate(entitiesToUpdate, bulkConfig);
var list = bulkConfig.TimeStampInfo?.EntitiesOutput.Cast<File>().ToList();
Assert.Equal(9, list.Count());
Assert.Equal(1, bulkConfig.TimeStampInfo.NumberOfSkippedForUpdate);
if (bulkConfig.TimeStampInfo?.NumberOfSkippedForUpdate > 0)
{
transaction.Rollback();
}
else
{
transaction.Commit();
}
}
}
}
[Theory]
[InlineData(DbServer.SqlServer)]
[InlineData(DbServer.Sqlite)]
private void CompositeKeyTest(DbServer databaseType)
{
ContextUtil.DbServer = databaseType;
using (var context = new TestContext(ContextUtil.GetOptions()))
{
context.Truncate<UserRole>();
var entities = new List<UserRole>();
for (int i = 0; i < EntitiesNumber; i++)
{
entities.Add(new UserRole
{
UserId = i / 10,
RoleId = i % 10,
Description = "desc"
});
}
context.BulkInsert(entities);
}
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = context.UserRoles.ToList();
int entitiesCount = entities.Count();
for (int i = 0; i < entitiesCount; i++)
{
entities[i].Description = "desc updated " + i;
}
context.BulkUpdate(entities);
}
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = context.UserRoles.ToList();
Assert.Equal(EntitiesNumber, entities.Count());
context.BulkDelete(entities);
}
}
[Theory]
[InlineData(DbServer.SqlServer)]
[InlineData(DbServer.Sqlite)]
private void DiscriminatorShadowTest(DbServer databaseType)
{
ContextUtil.DbServer = databaseType;
using (var context = new TestContext(ContextUtil.GetOptions()))
{
context.BulkDelete(context.Students.ToList());
}
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = new List<Student>();
for (int i = 1; i <= EntitiesNumber; i++)
{
entities.Add(new Student
{
Name = "name " + i,
Subject = "Math"
});
}
context.Students.AddRange(entities); // adding to Context so that Shadow property 'Discriminator' gets set
context.BulkInsert(entities);
}
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = new List<Student>();
for (int i = 1; i <= EntitiesNumber / 2; i += 2)
{
entities.Add(new Student
{
Name = "name " + i,
Subject = "Math Upd"
});
}
context.Students.AddRange(entities); // adding to Context so that Shadow property 'Discriminator' gets set
context.BulkInsertOrUpdate(entities, new BulkConfig
{
UpdateByProperties = new List<string> { nameof(Student.Name) },
PropertiesToExclude = new List<string> { nameof(Student.PersonId) },
});
}
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = context.Students.ToList();
Assert.Equal(EntitiesNumber, entities.Count());
}
}
[Theory]
[InlineData(DbServer.SqlServer)]
[InlineData(DbServer.Sqlite)]
private void ValueConversionTest(DbServer databaseType)
{
ContextUtil.DbServer = databaseType;
var dateTime = DateTime.Today;
using (var context = new TestContext(ContextUtil.GetOptions()))
{
context.BulkDelete(context.Infos.ToList());
}
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = new List<Info>();
for (int i = 1; i <= EntitiesNumber; i++)
{
entities.Add(new Info
{
Message = "Msg " + i,
ConvertedTime = dateTime,
InfoType = InfoType.InfoTypeA
});
}
context.BulkInsert(entities);
}
if (databaseType == DbServer.SqlServer)
{
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = context.Infos.ToList();
var entity = entities.FirstOrDefault();
Assert.Equal(entity.ConvertedTime, dateTime);
var conn = context.Database.GetDbConnection();
if (conn.State != ConnectionState.Open)
conn.Open();
using (var command = conn.CreateCommand())
{
command.CommandText = $"SELECT TOP 1 * FROM {nameof(Info)} ORDER BY {nameof(Info.InfoId)} DESC";
var reader = command.ExecuteReader();
reader.Read();
var row = new Info()
{
ConvertedTime = reader.Field<DateTime>(nameof(Info.ConvertedTime))
};
Assert.Equal(row.ConvertedTime, dateTime.AddDays(1));
}
}
}
}
[Theory]
[InlineData(DbServer.SqlServer)]
[InlineData(DbServer.Sqlite)]
private void OwnedTypesTest(DbServer databaseType)
{
ContextUtil.DbServer = databaseType;
using (var context = new TestContext(ContextUtil.GetOptions()))
{
if (databaseType == DbServer.SqlServer)
{
context.Truncate<ChangeLog>();
context.Database.ExecuteSqlRaw("TRUNCATE TABLE [" + nameof(ChangeLog) + "]");
}
else
{
//context.ChangeLogs.BatchDelete(); // TODO
context.BulkDelete(context.ChangeLogs.ToList());
}
var entities = new List<ChangeLog>();
for (int i = 1; i <= EntitiesNumber; i++)
{
entities.Add(new ChangeLog
{
Description = "Dsc " + i,
Audit = new Audit
{
ChangedBy = "User" + 1,
ChangedTime = DateTime.Now,
InfoType = InfoType.InfoTypeA
}/*,
AuditExtended = new AuditExtended
{
CreatedBy = "UserS" + 1,
Remark = "test",
CreatedTime = DateTime.Now
},
AuditExtendedSecond = new AuditExtended
{
CreatedBy = "UserS" + 1,
Remark = "sec",
CreatedTime = DateTime.Now
}*/
});
}
context.BulkInsert(entities);
if (databaseType == DbServer.SqlServer)
{
context.BulkRead(
entities,
new BulkConfig
{
UpdateByProperties = new List<string> { nameof(Item.Description) }
}
);
Assert.Equal(2, entities[1].ChangeLogId);
}
}
}
[Theory]
[InlineData(DbServer.SqlServer)]
//[InlineData(DbServer.Sqlite)] Not supported
private void ShadowFKPropertiesTest(DbServer databaseType) // with Foreign Key as Shadow Property
{
ContextUtil.DbServer = databaseType;
using (var context = new TestContext(ContextUtil.GetOptions()))
{
if (databaseType == DbServer.SqlServer)
{
context.Truncate<ItemLink>();
context.Database.ExecuteSqlRaw("TRUNCATE TABLE [" + nameof(ItemLink) + "]");
}
else
{
//context.ChangeLogs.BatchDelete(); // TODO
context.BulkDelete(context.ItemLinks.ToList());
}
context.BulkDelete(context.Items.ToList()); // On table with FK Truncate does not work
for (int i = 1; i < 10; ++i)
{
var entity = new Item
{
ItemId = 0,
Name = "name " + i,
Description = "info " + Guid.NewGuid().ToString().Substring(0, 3),
Quantity = i % 10,
Price = i / (i % 5 + 1),
TimeUpdated = DateTime.Now,
ItemHistories = new List<ItemHistory>()
};
context.Items.Add(entity);
}
context.SaveChanges();
var items = context.Items.ToList();
var entities = new List<ItemLink>();
for (int i = 0; i <= EntitiesNumber - 1; i++)
{
entities.Add(new ItemLink
{
ItemLinkId = 0,
Item = items[i % items.Count]
});
}
context.BulkInsert(entities);
if (databaseType == DbServer.SqlServer)
{
context.BulkRead(entities);
foreach (var entity in entities)
{
Assert.NotNull(entity.Item);
}
}
context.BulkDelete(context.ItemLinks.ToList());
}
}
[Fact]
private void GeometryColumnTest()
{
ContextUtil.DbServer = DbServer.SqlServer;
using (var context = new TestContext(ContextUtil.GetOptions()))
{
context.BulkDelete(context.Addresses.ToList());
}
using (var context = new TestContext(ContextUtil.GetOptions()))
{
var entities = new List<Address> {
new Address {
Street = "Some Street nn",
Location = new Point(52, 13)
}
};
context.BulkInsertOrUpdate(entities);
}
}
}
}