1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using System . Net ;
4
5
using System . Net . Http ;
5
6
using System . Net . Http . Headers ;
6
7
using System . Threading . Tasks ;
7
8
using Bogus ;
9
+ using JsonApiDotNetCore . Models ;
8
10
using JsonApiDotNetCore . Serialization ;
9
11
using JsonApiDotNetCoreExample . Data ;
10
12
using JsonApiDotNetCoreExample . Models ;
@@ -24,25 +26,69 @@ public class ManyToManyTests
24
26
private static readonly Faker < Tag > _tagFaker = new Faker < Tag > ( ) . RuleFor ( a => a . Name , f => f . Random . AlphaNumeric ( 10 ) ) ;
25
27
26
28
private TestFixture < TestStartup > _fixture ;
27
- public ManyToManyTests ( TestFixture < TestStartup > fixture )
29
+ public ManyToManyTests ( TestFixture < TestStartup > fixture )
28
30
{
29
31
_fixture = fixture ;
30
32
}
31
33
32
34
[ Fact ]
33
- public async Task Can_Fetch_Many_To_Many_Through ( )
35
+ public async Task Can_Fetch_Many_To_Many_Through_All ( )
34
36
{
35
37
// arrange
36
38
var context = _fixture . GetService < AppDbContext > ( ) ;
37
39
var article = _articleFaker . Generate ( ) ;
38
40
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
+ {
40
47
Article = article ,
41
48
Tag = tag
42
49
} ;
43
50
context . ArticleTags . Add ( articleTag ) ;
44
51
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 ) ;
45
67
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
+
46
92
var route = $ "/api/v1/articles/{ article . Id } ?include=tags";
47
93
48
94
// act
@@ -52,12 +98,16 @@ public async Task Can_Fetch_Many_To_Many_Through()
52
98
var body = await response . Content . ReadAsStringAsync ( ) ;
53
99
Assert . True ( HttpStatusCode . OK == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
54
100
101
+ var document = JsonConvert . DeserializeObject < Document > ( body ) ;
102
+ Assert . NotEmpty ( document . Included ) ;
103
+
55
104
var articleResponse = _fixture . GetService < IJsonApiDeSerializer > ( ) . Deserialize < Article > ( body ) ;
56
105
Assert . NotNull ( articleResponse ) ;
57
106
Assert . Equal ( article . Id , articleResponse . Id ) ;
58
-
107
+
59
108
var tagResponse = Assert . Single ( articleResponse . Tags ) ;
60
109
Assert . Equal ( tag . Id , tagResponse . Id ) ;
110
+ Assert . Equal ( tag . Name , tagResponse . Name ) ;
61
111
}
62
112
63
113
[ Fact ]
0 commit comments