Skip to content

Commit 0366d02

Browse files
committed
test(*): fix tests broken after aspnet and ef core 2.0 upgrades
1 parent 4ba303c commit 0366d02

File tree

10 files changed

+61
-85
lines changed

10 files changed

+61
-85
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ public async Task Can_Create_And_Set_HasMany_Relationships()
272272
var body = await response.Content.ReadAsStringAsync();
273273
var deserializedBody = (TodoItemCollection)_fixture.GetService<IJsonApiDeSerializer>().Deserialize(body);
274274
var newId = deserializedBody.Id;
275+
276+
context = _fixture.GetService<AppDbContext>();
275277
var contextCollection = context.TodoItemCollections
276278
.Include(c => c.Owner)
277279
.Include(c => c.TodoItems)

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/Included.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Net;
34
using System.Net.Http;
45
using System.Threading.Tasks;
@@ -45,8 +46,14 @@ public Included(TestFixture<Startup> fixture)
4546
public async Task GET_Included_Contains_SideloadedData_ForManyToOne()
4647
{
4748
// arrange
48-
var builder = new WebHostBuilder()
49-
.UseStartup<Startup>();
49+
var person = _personFaker.Generate();
50+
var todoItem = _todoItemFaker.Generate();
51+
todoItem.Owner = person;
52+
_context.TodoItems.RemoveRange(_context.TodoItems);
53+
_context.TodoItems.Add(todoItem);
54+
_context.SaveChanges();
55+
56+
var builder = new WebHostBuilder().UseStartup<Startup>();
5057

5158
var httpMethod = new HttpMethod("GET");
5259
var route = $"/api/v1/todo-items?include=owner";
@@ -57,13 +64,16 @@ public async Task GET_Included_Contains_SideloadedData_ForManyToOne()
5764

5865
// act
5966
var response = await client.SendAsync(request);
60-
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
61-
var data = documents.Data[0];
6267

6368
// assert
69+
var json = await response.Content.ReadAsStringAsync();
70+
var documents = JsonConvert.DeserializeObject<Documents>(json);
71+
// we only care about counting the todo-items that have owners
72+
var expectedCount = documents.Data.Count(d => d.Relationships["owner"].SingleData != null);
73+
6474
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
6575
Assert.NotEmpty(documents.Included);
66-
Assert.Equal(documents.Data.Count, documents.Included.Count);
76+
Assert.Equal(expectedCount, documents.Included.Count);
6777
}
6878

6979
[Fact]

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingRelationshipsTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public async Task Request_ForRelationshipLink_ThatDoesNotExist_Returns_404()
6666
{
6767
// arrange
6868
var context = _fixture.GetService<AppDbContext>();
69-
var todoItem = context.TodoItems.First();
69+
70+
var todoItem = _todoItemFaker.Generate();
71+
context.TodoItems.Add(todoItem);
72+
await context.SaveChangesAsync();
73+
7074
var todoItemId = todoItem.Id;
7175
context.TodoItems.Remove(todoItem);
7276
await context.SaveChangesAsync();

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/PagingTests.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
1-
using System.Collections.Generic;
2-
using System;
31
using System.Linq;
42
using System.Net;
5-
using System.Net.Http;
63
using System.Threading.Tasks;
74
using Bogus;
8-
using DotNetCoreDocs;
9-
using DotNetCoreDocs.Models;
10-
using DotNetCoreDocs.Writers;
115
using JsonApiDotNetCore.Serialization;
12-
using JsonApiDotNetCore.Services;
136
using JsonApiDotNetCoreExample;
14-
using JsonApiDotNetCoreExample.Data;
157
using JsonApiDotNetCoreExample.Models;
168
using Xunit;
179
using Person = JsonApiDotNetCoreExample.Models.Person;
1810

19-
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
20-
{
21-
public class PagingTests : TestFixture<Startup>
22-
{
11+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec {
12+
public class PagingTests : TestFixture<Startup> {
2313
private readonly Faker<TodoItem> _todoItemFaker = new Faker<TodoItem>()
24-
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
25-
.RuleFor(t => t.Ordinal, f => f.Random.Number())
26-
.RuleFor(t => t.CreatedDate, f => f.Date.Past());
14+
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
15+
.RuleFor(t => t.Ordinal, f => f.Random.Number())
16+
.RuleFor(t => t.CreatedDate, f => f.Date.Past());
2717

2818
[Fact]
29-
public async Task Can_Paginate_TodoItems()
30-
{
19+
public async Task Can_Paginate_TodoItems() {
3120
// Arrange
3221
const int expectedEntitiesPerPage = 2;
3322
var totalCount = expectedEntitiesPerPage * 2;
@@ -56,8 +45,7 @@ public async Task Can_Paginate_TodoItems()
5645
}
5746

5847
[Fact]
59-
public async Task Can_Paginate_TodoItems_From_Start()
60-
{
48+
public async Task Can_Paginate_TodoItems_From_Start() {
6149
// Arrange
6250
const int expectedEntitiesPerPage = 2;
6351
var totalCount = expectedEntitiesPerPage * 2;
@@ -91,8 +79,7 @@ public async Task Can_Paginate_TodoItems_From_Start()
9179
}
9280

9381
[Fact]
94-
public async Task Can_Paginate_TodoItems_From_End()
95-
{
82+
public async Task Can_Paginate_TodoItems_From_End() {
9683
// Arrange
9784
const int expectedEntitiesPerPage = 2;
9885
var totalCount = expectedEntitiesPerPage * 2;

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingRelationshipsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public async Task Can_Update_ToMany_Relationship_ThroughLink()
7777

7878
// Act
7979
var response = await client.SendAsync(request);
80+
_context = _fixture.GetService<AppDbContext>();
8081
var personsTodoItems = _context.People.Include(p => p.TodoItems).Single(p => p.Id == person.Id).TodoItems;
8182

8283
// Assert

test/JsonApiDotNetCoreExampleTests/Acceptance/TestFixture.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
using Microsoft.AspNetCore.Hosting;
66
using Microsoft.AspNetCore.TestHost;
77
using JsonApiDotNetCore.Services;
8-
using Newtonsoft.Json;
98

109
namespace JsonApiDotNetCoreExampleTests.Acceptance
1110
{
12-
public class TestFixture<TStartup> where TStartup : class
11+
public class TestFixture<TStartup> : IDisposable where TStartup : class
1312
{
1413
private readonly TestServer _server;
1514
private IServiceProvider _services;
@@ -33,5 +32,25 @@ public TestFixture()
3332
public IJsonApiDeSerializer DeSerializer { get; private set; }
3433
public IJsonApiContext JsonApiContext { get; private set; }
3534
public T GetService<T>() => (T)_services.GetService(typeof(T));
35+
36+
private bool disposedValue = false;
37+
protected virtual void Dispose(bool disposing)
38+
{
39+
if (!disposedValue)
40+
{
41+
if (disposing)
42+
{
43+
Client.Dispose();
44+
_server.Dispose();
45+
}
46+
47+
disposedValue = true;
48+
}
49+
}
50+
51+
public void Dispose()
52+
{
53+
Dispose(true);
54+
}
3655
}
3756
}

test/JsonApiDotNetCoreExampleTests/Helpers/Extensions/IQueryableExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
using Microsoft.EntityFrameworkCore.Internal;
55
using Microsoft.EntityFrameworkCore.Query;
66
using Microsoft.EntityFrameworkCore.Query.Internal;
7+
using Microsoft.EntityFrameworkCore.Storage;
78
using Remotion.Linq.Parsing.Structure;
89
using Database = Microsoft.EntityFrameworkCore.Storage.Database;
910

1011
namespace JsonApiDotNetCoreExampleTests.Helpers.Extensions
1112
{
12-
1313
public static class IQueryableExtensions
1414
{
1515
private static readonly TypeInfo QueryCompilerTypeInfo = typeof(QueryCompiler).GetTypeInfo();
@@ -22,19 +22,22 @@ public static class IQueryableExtensions
2222

2323
private static readonly FieldInfo DataBaseField = QueryCompilerTypeInfo.DeclaredFields.Single(x => x.Name == "_database");
2424

25-
private static readonly FieldInfo QueryCompilationContextFactoryField = typeof(Database).GetTypeInfo().DeclaredFields.Single(x => x.Name == "_queryCompilationContextFactory");
25+
private static readonly PropertyInfo DatabaseDependenciesField
26+
= typeof(Database).GetTypeInfo().DeclaredProperties.Single(x => x.Name == "Dependencies");
2627

2728
public static string ToSql<TEntity>(this IQueryable<TEntity> query) where TEntity : class
2829
{
2930
if (!(query is EntityQueryable<TEntity>) && !(query is InternalDbSet<TEntity>))
31+
{
3032
throw new ArgumentException("Invalid query");
33+
}
3134

3235
var queryCompiler = (IQueryCompiler)QueryCompilerField.GetValue(query.Provider);
3336
var nodeTypeProvider = (INodeTypeProvider)NodeTypeProviderField.GetValue(queryCompiler);
3437
var parser = (IQueryParser)CreateQueryParserMethod.Invoke(queryCompiler, new object[] { nodeTypeProvider });
3538
var queryModel = parser.GetParsedQuery(query.Expression);
3639
var database = DataBaseField.GetValue(queryCompiler);
37-
var queryCompilationContextFactory = (IQueryCompilationContextFactory)QueryCompilationContextFactoryField.GetValue(database);
40+
var queryCompilationContextFactory = ((DatabaseDependencies)DatabaseDependenciesField.GetValue(database)).QueryCompilationContextFactory;
3841
var queryCompilationContext = queryCompilationContextFactory.Create(false);
3942
var modelVisitor = (RelationalQueryModelVisitor)queryCompilationContext.CreateQueryModelVisitor();
4043
modelVisitor.CreateQueryExecutor<TEntity>(queryModel);

test/JsonApiDotNetCoreExampleTests/TestFixture.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

test/JsonApiDotNetCoreExampleTests/WebHostCollection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using JsonApiDotNetCoreExample;
2+
using JsonApiDotNetCoreExampleTests.Acceptance;
23
using Xunit;
34

45
namespace JsonApiDotNetCoreExampleTests

test/JsonApiDotNetCoreExampleTests/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"Logging": {
77
"IncludeScopes": false,
88
"LogLevel": {
9-
"Default": "Debug",
9+
"Default": "Error",
1010
"System": "Information",
1111
"Microsoft": "Information"
1212
}

0 commit comments

Comments
 (0)