-
Notifications
You must be signed in to change notification settings - Fork 580
Expand file tree
/
Copy pathCrudFixture.cs
More file actions
420 lines (359 loc) · 17.7 KB
/
CrudFixture.cs
File metadata and controls
420 lines (359 loc) · 17.7 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using DapperExtensions.Predicate;
using DapperExtensions.Test.Data.Common;
using FluentAssertions;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DapperExtensions.Test.IntegrationTests.SqlServer
{
[TestFixture]
[Parallelizable(ParallelScope.Self)]
public static class CrudFixture
{
[TestFixture]
public class InsertMethod : SqlServerBaseFixture
{
[Test]
public void AddsEntityToDatabase_ReturnsKey()
{
Person p = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
var id = Db.Insert(p);
Assert.AreEqual(1, id);
Assert.AreEqual(1, p.Id);
}
[Test]
public void AddsEntityToDatabase_ReturnsCompositeKey()
{
Multikey m = new Multikey { Key2 = "key", Value = "foo" };
var key = Db.Insert(m);
Assert.AreEqual(1, key.Key1);
Assert.AreEqual("key", key.Key2);
}
[Test]
public void AddsEntityToDatabase_ReturnsGeneratedPrimaryKey()
{
Animal a1 = new Animal { Name = "Foo" };
Db.Insert(a1);
var a2 = Db.Get<Animal>(a1.Id);
Assert.AreNotEqual(Guid.Empty, a2.Id);
Assert.AreEqual(a1.Id, a2.Id);
}
[Test]
public void AddsMultipleEntitiesToDatabase()
{
Animal a1 = new Animal { Name = "Foo" };
Animal a2 = new Animal { Name = "Bar" };
Animal a3 = new Animal { Name = "Baz" };
Db.Insert<Animal>(new[] { a1, a2, a3 });
var animals = Db.GetList<Animal>().ToList();
Assert.AreEqual(3, animals.Count);
}
}
[TestFixture]
public class GetMethod : SqlServerBaseFixture
{
[Test]
public void UsingKey_ReturnsEntity()
{
Person p0 = new Person
{
Active = true,
FirstName = "Foo",
LastName = "Bar",
DateCreated = DateTime.UtcNow
};
Db.Insert(p0);
Person p1 = new Person
{
Active = true,
FirstName = "Foo",
LastName = "Bar",
DateCreated = DateTime.UtcNow
};
var id = Db.Insert(p1);
Person p2 = Db.Get<Person>(id);
Assert.AreEqual(id, p2.Id);
Assert.AreEqual("Foo", p2.FirstName);
Assert.AreEqual("Bar", p2.LastName);
}
[Test]
public void UsingCompositeKey_ReturnsEntity()
{
Multikey m1 = new Multikey { Key2 = "key", Value = "bar" };
var key = Db.Insert(m1);
Multikey m2 = Db.Get<Multikey>(new { key.Key1, key.Key2 });
Assert.AreEqual(1, m2.Key1);
Assert.AreEqual("key", m2.Key2);
Assert.AreEqual("bar", m2.Value);
}
}
[TestFixture]
public class DeleteMethod : SqlServerBaseFixture
{
[Test]
public void UsingKey_DeletesFromDatabase()
{
Person p1 = new Person
{
Active = true,
FirstName = "Foo",
LastName = "Bar",
DateCreated = DateTime.UtcNow
};
var id = Db.Insert(p1);
Person p2 = Db.Get<Person>(id);
Db.Delete(p2);
Assert.IsNull(Db.Get<Person>(id));
}
[Test]
public void UsingCompositeKey_DeletesFromDatabase()
{
Multikey m1 = new Multikey { Key2 = "key", Value = "bar" };
var key = Db.Insert(m1);
Multikey m2 = Db.Get<Multikey>(new { key.Key1, key.Key2 });
Db.Delete(m2);
Assert.IsNull(Db.Get<Multikey>(new { key.Key1, key.Key2 }));
}
[Test]
public void UsingPredicate_DeletesRows()
{
Person p1 = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
Person p2 = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
Person p3 = new Person { Active = true, FirstName = "Foo", LastName = "Barz", DateCreated = DateTime.UtcNow };
Db.Insert(p1);
Db.Insert(p2);
Db.Insert(p3);
var list = Db.GetList<Person>();
Assert.AreEqual(3, list.Count());
IPredicate pred = Predicates.Field<Person>(p => p.LastName, Operator.Eq, "Bar");
var result = Db.Delete<Person>(pred);
Assert.IsTrue(result);
list = Db.GetList<Person>();
Assert.AreEqual(1, list.Count());
}
[Test]
public void UsingObject_DeletesRows()
{
Person p1 = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
Person p2 = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
Person p3 = new Person { Active = true, FirstName = "Foo", LastName = "Barz", DateCreated = DateTime.UtcNow };
Db.Insert(p1);
Db.Insert(p2);
Db.Insert(p3);
var list = Db.GetList<Person>();
Assert.AreEqual(3, list.Count());
var result = Db.Delete<Person>(new { LastName = "Bar" });
Assert.IsTrue(result);
list = Db.GetList<Person>();
Assert.AreEqual(1, list.Count());
}
}
[TestFixture]
public class UpdateMethod : SqlServerBaseFixture
{
[Test]
public void UsingKey_UpdatesEntity()
{
Person p1 = new Person
{
Active = true,
FirstName = "Foo",
LastName = "Bar",
DateCreated = DateTime.UtcNow
};
var id = Db.Insert(p1);
var p2 = Db.Get<Person>(id);
p2.FirstName = "Baz";
p2.Active = false;
Db.Update(p2);
var p3 = Db.Get<Person>(id);
Assert.AreEqual("Baz", p3.FirstName);
Assert.AreEqual("Bar", p3.LastName);
Assert.AreEqual(false, p3.Active);
}
[Test]
public void UsingCompositeKey_UpdatesEntity()
{
Multikey m1 = new Multikey { Key2 = "key", Value = "bar" };
var key = Db.Insert(m1);
Multikey m2 = Db.Get<Multikey>(new { key.Key1, key.Key2 });
m2.Key2 = "key";
m2.Value = "barz";
Db.Update(m2);
Multikey m3 = Db.Get<Multikey>(new { Key1 = 1, Key2 = "key" });
Assert.AreEqual(1, m3.Key1);
Assert.AreEqual("key", m3.Key2);
Assert.AreEqual("barz", m3.Value);
}
}
[TestFixture]
public class GetListMethod : SqlServerBaseFixture
{
[Test]
public void UsingNullPredicate_ReturnsAll()
{
Db.Insert(new Person { Active = true, FirstName = "a", LastName = "a1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = true, FirstName = "c", LastName = "c1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = false, FirstName = "d", LastName = "d1", DateCreated = DateTime.UtcNow });
IEnumerable<Person> list = Db.GetList<Person>();
Assert.AreEqual(4, list.Count());
}
[Test]
public void UsingPredicate_ReturnsMatching()
{
Db.Insert(new Person { Active = true, FirstName = "a", LastName = "a1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = true, FirstName = "c", LastName = "c1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = false, FirstName = "d", LastName = "d1", DateCreated = DateTime.UtcNow });
var predicate = Predicates.Field<Person>(f => f.Active, Operator.Eq, true);
IEnumerable<Person> list = Db.GetList<Person>(predicate, null);
Assert.AreEqual(2, list.Count());
Assert.IsTrue(list.All(p => p.FirstName == "a" || p.FirstName == "c"));
}
[Test]
public void UsingObject_ReturnsMatching()
{
Db.Insert(new Person { Active = true, FirstName = "a", LastName = "a1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = true, FirstName = "c", LastName = "c1", DateCreated = DateTime.UtcNow });
Db.Insert(new Person { Active = false, FirstName = "d", LastName = "d1", DateCreated = DateTime.UtcNow });
var predicate = new { Active = true, FirstName = "c" };
IEnumerable<Person> list = Db.GetList<Person>(predicate, null);
Assert.AreEqual(1, list.Count());
Assert.IsTrue(list.All(p => p.FirstName == "c"));
}
}
[TestFixture]
public class GetPageMethod : SqlServerBaseFixture
{
private void SetData(out dynamic id1, out dynamic id2, out dynamic id3, out dynamic id4)
{
id1 = Db.Insert(new Person { Active = true, FirstName = "Sigma", LastName = "Alpha", DateCreated = DateTime.UtcNow });
id2 = Db.Insert(new Person { Active = false, FirstName = "Delta", LastName = "Alpha", DateCreated = DateTime.UtcNow });
id3 = Db.Insert(new Person { Active = true, FirstName = "Theta", LastName = "Gamma", DateCreated = DateTime.UtcNow });
id4 = Db.Insert(new Person { Active = false, FirstName = "Iota", LastName = "Beta", DateCreated = DateTime.UtcNow });
}
[Test]
public void UsingNullPredicate_ReturnsMatching()
{
SetData(out var id1, out var id2, out var id3, out var id4);
IList<ISort> sort = new List<ISort>
{
Predicates.Sort<Person>(p => p.LastName),
Predicates.Sort<Person>("FirstName")
};
IEnumerable<Person> list = Db.GetPage<Person>(null, sort, 0, 2);
Assert.AreEqual(2, list.Count());
Assert.AreEqual(id2, list.First().Id);
Assert.AreEqual(id1, list.Skip(1).First().Id);
}
[Test]
public void UsingPredicate_ReturnsMatching()
{
SetData(out var id1, out var id2, out var id3, out var id4);
var predicate = Predicates.Field<Person>(f => f.Active, Operator.Eq, true);
IList<ISort> sort = new List<ISort>
{
Predicates.Sort<Person>(p => p.LastName),
Predicates.Sort<Person>("FirstName")
};
IEnumerable<Person> list = Db.GetPage<Person>(predicate, sort, 0, 2);
Assert.AreEqual(2, list.Count());
Assert.IsTrue(list.All(p => p.FirstName == "Sigma" || p.FirstName == "Theta" || p.FirstName == "Iota"));
}
[Test]
public void NotFirstPage_Returns_NextResults()
{
SetData(out var id1, out var id2, out var id3, out var id4);
IList<ISort> sort = new List<ISort>
{
Predicates.Sort<Person>(p => p.LastName),
Predicates.Sort<Person>("FirstName")
};
IEnumerable<Person> list = Db.GetPage<Person>(null, sort, 2, 2);
Assert.AreEqual(2, list.Count());
Assert.AreEqual(id4, list.First().Id);
Assert.AreEqual(id3, list.Skip(1).First().Id);
}
[Test]
public void UsingObject_ReturnsMatching()
{
SetData(out var id1, out var id2, out var id3, out var id4);
var predicate = new { Active = true };
IList<ISort> sort = new List<ISort>
{
Predicates.Sort<Person>(p => p.LastName),
Predicates.Sort<Person>("FirstName")
};
IEnumerable<Person> list = Db.GetPage<Person>(predicate, sort, 0, 3);
Assert.AreEqual(2, list.Count());
Assert.IsTrue(list.All(p => p.FirstName == "Sigma" || p.FirstName == "Theta"));
}
}
[TestFixture]
public class CountMethod : SqlServerBaseFixture
{
[Test]
public void UsingNullPredicate_Returns_Count()
{
Db.Insert(new Person { Active = true, FirstName = "a", LastName = "a1", DateCreated = DateTime.UtcNow.AddDays(-10) });
Db.Insert(new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow.AddDays(-10) });
Db.Insert(new Person { Active = true, FirstName = "c", LastName = "c1", DateCreated = DateTime.UtcNow.AddDays(-3) });
Db.Insert(new Person { Active = false, FirstName = "d", LastName = "d1", DateCreated = DateTime.UtcNow.AddDays(-1) });
int count = Db.Count<Person>(null);
Assert.AreEqual(4, count);
}
[Test]
public void UsingPredicate_Returns_Count()
{
Db.Insert(new Person { Active = true, FirstName = "a", LastName = "a1", DateCreated = DateTime.UtcNow.AddDays(-10) });
Db.Insert(new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow.AddDays(-10) });
Db.Insert(new Person { Active = true, FirstName = "c", LastName = "c1", DateCreated = DateTime.UtcNow.AddDays(-3) });
Db.Insert(new Person { Active = false, FirstName = "d", LastName = "d1", DateCreated = DateTime.UtcNow.AddDays(-1) });
var predicate = Predicates.Field<Person>(f => f.DateCreated, Operator.Lt, DateTime.UtcNow.AddDays(-5));
int count = Db.Count<Person>(predicate);
Assert.AreEqual(2, count);
}
[Test]
public void UsingObject_Returns_Count()
{
Db.Insert(new Person { Active = true, FirstName = "a", LastName = "a1", DateCreated = DateTime.UtcNow.AddDays(-10) });
Db.Insert(new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow.AddDays(-10) });
Db.Insert(new Person { Active = true, FirstName = "c", LastName = "c1", DateCreated = DateTime.UtcNow.AddDays(-3) });
Db.Insert(new Person { Active = false, FirstName = "d", LastName = "d1", DateCreated = DateTime.UtcNow.AddDays(-1) });
var predicate = new { FirstName = new[] { "b", "d" } };
int count = Db.Count<Person>(predicate);
Assert.AreEqual(2, count);
}
}
[TestFixture]
public class GetMultipleMethod : SqlServerBaseFixture
{
[Test]
public void ReturnsItems()
{
Db.Insert(new Person { Active = true, FirstName = "a", LastName = "a1", DateCreated = DateTime.UtcNow.AddDays(-10) });
Db.Insert(new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow.AddDays(-10) });
Db.Insert(new Person { Active = true, FirstName = "c", LastName = "c1", DateCreated = DateTime.UtcNow.AddDays(-3) });
Db.Insert(new Person { Active = false, FirstName = "d", LastName = "d1", DateCreated = DateTime.UtcNow.AddDays(-1) });
Db.Insert(new Animal { Name = "Foo" });
Db.Insert(new Animal { Name = "Bar" });
Db.Insert(new Animal { Name = "Baz" });
var predicate = new GetMultiplePredicate();
predicate.Add<Person>(null);
predicate.Add<Animal>(Predicates.Field<Animal>(a => a.Name, Operator.Like, "Ba%"));
predicate.Add<Person>(Predicates.Field<Person>(a => a.LastName, Operator.Eq, "c1"));
var result = Db.GetMultiple(predicate);
var people = result.Read<Person>().ToList();
var animals = result.Read<Animal>().ToList();
var people2 = result.Read<Person>().ToList();
people.Should().HaveCount(4);
animals.Should().HaveCount(2);
people2.Should().HaveCount(1);
Dispose();
}
}
}
}