Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit fffc33c

Browse files
author
Morten Turn Pedersen
authored
Merge pull request #27 from nodes-dotnet/feature/17-hard-delete
Feature/17 hard delete
2 parents b76ec87 + 693f3ed commit fffc33c

File tree

11 files changed

+676
-214
lines changed

11 files changed

+676
-214
lines changed

NetCoreEntityFramework.Tests/EntityRepositoryTests.cs

Lines changed: 29 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -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,28 +62,27 @@ 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

8571
[Test]
86-
public async Task AddEntityWithIdKeepsId()
72+
[AutoData]
73+
public async Task AddEntityWithIdKeepsId(Guid idToCreate)
8774
{
88-
Guid id = Guid.NewGuid();
8975
var entity = new TestEntity
9076
{
91-
Id = id
77+
Id = idToCreate
9278
};
9379

9480
await using (_repository)
9581
{
9682
await _repository.Add(entity);
9783
}
9884

99-
Assert.AreEqual(id, entity.Id);
85+
Assert.AreEqual(idToCreate, entity.Id);
10086
}
10187

10288
[Test]
@@ -107,28 +93,13 @@ public void AddThrowsExceptionIfEntityIsNull()
10793
#endregion
10894

10995
#region List
110-
[Test]
111-
public async Task GetListReturnsAllNotDeleted()
112-
{
113-
var entities = await _repository.GetList();
114-
115-
Assert.AreEqual(_listEntities.Count() + 1, entities.Count());
116-
}
11796

11897
[Test]
11998
public async Task GetListReturnsAll()
12099
{
121-
var entities = await _repository.GetList(null, null, OrderBy.Ascending, GetListMode.IncludeDeleted);
100+
var entities = await _repository.GetList(null, null, OrderBy.Ascending);
122101

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);
130-
131-
Assert.AreEqual(1, entities.Count());
102+
Assert.AreEqual(_listEntities.Count() + 1, entities.Count());
132103
}
133104

134105
[Test]
@@ -233,26 +204,19 @@ private TestEntity GetTestEntity(string property)
233204
[Test]
234205
public async Task GetValidEntityReturnsEntity()
235206
{
236-
var entity = await _repository.Get((Guid)_entity.Id);
207+
var entity = await _repository.Get(_entity.Id);
237208

238209
Assert.AreSame(_entity, entity);
239210
}
240211

241212
[Test]
242-
public async Task DontGetDeletedEntityWithoutFlag()
213+
[AutoData]
214+
public async Task DontGetNonExistantEntity(Guid nonExistantId)
243215
{
244-
var entity = await _repository.Get((Guid)_deletedEntity.Id);
216+
var entity = await _repository.Get(nonExistantId);
245217

246218
Assert.IsNull(entity);
247219
}
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-
}
256220
#endregion
257221

258222
#region Update
@@ -269,7 +233,7 @@ public async Task UpdateUpdatesUpdated(string propertyValue)
269233
await _repository.Update(_entity);
270234
}
271235

272-
var entity = await _repository.Get((Guid)_entity.Id);
236+
var entity = await _repository.Get(_entity.Id);
273237

274238
Assert.AreEqual(propertyValue, entity.Property);
275239
Assert.AreNotEqual(oldUpdated, entity.Updated);
@@ -285,121 +249,60 @@ public void UpdateThrowsExceptionIfNull()
285249

286250
#region Delete
287251
[Test]
288-
public async Task DeleteSoftDeletesAndSetsDeletedAt()
252+
public async Task DeleteDeletesEntity()
289253
{
290254
bool success;
255+
var expectedEntityCount = _context.Table.Count() - 1;
291256
await using(_repository)
292257
{
293258
success = await _repository.Delete(_entity);
294259
}
295260

296-
var newlyDeletedEntity = await _repository.Get((Guid)_entity.Id, true);
261+
var newlyDeletedEntity = await _repository.Get(_entity.Id);
297262
Assert.IsTrue(success);
298-
Assert.IsTrue(newlyDeletedEntity.Deleted);
299-
Assert.NotNull(newlyDeletedEntity.DeletedAt);
300-
}
301-
302-
[Test]
303-
public void DeleteThrowsExceptionIfArgumentNull()
304-
{
305-
Assert.ThrowsAsync<ArgumentNullException>(() => _repository.Delete(null));
263+
Assert.IsNull(newlyDeletedEntity);
264+
Assert.AreEqual(expectedEntityCount, _context.Table.Count());
306265
}
307-
308266
[Test]
309-
public async Task DeleteWithValidIdDeletesAndSetsDeletedAt()
267+
public async Task DeleteOnIdDeletesEntity()
310268
{
311269
bool success;
312-
Guid id = (Guid)_entity.Id;
270+
var expectedEntityCount = _context.Table.Count() - 1;
313271
await using (_repository)
314272
{
315-
success = await _repository.Delete(id);
273+
success = await _repository.Delete(_entity.Id);
316274
}
317275

318-
var newlyDeletedEntity = await _repository.Get(id, true);
276+
var newlyDeletedEntity = await _repository.Get(_entity.Id);
319277
Assert.IsTrue(success);
320-
Assert.IsTrue(newlyDeletedEntity.Deleted);
321-
Assert.NotNull(newlyDeletedEntity.DeletedAt);
278+
Assert.IsNull(newlyDeletedEntity);
279+
Assert.AreEqual(expectedEntityCount, _context.Table.Count());
322280
}
323281

324282
[Test]
325-
[AutoData]
326-
public async Task DeleteWithInvalidIdReturnsFalse(Guid randomId)
327-
{
328-
bool success;
329-
330-
await using(_repository)
331-
{
332-
success = await _repository.Delete(randomId);
333-
}
334-
335-
Assert.IsFalse(success);
336-
}
337-
338-
[Test]
339-
public void DeleteWithEmptyGuidThrowsException()
340-
{
341-
Assert.ThrowsAsync<ArgumentException>(() => _repository.Delete(Guid.Empty));
342-
}
343-
#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()
283+
public void DeleteThrowsExceptionIfArgumentNull()
370284
{
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);
285+
Assert.ThrowsAsync<ArgumentNullException>(() => _repository.Delete(null));
383286
}
384287

385288
[Test]
386289
[AutoData]
387-
public async Task RestoreOnInvalidIdReturnsFalse(Guid randomId)
290+
public async Task DeleteWithInvalidIdReturnsFalse(Guid randomId)
388291
{
389292
bool success;
390293

391294
await using(_repository)
392295
{
393-
success = await _repository.Restore(randomId);
296+
success = await _repository.Delete(randomId);
394297
}
395298

396299
Assert.IsFalse(success);
397300
}
398301

399302
[Test]
400-
public void RestoreOnEmptyGuidThrowsException()
303+
public void DeleteWithEmptyGuidThrowsException()
401304
{
402-
Assert.ThrowsAsync<ArgumentException>(() => _repository.Restore(Guid.Empty));
305+
Assert.ThrowsAsync<ArgumentException>(() => _repository.Delete(Guid.Empty));
403306
}
404307
#endregion
405308
}

0 commit comments

Comments
 (0)