Skip to content

Commit

Permalink
Use Constraint Model in Services tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Apoktieno committed Aug 11, 2023
1 parent bb7355a commit 2978aa6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
54 changes: 27 additions & 27 deletions Backend.Tests/Services/MergeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void MergeWordsOneChildTest()

// There should only be 1 word added and it should be identical to what we passed in
Assert.That(newWords, Has.Count.EqualTo(1));
Assert.That(newWords.First().ContentEquals(thisWord));
Assert.That(newWords.First().ContentEquals(thisWord), Is.True);

// Check that the only word in the frontier is the new word
var frontier = _wordRepo.GetFrontier(ProjId).Result;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void MergeWordsMultiChildTest()
{
var child = Util.RandomWord(ProjId);
var id = _wordRepo.Create(child).Result.Id;
Assert.IsNotNull(_wordRepo.GetWord(ProjId, id).Result);
Assert.That(_wordRepo.GetWord(ProjId, id).Result, Is.Not.Null);
mergeWords.Children.Add(new MergeSourceWord { SrcWordId = id });
}
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren));
Expand All @@ -105,7 +105,7 @@ public void MergeWordsMultiChildTest()
Assert.That(dbParent.History, Has.Count.EqualTo(numberOfChildren));

// Confirm that parent added to repo and children not in frontier.
Assert.IsNotNull(_wordRepo.GetWord(ProjId, dbParent.Id).Result);
Assert.That(_wordRepo.GetWord(ProjId, dbParent.Id).Result, Is.Not.Null);
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(1));
}

Expand All @@ -118,13 +118,13 @@ public void MergeWordsMultipleTest()
var newWords = _mergeService.Merge(ProjId, mergeWordsList).Result;

Assert.That(newWords, Has.Count.EqualTo(wordCount));
Assert.AreNotEqual(newWords.First().Id, newWords.Last().Id);
Assert.That(newWords.First().Id, Is.Not.EqualTo(newWords.Last().Id));

var frontier = _wordRepo.GetFrontier(ProjId).Result;
Assert.That(frontier, Has.Count.EqualTo(wordCount));
Assert.AreNotEqual(frontier.First().Id, frontier.Last().Id);
Assert.Contains(frontier.First(), newWords);
Assert.Contains(frontier.Last(), newWords);
Assert.That(frontier.First().Id, Is.Not.EqualTo(frontier.Last().Id));
Assert.That(newWords, Does.Contain(frontier.First()));
Assert.That(newWords, Does.Contain(frontier.Last()));
}

[Test]
Expand All @@ -146,19 +146,19 @@ public void UndoMergeOneChildTest()

// There should only be 1 word added and it should be identical to what we passed in
Assert.That(newWords, Has.Count.EqualTo(1));
Assert.That(newWords.First().ContentEquals(thisWord));
Assert.That(newWords.First().ContentEquals(thisWord), Is.True);

var childIds = mergeObject.Children.Select(word => word.SrcWordId).ToList();
var parentIds = new List<string> { newWords[0].Id };
var mergedWord = new MergeUndoIds(parentIds, childIds);
var undo = _mergeService.UndoMerge(ProjId, mergedWord).Result;
Assert.IsTrue(undo);
Assert.That(undo, Is.True);

var frontierWords = _wordRepo.GetFrontier(ProjId).Result;
var frontierWordIds = frontierWords.Select(word => word.Id).ToList();

Assert.That(frontierWords, Has.Count.EqualTo(1));
Assert.Contains(childIds[0], frontierWordIds);
Assert.That(frontierWordIds, Does.Contain(childIds[0]));
}

[Test]
Expand All @@ -171,7 +171,7 @@ public void UndoMergeMultiChildTest()
{
var child = Util.RandomWord(ProjId);
var id = _wordRepo.Create(child).Result.Id;
Assert.IsNotNull(_wordRepo.GetWord(ProjId, id).Result);
Assert.That(_wordRepo.GetWord(ProjId, id).Result, Is.Not.Null);
mergeWords.Children.Add(new MergeSourceWord { SrcWordId = id });
}
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren));
Expand All @@ -185,15 +185,15 @@ public void UndoMergeMultiChildTest()
var parentIds = new List<string> { newWords[0].Id };
var mergedWord = new MergeUndoIds(parentIds, childIds);
var undo = _mergeService.UndoMerge(ProjId, mergedWord).Result;
Assert.IsTrue(undo);
Assert.That(undo, Is.True);

var frontierWords = _wordRepo.GetFrontier(ProjId).Result;
var frontierWordIds = frontierWords.Select(word => word.Id).ToList();

Assert.That(frontierWords, Has.Count.EqualTo(numberOfChildren));
Assert.Contains(childIds[0], frontierWordIds);
Assert.Contains(childIds[1], frontierWordIds);
Assert.Contains(childIds[2], frontierWordIds);
Assert.That(frontierWordIds, Does.Contain(childIds[0]));
Assert.That(frontierWordIds, Does.Contain(childIds[1]));
Assert.That(frontierWordIds, Does.Contain(childIds[2]));
}

[Test]
Expand All @@ -205,7 +205,7 @@ public void AddMergeToBlacklistTest()
var blacklist = _mergeBlacklistRepo.GetAllEntries(ProjId).Result;
Assert.That(blacklist, Has.Count.EqualTo(1));
var expectedEntry = new MergeBlacklistEntry { ProjectId = ProjId, UserId = UserId, WordIds = wordIds };
Assert.That(expectedEntry.ContentEquals(blacklist.First()));
Assert.That(expectedEntry.ContentEquals(blacklist.First()), Is.True);
}

[Test]
Expand All @@ -214,10 +214,10 @@ public void AddMergeToBlacklistErrorTest()
_ = _mergeBlacklistRepo.DeleteAllEntries(ProjId).Result;
var wordIds0 = new List<string>();
var wordIds1 = new List<string> { "1" };
Assert.ThrowsAsync<MergeService.InvalidBlacklistEntryException>(
async () => { await _mergeService.AddToMergeBlacklist(ProjId, UserId, wordIds0); });
Assert.ThrowsAsync<MergeService.InvalidBlacklistEntryException>(
async () => { await _mergeService.AddToMergeBlacklist(ProjId, UserId, wordIds1); });
Assert.That(
async () => { await _mergeService.AddToMergeBlacklist(ProjId, UserId, wordIds0); }, Throws.TypeOf<MergeService.InvalidBlacklistEntryException>());
Assert.That(
async () => { await _mergeService.AddToMergeBlacklist(ProjId, UserId, wordIds1); }, Throws.TypeOf<MergeService.InvalidBlacklistEntryException>());
}

[Test]
Expand All @@ -227,9 +227,9 @@ public void IsInMergeBlacklistTest()
var wordIds = new List<string> { "1", "2", "3" };
var subWordIds = new List<string> { "3", "2" };

Assert.IsFalse(_mergeService.IsInMergeBlacklist(ProjId, subWordIds).Result);
Assert.That(_mergeService.IsInMergeBlacklist(ProjId, subWordIds).Result, Is.False);
_ = _mergeService.AddToMergeBlacklist(ProjId, UserId, wordIds).Result;
Assert.IsTrue(_mergeService.IsInMergeBlacklist(ProjId, subWordIds).Result);
Assert.That(_mergeService.IsInMergeBlacklist(ProjId, subWordIds).Result, Is.True);
}

[Test]
Expand All @@ -238,10 +238,10 @@ public void IsInMergeBlacklistErrorTest()
_ = _mergeBlacklistRepo.DeleteAllEntries(ProjId).Result;
var wordIds0 = new List<string>();
var wordIds1 = new List<string> { "1" };
Assert.ThrowsAsync<MergeService.InvalidBlacklistEntryException>(
async () => { await _mergeService.IsInMergeBlacklist(ProjId, wordIds0); });
Assert.ThrowsAsync<MergeService.InvalidBlacklistEntryException>(
async () => { await _mergeService.IsInMergeBlacklist(ProjId, wordIds1); });
Assert.That(
async () => { await _mergeService.IsInMergeBlacklist(ProjId, wordIds0); }, Throws.TypeOf<MergeService.InvalidBlacklistEntryException>());
Assert.That(
async () => { await _mergeService.IsInMergeBlacklist(ProjId, wordIds1); }, Throws.TypeOf<MergeService.InvalidBlacklistEntryException>());
}

[Test]
Expand Down Expand Up @@ -284,7 +284,7 @@ public void UpdateMergeBlacklistTest()
// The only blacklistEntry with at least two ids in the frontier is A.
var newBlacklist = _mergeBlacklistRepo.GetAllEntries(ProjId).Result;
Assert.That(newBlacklist, Has.Count.EqualTo(1));
Assert.AreEqual(newBlacklist.First().WordIds, new List<string> { "2", "3" });
Assert.That(newBlacklist.First().WordIds, Is.EqualTo(new List<string> { "2", "3" }));
}
}
}
12 changes: 6 additions & 6 deletions Backend.Tests/Services/PasswordResetServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void CreateRequest()
_userRepo.Create(user);

var res = _passwordResetService.CreatePasswordReset(Email).Result;
Assert.Contains(res, _passwordResets.GetResets());
Assert.That(_passwordResets.GetResets(), Does.Contain(res));
}

[Test]
Expand All @@ -41,8 +41,8 @@ public void ResetSuccess()
_userRepo.Create(user);

var request = _passwordResetService.CreatePasswordReset(Email).Result;
Assert.IsTrue(_passwordResetService.ResetPassword(request.Token, Password).Result);
Assert.IsEmpty(_passwordResets.GetResets());
Assert.That(_passwordResetService.ResetPassword(request.Token, Password).Result, Is.True);
Assert.That(_passwordResets.GetResets(), Is.Empty);
}

[Test]
Expand All @@ -54,7 +54,7 @@ public void ResetExpired()
var request = _passwordResetService.CreatePasswordReset(Email).Result;
request.ExpireTime = DateTime.Now.AddMinutes(-1);

Assert.IsFalse(_passwordResetService.ResetPassword(request.Token, Password).Result);
Assert.That(_passwordResetService.ResetPassword(request.Token, Password).Result, Is.False);
}

[Test]
Expand All @@ -64,9 +64,9 @@ public void ResetBadToken()
_userRepo.Create(user);

var request = _passwordResetService.CreatePasswordReset(Email).Result;
Assert.That(request.Email == Email);
Assert.That(request.Email == Email, Is.True);
var task = _passwordResetService.ResetPassword("NotARealToken", Password);
Assert.IsFalse(task.Result);
Assert.That(task.Result, Is.False);
}
}
}
2 changes: 1 addition & 1 deletion Backend.Tests/Services/PermissionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void MakeJwtTestReturnsUser()
{
var user = _userRepo.Create(new User()).Result!;
var result = _permService.MakeJwt(user).Result;
Assert.IsInstanceOf<User>(result);
Assert.That(result, Is.InstanceOf<User>());
}

[Test]
Expand Down

0 comments on commit 2978aa6

Please sign in to comment.