From fbb6780627bf4444b06d2c94568667248951a11d Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Thu, 28 Dec 2023 13:47:54 -0500 Subject: [PATCH 1/3] 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); + } +} From d731d8d1f160864a44ea4d8a54fb33f428c8222b Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Fri, 29 Dec 2023 15:29:54 -0500 Subject: [PATCH 2/3] refactor: Avoid harcoding the new line --- src/Models/ModelTag.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/ModelTag.cs b/src/Models/ModelTag.cs index 77995ba..8eb7204 100644 --- a/src/Models/ModelTag.cs +++ b/src/Models/ModelTag.cs @@ -31,5 +31,5 @@ public void Deconstruct(out string name, out string sqlStatement) /// /// A string that represents the current object of type . public override string ToString() - => $"-- name: {Name}\n{SqlStatement}\n"; + => $"-- name: {Name}{Environment.NewLine}{SqlStatement}{Environment.NewLine}"; } From b5ba72ae336a2cd7d5d154be1b6bf8ac5d1f1145 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Fri, 29 Dec 2023 18:37:56 -0500 Subject: [PATCH 3/3] refactor: Rename test method --- tests/YeSqlDictionaryTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/YeSqlDictionaryTests.cs b/tests/YeSqlDictionaryTests.cs index 6170e90..d446f2f 100644 --- a/tests/YeSqlDictionaryTests.cs +++ b/tests/YeSqlDictionaryTests.cs @@ -147,7 +147,7 @@ public void ToString_WhenThereAreNoSqlStatements_ShouldReturnsEmptyString() } [Test] - public void EnumeratorTest() + public void GetEnumerator_WhenThereAreSqlStatements_ShouldAllowIteratingOverThem() { // Arrange var expected = new List