Skip to content

Commit

Permalink
test: Create unit tests for the YeSqlDictionary type
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed Dec 28, 2023
1 parent e46c8e9 commit fbb6780
Showing 1 changed file with 170 additions and 0 deletions.
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 EnumeratorTest()
{
// 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);
}
}

0 comments on commit fbb6780

Please sign in to comment.