Skip to content

Commit 9089bbd

Browse files
committed
test(acceptance): failing test for related attr filter
1 parent c4b3361 commit 9089bbd

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/JsonApiDotNetCoreExample/Models/Person.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using JsonApiDotNetCore.Internal;
32
using JsonApiDotNetCore.Models;
43
using JsonApiDotNetCore.Services;
54

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/AttributeFilterTests.cs

+41-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
using System.Net.Http;
33
using System.Threading.Tasks;
44
using DotNetCoreDocs;
5-
using DotNetCoreDocs.Models;
65
using DotNetCoreDocs.Writers;
76
using JsonApiDotNetCoreExample;
87
using Microsoft.AspNetCore.Hosting;
98
using Microsoft.AspNetCore.TestHost;
10-
using Newtonsoft.Json;
119
using Xunit;
12-
using JsonApiDotNetCore.Internal;
1310
using JsonApiDotNetCoreExample.Data;
1411
using Bogus;
1512
using JsonApiDotNetCoreExample.Models;
1613
using JsonApiDotNetCore.Serialization;
1714
using System.Linq;
15+
using Person = JsonApiDotNetCoreExample.Models.Person;
1816

1917
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
2018
{
@@ -23,13 +21,18 @@ public class AttributeFilterTests
2321
{
2422
private DocsFixture<Startup, JsonDocWriter> _fixture;
2523
private Faker<TodoItem> _todoItemFaker;
26-
24+
private readonly Faker<Person> _personFaker;
25+
2726
public AttributeFilterTests(DocsFixture<Startup, JsonDocWriter> fixture)
2827
{
2928
_fixture = fixture;
3029
_todoItemFaker = new Faker<TodoItem>()
3130
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
3231
.RuleFor(t => t.Ordinal, f => f.Random.Number());
32+
33+
_personFaker = new Faker<Person>()
34+
.RuleFor(p => p.FirstName, f => f.Name.FirstName())
35+
.RuleFor(p => p.LastName, f => f.Name.LastName());
3336
}
3437

3538
[Fact]
@@ -63,5 +66,39 @@ public async Task Can_Filter_On_Guid_Properties()
6366
Assert.Equal(todoItem.Id, todoItemResponse.Id);
6467
Assert.Equal(todoItem.GuidProperty, todoItemResponse.GuidProperty);
6568
}
69+
70+
71+
[Fact]
72+
public async Task Can_Filter_On_Related_Attrs()
73+
{
74+
// arrange
75+
var context = _fixture.GetService<AppDbContext>();
76+
var person = _personFaker.Generate();
77+
var todoItem = _todoItemFaker.Generate();
78+
todoItem.Owner = person;
79+
context.TodoItems.Add(todoItem);
80+
await context.SaveChangesAsync();
81+
82+
var builder = new WebHostBuilder()
83+
.UseStartup<Startup>();
84+
var httpMethod = new HttpMethod("GET");
85+
var route = $"/api/v1/todo-items?include[owner]&filter[owner.first-name]={person.FirstName}";
86+
var server = new TestServer(builder);
87+
var client = server.CreateClient();
88+
var request = new HttpRequestMessage(httpMethod, route);
89+
90+
// act
91+
var response = await client.SendAsync(request);
92+
var body = await response.Content.ReadAsStringAsync();
93+
var deserializedBody = _fixture
94+
.GetService<IJsonApiDeSerializer>()
95+
.DeserializeList<TodoItem>(body);
96+
97+
// assert
98+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
99+
Assert.NotEmpty(deserializedBody);
100+
foreach(var item in deserializedBody)
101+
Assert.Equal(person.FirstName, item.Owner.FirstName);
102+
}
66103
}
67104
}

0 commit comments

Comments
 (0)