Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 29 additions & 126 deletions NetCoreEntityFramework.Tests/EntityRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class EntityRepositoryTests
private TestEntityRepository _repository;
private TestContext _context;
private TestEntity _entity;
private TestEntity _deletedEntity;
private IEnumerable<TestEntity> _listEntities;

[SetUp]
Expand All @@ -35,24 +34,12 @@ public void Setup()
_entity = new TestEntity
{
Created = now,
Deleted = false,
Id = Guid.NewGuid(),
Updated = now,
Property = string.Empty
};

_deletedEntity = new TestEntity
{
Created = now.AddMinutes(-42),
Deleted = true,
DeletedAt = now,
Id = Guid.NewGuid(),
Updated = now.AddMinutes(-42),
Property = "I'm deleted"
};

_context.Table.Add(_entity);
_context.Table.Add(_deletedEntity);

_listEntities = GetTestList();
_context.Table.AddRange(_listEntities);
Expand All @@ -75,28 +62,27 @@ public async Task AddAddsEntityAndSetsAttributes()
await _repository.Add(entity);
}

Assert.NotNull(entity.Id);
Assert.AreNotEqual(Guid.Empty, entity.Id);
Assert.AreNotEqual(default(DateTime), entity.Created);
Assert.AreNotEqual(default(DateTime), entity.Updated);
Assert.IsFalse(entity.Deleted);
Assert.AreEqual(expectedSize, await _context.Table.CountAsync());
}

[Test]
public async Task AddEntityWithIdKeepsId()
[AutoData]
public async Task AddEntityWithIdKeepsId(Guid idToCreate)
{
Guid id = Guid.NewGuid();
var entity = new TestEntity
{
Id = id
Id = idToCreate
};

await using (_repository)
{
await _repository.Add(entity);
}

Assert.AreEqual(id, entity.Id);
Assert.AreEqual(idToCreate, entity.Id);
}

[Test]
Expand All @@ -107,28 +93,13 @@ public void AddThrowsExceptionIfEntityIsNull()
#endregion

#region List
[Test]
public async Task GetListReturnsAllNotDeleted()
{
var entities = await _repository.GetList();

Assert.AreEqual(_listEntities.Count() + 1, entities.Count());
}

[Test]
public async Task GetListReturnsAll()
{
var entities = await _repository.GetList(null, null, OrderBy.Ascending, GetListMode.IncludeDeleted);
var entities = await _repository.GetList(null, null, OrderBy.Ascending);

Assert.AreEqual(_listEntities.Count() + 2, entities.Count());
}

[Test]
public async Task GetListReturnsAllDeleted()
{
var entities = await _repository.GetList(null, null, OrderBy.Ascending, GetListMode.OnlyDeleted);

Assert.AreEqual(1, entities.Count());
Assert.AreEqual(_listEntities.Count() + 1, entities.Count());
}

[Test]
Expand Down Expand Up @@ -233,26 +204,19 @@ private TestEntity GetTestEntity(string property)
[Test]
public async Task GetValidEntityReturnsEntity()
{
var entity = await _repository.Get((Guid)_entity.Id);
var entity = await _repository.Get(_entity.Id);

Assert.AreSame(_entity, entity);
}

[Test]
public async Task DontGetDeletedEntityWithoutFlag()
[AutoData]
public async Task DontGetNonExistantEntity(Guid nonExistantId)
{
var entity = await _repository.Get((Guid)_deletedEntity.Id);
var entity = await _repository.Get(nonExistantId);

Assert.IsNull(entity);
}

[Test]
public async Task GetDeletedEntityWithFlag()
{
var entity = await _repository.Get((Guid)_deletedEntity.Id, true);

Assert.AreSame(_deletedEntity, entity);
}
#endregion

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

var entity = await _repository.Get((Guid)_entity.Id);
var entity = await _repository.Get(_entity.Id);

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

#region Delete
[Test]
public async Task DeleteSoftDeletesAndSetsDeletedAt()
public async Task DeleteDeletesEntity()
{
bool success;
var expectedEntityCount = _context.Table.Count() - 1;
await using(_repository)
{
success = await _repository.Delete(_entity);
}

var newlyDeletedEntity = await _repository.Get((Guid)_entity.Id, true);
var newlyDeletedEntity = await _repository.Get(_entity.Id);
Assert.IsTrue(success);
Assert.IsTrue(newlyDeletedEntity.Deleted);
Assert.NotNull(newlyDeletedEntity.DeletedAt);
}

[Test]
public void DeleteThrowsExceptionIfArgumentNull()
{
Assert.ThrowsAsync<ArgumentNullException>(() => _repository.Delete(null));
Assert.IsNull(newlyDeletedEntity);
Assert.AreEqual(expectedEntityCount, _context.Table.Count());
}

[Test]
public async Task DeleteWithValidIdDeletesAndSetsDeletedAt()
public async Task DeleteOnIdDeletesEntity()
{
bool success;
Guid id = (Guid)_entity.Id;
var expectedEntityCount = _context.Table.Count() - 1;
await using (_repository)
{
success = await _repository.Delete(id);
success = await _repository.Delete(_entity.Id);
}

var newlyDeletedEntity = await _repository.Get(id, true);
var newlyDeletedEntity = await _repository.Get(_entity.Id);
Assert.IsTrue(success);
Assert.IsTrue(newlyDeletedEntity.Deleted);
Assert.NotNull(newlyDeletedEntity.DeletedAt);
Assert.IsNull(newlyDeletedEntity);
Assert.AreEqual(expectedEntityCount, _context.Table.Count());
}

[Test]
[AutoData]
public async Task DeleteWithInvalidIdReturnsFalse(Guid randomId)
{
bool success;

await using(_repository)
{
success = await _repository.Delete(randomId);
}

Assert.IsFalse(success);
}

[Test]
public void DeleteWithEmptyGuidThrowsException()
{
Assert.ThrowsAsync<ArgumentException>(() => _repository.Delete(Guid.Empty));
}
#endregion

#region Restore
[Test]
public async Task RestoreSetsDeletedFalse()
{
bool success;

await using(_repository)
{
success = await _repository.Restore(_deletedEntity);
}

var restoredEntity = await _repository.Get((Guid)_deletedEntity.Id);
Assert.IsTrue(success);
Assert.IsFalse(restoredEntity.Deleted);
Assert.IsNull(restoredEntity.DeletedAt);
}

[Test]
public void RestoreThrowsExceptionWhenEntityNull()
{
Assert.ThrowsAsync<ArgumentNullException>(() => _repository.Restore(null));
}

[Test]
public async Task RestoreOnIdSetsDeletedFalse()
public void DeleteThrowsExceptionIfArgumentNull()
{
bool success;
Guid id = (Guid)_deletedEntity.Id;

await using (_repository)
{
success = await _repository.Restore(id);
}

var restoredEntity = await _repository.Get(id);
Assert.IsTrue(success);
Assert.IsFalse(restoredEntity.Deleted);
Assert.IsNull(restoredEntity.DeletedAt);
Assert.ThrowsAsync<ArgumentNullException>(() => _repository.Delete(null));
}

[Test]
[AutoData]
public async Task RestoreOnInvalidIdReturnsFalse(Guid randomId)
public async Task DeleteWithInvalidIdReturnsFalse(Guid randomId)
{
bool success;

await using(_repository)
{
success = await _repository.Restore(randomId);
success = await _repository.Delete(randomId);
}

Assert.IsFalse(success);
}

[Test]
public void RestoreOnEmptyGuidThrowsException()
public void DeleteWithEmptyGuidThrowsException()
{
Assert.ThrowsAsync<ArgumentException>(() => _repository.Restore(Guid.Empty));
Assert.ThrowsAsync<ArgumentException>(() => _repository.Delete(Guid.Empty));
}
#endregion
}
Expand Down
Loading