Skip to content

Commit 2a68ce7

Browse files
author
Bart Koelman
authored
Added test for accessing many-to-many data through id, for example: /articles/1/tags (#682)
1 parent 33cf4a6 commit 2a68ce7

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using JsonApiDotNetCoreExample.Models;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace JsonApiDotNetCoreExample.Controllers
8+
{
9+
public class TagsController : JsonApiController<Tag>
10+
{
11+
public TagsController(
12+
IJsonApiOptions jsonApiOptions,
13+
IResourceService<Tag> resourceService,
14+
ILoggerFactory loggerFactory)
15+
: base(jsonApiOptions, resourceService, loggerFactory)
16+
{ }
17+
}
18+
}

test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,45 @@ public async Task Can_Fetch_Many_To_Many_Through_GetById()
122122
Assert.Equal(tag.Name, tagResponse.Name);
123123
}
124124

125+
[Fact]
126+
public async Task Can_Fetch_Many_To_Many_Through_Id()
127+
{
128+
// Arrange
129+
var context = _fixture.GetService<AppDbContext>();
130+
var article = _articleFaker.Generate();
131+
var tag = _tagFaker.Generate();
132+
var articleTag = new ArticleTag
133+
{
134+
Article = article,
135+
Tag = tag
136+
};
137+
context.ArticleTags.Add(articleTag);
138+
await context.SaveChangesAsync();
139+
140+
var route = $"/api/v1/articles/{article.Id}/tags";
141+
142+
// @TODO - Use fixture
143+
var builder = new WebHostBuilder()
144+
.UseStartup<Startup>();
145+
var server = new TestServer(builder);
146+
var client = server.CreateClient();
147+
148+
// Act
149+
var response = await client.GetAsync(route);
150+
151+
// Assert
152+
var body = await response.Content.ReadAsStringAsync();
153+
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");
154+
155+
var document = JsonConvert.DeserializeObject<Document>(body);
156+
Assert.Single(document.ManyData);
157+
158+
var tagResponse = _fixture.GetDeserializer().DeserializeList<Tag>(body).Data.First();
159+
Assert.NotNull(tagResponse);
160+
Assert.Equal(tag.Id, tagResponse.Id);
161+
Assert.Equal(tag.Name, tagResponse.Name);
162+
}
163+
125164
[Fact]
126165
public async Task Can_Fetch_Many_To_Many_Through_GetById_Relationship_Link()
127166
{
@@ -160,7 +199,6 @@ public async Task Can_Fetch_Many_To_Many_Through_GetById_Relationship_Link()
160199
Assert.Equal(tag.Id, tagResponse.Id);
161200
}
162201

163-
164202
[Fact]
165203
public async Task Can_Fetch_Many_To_Many_Through_Relationship_Link()
166204
{

0 commit comments

Comments
 (0)