diff --git a/tests/Loader/YeSqlLoaderTests.Directories.cs b/tests/Loader/YeSqlLoaderTests.Directories.cs new file mode 100644 index 0000000..c2596ba --- /dev/null +++ b/tests/Loader/YeSqlLoaderTests.Directories.cs @@ -0,0 +1,169 @@ +namespace YeSql.Net.Tests.Loader; + +public partial class YeSqlLoaderTests +{ + [Test] + public void LoadFromDirectories_WhenErrorsAreFound_ShouldThrowAggregateException() + { + // Arrange + var loader = new YeSqlLoader(); + var directories = new[] + { + "error", + "directory_not_found", + "env", + }; + var loaderErrors = new[] + { + string.Format(ExceptionMessages.DirectoryNotFound, "directory_not_found"), + string.Format(ExceptionMessages.NoneFileFoundInSpecifiedDirectory, "env") + }; + var parserErrors = new[] + { + $"errors.sql:(line 2, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT * FROM users;")}", + $"errors.sql:(line 9, col 9): error: {ExceptionMessages.TagIsEmptyOrWhitespace}", + $"errors.sql:(line 10, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT name FROM roles;")}", + $"errors.sql:(line 12, col 9): error: {string.Format(ExceptionMessages.DuplicateTagName, "GetUsers")}", + $"errors.sql:(line 15, col 9): error: {ExceptionMessages.TagIsEmptyOrWhitespace}", + $"errors.sql:(line 16, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT * FROM roles;")}" + }; + var expectedErrors = new[] + { + string.Join(Environment.NewLine, loaderErrors), + string.Join(Environment.NewLine, parserErrors) + }; + + // Act + Action action = () => loader.LoadFromDirectories(directories); + + // Assert + action.Should() + .Throw() + .Which + .InnerExceptions + .Select(innerException => innerException.Message) + .Should() + .BeEquivalentTo(expectedErrors); + } + + [Test] + public void LoadFromDirectories_WhenDirectoriesPathIsNull_ShouldThrowArgumentNullException() + { + // Arrange + var loader = new YeSqlLoader(); + + // Act + Action action = () => loader.LoadFromDirectories(null); + + // Assert + action.Should().Throw(); + } + + [TestCase("")] + [TestCase(" ")] + [TestCase(" ")] + [TestCase("data/", null)] + [TestCase("sql/", "non/", " ", "", null, "tests/")] + public void LoadFromDirectories_WhenCollectionHasNullValueOrOnlyWhitespace_ShouldThrowArgumentException(params string[] directories) + { + // Arrange + var loader = new YeSqlLoader(); + var expectedMessage = string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(directories)); + + // Act + Action action = () => loader.LoadFromDirectories(directories); + + // Assert + action.Should() + .Throw() + .WithMessage(expectedMessage); + } + + [Test] + public void LoadFromDirectories_WhenNotExistsSqlFilesInDirectory_ShouldThrowAggregateException() + { + // Arrange + var loader = new YeSqlLoader(); + var directory = "env"; + var expectedMessage = string.Format(ExceptionMessages.NoneFileFoundInSpecifiedDirectory, directory); + + // Act + Action action = () => loader.LoadFromDirectories(directory); + + // Assert + action.Should() + .Throw() + .WithInnerException() + .WithMessage(expectedMessage); + } + + [Test] + public void LoadFromDirectories_WhenSqlFilesExistsInDirectory_ShouldReturnsYeSqlCollection() + { + // Arrange + var loader = new YeSqlLoader(); + var directory = "./sql"; + var expectedCollection = new Dictionary + { + { "GetUsers", "SELECT* FROM [user];" } + }; + + // Act + var sqlStatements = loader.LoadFromDirectories(directory); + + // Assert + sqlStatements.Should().BeEquivalentTo(expectedCollection); + } + + [Test] + public void LoadFromDirectories_WhenPathsAreAbsolute_ShouldBeAbleToLoadSqlFiles() + { + // Arrange + var loader = new YeSqlLoader(); + var directory = Path.Combine(AppContext.BaseDirectory, "sql"); + var expectedCollection = new Dictionary + { + { "GetUsers", "SELECT* FROM [user];" } + }; + + // Act + var sqlStatements = loader.LoadFromDirectories(directory); + + // Assert + sqlStatements.Should().BeEquivalentTo(expectedCollection); + } + + [Test] + public void LoadFromDirectories_WhenDirectoryNotExists_ShouldThrowAggregateException() + { + // Arrange + var loader = new YeSqlLoader(); + var directory = "directory_not_found"; + var expectedMessage = string.Format(ExceptionMessages.DirectoryNotFound, directory); + + // Act + Action action = () => loader.LoadFromDirectories(directory); + + // Asserts + action.Should() + .Throw() + .WithInnerException() + .WithMessage(expectedMessage); + } + + [Test] + public void LoadFromDirectories_WhenParamsListIsZero_ShouldThrowArgumentException() + { + // Arrange + var loader = new YeSqlLoader(); + var expectedMessage = ExceptionMessages.LengthOfParamsListIsZero; + + // Act + Action action = () => loader.LoadFromDirectories(); + + // Assert + action.Should() + .Throw() + .WithMessage(expectedMessage); + } +} diff --git a/tests/Loader/YeSqlLoaderTests.Files.cs b/tests/Loader/YeSqlLoaderTests.Files.cs new file mode 100644 index 0000000..4c25ed2 --- /dev/null +++ b/tests/Loader/YeSqlLoaderTests.Files.cs @@ -0,0 +1,172 @@ +namespace YeSql.Net.Tests.Loader; + +public partial class YeSqlLoaderTests +{ + [Test] + public void LoadFromFiles_WhenErrorsAreFound_ShouldThrowAggregateException() + { + // Arrange + var loader = new YeSqlLoader(); + var files = new[] + { + "errors/errors_1.sql", + "errors/errors_2.sql", + "file_not_found.sql", + "file_without_extension" + }; + var loaderErrors = new[] + { + string.Format(ExceptionMessages.FileNotFound, "file_not_found.sql"), + string.Format(ExceptionMessages.FileHasNotSqlExtension, "file_without_extension") + }; + var parserErrors = new[] + { + $"errors_1.sql:(line 2, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT * FROM users;")}", + $"errors_1.sql:(line 9, col 9): error: {ExceptionMessages.TagIsEmptyOrWhitespace}", + $"errors_1.sql:(line 10, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT name FROM roles;")}", + $"errors_2.sql:(line 2, col 9): error: {string.Format(ExceptionMessages.DuplicateTagName, "GetUsers")}", + $"errors_2.sql:(line 5, col 9): error: {ExceptionMessages.TagIsEmptyOrWhitespace}", + $"errors_2.sql:(line 6, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT * FROM roles;")}" + }; + var expectedErrors = new[] + { + string.Join(Environment.NewLine, loaderErrors), + string.Join(Environment.NewLine, parserErrors) + }; + + // Act + Action action = () => loader.LoadFromFiles(files); + + // Assert + action.Should() + .Throw() + .Which + .InnerExceptions + .Select(innerException => innerException.Message) + .Should() + .BeEquivalentTo(expectedErrors); + } + + [Test] + public void LoadFromFiles_WhenSqlFilesIsNull_ShouldThrowArgumentNullException() + { + // Arrange + var loader = new YeSqlLoader(); + + // Act + Action action = () => loader.LoadFromFiles(null); + + // Assert + action.Should().Throw(); + } + + [Test] + public void LoadFromFiles_WhenSqlFilesExists_ShouldReturnsYeSqlCollection() + { + // Arrange + var loader = new YeSqlLoader(); + var file = "./sql/users.sql"; + var expectedCollection = new Dictionary + { + { "GetUsers", "SELECT* FROM [user];" } + }; + + // Act + var sqlStatements = loader.LoadFromFiles(file); + + // Assert + sqlStatements.Should().BeEquivalentTo(expectedCollection); + } + + [Test] + public void LoadFromFiles_WhenPathsAreAbsolute_ShouldBeAbleToLoadSqlFiles() + { + // Arrange + var loader = new YeSqlLoader(); + var file = Path.Combine(AppContext.BaseDirectory, "sql/users.sql"); + var expectedCollection = new Dictionary + { + { "GetUsers", "SELECT* FROM [user];" } + }; + + // Act + var sqlStatements = loader.LoadFromFiles(file); + + // Assert + sqlStatements.Should().BeEquivalentTo(expectedCollection); + } + + [TestCase("test.txt")] + [TestCase("test.png")] + [TestCase("test")] + [TestCase("test/")] + public void LoadFromFiles_WhenFileHasNotSqlExtension_ShouldThrowAggregateException(string file) + { + // Arrange + var loader = new YeSqlLoader(); + var expectedMessage = string.Format(ExceptionMessages.FileHasNotSqlExtension, file); + + // Act + Action action = () => loader.LoadFromFiles(file); + + // Assert + action.Should() + .Throw() + .WithInnerException() + .WithMessage(expectedMessage); + } + + [Test] + public void LoadFromFiles_WhenSqlFileNotExists_ShouldThrowAggregateException() + { + // Arrange + var loader = new YeSqlLoader(); + var file = "file_not_found.sql"; + var expectedMessage = string.Format(ExceptionMessages.FileNotFound, file); + + // Act + Action action = () => loader.LoadFromFiles(file); + + // Assert + action.Should() + .Throw() + .WithInnerException() + .WithMessage(expectedMessage); + } + + [TestCase("")] + [TestCase(" ")] + [TestCase(" ")] + [TestCase("data.sql", null)] + [TestCase("data.sql", "test.sql", " ", "", null, "hola.sql")] + public void LoadFromFiles_WhenCollectionHasNullValueOrOnlyWhitespace_ShouldThrowArgumentException(params string[] sqlFiles) + { + // Arrange + var loader = new YeSqlLoader(); + var expectedMessage = string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(sqlFiles)); + + // Act + Action action = () => loader.LoadFromFiles(sqlFiles); + + // Assert + action.Should() + .Throw() + .WithMessage(expectedMessage); + } + + [Test] + public void LoadFromFiles_WhenParamsListIsZero_ShouldThrowArgumentException() + { + // Arrange + var loader = new YeSqlLoader(); + var expectedMessage = ExceptionMessages.LengthOfParamsListIsZero; + + // Act + Action action = () => loader.LoadFromFiles(); + + // Assert + action.Should() + .Throw() + .WithMessage(expectedMessage); + } +} diff --git a/tests/Loader/YeSqlLoaderTests.cs b/tests/Loader/YeSqlLoaderTests.cs index 6daa6a2..28b2dd7 100644 --- a/tests/Loader/YeSqlLoaderTests.cs +++ b/tests/Loader/YeSqlLoaderTests.cs @@ -1,6 +1,6 @@ namespace YeSql.Net.Tests.Loader; -public class YeSqlLoaderTests +public partial class YeSqlLoaderTests { [Test] public void Load_WhenSqlFilesExistInDefaultDirectoryCalledYesql_ShouldReturnsYeSqlCollection() @@ -18,337 +18,4 @@ public void Load_WhenSqlFilesExistInDefaultDirectoryCalledYesql_ShouldReturnsYeS // Assert sqlStatements.Should().BeEquivalentTo(expectedCollection); } - - [Test] - public void LoadFromFiles_WhenErrorsAreFound_ShouldThrowAggregateException() - { - // Arrange - var loader = new YeSqlLoader(); - var files = new[] - { - "errors/errors_1.sql", - "errors/errors_2.sql", - "file_not_found.sql", - "file_without_extension" - }; - var loaderErrors = new[] - { - string.Format(ExceptionMessages.FileNotFound, "file_not_found.sql"), - string.Format(ExceptionMessages.FileHasNotSqlExtension, "file_without_extension") - }; - var parserErrors = new[] - { - $"errors_1.sql:(line 2, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT * FROM users;")}", - $"errors_1.sql:(line 9, col 9): error: {ExceptionMessages.TagIsEmptyOrWhitespace}", - $"errors_1.sql:(line 10, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT name FROM roles;")}", - $"errors_2.sql:(line 2, col 9): error: {string.Format(ExceptionMessages.DuplicateTagName, "GetUsers")}", - $"errors_2.sql:(line 5, col 9): error: {ExceptionMessages.TagIsEmptyOrWhitespace}", - $"errors_2.sql:(line 6, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT * FROM roles;")}" - }; - var expectedErrors = new[] - { - string.Join(Environment.NewLine, loaderErrors), - string.Join(Environment.NewLine, parserErrors) - }; - - // Act - Action action = () => loader.LoadFromFiles(files); - - // Assert - action.Should() - .Throw() - .Which - .InnerExceptions - .Select(innerException => innerException.Message) - .Should() - .BeEquivalentTo(expectedErrors); - } - - [Test] - public void LoadFromFiles_WhenSqlFilesIsNull_ShouldThrowArgumentNullException() - { - // Arrange - var loader = new YeSqlLoader(); - - // Act - Action action = () => loader.LoadFromFiles(null); - - // Assert - action.Should().Throw(); - } - - [Test] - public void LoadFromFiles_WhenSqlFilesExists_ShouldReturnsYeSqlCollection() - { - // Arrange - var loader = new YeSqlLoader(); - var file = "./sql/users.sql"; - var expectedCollection = new Dictionary - { - { "GetUsers", "SELECT* FROM [user];" } - }; - - // Act - var sqlStatements = loader.LoadFromFiles(file); - - // Assert - sqlStatements.Should().BeEquivalentTo(expectedCollection); - } - - [Test] - public void LoadFromFiles_WhenPathsAreAbsolute_ShouldBeAbleToLoadSqlFiles() - { - // Arrange - var loader = new YeSqlLoader(); - var file = Path.Combine(AppContext.BaseDirectory, "sql/users.sql"); - var expectedCollection = new Dictionary - { - { "GetUsers", "SELECT* FROM [user];" } - }; - - // Act - var sqlStatements = loader.LoadFromFiles(file); - - // Assert - sqlStatements.Should().BeEquivalentTo(expectedCollection); - } - - [TestCase("test.txt")] - [TestCase("test.png")] - [TestCase("test")] - [TestCase("test/")] - public void LoadFromFiles_WhenFileHasNotSqlExtension_ShouldThrowAggregateException(string file) - { - // Arrange - var loader = new YeSqlLoader(); - var expectedMessage = string.Format(ExceptionMessages.FileHasNotSqlExtension, file); - - // Act - Action action = () => loader.LoadFromFiles(file); - - // Assert - action.Should() - .Throw() - .WithInnerException() - .WithMessage(expectedMessage); - } - - [Test] - public void LoadFromFiles_WhenSqlFileNotExists_ShouldThrowAggregateException() - { - // Arrange - var loader = new YeSqlLoader(); - var file = "file_not_found.sql"; - var expectedMessage = string.Format(ExceptionMessages.FileNotFound, file); - - // Act - Action action = () => loader.LoadFromFiles(file); - - // Assert - action.Should() - .Throw() - .WithInnerException() - .WithMessage(expectedMessage); - } - - [TestCase("")] - [TestCase(" ")] - [TestCase(" ")] - [TestCase("data.sql", null)] - [TestCase("data.sql", "test.sql", " ", "", null, "hola.sql")] - public void LoadFromFiles_WhenCollectionHasNullValueOrOnlyWhitespace_ShouldThrowArgumentException(params string[] sqlFiles) - { - // Arrange - var loader = new YeSqlLoader(); - var expectedMessage = string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(sqlFiles)); - - // Act - Action action = () => loader.LoadFromFiles(sqlFiles); - - // Assert - action.Should() - .Throw() - .WithMessage(expectedMessage); - } - - [Test] - public void LoadFromFiles_WhenParamsListIsZero_ShouldThrowArgumentException() - { - // Arrange - var loader = new YeSqlLoader(); - var expectedMessage = ExceptionMessages.LengthOfParamsListIsZero; - - // Act - Action action = () => loader.LoadFromFiles(); - - // Assert - action.Should() - .Throw() - .WithMessage(expectedMessage); - } - - [Test] - public void LoadFromDirectories_WhenErrorsAreFound_ShouldThrowAggregateException() - { - // Arrange - var loader = new YeSqlLoader(); - var directories = new[] - { - "error", - "directory_not_found", - "env", - }; - var loaderErrors = new[] - { - string.Format(ExceptionMessages.DirectoryNotFound, "directory_not_found"), - string.Format(ExceptionMessages.NoneFileFoundInSpecifiedDirectory, "env") - }; - var parserErrors = new[] - { - $"errors.sql:(line 2, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT * FROM users;")}", - $"errors.sql:(line 9, col 9): error: {ExceptionMessages.TagIsEmptyOrWhitespace}", - $"errors.sql:(line 10, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT name FROM roles;")}", - $"errors.sql:(line 12, col 9): error: {string.Format(ExceptionMessages.DuplicateTagName, "GetUsers")}", - $"errors.sql:(line 15, col 9): error: {ExceptionMessages.TagIsEmptyOrWhitespace}", - $"errors.sql:(line 16, col 1): error: {string.Format(ExceptionMessages.LineIsNotAssociatedWithAnyTag, "SELECT * FROM roles;")}" - }; - var expectedErrors = new[] - { - string.Join(Environment.NewLine, loaderErrors), - string.Join(Environment.NewLine, parserErrors) - }; - - // Act - Action action = () => loader.LoadFromDirectories(directories); - - // Assert - action.Should() - .Throw() - .Which - .InnerExceptions - .Select(innerException => innerException.Message) - .Should() - .BeEquivalentTo(expectedErrors); - } - - [Test] - public void LoadFromDirectories_WhenDirectoriesPathIsNull_ShouldThrowArgumentNullException() - { - // Arrange - var loader = new YeSqlLoader(); - - // Act - Action action = () => loader.LoadFromDirectories(null); - - // Assert - action.Should().Throw(); - } - - [TestCase("")] - [TestCase(" ")] - [TestCase(" ")] - [TestCase("data/", null)] - [TestCase("sql/", "non/", " ", "", null, "tests/")] - public void LoadFromDirectories_WhenCollectionHasNullValueOrOnlyWhitespace_ShouldThrowArgumentException(params string[] directories) - { - // Arrange - var loader = new YeSqlLoader(); - var expectedMessage = string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(directories)); - - // Act - Action action = () => loader.LoadFromDirectories(directories); - - // Assert - action.Should() - .Throw() - .WithMessage(expectedMessage); - } - - [Test] - public void LoadFromDirectories_WhenNotExistsSqlFilesInDirectory_ShouldThrowAggregateException() - { - // Arrange - var loader = new YeSqlLoader(); - var directory = "env"; - var expectedMessage = string.Format(ExceptionMessages.NoneFileFoundInSpecifiedDirectory, directory); - - // Act - Action action = () => loader.LoadFromDirectories(directory); - - // Assert - action.Should() - .Throw() - .WithInnerException() - .WithMessage(expectedMessage); - } - - [Test] - public void LoadFromDirectories_WhenSqlFilesExistsInDirectory_ShouldReturnsYeSqlCollection() - { - // Arrange - var loader = new YeSqlLoader(); - var directory = "./sql"; - var expectedCollection = new Dictionary - { - { "GetUsers", "SELECT* FROM [user];" } - }; - - // Act - var sqlStatements = loader.LoadFromDirectories(directory); - - // Assert - sqlStatements.Should().BeEquivalentTo(expectedCollection); - } - - [Test] - public void LoadFromDirectories_WhenPathsAreAbsolute_ShouldBeAbleToLoadSqlFiles() - { - // Arrange - var loader = new YeSqlLoader(); - var directory = Path.Combine(AppContext.BaseDirectory, "sql"); - var expectedCollection = new Dictionary - { - { "GetUsers", "SELECT* FROM [user];" } - }; - - // Act - var sqlStatements = loader.LoadFromDirectories(directory); - - // Assert - sqlStatements.Should().BeEquivalentTo(expectedCollection); - } - - [Test] - public void LoadFromDirectories_WhenDirectoryNotExists_ShouldThrowAggregateException() - { - // Arrange - var loader = new YeSqlLoader(); - var directory = "directory_not_found"; - var expectedMessage = string.Format(ExceptionMessages.DirectoryNotFound, directory); - - // Act - Action action = () => loader.LoadFromDirectories(directory); - - // Asserts - action.Should() - .Throw() - .WithInnerException() - .WithMessage(expectedMessage); - } - - [Test] - public void LoadFromDirectories_WhenParamsListIsZero_ShouldThrowArgumentException() - { - // Arrange - var loader = new YeSqlLoader(); - var expectedMessage = ExceptionMessages.LengthOfParamsListIsZero; - - // Act - Action action = () => loader.LoadFromDirectories(); - - // Assert - action.Should() - .Throw() - .WithMessage(expectedMessage); - } }