Skip to content

Commit

Permalink
fixed delete behavior and added delete unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Oct 14, 2023
1 parent cb95b72 commit b1aaa65
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public JsonConfigurationQuery(string filePath)
{
var jsonString = await File.ReadAllTextAsync(_filePath);
var token = GetToken(path, jsonString);
return token == null ? default : JsonConvert.DeserializeObject<T>(token?.ToString());
return token == null ? default : JsonConvert.DeserializeObject<T>(token.ToString());
}
#endregion

Expand Down
10 changes: 4 additions & 6 deletions _src/Implementation/Repositories/AJsonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,12 @@ private ICollection<TInterface> PerformSave(ICollection<TInterface> currentConte
currentContent.Add(updatedEntity);
_updatedEntities.Remove(updatedEntity);
}

foreach (var id in _removedEntities)
for (var index = 0; index < _removedEntities.Count; index++)
{
if (!currentContent.Any(e => e.Id.Equals(id)))
{
throw new InvalidOperationException("Entity to remove not found in the repository");
}
var id = _removedEntities[index];
currentContent.Remove(currentContent.First(e => e.Id.Equals(id)));

_removedEntities.Remove(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ public async Task JRT_0022_Given_JsonFile_When_GetAllEntitiesAsyncCalled_Then_Al
Assert.Equal(4, allUsers.Count);
Assert.Equal(allUsers.First().Id, guid);
}

[Fact]
public void JRT_0031_Given_JsonFile_When_DeleteFirstFromRepository_Then_EntityIsDeleted()
{
var id = Guid.NewGuid();
var fileName = @".\Resources\JRT\0021-users.json";
var tempFileName = @$".\Resources\JRT\{id}.json";
File.Copy(fileName, tempFileName);
using var repository = CreateRepositoryFactory(@".\Resources\JRT").CreateJsonRepository<IUser, User>(id.ToString());
var fistUser = repository.GetAllEntities().First();
repository.Delete(fistUser.Id).SaveChanges();
Assert.True(File.Exists(tempFileName));
var newCollection = repository.GetAllEntities().ToList();
Assert.Equal(3, newCollection.Count);
var guid = Guid.Parse("b4b19e46-b845-4181-9ebe-ed7f5eafbd0d");
Assert.Equal(newCollection.First().Id, guid);
if (File.Exists(tempFileName))
{
File.Delete(tempFileName);
}
Assert.False(File.Exists(tempFileName));
}

private IApplicationSettings CreateMockApplicationSettings(string folder)
{
Expand Down

0 comments on commit b1aaa65

Please sign in to comment.