From fbb6780627bf4444b06d2c94568667248951a11d Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Thu, 28 Dec 2023 13:47:54 -0500 Subject: [PATCH] test: Create unit tests for the YeSqlDictionary type --- tests/YeSqlDictionaryTests.cs | 170 ++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 tests/YeSqlDictionaryTests.cs diff --git a/tests/YeSqlDictionaryTests.cs b/tests/YeSqlDictionaryTests.cs new file mode 100644 index 0000000..6170e90 --- /dev/null +++ b/tests/YeSqlDictionaryTests.cs @@ -0,0 +1,170 @@ +namespace YeSql.Net.Tests; + +public class YeSqlDictionaryTests +{ + [Test] + public void Indexer_WhenTagNameIsNull_ShouldThrowArgumentNullException() + { + // Arrange + var sqlCollection = new YeSqlDictionary(); + var tagName = default(string); + + // Act + Action action = () => + { + var value = sqlCollection[tagName]; + }; + + // Assert + action.Should() + .Throw() + .WithParameterName(nameof(tagName)); + } + + [Test] + public void Indexer_WhenTagNameIsNotFound_ShouldThrowTagNotFoundException() + { + // Arrange + var sqlCollection = new YeSqlDictionary(); + var tagName = "Test"; + var expectedMessage = string.Format(ExceptionMessages.TagNotFound, tagName); + + // Act + Action action = () => + { + var value = sqlCollection[tagName]; + }; + + // Assert + action.Should() + .Throw() + .WithMessage(expectedMessage); + } + + [Test] + public void Indexer_WhenTagNameIsFound_ShouldReturnsSqlStatement() + { + // Arrange + var sqlCollection = new YeSqlDictionary(); + var tagName = "GetUsers"; + var expectedSql = "SELECT* FROM users;"; + sqlCollection[tagName] = expectedSql; + + // Act + string actual = sqlCollection[tagName]; + + // Assert + actual.Should().Be(expectedSql); + } + + [Test] + public void TryGetStatement_WhenTagNameIsNull_ShouldThrowArgumentNullException() + { + // Arrange + var sqlCollection = new YeSqlDictionary(); + var tagName = default(string); + + // Act + Action action = () => + { + var value = sqlCollection.TryGetStatement(tagName, out _); + }; + + // Assert + action.Should() + .Throw() + .WithParameterName(nameof(tagName)); + } + + [Test] + public void TryGetStatement_WhenTagNameIsNotFound_ShouldReturnsFalse() + { + // Arrange + var sqlCollection = new YeSqlDictionary(); + var tagName = "Test"; + + // Act + bool actual = sqlCollection.TryGetStatement(tagName, out _); + + // Assert + actual.Should().BeFalse(); + } + + [Test] + public void TryGetStatement_WhenTagNameIsFound_ShouldReturnsTrue() + { + // Arrange + var sqlCollection = new YeSqlDictionary(); + var tagName = "GetUsers"; + var expectedSql = "SELECT* FROM users;"; + sqlCollection[tagName] = expectedSql; + + // Act + bool actual = sqlCollection.TryGetStatement(tagName, out string currentSql); + + // Asserts + actual.Should().BeTrue(); + currentSql.Should().Be(expectedSql); + } + + [Test] + public void ToString_WhenConvertingYeSqlDictionaryToString_ShouldReturnsString() + { + // Arrange + var expected = + """ + -- name: GetUsers + SELECT* FROM users; + -- name: GetProducts + SELECT* FROM products; + -- name: GetRoles + SELECT* FROM roles; + """; + expected += Environment.NewLine; + var sqlCollection = new YeSqlDictionary(); + sqlCollection["GetUsers"] = "SELECT* FROM users;"; + sqlCollection["GetProducts"] = "SELECT* FROM products;"; + sqlCollection["GetRoles"] = "SELECT* FROM roles;"; + + // Act + string actual = sqlCollection.ToString(); + + // Assert + actual.Should().Be(expected); + } + + [Test] + public void ToString_WhenThereAreNoSqlStatements_ShouldReturnsEmptyString() + { + // Arrange + var sqlCollection = new YeSqlDictionary(); + + // Act + string actual = sqlCollection.ToString(); + + // Assert + actual.Should().BeEmpty(); + } + + [Test] + public void EnumeratorTest() + { + // Arrange + var expected = new List + { + new() { Name = "GetUsers", SqlStatement = "SELECT* FROM users;" }, + new() { Name = "GetProducts", SqlStatement = "SELECT* FROM products;" }, + new() { Name = "GetRoles", SqlStatement = "SELECT* FROM roles;" } + }; + var sqlCollection = new YeSqlDictionary(); + sqlCollection["GetUsers"] = "SELECT* FROM users;"; + sqlCollection["GetProducts"] = "SELECT* FROM products;"; + sqlCollection["GetRoles"] = "SELECT* FROM roles;"; + + // Act + IEnumerable actual = sqlCollection.AsEnumerable(); + + // Assert + actual.Should().BeEquivalentTo(expected); + } +}