1+ using AutoFixture . NUnit3 ;
2+ using Microsoft . EntityFrameworkCore ;
3+ using Nodes . NetCore . EntityFramework . Tests . Mocks ;
4+ using NUnit . Framework ;
5+ using System ;
6+ using System . Threading . Tasks ;
7+ using TestContext = Nodes . NetCore . EntityFramework . Tests . Mocks . TestContext ;
8+
9+ namespace Nodes . NetCore . EntityFramework . Tests
10+ {
11+ public class EntityRepositoryTests
12+ {
13+ private TestEntityRepository _repository ;
14+ private TestContext _context ;
15+ private TestEntity _entity ;
16+ private TestEntity _deletedEntity ;
17+
18+ [ SetUp ]
19+ public void Setup ( )
20+ {
21+ var options = new DbContextOptionsBuilder < TestContext > ( )
22+ . UseInMemoryDatabase ( Guid . NewGuid ( ) . ToString ( ) )
23+ . Options ;
24+
25+ _context = new TestContext ( options ) ;
26+
27+ _repository = new TestEntityRepository ( _context ) ;
28+
29+ DateTime now = DateTime . UtcNow ;
30+
31+ _entity = new TestEntity
32+ {
33+ Created = now ,
34+ Deleted = false ,
35+ Id = Guid . NewGuid ( ) ,
36+ Updated = now ,
37+ Property = string . Empty
38+ } ;
39+
40+ _deletedEntity = new TestEntity
41+ {
42+ Created = now . AddMinutes ( - 42 ) ,
43+ Deleted = true ,
44+ DeletedAt = now ,
45+ Id = Guid . NewGuid ( ) ,
46+ Updated = now . AddMinutes ( - 42 ) ,
47+ Property = "I'm deleted"
48+ } ;
49+
50+ _context . Table . Add ( _entity ) ;
51+ _context . Table . Add ( _deletedEntity ) ;
52+
53+ _context . SaveChanges ( ) ;
54+
55+ _repository = new TestEntityRepository ( _context ) ;
56+ }
57+
58+ #region Add
59+ [ Test ]
60+ public async Task AddAddsEntityAndSetsAttributes ( )
61+ {
62+ int startSize = await _context . Table . CountAsync ( ) ;
63+ int expectedSize = startSize + 1 ;
64+ var entity = new TestEntity ( ) ;
65+
66+ using ( _repository )
67+ {
68+ await _repository . Add ( entity ) ;
69+ }
70+
71+ Assert . NotNull ( entity . Id ) ;
72+ Assert . AreNotEqual ( default ( DateTime ) , entity . Created ) ;
73+ Assert . AreNotEqual ( default ( DateTime ) , entity . Updated ) ;
74+ Assert . IsFalse ( entity . Deleted ) ;
75+ Assert . AreEqual ( expectedSize , await _context . Table . CountAsync ( ) ) ;
76+ }
77+
78+ [ Test ]
79+ public async Task AddEntityWithIdKeepsId ( )
80+ {
81+ Guid id = Guid . NewGuid ( ) ;
82+ var entity = new TestEntity
83+ {
84+ Id = id
85+ } ;
86+
87+ using ( _repository )
88+ {
89+ await _repository . Add ( entity ) ;
90+ }
91+
92+ Assert . AreEqual ( id , entity . Id ) ;
93+ }
94+
95+ [ Test ]
96+ public void AddThrowsExceptionIfEntityIsNull ( )
97+ {
98+ Assert . ThrowsAsync < ArgumentNullException > ( ( ) => _repository . Add ( null ) ) ;
99+ }
100+ #endregion
101+
102+ #region Get
103+ [ Test ]
104+ public async Task GetValidEntityReturnsEntity ( )
105+ {
106+ var entity = await _repository . Get ( ( Guid ) _entity . Id ) ;
107+
108+ Assert . AreSame ( _entity , entity ) ;
109+ }
110+
111+ [ Test ]
112+ public async Task DontGetDeletedEntityWithoutFlag ( )
113+ {
114+ var entity = await _repository . Get ( ( Guid ) _deletedEntity . Id ) ;
115+
116+ Assert . IsNull ( entity ) ;
117+ }
118+
119+ [ Test ]
120+ public async Task GetDeletedEntityWithFlag ( )
121+ {
122+ var entity = await _repository . Get ( ( Guid ) _deletedEntity . Id , true ) ;
123+
124+ Assert . AreSame ( _deletedEntity , entity ) ;
125+ }
126+ #endregion
127+
128+ #region Update
129+ [ Test ]
130+ [ AutoData ]
131+ public async Task UpdateUpdatesUpdated ( string propertyValue )
132+ {
133+ DateTime oldUpdated = _entity . Updated ;
134+ DateTime oldCreated = _entity . Created ;
135+ _entity . Property = propertyValue ;
136+
137+ using ( _repository )
138+ {
139+ await _repository . Update ( _entity ) ;
140+ }
141+
142+ var entity = await _repository . Get ( ( Guid ) _entity . Id ) ;
143+
144+ Assert . AreEqual ( propertyValue , entity . Property ) ;
145+ Assert . AreNotEqual ( oldUpdated , entity . Updated ) ;
146+ Assert . AreEqual ( oldCreated , entity . Created ) ;
147+ }
148+
149+ [ Test ]
150+ public void UpdateThrowsExceptionIfNull ( )
151+ {
152+ Assert . ThrowsAsync < ArgumentNullException > ( ( ) => _repository . Update ( null ) ) ;
153+ }
154+ #endregion
155+
156+ #region Delete
157+ [ Test ]
158+ public async Task DeleteSoftDeletesAndSetsDeletedAt ( )
159+ {
160+ bool success ;
161+ using ( _repository )
162+ {
163+ success = await _repository . Delete ( _entity ) ;
164+ }
165+
166+ var newlyDeletedEntity = await _repository . Get ( ( Guid ) _entity . Id , true ) ;
167+ Assert . IsTrue ( success ) ;
168+ Assert . IsTrue ( newlyDeletedEntity . Deleted ) ;
169+ Assert . NotNull ( newlyDeletedEntity . DeletedAt ) ;
170+ }
171+
172+ [ Test ]
173+ public void DeleteThrowsExceptionIfArgumentNull ( )
174+ {
175+ Assert . ThrowsAsync < ArgumentNullException > ( ( ) => _repository . Delete ( null ) ) ;
176+ }
177+
178+ [ Test ]
179+ public async Task DeleteWithValidIdDeletesAndSetsDeletedAt ( )
180+ {
181+ bool success ;
182+ Guid id = ( Guid ) _entity . Id ;
183+ using ( _repository )
184+ {
185+ success = await _repository . Delete ( id ) ;
186+ }
187+
188+ var newlyDeletedEntity = await _repository . Get ( id , true ) ;
189+ Assert . IsTrue ( success ) ;
190+ Assert . IsTrue ( newlyDeletedEntity . Deleted ) ;
191+ Assert . NotNull ( newlyDeletedEntity . DeletedAt ) ;
192+ }
193+
194+ [ Test ]
195+ [ AutoData ]
196+ public async Task DeleteWithInvalidIdReturnsFalse ( Guid randomId )
197+ {
198+ bool success ;
199+
200+ using ( _repository )
201+ {
202+ success = await _repository . Delete ( randomId ) ;
203+ }
204+
205+ Assert . IsFalse ( success ) ;
206+ }
207+
208+ [ Test ]
209+ public void DeleteWithEmptyGuidThrowsException ( )
210+ {
211+ Assert . ThrowsAsync < ArgumentException > ( ( ) => _repository . Delete ( Guid . Empty ) ) ;
212+ }
213+ #endregion
214+
215+ #region Restore
216+ [ Test ]
217+ public async Task RestoreSetsDeletedFalse ( )
218+ {
219+ bool success ;
220+
221+ using ( _repository )
222+ {
223+ success = await _repository . Restore ( _deletedEntity ) ;
224+ }
225+
226+ var restoredEntity = await _repository . Get ( ( Guid ) _deletedEntity . Id ) ;
227+ Assert . IsTrue ( success ) ;
228+ Assert . IsFalse ( restoredEntity ? . Deleted ) ;
229+ }
230+
231+ [ Test ]
232+ public void RestoreThrowsExceptionWhenEntityNull ( )
233+ {
234+ Assert . ThrowsAsync < ArgumentNullException > ( ( ) => _repository . Restore ( null ) ) ;
235+ }
236+
237+ [ Test ]
238+ public async Task RestoreOnIdSetsDeletedFalse ( )
239+ {
240+ bool success ;
241+ Guid id = ( Guid ) _deletedEntity . Id ;
242+
243+ using ( _repository )
244+ {
245+ success = await _repository . Restore ( id ) ;
246+ }
247+
248+ var restoredEntity = await _repository . Get ( id ) ;
249+ Assert . IsTrue ( success ) ;
250+ Assert . IsFalse ( restoredEntity ? . Deleted ) ;
251+ }
252+
253+ [ Test ]
254+ [ AutoData ]
255+ public async Task RestoreOnInvalidIdReturnsFalse ( Guid randomId )
256+ {
257+ bool success ;
258+
259+ using ( _repository )
260+ {
261+ success = await _repository . Restore ( randomId ) ;
262+ }
263+
264+ Assert . IsFalse ( success ) ;
265+ }
266+
267+ [ Test ]
268+ public void RestoreOnEmptyGuidThrowsException ( )
269+ {
270+ Assert . ThrowsAsync < ArgumentException > ( ( ) => _repository . Restore ( Guid . Empty ) ) ;
271+ }
272+ #endregion
273+ }
274+ }
0 commit comments