@@ -16,7 +16,6 @@ public class EntityRepositoryTests
1616 private TestEntityRepository _repository ;
1717 private TestContext _context ;
1818 private TestEntity _entity ;
19- private TestEntity _deletedEntity ;
2019 private IEnumerable < TestEntity > _listEntities ;
2120
2221 [ SetUp ]
@@ -35,24 +34,12 @@ public void Setup()
3534 _entity = new TestEntity
3635 {
3736 Created = now ,
38- Deleted = false ,
3937 Id = Guid . NewGuid ( ) ,
4038 Updated = now ,
4139 Property = string . Empty
4240 } ;
4341
44- _deletedEntity = new TestEntity
45- {
46- Created = now . AddMinutes ( - 42 ) ,
47- Deleted = true ,
48- DeletedAt = now ,
49- Id = Guid . NewGuid ( ) ,
50- Updated = now . AddMinutes ( - 42 ) ,
51- Property = "I'm deleted"
52- } ;
53-
5442 _context . Table . Add ( _entity ) ;
55- _context . Table . Add ( _deletedEntity ) ;
5643
5744 _listEntities = GetTestList ( ) ;
5845 _context . Table . AddRange ( _listEntities ) ;
@@ -75,10 +62,9 @@ public async Task AddAddsEntityAndSetsAttributes()
7562 await _repository . Add ( entity ) ;
7663 }
7764
78- Assert . NotNull ( entity . Id ) ;
65+ Assert . AreNotEqual ( Guid . Empty , entity . Id ) ;
7966 Assert . AreNotEqual ( default ( DateTime ) , entity . Created ) ;
8067 Assert . AreNotEqual ( default ( DateTime ) , entity . Updated ) ;
81- Assert . IsFalse ( entity . Deleted ) ;
8268 Assert . AreEqual ( expectedSize , await _context . Table . CountAsync ( ) ) ;
8369 }
8470
@@ -118,17 +104,9 @@ public async Task GetListReturnsAllNotDeleted()
118104 [ Test ]
119105 public async Task GetListReturnsAll ( )
120106 {
121- var entities = await _repository . GetList ( null , null , OrderBy . Ascending , GetListMode . IncludeDeleted ) ;
122-
123- Assert . AreEqual ( _listEntities . Count ( ) + 2 , entities . Count ( ) ) ;
124- }
125-
126- [ Test ]
127- public async Task GetListReturnsAllDeleted ( )
128- {
129- var entities = await _repository . GetList ( null , null , OrderBy . Ascending , GetListMode . OnlyDeleted ) ;
107+ var entities = await _repository . GetList ( null , null , OrderBy . Ascending ) ;
130108
131- Assert . AreEqual ( 1 , entities . Count ( ) ) ;
109+ Assert . AreEqual ( _listEntities . Count ( ) + 1 , entities . Count ( ) ) ;
132110 }
133111
134112 [ Test ]
@@ -239,20 +217,13 @@ public async Task GetValidEntityReturnsEntity()
239217 }
240218
241219 [ Test ]
242- public async Task DontGetDeletedEntityWithoutFlag ( )
220+ [ AutoData ]
221+ public async Task DontGetNonExistantEntity ( Guid nonExistantId )
243222 {
244- var entity = await _repository . Get ( ( Guid ) _deletedEntity . Id ) ;
223+ var entity = await _repository . Get ( nonExistantId ) ;
245224
246225 Assert . IsNull ( entity ) ;
247226 }
248-
249- [ Test ]
250- public async Task GetDeletedEntityWithFlag ( )
251- {
252- var entity = await _repository . Get ( ( Guid ) _deletedEntity . Id , true ) ;
253-
254- Assert . AreSame ( _deletedEntity , entity ) ;
255- }
256227 #endregion
257228
258229 #region Update
@@ -285,18 +256,19 @@ public void UpdateThrowsExceptionIfNull()
285256
286257 #region Delete
287258 [ Test ]
288- public async Task DeleteSoftDeletesAndSetsDeletedAt ( )
259+ public async Task DeleteDeletesEntity ( )
289260 {
290261 bool success ;
262+ var currentEntityCount = _context . Table . Count ( ) ;
291263 await using ( _repository )
292264 {
293265 success = await _repository . Delete ( _entity ) ;
294266 }
295267
296- var newlyDeletedEntity = await _repository . Get ( ( Guid ) _entity . Id , true ) ;
268+ var newlyDeletedEntity = await _repository . Get ( _entity . Id ) ;
297269 Assert . IsTrue ( success ) ;
298- Assert . IsTrue ( newlyDeletedEntity . Deleted ) ;
299- Assert . NotNull ( newlyDeletedEntity . DeletedAt ) ;
270+ Assert . IsNull ( newlyDeletedEntity ) ;
271+ Assert . AreEqual ( currentEntityCount - 1 , _context . Table . Count ( ) ) ;
300272 }
301273
302274 [ Test ]
@@ -305,22 +277,6 @@ public void DeleteThrowsExceptionIfArgumentNull()
305277 Assert . ThrowsAsync < ArgumentNullException > ( ( ) => _repository . Delete ( null ) ) ;
306278 }
307279
308- [ Test ]
309- public async Task DeleteWithValidIdDeletesAndSetsDeletedAt ( )
310- {
311- bool success ;
312- Guid id = ( Guid ) _entity . Id ;
313- await using ( _repository )
314- {
315- success = await _repository . Delete ( id ) ;
316- }
317-
318- var newlyDeletedEntity = await _repository . Get ( id , true ) ;
319- Assert . IsTrue ( success ) ;
320- Assert . IsTrue ( newlyDeletedEntity . Deleted ) ;
321- Assert . NotNull ( newlyDeletedEntity . DeletedAt ) ;
322- }
323-
324280 [ Test ]
325281 [ AutoData ]
326282 public async Task DeleteWithInvalidIdReturnsFalse ( Guid randomId )
@@ -341,66 +297,5 @@ public void DeleteWithEmptyGuidThrowsException()
341297 Assert . ThrowsAsync < ArgumentException > ( ( ) => _repository . Delete ( Guid . Empty ) ) ;
342298 }
343299 #endregion
344-
345- #region Restore
346- [ Test ]
347- public async Task RestoreSetsDeletedFalse ( )
348- {
349- bool success ;
350-
351- await using ( _repository )
352- {
353- success = await _repository . Restore ( _deletedEntity ) ;
354- }
355-
356- var restoredEntity = await _repository . Get ( ( Guid ) _deletedEntity . Id ) ;
357- Assert . IsTrue ( success ) ;
358- Assert . IsFalse ( restoredEntity . Deleted ) ;
359- Assert . IsNull ( restoredEntity . DeletedAt ) ;
360- }
361-
362- [ Test ]
363- public void RestoreThrowsExceptionWhenEntityNull ( )
364- {
365- Assert . ThrowsAsync < ArgumentNullException > ( ( ) => _repository . Restore ( null ) ) ;
366- }
367-
368- [ Test ]
369- public async Task RestoreOnIdSetsDeletedFalse ( )
370- {
371- bool success ;
372- Guid id = ( Guid ) _deletedEntity . Id ;
373-
374- await using ( _repository )
375- {
376- success = await _repository . Restore ( id ) ;
377- }
378-
379- var restoredEntity = await _repository . Get ( id ) ;
380- Assert . IsTrue ( success ) ;
381- Assert . IsFalse ( restoredEntity . Deleted ) ;
382- Assert . IsNull ( restoredEntity . DeletedAt ) ;
383- }
384-
385- [ Test ]
386- [ AutoData ]
387- public async Task RestoreOnInvalidIdReturnsFalse ( Guid randomId )
388- {
389- bool success ;
390-
391- await using ( _repository )
392- {
393- success = await _repository . Restore ( randomId ) ;
394- }
395-
396- Assert . IsFalse ( success ) ;
397- }
398-
399- [ Test ]
400- public void RestoreOnEmptyGuidThrowsException ( )
401- {
402- Assert . ThrowsAsync < ArgumentException > ( ( ) => _repository . Restore ( Guid . Empty ) ) ;
403- }
404- #endregion
405300 }
406301}
0 commit comments