1
1
using Cnblogs . Architecture . Ddd . Domain . Abstractions ;
2
2
using Cnblogs . Architecture . TestShared ;
3
3
using Cnblogs . Architecture . UnitTests . Infrastructure . FakeObjects ;
4
-
5
4
using FluentAssertions ;
6
-
7
5
using MediatR ;
8
-
9
6
using Microsoft . EntityFrameworkCore ;
10
-
11
7
using Moq ;
12
8
13
9
namespace Cnblogs . Architecture . UnitTests . Infrastructure . EntityFramework ;
14
10
15
11
public class BaseRepositoryTests
16
12
{
13
+ [ Fact ]
14
+ public async Task GetEntityAsync_Include_GetEntityAsync ( )
15
+ {
16
+ // Arrange
17
+ var entity = new EntityGenerator < FakeBlog > ( new FakeBlog ( ) )
18
+ . Setup ( x => x . DateUpdated = DateTimeOffset . Now . AddDays ( - 1 ) )
19
+ . HasManyForEachEntity (
20
+ x => x . Posts ,
21
+ x => x . Blog ,
22
+ new EntityGenerator < FakePost > ( new FakePost ( ) )
23
+ . Setup ( x => x . DateUpdated = DateTimeOffset . Now . AddDays ( - 1 ) ) )
24
+ . GenerateSingle ( ) ;
25
+ var db = new FakeDbContext (
26
+ new DbContextOptionsBuilder < FakeDbContext > ( ) . UseInMemoryDatabase ( "inmemory" ) . Options ) ;
27
+ db . Add ( entity ) ;
28
+ await db . SaveChangesAsync ( ) ;
29
+ var repository = new TestRepository ( Mock . Of < IMediator > ( ) , db ) ;
30
+
31
+ // Act
32
+ var got = await repository . GetAsync ( entity . Id , e => e . Posts ) ;
33
+
34
+ // Assert
35
+ got . Should ( ) . NotBeNull ( ) ;
36
+ got ! . Posts . Should ( ) . BeEquivalentTo ( entity . Posts ) ;
37
+ }
38
+
39
+ [ Fact ]
40
+ public async Task GetEntityAsync_StringBasedInclude_NotNullAsync ( )
41
+ {
42
+ // Arrange
43
+ var entity = new EntityGenerator < FakeBlog > ( new FakeBlog ( ) )
44
+ . Setup ( x => x . DateUpdated = DateTimeOffset . Now . AddDays ( - 1 ) )
45
+ . HasManyForEachEntity (
46
+ x => x . Posts ,
47
+ x => x . Blog ,
48
+ new EntityGenerator < FakePost > ( new FakePost ( ) )
49
+ . Setup ( x => x . DateUpdated = DateTimeOffset . Now . AddDays ( - 1 ) ) )
50
+ . GenerateSingle ( ) ;
51
+ var db = new FakeDbContext (
52
+ new DbContextOptionsBuilder < FakeDbContext > ( ) . UseInMemoryDatabase ( "inmemory" ) . Options ) ;
53
+ db . Add ( entity ) ;
54
+ await db . SaveChangesAsync ( ) ;
55
+ var repository = new TestRepository ( Mock . Of < IMediator > ( ) , db ) ;
56
+
57
+ // Act
58
+ var got = await repository . GetAsync ( entity . Id , nameof ( entity . Posts ) ) ;
59
+
60
+ // Assert
61
+ got . Should ( ) . NotBeNull ( ) ;
62
+ got ! . Posts . Should ( ) . BeEquivalentTo ( entity . Posts ) ;
63
+ }
64
+
65
+ [ Fact ]
66
+ public async Task GetEntityAsync_ThenInclude_NotNullAsync ( )
67
+ {
68
+ // Arrange
69
+ var entity = new EntityGenerator < FakeBlog > ( new FakeBlog ( ) )
70
+ . Setup ( x => x . DateUpdated = DateTimeOffset . Now . AddDays ( - 1 ) )
71
+ . HasManyForEachEntity (
72
+ x => x . Posts ,
73
+ x => x . Blog ,
74
+ new EntityGenerator < FakePost > ( new FakePost ( ) )
75
+ . HasManyForEachEntity ( x => x . Tags , new EntityGenerator < FakeTag > ( new FakeTag ( ) ) )
76
+ . Setup ( x => x . DateUpdated = DateTimeOffset . Now . AddDays ( - 1 ) ) )
77
+ . GenerateSingle ( ) ;
78
+ var db = new FakeDbContext (
79
+ new DbContextOptionsBuilder < FakeDbContext > ( ) . UseInMemoryDatabase ( "inmemory" ) . Options ) ;
80
+ db . Add ( entity ) ;
81
+ await db . SaveChangesAsync ( ) ;
82
+ var repository = new TestRepository ( Mock . Of < IMediator > ( ) , db ) ;
83
+
84
+ // Act
85
+ var got = await repository . GetAsync ( entity . Id , "Posts.Tags" ) ;
86
+
87
+ // Assert
88
+ got . Should ( ) . NotBeNull ( ) ;
89
+ got ! . Posts . Should ( ) . BeEquivalentTo ( entity . Posts ) ;
90
+ }
91
+
17
92
[ Fact ]
18
93
public async Task SaveEntitiesAsync_CallBeforeUpdateForRelatedEntityAsync ( )
19
94
{
@@ -69,10 +144,14 @@ public async Task SaveEntitiesAsync_DispatchEntityDomainEventsAsync()
69
144
70
145
// Assert
71
146
mediator . Verify (
72
- x => x . Publish ( It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . FakeValue == 1 ) , It . IsAny < CancellationToken > ( ) ) ,
147
+ x => x . Publish (
148
+ It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . FakeValue == 1 ) ,
149
+ It . IsAny < CancellationToken > ( ) ) ,
73
150
Times . Once ) ;
74
151
mediator . Verify (
75
- x => x . Publish ( It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . FakeValue == 2 ) , It . IsAny < CancellationToken > ( ) ) ,
152
+ x => x . Publish (
153
+ It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . FakeValue == 2 ) ,
154
+ It . IsAny < CancellationToken > ( ) ) ,
76
155
Times . Once ) ;
77
156
}
78
157
@@ -104,10 +183,14 @@ public async Task SaveEntitiesAsync_DispatchRelatedEntityDomainEventsAsync()
104
183
105
184
// Assert
106
185
mediator . Verify (
107
- x => x . Publish ( It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . FakeValue == 1 ) , It . IsAny < CancellationToken > ( ) ) ,
186
+ x => x . Publish (
187
+ It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . FakeValue == 1 ) ,
188
+ It . IsAny < CancellationToken > ( ) ) ,
108
189
Times . Once ) ;
109
190
mediator . Verify (
110
- x => x . Publish ( It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . FakeValue == 2 ) , It . IsAny < CancellationToken > ( ) ) ,
191
+ x => x . Publish (
192
+ It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . FakeValue == 2 ) ,
193
+ It . IsAny < CancellationToken > ( ) ) ,
111
194
Times . Once ) ;
112
195
}
113
196
@@ -145,4 +228,4 @@ public async Task SaveEntitiesAsync_DispatchEntityDomainEventsWithGeneratedIdAsy
145
228
It . IsAny < CancellationToken > ( ) ) ,
146
229
Times . Exactly ( entity . Posts . Count ) ) ;
147
230
}
148
- }
231
+ }
0 commit comments