Skip to content

Commit

Permalink
How to handle two objs of the same type in the EntityMapper #2
Browse files Browse the repository at this point in the history
  • Loading branch information
dougrday committed Mar 16, 2018
1 parent b58a494 commit 6340df6
Show file tree
Hide file tree
Showing 16 changed files with 379 additions and 129 deletions.
12 changes: 9 additions & 3 deletions Dapper.GraphQL.Test/Dapper.GraphQL.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
<TargetFramework>netcoreapp1.1</TargetFramework>

<IsPackable>false</IsPackable>

<AssemblyVersion>0.2.0.0</AssemblyVersion>

<FileVersion>0.2.0.0</FileVersion>

<Version>0.2.0</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -21,10 +27,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="dbup-core" Version="4.0.0-beta0002" />
<PackageReference Include="dbup-sqlite" Version="4.0.0-beta0002" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="1.1.1" />
<PackageReference Include="dbup-core" Version="4.0.0-beta0004" />
<PackageReference Include="dbup-sqlserver" Version="4.0.0-beta0004" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.3" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.analyzers" Version="0.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
Expand Down
2 changes: 1 addition & 1 deletion Dapper.GraphQL.Test/EntityMappers/PersonEntityMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override Person Map(

// NOTE: order matters here, if both supervisor
// and careerCounselor exist, then supervisor must appear
// first in the list.
// first in the list. This order is guaranteed in PersonQueryBuilder.
if (fields.ContainsKey("supervisor"))
{
supervisorIndex = splitOn.IndexOf(typeof(Person), startingIndex);
Expand Down
85 changes: 81 additions & 4 deletions Dapper.GraphQL.Test/GraphQLTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(@"
Expand Down Expand Up @@ -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));
Expand Down
95 changes: 57 additions & 38 deletions Dapper.GraphQL.Test/InsertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
}
}
}
}
17 changes: 9 additions & 8 deletions Dapper.GraphQL.Test/QueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
using Dapper.GraphQL.Test.Models;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Xunit;

namespace Dapper.GraphQL.Test
{
public class QueryTests : IClassFixture<TestFixture>
{
private readonly TestFixture fixture;
private readonly TestFixture fixture;

public QueryTests(TestFixture fixture)
{
this.fixture = fixture;
this.fixture = fixture;
}

[Fact(DisplayName = "ORDER BY should work")]
public void OrderByShouldWork()
{
var query = SqlBuilder
.From("Person person")
.Select("person.Id", "notAnAlias.Id")
.Select("person.Id")
.SplitOn<Person>("Id")
.OrderBy("LastName");

Expand All @@ -31,7 +29,7 @@ public void OrderByShouldWork()
[Fact(DisplayName = "SELECT without matching alias should throw")]
public void SelectWithoutMatchingAliasShouldThrow()
{
Assert.ThrowsAsync<SqliteException>(async () =>
Assert.Throws<SqlException>(() =>
{
var query = SqlBuilder
.From("Person person")
Expand All @@ -44,7 +42,10 @@ public void SelectWithoutMatchingAliasShouldThrow()
.GetRequiredService<IEntityMapperFactory>()
.Build<Person>(person => person.Id);

query.Execute<Person>(fixture.DbConnection, personMapper);
using (var db = fixture.GetDbConnection())
{
query.Execute<Person>(db, personMapper);
}
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions Dapper.GraphQL.Test/Sql/1-Create.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE Person (
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
FirstName TEXT,
LastName TEXT,
Id INTEGER NOT NULL PRIMARY KEY IDENTITY(1, 1),
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
-- Known issue with FK reference to non-null numeric types
-- https://github.com/StackExchange/Dapper/issues/917
SupervisorId INTEGER,
Expand All @@ -11,15 +11,15 @@
);

CREATE TABLE Email (
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[Address] TEXT,
Id INTEGER NOT NULL PRIMARY KEY IDENTITY(1, 1),
[Address] NVARCHAR(250),
PersonId INTEGER,
FOREIGN KEY(PersonId) REFERENCES Person(Id)
);

CREATE TABLE Phone (
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
Number TEXT,
Id INTEGER NOT NULL PRIMARY KEY IDENTITY(1, 1),
Number NVARCHAR(16),
[Type] INTEGER,
PersonId INTEGER,
FOREIGN KEY(PersonId) REFERENCES Person(Id)
Expand Down
8 changes: 7 additions & 1 deletion Dapper.GraphQL.Test/Sql/2-Data.sql
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
Loading

0 comments on commit 6340df6

Please sign in to comment.