Skip to content

Commit

Permalink
[Backend.Tests] Use Constraint Model in Helper, Models, and Services …
Browse files Browse the repository at this point in the history
…tests (#2466)
  • Loading branch information
Apoktieno authored Aug 15, 2023
1 parent a7e7fbf commit 1a183fe
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 279 deletions.
8 changes: 4 additions & 4 deletions Backend.Tests/Helper/LevenshteinDistanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ public void GetDistanceDifferentWordTest()
[Test]
public void TestConstructorNegativeInputs()
{
Assert.Throws<ArgumentException>(() =>
Assert.That(() =>
{
var _ = new LevenshteinDistance(-1);
});
}, Throws.TypeOf<ArgumentException>());
}

[Test]
public void TestConstructorSmallSubstitute()
{
Assert.Throws<ArgumentException>(() =>
Assert.That(() =>
{
var _ = new LevenshteinDistance(1, 1, 99);
});
}, Throws.TypeOf<ArgumentException>());
}
}
}
8 changes: 4 additions & 4 deletions Backend.Tests/Helper/PasswordHashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class PasswordHashTests
public void HashPasswordValidRoundtrip()
{
var hash = HashPassword(Password);
Assert.AreNotEqual(Password, hash);
Assert.That(ValidatePassword(hash, Password));
Assert.That(hash, Is.Not.EqualTo(Password));
Assert.That(ValidatePassword(hash, Password), Is.True);
}

[Test]
Expand All @@ -21,15 +21,15 @@ public void HashPasswordInvalidHashByteRoundtrip()
var hash = HashPassword(Password);
// Change a single byte of the hash and validate that the hash fails.
hash[0] ^= 0xff;
Assert.IsFalse(ValidatePassword(hash, Password));
Assert.That(ValidatePassword(hash, Password), Is.False);
}

[Test]
public void HashPasswordInvalidPasswordCharacterRoundtrip()
{
var hash = HashPassword(Password);
var mutatedPassword = $"Z{Password}";
Assert.IsFalse(ValidatePassword(hash, mutatedPassword));
Assert.That(ValidatePassword(hash, mutatedPassword), Is.False);
}
}
}
4 changes: 2 additions & 2 deletions Backend.Tests/Helper/TimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public void TestToUtcIso8601()
public void TestUtcNowIso8601()
{
var time = UtcNowIso8601();
Assert.That(time.Contains('T'));
Assert.That(time.EndsWith('Z'));
Assert.That(time.Contains('T'), Is.True);
Assert.That(time.EndsWith('Z'), Is.True);
Assert.That(DateTime.Now, Is.EqualTo(DateTime.Parse(time)).Within(TimeSpan.FromSeconds(10)));
}
}
Expand Down
20 changes: 10 additions & 10 deletions Backend.Tests/Models/BannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ public class SiteBannerTests
[Test]
public void TestEquals()
{
Assert.That(_siteBanner.Equals(new SiteBanner { Type = Type, Text = Text }));
Assert.That(_siteBanner.Equals(new SiteBanner { Type = Type, Text = Text }), Is.True);
}

[Test]
public void TestNotEquals()
{
Assert.IsFalse(_siteBanner.Equals(
new SiteBanner { Type = BannerType.Announcement, Text = Text }));
Assert.IsFalse(_siteBanner.Equals(
new SiteBanner { Type = Type, Text = "Different Text" }));
Assert.That(_siteBanner.Equals(
new SiteBanner { Type = BannerType.Announcement, Text = Text }), Is.False);
Assert.That(_siteBanner.Equals(
new SiteBanner { Type = Type, Text = "Different Text" }), Is.False);
}

[Test]
public void TestEqualsNull()
{
Assert.IsFalse(_siteBanner.Equals(null));
Assert.That(_siteBanner.Equals(null), Is.False);
}

[Test]
public void TestHashCode()
{
Assert.AreNotEqual(
Assert.That(
_siteBanner.GetHashCode(),
new SiteBanner { Type = BannerType.Announcement, Text = Text }.GetHashCode());
Assert.AreNotEqual(
Is.Not.EqualTo(new SiteBanner { Type = BannerType.Announcement, Text = Text }.GetHashCode()));
Assert.That(
_siteBanner.GetHashCode(),
new SiteBanner { Type = Type, Text = "Different Text" }.GetHashCode());
Is.Not.EqualTo(new SiteBanner { Type = Type, Text = "Different Text" }.GetHashCode()));
}
}
}
36 changes: 18 additions & 18 deletions Backend.Tests/Models/EmailInviteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ public class EmailInviteTests
public void TestCloneEquals()
{
var field = new EmailInvite { Email = Email1, Token = Token1, Role = Role1, ExpireTime = _expireTime1 };
Assert.AreEqual(field, field.Clone());
Assert.That(field, Is.EqualTo(field.Clone()));
}

[Test]
public void TestEqualsNull()
{
Assert.AreNotEqual(new EmailInvite(), null);
Assert.That(new EmailInvite().Equals(null), Is.False);
}

[Test]
public void TestNotEquals()
{
var invite = new EmailInvite { Email = Email1, Token = Token1, Role = Role1, ExpireTime = _expireTime1 };
Assert.AreNotEqual(invite,
new EmailInvite { Email = Email2, Token = Token1, Role = Role1, ExpireTime = _expireTime1 });
Assert.AreNotEqual(invite,
new EmailInvite { Email = Email1, Token = Token2, Role = Role1, ExpireTime = _expireTime1 });
Assert.AreNotEqual(invite,
new EmailInvite { Email = Email1, Token = Token1, Role = Role2, ExpireTime = _expireTime1 });
Assert.AreNotEqual(invite,
new EmailInvite { Email = Email1, Token = Token1, Role = Role1, ExpireTime = _expireTime2 });
Assert.That(invite,
Is.Not.EqualTo(new EmailInvite { Email = Email2, Token = Token1, Role = Role1, ExpireTime = _expireTime1 }));
Assert.That(invite,
Is.Not.EqualTo(new EmailInvite { Email = Email1, Token = Token2, Role = Role1, ExpireTime = _expireTime1 }));
Assert.That(invite,
Is.Not.EqualTo(new EmailInvite { Email = Email1, Token = Token1, Role = Role2, ExpireTime = _expireTime1 }));
Assert.That(invite,
Is.Not.EqualTo(new EmailInvite { Email = Email1, Token = Token1, Role = Role1, ExpireTime = _expireTime2 }));
}

[Test]
Expand All @@ -56,21 +56,21 @@ public void TestToken()
[Test]
public void TestHashCode()
{
Assert.AreNotEqual(
Assert.That(
new EmailInvite { Email = Email1 }.GetHashCode(),
new EmailInvite { Email = Email2 }.GetHashCode()
Is.Not.EqualTo(new EmailInvite { Email = Email2 }.GetHashCode())
);
Assert.AreNotEqual(
Assert.That(
new EmailInvite { Token = Token1 }.GetHashCode(),
new EmailInvite { Token = Token2 }.GetHashCode()
Is.Not.EqualTo(new EmailInvite { Token = Token2 }.GetHashCode())
);
Assert.AreNotEqual(
Assert.That(
new EmailInvite { Role = Role1 }.GetHashCode(),
new EmailInvite { Role = Role2 }.GetHashCode()
Is.Not.EqualTo(new EmailInvite { Role = Role2 }.GetHashCode())
);
Assert.AreNotEqual(
Assert.That(
new EmailInvite { ExpireTime = _expireTime1 }.GetHashCode(),
new EmailInvite { ExpireTime = _expireTime2 }.GetHashCode()
Is.Not.EqualTo(new EmailInvite { ExpireTime = _expireTime2 }.GetHashCode())
);
}
}
Expand Down
16 changes: 8 additions & 8 deletions Backend.Tests/Models/MergeBlacklistEntryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void TestClone()
WordIds = _wordIds
};
var entryB = entryA.Clone();
Assert.That(entryA.Equals(entryB));
Assert.That(entryA.Equals(entryB), Is.True);
}

[Test]
Expand All @@ -43,7 +43,7 @@ public void TestEquals()
UserId = UserId,
WordIds = _wordIdsReversed
};
Assert.That(entryA.Equals(entryB));
Assert.That(entryA.Equals(entryB), Is.True);
}

[Test]
Expand All @@ -52,26 +52,26 @@ public void TestEqualsFalse()
var entryA = new MergeBlacklistEntry();
var entryB = new MergeBlacklistEntry();
entryA.Id = EntryId;
Assert.IsFalse(entryA.Equals(entryB));
Assert.That(entryA.Equals(entryB), Is.False);

entryB = entryA.Clone();
entryA.ProjectId = ProjId;
Assert.IsFalse(entryA.Equals(entryB));
Assert.That(entryA.Equals(entryB), Is.False);

entryB = entryA.Clone();
entryA.UserId = UserId;
Assert.IsFalse(entryA.Equals(entryB));
Assert.That(entryA.Equals(entryB), Is.False);

entryB = entryA.Clone();
entryA.WordIds = _wordIds;
Assert.IsFalse(entryA.Equals(entryB));
Assert.That(entryA.Equals(entryB), Is.False);
}

[Test]
public void TestEqualsNull()
{
var edit = new MergeBlacklistEntry { ProjectId = ProjId };
Assert.IsFalse(edit.Equals(null));
Assert.That(edit.Equals(null), Is.False);
}

[Test]
Expand All @@ -91,7 +91,7 @@ public void TestHashCode()
UserId = UserId,
WordIds = _wordIdsReversed
};
Assert.AreNotEqual(entryA.GetHashCode(), entryB.GetHashCode());
Assert.That(entryA.GetHashCode(), Is.Not.EqualTo(entryB.GetHashCode()));
}
}
}
24 changes: 12 additions & 12 deletions Backend.Tests/Models/MergeWordsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public void TestEquals()
var mergeEmpty = new MergeUndoIds();
var mergeNonEmpty = new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds1 };

Assert.That(mergeEmpty.Equals(new MergeUndoIds()));
Assert.That(mergeNonEmpty.Equals(new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds1 }));
Assert.That(mergeEmpty.Equals(new MergeUndoIds()), Is.True);
Assert.That(mergeNonEmpty.Equals(new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds1 }), Is.True);
}

[Test]
public void TestCloneEquals()
{
var merge = new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds1 };
Assert.That(merge.Equals(merge.Clone()));
Assert.That(merge.Equals(merge.Clone()), Is.True);
}

[Test]
Expand All @@ -40,15 +40,15 @@ public void TestNotEquals()
var mergeNonEmpty4 = new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds3 };
var mergeNonEmpty5 = new MergeUndoIds { ParentIds = _parentIds3, ChildIds = _childIds1 };

Assert.IsFalse(mergeEmpty.Equals(mergeNonEmpty1));
Assert.That(mergeEmpty.Equals(mergeNonEmpty1), Is.False);
// Test one added child.
Assert.IsFalse(mergeNonEmpty1.Equals(mergeNonEmpty2));
Assert.That(mergeNonEmpty1.Equals(mergeNonEmpty2), Is.False);
// Test one added parent.
Assert.IsFalse(mergeNonEmpty1.Equals(mergeNonEmpty3));
Assert.That(mergeNonEmpty1.Equals(mergeNonEmpty3), Is.False);
// Test one different child.
Assert.IsFalse(mergeNonEmpty1.Equals(mergeNonEmpty4));
Assert.That(mergeNonEmpty1.Equals(mergeNonEmpty4), Is.False);
// Test one different parent.
Assert.IsFalse(mergeNonEmpty1.Equals(mergeNonEmpty5));
Assert.That(mergeNonEmpty1.Equals(mergeNonEmpty5), Is.False);
}

[Test]
Expand All @@ -57,16 +57,16 @@ public void TestEqualsNull()
var mergeEmpty = new MergeUndoIds();
var mergeNonEmpty = new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds1 };

Assert.IsFalse(mergeEmpty.Equals(null));
Assert.IsFalse(mergeNonEmpty.Equals(null));
Assert.That(mergeEmpty.Equals(null), Is.False);
Assert.That(mergeNonEmpty.Equals(null), Is.False);
}

[Test]
public void TestHashCode()
{
Assert.AreEqual(
Assert.That(
new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds1 }.GetHashCode(),
new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds1 }.GetHashCode());
Is.EqualTo(new MergeUndoIds { ParentIds = _parentIds1, ChildIds = _childIds1 }.GetHashCode()));
}
}
}
Loading

0 comments on commit 1a183fe

Please sign in to comment.