@@ -24,25 +24,66 @@ public class ManyToManyTests
2424 private static readonly Faker < Tag > _tagFaker = new Faker < Tag > ( ) . RuleFor ( a => a . Name , f => f . Random . AlphaNumeric ( 10 ) ) ;
2525
2626 private TestFixture < TestStartup > _fixture ;
27- public ManyToManyTests ( TestFixture < TestStartup > fixture )
27+ public ManyToManyTests ( TestFixture < TestStartup > fixture )
2828 {
2929 _fixture = fixture ;
3030 }
3131
3232 [ Fact ]
33- public async Task Can_Fetch_Many_To_Many_Through ( )
33+ public async Task Can_Fetch_Many_To_Many_Through_All ( )
3434 {
3535 // arrange
3636 var context = _fixture . GetService < AppDbContext > ( ) ;
3737 var article = _articleFaker . Generate ( ) ;
3838 var tag = _tagFaker . Generate ( ) ;
39- var articleTag = new ArticleTag {
39+ var articleTag = new ArticleTag
40+ {
4041 Article = article ,
4142 Tag = tag
4243 } ;
4344 context . ArticleTags . Add ( articleTag ) ;
4445 await context . SaveChangesAsync ( ) ;
45-
46+
47+ var route = $ "/api/v1/articles?include=tags";
48+
49+
50+
51+ // act
52+ var response = await _fixture . Client . GetAsync ( route ) ;
53+
54+ // assert
55+ var body = await response . Content . ReadAsStringAsync ( ) ;
56+ Assert . True ( HttpStatusCode . OK == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
57+
58+ Assert . True ( body . Contains ( "include" ) ) ;
59+
60+ var articleResponseList = _fixture . GetService < IJsonApiDeSerializer > ( ) . DeserializeList < Article > ( body ) ;
61+ Assert . NotNull ( articleResponseList ) ;
62+ var articleResponse = articleResponseList . FirstOrDefault ( a => a . Id == article . Id ) ;
63+ Assert . NotNull ( articleResponse ) ;
64+ Assert . Equal ( article . Name , articleResponse . Name ) ;
65+
66+ var tagResponse = Assert . Single ( articleResponse . Tags ) ;
67+ Assert . Equal ( tag . Id , tagResponse . Id ) ;
68+ Assert . Equal ( tag . Name , tagResponse . Name ) ;
69+
70+ }
71+
72+ [ Fact ]
73+ public async Task Can_Fetch_Many_To_Many_Through_GetById ( )
74+ {
75+ // arrange
76+ var context = _fixture . GetService < AppDbContext > ( ) ;
77+ var article = _articleFaker . Generate ( ) ;
78+ var tag = _tagFaker . Generate ( ) ;
79+ var articleTag = new ArticleTag
80+ {
81+ Article = article ,
82+ Tag = tag
83+ } ;
84+ context . ArticleTags . Add ( articleTag ) ;
85+ await context . SaveChangesAsync ( ) ;
86+
4687 var route = $ "/api/v1/articles/{ article . Id } ?include=tags";
4788
4889 // act
@@ -51,13 +92,16 @@ public async Task Can_Fetch_Many_To_Many_Through()
5192 // assert
5293 var body = await response . Content . ReadAsStringAsync ( ) ;
5394 Assert . True ( HttpStatusCode . OK == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
54-
95+ Assert . True ( body . Contains ( "include" ) ) ;
96+
5597 var articleResponse = _fixture . GetService < IJsonApiDeSerializer > ( ) . Deserialize < Article > ( body ) ;
5698 Assert . NotNull ( articleResponse ) ;
5799 Assert . Equal ( article . Id , articleResponse . Id ) ;
58-
100+
59101 var tagResponse = Assert . Single ( articleResponse . Tags ) ;
60102 Assert . Equal ( tag . Id , tagResponse . Id ) ;
103+ Assert . Equal ( tag . Name , tagResponse . Name ) ;
104+
61105 }
62106
63107 [ Fact ]
0 commit comments