-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
How to handle two objs of the same type in the EntityMapper #2
- Loading branch information
Showing
16 changed files
with
379 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
using Dapper.GraphQL.Test.Models; | ||
using Dapper.GraphQL.Test.QueryBuilders; | ||
using DbUp; | ||
using DbUp.SQLite.Helpers; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
|
@@ -25,7 +23,7 @@ public GraphQLTests( | |
this.fixture = fixture; | ||
} | ||
|
||
[Fact(DisplayName = "Full people query should succeed", Skip = "SQLite issues with dynamic typecasting cause this to incorrectly fail.")] | ||
[Fact(DisplayName = "Full people query should succeed")] | ||
public async Task FullPeopleQuery() | ||
{ | ||
var json = await fixture.QueryGraphQLAsync(@" | ||
|
@@ -57,7 +55,86 @@ public async Task FullPeopleQuery() | |
}"); | ||
|
||
var expectedJson = @" | ||
{ | ||
{ | ||
""data"": { | ||
""people"": [ | ||
{ | ||
""id"": 1, | ||
""firstName"": ""Hyrum"", | ||
""lastName"": ""Clyde"", | ||
""emails"": [ | ||
{ | ||
""id"": 1, | ||
""address"": ""[email protected]"" | ||
} | ||
], | ||
""phones"": [], | ||
""supervisor"": null, | ||
""careerCounselor"": null | ||
}, | ||
{ | ||
""id"": 2, | ||
""firstName"": ""Doug"", | ||
""lastName"": ""Day"", | ||
""emails"": [ | ||
{ | ||
""id"": 2, | ||
""address"": ""[email protected]"" | ||
}, | ||
{ | ||
""id"": 3, | ||
""address"": ""[email protected]"" | ||
} | ||
], | ||
""phones"": [ | ||
{ | ||
""id"": 1, | ||
""number"": ""8011234567"", | ||
""type"": 3 | ||
} | ||
], | ||
""supervisor"": null, | ||
""careerCounselor"": { | ||
""id"": 1, | ||
""firstName"": ""Hyrum"", | ||
""lastName"": ""Clyde"" | ||
} | ||
}, | ||
{ | ||
""id"": 3, | ||
""firstName"": ""Kevin"", | ||
""lastName"": ""Russon"", | ||
""emails"": [ | ||
{ | ||
""id"": 4, | ||
""address"": ""[email protected]"" | ||
} | ||
], | ||
""phones"": [ | ||
{ | ||
""id"": 2, | ||
""number"": ""8019876543"", | ||
""type"": 3 | ||
}, | ||
{ | ||
""id"": 3, | ||
""number"": ""8011111111"", | ||
""type"": 1 | ||
} | ||
], | ||
""supervisor"": { | ||
""id"": 1, | ||
""firstName"": ""Hyrum"", | ||
""lastName"": ""Clyde"" | ||
}, | ||
""careerCounselor"": { | ||
""id"": 2, | ||
""firstName"": ""Doug"", | ||
""lastName"": ""Day"" | ||
} | ||
} | ||
] | ||
} | ||
}"; | ||
|
||
Assert.True(fixture.JsonEquals(expectedJson, json)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
using Dapper.GraphQL.Test.Models; | ||
using Dapper.GraphQL.Test.QueryBuilders; | ||
using DbUp; | ||
using DbUp.SQLite.Helpers; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
|
@@ -31,13 +29,17 @@ public void InsertPerson() | |
var person = new Person | ||
{ | ||
FirstName = "Steven", | ||
LastName = "Rollman", | ||
LastName = "Rollman", | ||
}; | ||
|
||
// Ensure inserting a person works and we get the person's Id back | ||
var personId = SqlBuilder | ||
.Insert(person) | ||
.ExecuteWithSqliteIdentity(fixture.DbConnection); | ||
int personId; | ||
using (var db = fixture.GetDbConnection()) | ||
{ | ||
personId = SqlBuilder | ||
.Insert(person) | ||
.ExecuteWithSqlIdentity<int>(db); | ||
} | ||
|
||
Assert.True(personId > 0); | ||
|
||
|
@@ -55,43 +57,60 @@ public void InsertPerson() | |
}; | ||
|
||
// Add email and phone number to the person | ||
var insertedCount = SqlBuilder | ||
.Insert(email) | ||
.Insert(phone) | ||
.Execute(fixture.DbConnection); | ||
int insertedCount; | ||
using (var db = fixture.GetDbConnection()) | ||
{ | ||
insertedCount = SqlBuilder | ||
.Insert(email) | ||
.Insert(phone) | ||
.Execute(fixture.GetDbConnection()); | ||
|
||
// Ensure both were inserted properly | ||
Assert.Equal(2, insertedCount); | ||
// Ensure both were inserted properly | ||
Assert.Equal(2, insertedCount); | ||
|
||
// Build an identity mapper for person | ||
var personMapper = fixture.BuildMapper<Person>(p => p.Id); | ||
// Build an identity mapper for person | ||
var personMapper = fixture.BuildMapper<Person>(p => p.Id); | ||
|
||
// Query the person from the database | ||
var query = SqlBuilder | ||
.From<Person>("person") | ||
.LeftJoin("Email email on person.Id = email.PersonId") | ||
.LeftJoin("Phone phone on person.Id = phone.PersonId") | ||
.Select("person.*, email.*, phone.*") | ||
.SplitOn<Person>("Id") | ||
.SplitOn<Email>("Id") | ||
.SplitOn<Phone>("Id") | ||
.Where("person.Id = @id", new { id = personId }); | ||
// Query the person from the database | ||
var query = SqlBuilder | ||
.From<Person>("person") | ||
.LeftJoin("Email email on person.Id = email.PersonId") | ||
.LeftJoin("Phone phone on person.Id = phone.PersonId") | ||
.Select("person.*, email.*, phone.*") | ||
.SplitOn<Person>("Id") | ||
.SplitOn<Email>("Id") | ||
.SplitOn<Phone>("Id") | ||
.Where("person.Id = @id", new { id = personId }); | ||
|
||
person = query | ||
.Execute(fixture.DbConnection, personMapper) | ||
.FirstOrDefault(); | ||
person = query | ||
.Execute(fixture.GetDbConnection(), personMapper) | ||
.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("[email protected]", person.Emails[0].Address); | ||
Assert.Equal(personId, person.Emails[0].PersonId); | ||
Assert.Equal(1, person.Phones.Count); | ||
Assert.Equal("8011115555", person.Phones[0].Number); | ||
Assert.Equal(personId, person.Phones[0].PersonId); | ||
try | ||
{ | ||
// 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("[email protected]", person.Emails[0].Address); | ||
Assert.Equal(personId, person.Emails[0].PersonId); | ||
Assert.Equal(1, person.Phones.Count); | ||
Assert.Equal("8011115555", person.Phones[0].Number); | ||
Assert.Equal(personId, person.Phones[0].PersonId); | ||
} | ||
finally | ||
{ | ||
// Delete the items we inserted so it doesn't skew other tests | ||
using (var db = fixture.GetDbConnection()) | ||
{ | ||
SqlBuilder.Delete<Email>(new { Id = person.Emails[0].Id }).Execute(db); | ||
SqlBuilder.Delete<Phone>(new { Id = person.Phones[0].Id }).Execute(db); | ||
SqlBuilder.Delete<Person>(new { Id = person.Id }).Execute(db); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
INSERT INTO Person (Id, FirstName, LastName, SupervisorId, CareerCounselorId) VALUES (1, 'Hyrum', 'Clyde', NULL, NULL); | ||
SET IDENTITY_INSERT Person ON | ||
INSERT INTO Person (Id, FirstName, LastName, SupervisorId, CareerCounselorId) VALUES (1, 'Hyrum', 'Clyde', NULL, NULL); | ||
INSERT INTO Person (Id, FirstName, LastName, SupervisorId, CareerCounselorId) VALUES (2, 'Doug', 'Day', NULL, 1); | ||
INSERT INTO Person (Id, FirstName, LastName, SupervisorId, CareerCounselorId) VALUES (3, 'Kevin', 'Russon', 1, 2); | ||
SET IDENTITY_INSERT Person OFF | ||
|
||
-- Add the relationship once all records are inserted | ||
--UPDATE Person | ||
--SET CareerCounselorId = 2 | ||
--WHERE Id = 1; | ||
|
||
SET IDENTITY_INSERT Email ON | ||
INSERT INTO Email (Id, [Address], PersonId) VALUES (1, '[email protected]', 1); | ||
INSERT INTO Email (Id, [Address], PersonId) VALUES (2, '[email protected]', 2); | ||
INSERT INTO Email (Id, [Address], PersonId) VALUES (3, '[email protected]', 2); | ||
INSERT INTO Email (Id, [Address], PersonId) VALUES (4, '[email protected]', 3); | ||
SET IDENTITY_INSERT Email OFF | ||
|
||
SET IDENTITY_INSERT Phone ON | ||
INSERT INTO Phone (Id, Number, [Type], PersonId) VALUES (1, '8011234567', 3, 2); | ||
INSERT INTO Phone (Id, Number, [Type], PersonId) VALUES (2, '8019876543', 3, 3); | ||
INSERT INTO Phone (Id, Number, [Type], PersonId) VALUES (3, '8011111111', 1, 3); | ||
SET IDENTITY_INSERT Phone OFF |
Oops, something went wrong.