1+ using System ;
12using System . Collections . Generic ;
23using System . Linq ;
34using System . Net ;
45using System . Net . Http ;
56using System . Net . Http . Headers ;
67using System . Threading . Tasks ;
78using Bogus ;
9+ using JsonApiDotNetCore . Models ;
810using JsonApiDotNetCore . Serialization ;
911using JsonApiDotNetCoreExample . Data ;
1012using JsonApiDotNetCoreExample . Models ;
@@ -24,25 +26,69 @@ public class ManyToManyTests
2426 private static readonly Faker < Tag > _tagFaker = new Faker < Tag > ( ) . RuleFor ( a => a . Name , f => f . Random . AlphaNumeric ( 10 ) ) ;
2527
2628 private TestFixture < TestStartup > _fixture ;
27- public ManyToManyTests ( TestFixture < TestStartup > fixture )
29+ public ManyToManyTests ( TestFixture < TestStartup > fixture )
2830 {
2931 _fixture = fixture ;
3032 }
3133
3234 [ Fact ]
33- public async Task Can_Fetch_Many_To_Many_Through ( )
35+ public async Task Can_Fetch_Many_To_Many_Through_All ( )
3436 {
3537 // arrange
3638 var context = _fixture . GetService < AppDbContext > ( ) ;
3739 var article = _articleFaker . Generate ( ) ;
3840 var tag = _tagFaker . Generate ( ) ;
39- var articleTag = new ArticleTag {
41+
42+ context . Articles . RemoveRange ( context . Articles ) ;
43+ await context . SaveChangesAsync ( ) ;
44+
45+ var articleTag = new ArticleTag
46+ {
4047 Article = article ,
4148 Tag = tag
4249 } ;
4350 context . ArticleTags . Add ( articleTag ) ;
4451 await context . SaveChangesAsync ( ) ;
52+
53+ var route = $ "/api/v1/articles?include=tags";
54+
55+ // act
56+ var response = await _fixture . Client . GetAsync ( route ) ;
57+
58+ // assert
59+ var body = await response . Content . ReadAsStringAsync ( ) ;
60+ Assert . True ( HttpStatusCode . OK == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
61+
62+ var document = JsonConvert . DeserializeObject < Documents > ( body ) ;
63+ Assert . NotEmpty ( document . Included ) ;
64+
65+ var articleResponseList = _fixture . GetService < IJsonApiDeSerializer > ( ) . DeserializeList < Article > ( body ) ;
66+ Assert . NotNull ( articleResponseList ) ;
4567
68+ var articleResponse = articleResponseList . FirstOrDefault ( a => a . Id == article . Id ) ;
69+ Assert . NotNull ( articleResponse ) ;
70+ Assert . Equal ( article . Name , articleResponse . Name ) ;
71+
72+ var tagResponse = Assert . Single ( articleResponse . Tags ) ;
73+ Assert . Equal ( tag . Id , tagResponse . Id ) ;
74+ Assert . Equal ( tag . Name , tagResponse . Name ) ;
75+ }
76+
77+ [ Fact ]
78+ public async Task Can_Fetch_Many_To_Many_Through_GetById ( )
79+ {
80+ // arrange
81+ var context = _fixture . GetService < AppDbContext > ( ) ;
82+ var article = _articleFaker . Generate ( ) ;
83+ var tag = _tagFaker . Generate ( ) ;
84+ var articleTag = new ArticleTag
85+ {
86+ Article = article ,
87+ Tag = tag
88+ } ;
89+ context . ArticleTags . Add ( articleTag ) ;
90+ await context . SaveChangesAsync ( ) ;
91+
4692 var route = $ "/api/v1/articles/{ article . Id } ?include=tags";
4793
4894 // act
@@ -52,12 +98,16 @@ public async Task Can_Fetch_Many_To_Many_Through()
5298 var body = await response . Content . ReadAsStringAsync ( ) ;
5399 Assert . True ( HttpStatusCode . OK == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
54100
101+ var document = JsonConvert . DeserializeObject < Document > ( body ) ;
102+ Assert . NotEmpty ( document . Included ) ;
103+
55104 var articleResponse = _fixture . GetService < IJsonApiDeSerializer > ( ) . Deserialize < Article > ( body ) ;
56105 Assert . NotNull ( articleResponse ) ;
57106 Assert . Equal ( article . Id , articleResponse . Id ) ;
58-
107+
59108 var tagResponse = Assert . Single ( articleResponse . Tags ) ;
60109 Assert . Equal ( tag . Id , tagResponse . Id ) ;
110+ Assert . Equal ( tag . Name , tagResponse . Name ) ;
61111 }
62112
63113 [ Fact ]
0 commit comments