Skip to content

Commit 5680190

Browse files
author
Bart Koelman
committed
Fixed: duplicate tests
1 parent 3697967 commit 5680190

File tree

1 file changed

+35
-26
lines changed
  • test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests

1 file changed

+35
-26
lines changed

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

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Linq;
1212
using Bogus;
1313
using JsonApiDotNetCoreExample.Models;
14+
using Person = JsonApiDotNetCoreExample.Models.Person;
1415

1516
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec.DocumentTests
1617
{
@@ -19,6 +20,7 @@ public sealed class Relationships
1920
{
2021
private readonly AppDbContext _context;
2122
private readonly Faker<TodoItem> _todoItemFaker;
23+
private readonly Faker<Person> _personFaker;
2224

2325
public Relationships(TestFixture<Startup> fixture)
2426
{
@@ -27,32 +29,36 @@ public Relationships(TestFixture<Startup> fixture)
2729
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
2830
.RuleFor(t => t.Ordinal, f => f.Random.Number())
2931
.RuleFor(t => t.CreatedDate, f => f.Date.Past());
32+
_personFaker = new Faker<Person>()
33+
.RuleFor(p => p.FirstName, f => f.Name.FirstName())
34+
.RuleFor(p => p.LastName, f => f.Name.LastName());
3035
}
3136

3237
[Fact]
3338
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
3439
{
3540
// Arrange
36-
var builder = new WebHostBuilder()
37-
.UseStartup<Startup>();
38-
41+
_context.TodoItems.RemoveRange(_context.TodoItems);
42+
await _context.SaveChangesAsync();
43+
3944
var todoItem = _todoItemFaker.Generate();
4045
_context.TodoItems.Add(todoItem);
4146
await _context.SaveChangesAsync();
4247

4348
var httpMethod = new HttpMethod("GET");
44-
var route = $"/api/v1/todoItems/{todoItem.Id}";
49+
var route = "/api/v1/todoItems";
4550

51+
var builder = new WebHostBuilder().UseStartup<Startup>();
4652
var server = new TestServer(builder);
4753
var client = server.CreateClient();
4854
var request = new HttpRequestMessage(httpMethod, route);
4955

5056
// Act
5157
var response = await client.SendAsync(request);
52-
var document = JsonConvert.DeserializeObject<Document>(await response.Content.ReadAsStringAsync());
53-
var data = document.SingleData;
54-
var expectedOwnerSelfLink = $"http://localhost/api/v1/todoItems/{data.Id}/relationships/owner";
55-
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todoItems/{data.Id}/owner";
58+
var responseString = await response.Content.ReadAsStringAsync();
59+
var data = JsonConvert.DeserializeObject<Document>(responseString).ManyData[0];
60+
var expectedOwnerSelfLink = $"http://localhost/api/v1/todoItems/{todoItem.Id}/relationships/owner";
61+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todoItems/{todoItem.Id}/owner";
5662

5763
// Assert
5864
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -64,16 +70,14 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
6470
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById()
6571
{
6672
// Arrange
67-
var builder = new WebHostBuilder()
68-
.UseStartup<Startup>();
69-
7073
var todoItem = _todoItemFaker.Generate();
7174
_context.TodoItems.Add(todoItem);
7275
await _context.SaveChangesAsync();
7376

7477
var httpMethod = new HttpMethod("GET");
7578
var route = $"/api/v1/todoItems/{todoItem.Id}";
7679

80+
var builder = new WebHostBuilder().UseStartup<Startup>();
7781
var server = new TestServer(builder);
7882
var client = server.CreateClient();
7983
var request = new HttpRequestMessage(httpMethod, route);
@@ -87,30 +91,35 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById()
8791

8892
// Assert
8993
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
90-
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links?.Self);
94+
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links.Self);
9195
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related);
9296
}
9397

9498
[Fact]
9599
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships()
96100
{
97101
// Arrange
98-
var builder = new WebHostBuilder()
99-
.UseStartup<Startup>();
102+
_context.People.RemoveRange(_context.People);
103+
await _context.SaveChangesAsync();
104+
105+
var person = _personFaker.Generate();
106+
_context.People.Add(person);
107+
await _context.SaveChangesAsync();
100108

101109
var httpMethod = new HttpMethod("GET");
102110
var route = "/api/v1/people";
103111

112+
var builder = new WebHostBuilder().UseStartup<Startup>();
104113
var server = new TestServer(builder);
105114
var client = server.CreateClient();
106115
var request = new HttpRequestMessage(httpMethod, route);
107116

108117
// Act
109118
var response = await client.SendAsync(request);
110-
var documents = JsonConvert.DeserializeObject<Document>(await response.Content.ReadAsStringAsync());
111-
var data = documents.ManyData.First();
112-
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{data.Id}/relationships/todoItems";
113-
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{data.Id}/todoItems";
119+
var responseString = await response.Content.ReadAsStringAsync();
120+
var data = JsonConvert.DeserializeObject<Document>(responseString).ManyData[0];
121+
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{person.Id}/relationships/todoItems";
122+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{person.Id}/todoItems";
114123

115124
// Assert
116125
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -122,14 +131,14 @@ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships()
122131
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById()
123132
{
124133
// Arrange
125-
var personId = _context.People.AsEnumerable().Last().Id;
126-
127-
var builder = new WebHostBuilder()
128-
.UseStartup<Startup>();
134+
var person = _personFaker.Generate();
135+
_context.People.Add(person);
136+
await _context.SaveChangesAsync();
129137

130138
var httpMethod = new HttpMethod("GET");
131-
var route = $"/api/v1/people/{personId}";
139+
var route = $"/api/v1/people/{person.Id}";
132140

141+
var builder = new WebHostBuilder().UseStartup<Startup>();
133142
var server = new TestServer(builder);
134143
var client = server.CreateClient();
135144
var request = new HttpRequestMessage(httpMethod, route);
@@ -138,12 +147,12 @@ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById()
138147
var response = await client.SendAsync(request);
139148
var responseString = await response.Content.ReadAsStringAsync();
140149
var data = JsonConvert.DeserializeObject<Document>(responseString).SingleData;
141-
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{personId}/relationships/todoItems";
142-
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{personId}/todoItems";
150+
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{person.Id}/relationships/todoItems";
151+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{person.Id}/todoItems";
143152

144153
// Assert
145154
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
146-
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todoItems"].Links?.Self);
155+
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todoItems"].Links.Self);
147156
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todoItems"].Links.Related);
148157
}
149158
}

0 commit comments

Comments
 (0)