Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Create unit tests for the YeSqlDictionary type #78

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Models/ModelTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public void Deconstruct(out string name, out string sqlStatement)
/// </summary>
/// <returns>A string that represents the current object of type <see cref="ModelTag" />.</returns>
public override string ToString()
=> $"-- name: {Name}\n{SqlStatement}\n";
=> $"-- name: {Name}{Environment.NewLine}{SqlStatement}{Environment.NewLine}";
}
170 changes: 170 additions & 0 deletions tests/YeSqlDictionaryTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>()
.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<TagNotFoundException>()
.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<ArgumentNullException>()
.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<ModelTag>
{
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<ModelTag> actual = sqlCollection.AsEnumerable();

// Assert
actual.Should().BeEquivalentTo(expected);
}
}