test: DbConnectionFactory

This commit is contained in:
Kyle Ratti 2024-06-23 18:25:03 -04:00
parent de8e30dbb4
commit 2e2dc40cfb
No known key found for this signature in database
GPG Key ID: 4D429B6287C68DD9
4 changed files with 114 additions and 2 deletions

View File

@ -16,7 +16,7 @@ public class DbConnectionFactory : IDbConnectionFactory
public INonTransactionalDbConnection<ReadWrite> 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<ReadWrite>(_readWriteConnectionString);
return connection;
@ -25,7 +25,7 @@ public class DbConnectionFactory : IDbConnectionFactory
public INonTransactionalDbConnection<ReadOnly> 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<ReadOnly>(_readOnlyConnectionString);
return connection;

View File

@ -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<ApplicationException>(() => 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<ApplicationException>(() => 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<INonTransactionalDbConnection<ReadWrite>>());
}
[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<INonTransactionalDbConnection<ReadOnly>>());
}
}

View File

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FruityFoundation.DataAccess.Sqlite\FruityFoundation.DataAccess.Sqlite.csproj" />
</ItemGroup>
</Project>

View File

@ -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