-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathInsertTests.cs
319 lines (282 loc) · 11.3 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
309
310
311
312
313
314
315
316
317
318
319
using Dapper.GraphQL.Test.EntityMappers;
using Dapper.GraphQL.Test.Models;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Dapper.GraphQL.Test
{
public class InsertTests : IClassFixture<TestFixture>
{
private readonly TestFixture fixture;
public InsertTests(TestFixture fixture)
{
this.fixture = fixture;
}
[Fact(DisplayName = "INSERT person succeeds")]
public void InsertPerson()
{
Person person = null;
// Ensure inserting a person works and we get IDs back
int emailId = -1;
int personId = -1;
int phoneId = -1;
try
{
using (var db = fixture.GetDbConnection())
{
db.Open();
// Get the next identity aggressively, as we need to assign
// it to both Id/MergedToPersonId
personId = PostgreSql.NextIdentity(db, (Person p) => p.Id);
Assert.True(personId > 0);
person = new Person
{
Id = personId,
FirstName = "Steven",
LastName = "Rollman",
MergedToPersonId = personId,
};
int insertedCount;
insertedCount = SqlBuilder
.Insert(person)
.Execute(db);
Assert.Equal(1, insertedCount);
emailId = PostgreSql.NextIdentity(db, (Email e) => e.Id);
var email = new Email
{
Id = emailId,
Address = "srollman@landmarkhw.com",
};
var personEmail = new
{
PersonId = personId,
EmailId = emailId,
};
phoneId = PostgreSql.NextIdentity(db, (Phone p) => p.Id);
var phone = new Phone
{
Id = phoneId,
Number = "8011115555",
Type = PhoneType.Mobile,
};
var personPhone = new
{
PersonId = personId,
PhoneId = phoneId,
};
// Add email and phone number to the person
insertedCount = SqlBuilder
.Insert(email)
.Insert(phone)
.Insert("PersonEmail", personEmail)
.Insert("PersonPhone", personPhone)
.Execute(db);
// Ensure all were inserted properly
Assert.Equal(4, insertedCount);
// Build an entity mapper for person
var personMapper = new PersonEntityMapper();
// Query the person from the database
var query = SqlBuilder
.From<Person>("person")
.LeftJoin("PersonEmail personEmail on person.Id = personEmail.Id")
.LeftJoin("Email email on personEmail.EmailId = email.Id")
.LeftJoin("PersonPhone personPhone on person.Id = personPhone.PersonId")
.LeftJoin("Phone phone on personPhone.PhoneId = phone.Id")
.Select("person.*, email.*, phone.*")
.SplitOn<Person>("Id")
.SplitOn<Email>("Id")
.SplitOn<Phone>("Id")
.Where("person.Id = @id", new { id = personId });
var graphql = @"
{
person {
firstName
lastName
emails {
id
address
}
phones {
id
number
}
}
}";
var selection = fixture.BuildGraphQLSelection(graphql);
person = query
.Execute(db, selection, personMapper)
.Single();
}
// Ensure all inserted data is present
Assert.NotNull(person);
Assert.Equal(personId, person.Id);
Assert.Equal("Steven", person.FirstName);
Assert.Equal("Rollman", person.LastName);
Assert.Equal(1, person.Emails.Count);
Assert.Equal("srollman@landmarkhw.com", person.Emails[0].Address);
Assert.Equal(1, person.Phones.Count);
Assert.Equal("8011115555", person.Phones[0].Number);
}
finally
{
// Ensure the changes here don't affect other unit tests
using (var db = fixture.GetDbConnection())
{
if (emailId != default(int))
{
SqlBuilder
.Delete("PersonEmail", new { EmailId = emailId })
.Delete("Email", new { Id = emailId })
.Execute(db);
}
if (phoneId != default(int))
{
SqlBuilder
.Delete("PersonPhone", new { PhoneId = phoneId })
.Delete("Phone", new { Id = phoneId })
.Execute(db);
}
if (personId != default(int))
{
SqlBuilder
.Delete<Person>(new { Id = personId })
.Execute(db);
}
}
}
}
[Fact(DisplayName = "INSERT person asynchronously succeeds")]
public async Task InsertPersonAsync()
{
Person person = null;
// Ensure inserting a person works and we get IDs back
int emailId = -1;
int personId = -1;
int phoneId = -1;
try
{
using (var db = fixture.GetDbConnection())
{
db.Open();
// Get the next identity aggressively, as we need to assign
// it to both Id/MergedToPersonId
personId = PostgreSql.NextIdentity(db, (Person p) => p.Id);
Assert.True(personId > 0);
person = new Person
{
Id = personId,
FirstName = "Steven",
LastName = "Rollman",
MergedToPersonId = personId,
};
int insertedCount;
insertedCount = await SqlBuilder
.Insert(person)
.ExecuteAsync(db);
Assert.Equal(1, insertedCount);
emailId = PostgreSql.NextIdentity(db, (Email e) => e.Id);
var email = new Email
{
Id = emailId,
Address = "srollman@landmarkhw.com",
};
var personEmail = new
{
PersonId = personId,
EmailId = emailId,
};
phoneId = PostgreSql.NextIdentity(db, (Phone p) => p.Id);
var phone = new Phone
{
Id = phoneId,
Number = "8011115555",
Type = PhoneType.Mobile,
};
var personPhone = new
{
PersonId = personId,
PhoneId = phoneId,
};
// Add email and phone number to the person
insertedCount = await SqlBuilder
.Insert(email)
.Insert(phone)
.Insert("PersonEmail", personEmail)
.Insert("PersonPhone", personPhone)
.ExecuteAsync(db);
// Ensure all were inserted properly
Assert.Equal(4, insertedCount);
// Build an entity mapper for person
var personMapper = new PersonEntityMapper();
// Query the person from the database
var query = SqlBuilder
.From<Person>("person")
.LeftJoin("PersonEmail personEmail on person.Id = personEmail.Id")
.LeftJoin("Email email on personEmail.EmailId = email.Id")
.LeftJoin("PersonPhone personPhone on person.Id = personPhone.PersonId")
.LeftJoin("Phone phone on personPhone.PhoneId = phone.Id")
.Select("person.*, email.*, phone.*")
.SplitOn<Person>("Id")
.SplitOn<Email>("Id")
.SplitOn<Phone>("Id")
.Where("person.Id = @id", new { id = personId });
var graphql = @"
{
person {
firstName
lastName
emails {
id
address
}
phones {
id
number
}
}
}";
var selection = fixture.BuildGraphQLSelection(graphql);
var people = await query.ExecuteAsync(db, selection, personMapper);
person = people
.FirstOrDefault();
}
// Ensure all inserted data is present
Assert.NotNull(person);
Assert.Equal(personId, person.Id);
Assert.Equal("Steven", person.FirstName);
Assert.Equal("Rollman", person.LastName);
Assert.Equal(1, person.Emails.Count);
Assert.Equal("srollman@landmarkhw.com", person.Emails[0].Address);
Assert.Equal(1, person.Phones.Count);
Assert.Equal("8011115555", person.Phones[0].Number);
}
finally
{
// Ensure the changes here don't affect other unit tests
using (var db = fixture.GetDbConnection())
{
if (emailId != default(int))
{
await SqlBuilder
.Delete("PersonEmail", new { EmailId = emailId })
.Delete("Email", new { Id = emailId })
.ExecuteAsync(db);
}
if (phoneId != default(int))
{
await SqlBuilder
.Delete("PersonPhone", new { PhoneId = phoneId })
.Delete("Phone", new { Id = phoneId })
.ExecuteAsync(db);
}
if (personId != default(int))
{
await SqlBuilder
.Delete<Person>(new { Id = personId })
.ExecuteAsync(db);
}
}
}
}
}
}