Skip to content

Commit

Permalink
Define a common repository interface
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Oct 19, 2023
1 parent 5494338 commit 6c33f6b
Show file tree
Hide file tree
Showing 25 changed files with 259 additions and 249 deletions.
12 changes: 6 additions & 6 deletions Backend.Tests/Controllers/MergeControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ public void BlacklistAddTest()

// Add two Lists of wordIds.
_ = _mergeController.BlacklistAdd(ProjId, wordIdsA).Result;
var result = _mergeBlacklistRepo.GetAllSets(ProjId).Result;
var result = _mergeBlacklistRepo.GetAllEntries(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().WordIds, Is.EqualTo(wordIdsA));
_ = _mergeController.BlacklistAdd(ProjId, wordIdsB).Result;
result = _mergeBlacklistRepo.GetAllSets(ProjId).Result;
result = _mergeBlacklistRepo.GetAllEntries(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(2));

// Add a List of wordIds that contains both previous lists.
_ = _mergeController.BlacklistAdd(ProjId, wordIdsC).Result;
result = _mergeBlacklistRepo.GetAllSets(ProjId).Result;
result = _mergeBlacklistRepo.GetAllEntries(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().WordIds, Is.EqualTo(wordIdsC));
}
Expand All @@ -79,16 +79,16 @@ public void GreylistAddTest()

// Add two Lists of wordIds.
_ = _mergeController.GraylistAdd(ProjId, wordIdsA).Result;
var result = _mergeGraylistRepo.GetAllSets(ProjId).Result;
var result = _mergeGraylistRepo.GetAllEntries(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().WordIds, Is.EqualTo(wordIdsA));
_ = _mergeController.GraylistAdd(ProjId, wordIdsB).Result;
result = _mergeGraylistRepo.GetAllSets(ProjId).Result;
result = _mergeGraylistRepo.GetAllEntries(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(2));

// Add a List of wordIds that contains both previous lists.
_ = _mergeController.GraylistAdd(ProjId, wordIdsC).Result;
result = _mergeGraylistRepo.GetAllSets(ProjId).Result;
result = _mergeGraylistRepo.GetAllEntries(ProjId).Result;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().WordIds, Is.EqualTo(wordIdsC));
}
Expand Down
24 changes: 12 additions & 12 deletions Backend.Tests/Controllers/UserEditControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private UserEdit RandomUserEdit()
}

[Test]
public async Task TestGetAllUserEdits()
public async Task TestGetAllEntries()
{
await _userEditRepo.Create(RandomUserEdit());
await _userEditRepo.Create(RandomUserEdit());
Expand All @@ -90,11 +90,11 @@ public async Task TestGetAllUserEdits()

var edits = ((ObjectResult)getResult).Value as List<UserEdit>;
Assert.That(edits, Has.Count.EqualTo(3));
(await _userEditRepo.GetAllUserEdits(_projId)).ForEach(edit => Assert.That(edits, Does.Contain(edit)));
(await _userEditRepo.GetAllEntries(_projId)).ForEach(edit => Assert.That(edits, Does.Contain(edit)));
}

[Test]
public async Task TestGetAllUserEditsNoPermission()
public async Task TestGetAllEntriesNoPermission()
{
_userEditController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();

Expand All @@ -104,7 +104,7 @@ public async Task TestGetAllUserEditsNoPermission()
}

[Test]
public async Task TestGetAllUserEditsMissingProject()
public async Task TestGetAllEntriesMissingProject()
{
var result = await _userEditController.GetProjectUserEdits(MissingId);
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
Expand Down Expand Up @@ -154,7 +154,7 @@ public async Task TestCreateUserEdit()
var userEdit = new UserEdit { ProjectId = _projId };
var updatedUser = (User)((ObjectResult)await _userEditController.CreateUserEdit(_projId)).Value!;
userEdit.Id = updatedUser.WorkedProjects[_projId];
Assert.That(await _userEditRepo.GetAllUserEdits(_projId), Does.Contain(userEdit));
Assert.That(await _userEditRepo.GetAllEntries(_projId), Does.Contain(userEdit));
}

[Test]
Expand All @@ -177,7 +177,7 @@ public async Task TestAddGoalToUserEdit()

await _userEditController.UpdateUserEditGoal(_projId, userEdit.Id, newEdit);

var allUserEdits = await _userEditRepo.GetAllUserEdits(_projId);
var allUserEdits = await _userEditRepo.GetAllEntries(_projId);
Assert.That(allUserEdits, Does.Contain(updatedUserEdit));
}

Expand Down Expand Up @@ -229,9 +229,9 @@ public async Task TestAddStepToGoal()
await _userEditController.UpdateUserEditStep(_projId, origUserEdit.Id, stepWrapperObj);

// Step count should have increased by 1.
Assert.That(await _userEditRepo.GetAllUserEdits(_projId), Has.Count.EqualTo(count + 1));
Assert.That(await _userEditRepo.GetAllEntries(_projId), Has.Count.EqualTo(count + 1));

var userEdit = await _userEditRepo.GetUserEdit(_projId, origUserEdit.Id);
var userEdit = await _userEditRepo.GetEntry(_projId, origUserEdit.Id);
Assert.That(userEdit, Is.Not.Null);
Assert.That(userEdit!.Edits[modGoalIndex].StepData, Does.Contain(stringStep));

Expand All @@ -245,9 +245,9 @@ public async Task TestAddStepToGoal()
await _userEditController.UpdateUserEditStep(_projId, origUserEdit.Id, stepWrapperObj);

// Step count should not have further increased.
Assert.That(await _userEditRepo.GetAllUserEdits(_projId), Has.Count.EqualTo(count + 1));
Assert.That(await _userEditRepo.GetAllEntries(_projId), Has.Count.EqualTo(count + 1));

userEdit = await _userEditRepo.GetUserEdit(_projId, origUserEdit.Id);
userEdit = await _userEditRepo.GetEntry(_projId, origUserEdit.Id);
Assert.That(userEdit, Is.Not.Null);
Assert.That(userEdit!.Edits[modGoalIndex].StepData, Does.Contain(modStringStep));
}
Expand Down Expand Up @@ -279,10 +279,10 @@ public async Task TestUpdateUserEditStepMissingIds()
public async Task TestDeleteUserEdit()
{
var origUserEdit = await _userEditRepo.Create(RandomUserEdit());
Assert.That(await _userEditRepo.GetAllUserEdits(_projId), Has.Count.EqualTo(1));
Assert.That(await _userEditRepo.GetAllEntries(_projId), Has.Count.EqualTo(1));

await _userEditController.DeleteUserEdit(_projId, origUserEdit.Id);
Assert.That(await _userEditRepo.GetAllUserEdits(_projId), Is.Empty);
Assert.That(await _userEditRepo.GetAllEntries(_projId), Is.Empty);
}

[Test]
Expand Down
24 changes: 12 additions & 12 deletions Backend.Tests/Controllers/UserRoleControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private UserRole RandomUserRole(Role role = Role.Harvester)
}

[Test]
public async Task TestGetAllUserRoles()
public async Task TestGetProjectUserRoles()
{
var roles = new List<Role> { Role.Harvester, Role.Editor, Role.Administrator };
foreach (var role in roles)
Expand All @@ -67,18 +67,18 @@ public async Task TestGetAllUserRoles()

var userRoles = ((ObjectResult)getResult).Value as List<UserRole>;
Assert.That(roles, Has.Count.EqualTo(3));
(await _userRoleRepo.GetAllUserRoles(_projId)).ForEach(ur => Assert.That(userRoles, Does.Contain(ur)));
(await _userRoleRepo.GetAllEntries(_projId)).ForEach(ur => Assert.That(userRoles, Does.Contain(ur)));
}

[Test]
public async Task TestGetAllUserRolesMissingProject()
public async Task TestGetProjectUserRolesMissingProject()
{
var result = await _userRoleController.GetProjectUserRoles(MissingId);
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestGetAllUserRolesNotAuthorized()
public async Task TestGetProjectUserRolesNotAuthorized()
{
_userRoleController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();
var result = await _userRoleController.GetProjectUserRoles(_projId);
Expand Down Expand Up @@ -173,7 +173,7 @@ public async Task TestCreateUserRole()
var userRole = RandomUserRole();
var id = (string)((ObjectResult)await _userRoleController.CreateUserRole(_projId, userRole)).Value!;
userRole.Id = id;
Assert.That(await _userRoleRepo.GetAllUserRoles(_projId), Does.Contain(userRole));
Assert.That(await _userRoleRepo.GetAllEntries(_projId), Does.Contain(userRole));
}

[Test]
Expand Down Expand Up @@ -277,15 +277,15 @@ public async Task TestDeleteUserRole()
var user = new User { ProjectRoles = { [_projId] = userRole.Id } };
var userId = (await _userRepo.Create(user))!.Id;

Assert.That(await _userRoleRepo.GetAllUserRoles(_projId), Has.Count.EqualTo(1));
Assert.That(await _userRoleRepo.GetAllEntries(_projId), Has.Count.EqualTo(1));
var fetchedUser = await _userRepo.GetUser(userId);
Assert.That(fetchedUser, Is.Not.Null);
Assert.That(fetchedUser!.ProjectRoles, Does.ContainKey(_projId));
Assert.That(fetchedUser.ProjectRoles, Does.ContainValue(userRole.Id));

await _userRoleController.DeleteUserRole(_projId, userId);

Assert.That(await _userRoleRepo.GetAllUserRoles(_projId), Is.Empty);
Assert.That(await _userRoleRepo.GetAllEntries(_projId), Is.Empty);
fetchedUser = await _userRepo.GetUser(userId);
Assert.That(fetchedUser, Is.Not.Null);
Assert.That(fetchedUser!.ProjectRoles, Does.Not.ContainKey(_projId));
Expand Down Expand Up @@ -313,27 +313,27 @@ public async Task TestDeleteUserRoleMissingIds()
}

[Test]
public async Task TestDeleteAllUserRoles()
public async Task TestDeleteAll()
{
await _userRoleRepo.Create(RandomUserRole());
await _userRoleRepo.Create(RandomUserRole());
await _userRoleRepo.Create(RandomUserRole());

Assert.That(await _userRoleRepo.GetAllUserRoles(_projId), Has.Count.EqualTo(3));
Assert.That(await _userRoleRepo.GetAllEntries(_projId), Has.Count.EqualTo(3));

await _userRoleController.DeleteProjectUserRoles(_projId);
Assert.That(await _userRoleRepo.GetAllUserRoles(_projId), Is.Empty);
Assert.That(await _userRoleRepo.GetAllEntries(_projId), Is.Empty);
}

[Test]
public async Task TestDeleteAllUserRolesMissingProject()
public async Task TestDeleteAllMissingProject()
{
var result = await _userRoleController.DeleteProjectUserRoles(MissingId);
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestDeleteAllUserRolesNoPermission()
public async Task TestDeleteAllNoPermission()
{
_userRoleController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();
var result = await _userRoleController.DeleteProjectUserRoles(_projId);
Expand Down
33 changes: 18 additions & 15 deletions Backend.Tests/Mocks/MergeBlacklistRepositoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ public MergeBlacklistRepositoryMock()
_mergeBlacklist = new List<MergeWordSet>();
}

public Task<List<MergeWordSet>> GetAllSets(string projectId, string? userId = null)
public Task<List<MergeWordSet>> GetAllEntries(string projectId)
{
var cloneList = _mergeBlacklist.Select(e => e.Clone()).ToList();
var enumerable = userId is null ?
cloneList.Where(e => e.ProjectId == projectId) :
cloneList.Where(e => e.ProjectId == projectId && e.UserId == userId);
return Task.FromResult(enumerable.ToList());
var list = _mergeBlacklist.Where(e => e.ProjectId == projectId).ToList();
return Task.FromResult(list.Select(e => e.Clone()).ToList());
}

public Task<MergeWordSet?> GetSet(string projectId, string entryId)
public Task<List<MergeWordSet>> GetAllEntries(string projectId, string userId)
{
var list = _mergeBlacklist.Where(e => e.ProjectId == projectId && e.UserId == userId).ToList();
return Task.FromResult(list.Select(e => e.Clone()).ToList());
}

public Task<MergeWordSet?> GetEntry(string projectId, string entryId)
{
try
{
Expand All @@ -39,14 +42,14 @@ public Task<List<MergeWordSet>> GetAllSets(string projectId, string? userId = nu
}
}

public Task<MergeWordSet> Create(MergeWordSet wordSetEntry)
public Task<MergeWordSet> Create(MergeWordSet entry)
{
wordSetEntry.Id = Guid.NewGuid().ToString();
_mergeBlacklist.Add(wordSetEntry.Clone());
return Task.FromResult(wordSetEntry.Clone());
entry.Id = Guid.NewGuid().ToString();
_mergeBlacklist.Add(entry.Clone());
return Task.FromResult(entry.Clone());
}

public Task<bool> DeleteAllSets(string projectId)
public Task<bool> DeleteAll(string projectId)
{
_mergeBlacklist.Clear();
return Task.FromResult(true);
Expand All @@ -58,17 +61,17 @@ public Task<bool> Delete(string projectId, string entryId)
return Task.FromResult(_mergeBlacklist.Remove(foundMergeBlacklist));
}

public Task<ResultOfUpdate> Update(MergeWordSet wordSetEntry)
public Task<ResultOfUpdate> Update(MergeWordSet entry)
{
var foundEntry = _mergeBlacklist.Single(
e => e.ProjectId == wordSetEntry.ProjectId && e.Id == wordSetEntry.Id);
e => e.ProjectId == entry.ProjectId && e.Id == entry.Id);
var success = _mergeBlacklist.Remove(foundEntry);
if (!success)
{
return Task.FromResult(ResultOfUpdate.NotFound);
}

_mergeBlacklist.Add(wordSetEntry.Clone());
_mergeBlacklist.Add(entry.Clone());
return Task.FromResult(ResultOfUpdate.Updated);
}
}
Expand Down
33 changes: 18 additions & 15 deletions Backend.Tests/Mocks/MergeGraylistRepositoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ public MergeGraylistRepositoryMock()
_mergeGraylist = new List<MergeWordSet>();
}

public Task<List<MergeWordSet>> GetAllSets(string projectId, string? userId = null)
public Task<List<MergeWordSet>> GetAllEntries(string projectId)
{
var cloneList = _mergeGraylist.Select(e => e.Clone()).ToList();
var enumerable = userId is null ?
cloneList.Where(e => e.ProjectId == projectId) :
cloneList.Where(e => e.ProjectId == projectId && e.UserId == userId);
return Task.FromResult(enumerable.ToList());
var list = _mergeGraylist.Where(e => e.ProjectId == projectId).ToList();
return Task.FromResult(list.Select(e => e.Clone()).ToList());
}

public Task<MergeWordSet?> GetSet(string projectId, string entryId)
public Task<List<MergeWordSet>> GetAllEntries(string projectId, string userId)
{
var list = _mergeGraylist.Where(e => e.ProjectId == projectId && e.UserId == userId).ToList();
return Task.FromResult(list.Select(e => e.Clone()).ToList());
}

public Task<MergeWordSet?> GetEntry(string projectId, string entryId)
{
try
{
Expand All @@ -39,14 +42,14 @@ public Task<List<MergeWordSet>> GetAllSets(string projectId, string? userId = nu
}
}

public Task<MergeWordSet> Create(MergeWordSet wordSetEntry)
public Task<MergeWordSet> Create(MergeWordSet entry)
{
wordSetEntry.Id = Guid.NewGuid().ToString();
_mergeGraylist.Add(wordSetEntry.Clone());
return Task.FromResult(wordSetEntry.Clone());
entry.Id = Guid.NewGuid().ToString();
_mergeGraylist.Add(entry.Clone());
return Task.FromResult(entry.Clone());
}

public Task<bool> DeleteAllSets(string projectId)
public Task<bool> DeleteAll(string projectId)
{
_mergeGraylist.Clear();
return Task.FromResult(true);
Expand All @@ -58,17 +61,17 @@ public Task<bool> Delete(string projectId, string entryId)
return Task.FromResult(_mergeGraylist.Remove(foundMergeGraylist));
}

public Task<ResultOfUpdate> Update(MergeWordSet wordSetEntry)
public Task<ResultOfUpdate> Update(MergeWordSet entry)
{
var foundEntry = _mergeGraylist.Single(
e => e.ProjectId == wordSetEntry.ProjectId && e.Id == wordSetEntry.Id);
e => e.ProjectId == entry.ProjectId && e.Id == entry.Id);
var success = _mergeGraylist.Remove(foundEntry);
if (!success)
{
return Task.FromResult(ResultOfUpdate.NotFound);
}

_mergeGraylist.Add(wordSetEntry.Clone());
_mergeGraylist.Add(entry.Clone());
return Task.FromResult(ResultOfUpdate.Updated);
}
}
Expand Down
Loading

0 comments on commit 6c33f6b

Please sign in to comment.