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}"; } diff --git a/tests/YeSqlDictionaryTests.cs b/tests/YeSqlDictionaryTests.cs new file mode 100644 index 0000000..d446f2f --- /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 GetEnumerator_WhenThereAreSqlStatements_ShouldAllowIteratingOverThem() + { + // 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); + } +}