From 2e2dc40cfb02dd7e86b4e677b770982272220d97 Mon Sep 17 00:00:00 2001 From: Kyle Ratti Date: Sun, 23 Jun 2024 18:25:03 -0400 Subject: [PATCH] test: DbConnectionFactory --- .../DbConnectionFactory.cs | 4 +- .../DbConnectionFactoryTests.cs | 67 +++++++++++++++++++ ...yFoundation.Tests.DataAccess.Sqlite.csproj | 28 ++++++++ FruityFoundation.sln | 17 +++++ 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 FruityFoundation.Tests.DataAccess.Sqlite/DbConnectionFactoryTests.cs create mode 100644 FruityFoundation.Tests.DataAccess.Sqlite/FruityFoundation.Tests.DataAccess.Sqlite.csproj diff --git a/FruityFoundation.DataAccess.Sqlite/DbConnectionFactory.cs b/FruityFoundation.DataAccess.Sqlite/DbConnectionFactory.cs index 7760c4c..a946050 100644 --- a/FruityFoundation.DataAccess.Sqlite/DbConnectionFactory.cs +++ b/FruityFoundation.DataAccess.Sqlite/DbConnectionFactory.cs @@ -16,7 +16,7 @@ public class DbConnectionFactory : IDbConnectionFactory public INonTransactionalDbConnection CreateConnection() { if (string.IsNullOrWhiteSpace(_readWriteConnectionString)) - throw new ApplicationException("ReadWrite connection string was not found or empty."); + throw new ApplicationException("ReadWrite connection string cannot be null or empty."); var connection = new NonTransactionalDbConnection(_readWriteConnectionString); return connection; @@ -25,7 +25,7 @@ public class DbConnectionFactory : IDbConnectionFactory public INonTransactionalDbConnection CreateReadOnlyConnection() { if (string.IsNullOrWhiteSpace(_readOnlyConnectionString)) - throw new ApplicationException("ReadOnly connection string was not found or empty."); + throw new ApplicationException("ReadOnly connection string cannot be null or empty."); var connection = new NonTransactionalDbConnection(_readOnlyConnectionString); return connection; diff --git a/FruityFoundation.Tests.DataAccess.Sqlite/DbConnectionFactoryTests.cs b/FruityFoundation.Tests.DataAccess.Sqlite/DbConnectionFactoryTests.cs new file mode 100644 index 0000000..56a709d --- /dev/null +++ b/FruityFoundation.Tests.DataAccess.Sqlite/DbConnectionFactoryTests.cs @@ -0,0 +1,67 @@ +using FruityFoundation.DataAccess.Abstractions; +using FruityFoundation.DataAccess.Sqlite; + +namespace FruityFoundation.Tests.DataAccess.Sqlite; + +public class DbConnectionFactoryTests +{ + [TestCase("")] + [TestCase(" ")] + [TestCase(null)] + public void CreateConnection_ThrowsException_WhenConnectionStringIsNullOrEmpty(string? connectionString) + { + // Arrange + var dbConnectionFactory = new DbConnectionFactory(connectionString!, readOnlyConnectionString: "ReadOnlyConnectionString"); + + // Act + var exception = Assert.Throws(() => dbConnectionFactory.CreateConnection()); + + // Assert + Assert.That(exception, Is.Not.Null); + Assert.That(exception.Message, Is.EqualTo("ReadWrite connection string cannot be null or empty.")); + } + + [TestCase("")] + [TestCase(" ")] + [TestCase(null)] + public void CreateReadOnlyConnection_ThrowsException_WhenConnectionStringIsNullOrEmpty(string? connectionString) + { + // Arrange + var dbConnectionFactory = new DbConnectionFactory(readWriteConnectionString: "connectionString", readOnlyConnectionString: null!); + + // Act + var exception = Assert.Throws(() => dbConnectionFactory.CreateReadOnlyConnection()); + + // Assert + Assert.That(exception, Is.Not.Null); + Assert.That(exception.Message, Is.EqualTo("ReadOnly connection string cannot be null or empty.")); + } + + [Test] + public void CreateConnection_ReturnsNonTransactionalDbConnection_WhenConnectionStringIsValid() + { + // Arrange + var dbConnectionFactory = new DbConnectionFactory(readWriteConnectionString: "Data Source=:memory:", readOnlyConnectionString: null!); + + // Act + var connection = dbConnectionFactory.CreateConnection(); + + // Assert + Assert.That(connection, Is.Not.Null); + Assert.That(connection, Is.InstanceOf>()); + } + + [Test] + public void CreateReadOnlyConnection_ReturnsNonTransactionalDbConnection_WhenConnectionStringIsValid() + { + // Arrange + var dbConnectionFactory = new DbConnectionFactory(readWriteConnectionString: null!, readOnlyConnectionString: "Data Source=:memory:;Mode=ReadOnly"); + + // Act + var connection = dbConnectionFactory.CreateReadOnlyConnection(); + + // Assert + Assert.That(connection, Is.Not.Null); + Assert.That(connection, Is.InstanceOf>()); + } +} diff --git a/FruityFoundation.Tests.DataAccess.Sqlite/FruityFoundation.Tests.DataAccess.Sqlite.csproj b/FruityFoundation.Tests.DataAccess.Sqlite/FruityFoundation.Tests.DataAccess.Sqlite.csproj new file mode 100644 index 0000000..74f5c68 --- /dev/null +++ b/FruityFoundation.Tests.DataAccess.Sqlite/FruityFoundation.Tests.DataAccess.Sqlite.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/FruityFoundation.sln b/FruityFoundation.sln index 9083a66..f32a9e6 100644 --- a/FruityFoundation.sln +++ b/FruityFoundation.sln @@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FruityFoundation.DataAccess EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FruityFoundation.DataAccess.Sqlite", "FruityFoundation.DataAccess.Sqlite\FruityFoundation.DataAccess.Sqlite.csproj", "{BB25E92F-5D51-487A-8937-27E28EF5E20F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FruityFoundation.Tests.DataAccess.Sqlite", "FruityFoundation.Tests.DataAccess.Sqlite\FruityFoundation.Tests.DataAccess.Sqlite.csproj", "{A2E62C7C-62A1-43C0-BD60-752B0C84E518}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -50,10 +52,25 @@ Global {A64E73D3-EF87-4938-B01E-F9CC0B59F9DE}.Debug|Any CPU.Build.0 = Debug|Any CPU {A64E73D3-EF87-4938-B01E-F9CC0B59F9DE}.Release|Any CPU.ActiveCfg = Release|Any CPU {A64E73D3-EF87-4938-B01E-F9CC0B59F9DE}.Release|Any CPU.Build.0 = Release|Any CPU + {C003E247-C62E-4830-94E4-F274D8466A5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C003E247-C62E-4830-94E4-F274D8466A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C003E247-C62E-4830-94E4-F274D8466A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C003E247-C62E-4830-94E4-F274D8466A5C}.Release|Any CPU.Build.0 = Release|Any CPU + {BB25E92F-5D51-487A-8937-27E28EF5E20F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BB25E92F-5D51-487A-8937-27E28EF5E20F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB25E92F-5D51-487A-8937-27E28EF5E20F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BB25E92F-5D51-487A-8937-27E28EF5E20F}.Release|Any CPU.Build.0 = Release|Any CPU + {A2E62C7C-62A1-43C0-BD60-752B0C84E518}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A2E62C7C-62A1-43C0-BD60-752B0C84E518}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A2E62C7C-62A1-43C0-BD60-752B0C84E518}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A2E62C7C-62A1-43C0-BD60-752B0C84E518}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {50A75644-A1C3-4495-9DEB-DBB12D9334B5} = {B44178DF-5B81-4029-90FA-2BF8E2A1EDBF} {EBDC3640-4E47-43FE-BF0D-4BFFD07FE2EF} = {B44178DF-5B81-4029-90FA-2BF8E2A1EDBF} {A64E73D3-EF87-4938-B01E-F9CC0B59F9DE} = {B44178DF-5B81-4029-90FA-2BF8E2A1EDBF} + {C003E247-C62E-4830-94E4-F274D8466A5C} = {5C3A014A-7931-4A36-95F0-5EFE15AB06A3} + {BB25E92F-5D51-487A-8937-27E28EF5E20F} = {5C3A014A-7931-4A36-95F0-5EFE15AB06A3} + {A2E62C7C-62A1-43C0-BD60-752B0C84E518} = {B44178DF-5B81-4029-90FA-2BF8E2A1EDBF} EndGlobalSection EndGlobal