@@ -24,25 +24,66 @@ public class ManyToManyTests
24
24
private static readonly Faker < Tag > _tagFaker = new Faker < Tag > ( ) . RuleFor ( a => a . Name , f => f . Random . AlphaNumeric ( 10 ) ) ;
25
25
26
26
private TestFixture < TestStartup > _fixture ;
27
- public ManyToManyTests ( TestFixture < TestStartup > fixture )
27
+ public ManyToManyTests ( TestFixture < TestStartup > fixture )
28
28
{
29
29
_fixture = fixture ;
30
30
}
31
31
32
32
[ Fact ]
33
- public async Task Can_Fetch_Many_To_Many_Through ( )
33
+ public async Task Can_Fetch_Many_To_Many_Through_All ( )
34
34
{
35
35
// arrange
36
36
var context = _fixture . GetService < AppDbContext > ( ) ;
37
37
var article = _articleFaker . Generate ( ) ;
38
38
var tag = _tagFaker . Generate ( ) ;
39
- var articleTag = new ArticleTag {
39
+ var articleTag = new ArticleTag
40
+ {
40
41
Article = article ,
41
42
Tag = tag
42
43
} ;
43
44
context . ArticleTags . Add ( articleTag ) ;
44
45
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
+
46
87
var route = $ "/api/v1/articles/{ article . Id } ?include=tags";
47
88
48
89
// act
@@ -51,13 +92,16 @@ public async Task Can_Fetch_Many_To_Many_Through()
51
92
// assert
52
93
var body = await response . Content . ReadAsStringAsync ( ) ;
53
94
Assert . True ( HttpStatusCode . OK == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
54
-
95
+ Assert . True ( body . Contains ( "include" ) ) ;
96
+
55
97
var articleResponse = _fixture . GetService < IJsonApiDeSerializer > ( ) . Deserialize < Article > ( body ) ;
56
98
Assert . NotNull ( articleResponse ) ;
57
99
Assert . Equal ( article . Id , articleResponse . Id ) ;
58
-
100
+
59
101
var tagResponse = Assert . Single ( articleResponse . Tags ) ;
60
102
Assert . Equal ( tag . Id , tagResponse . Id ) ;
103
+ Assert . Equal ( tag . Name , tagResponse . Name ) ;
104
+
61
105
}
62
106
63
107
[ Fact ]
0 commit comments