Skip to content

Commit

Permalink
Merge pull request #22 from kubagdynia/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kubagdynia authored Oct 3, 2024
2 parents deee2ce + 437f8dc commit f0ad33c
Show file tree
Hide file tree
Showing 32 changed files with 802 additions and 828 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.Data.SQLite" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="nunit" Version="4.2.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,52 @@
using System.Data;
using System.IO;

namespace Dapper.CustomTypeHandlers.Tests.DbConnection
namespace Dapper.CustomTypeHandlers.Tests.DbConnection;

internal abstract class BaseSqliteConnectionFactory : IDbConnectionFactory
{
internal abstract class BaseSqliteConnectionFactory : IDbConnectionFactory
private readonly string _fileName;

protected BaseSqliteConnectionFactory(string dbFilename)
{
private readonly string _fileName;
_fileName = Path.Combine(Environment.CurrentDirectory, dbFilename);
InitializeDatabase();
}

protected BaseSqliteConnectionFactory(string dbFilename)
{
_fileName = Path.Combine(Environment.CurrentDirectory, dbFilename);
InitializeDatabase();
}
public IDbConnection Connection()
{
var connectionString = $"DataSource={_fileName}";
var conn = new SqliteConnection(connectionString);

public IDbConnection Connection()
{
var connectionString = $"DataSource={_fileName}";
var conn = new SqliteConnection(connectionString);
return conn;
}

return conn;
}
public IDbConnection Connection(string name)
{
return Connection();
}

protected abstract void CreateDb(IDbConnection dbConnection);

public IDbConnection Connection(string name)
private void InitializeDatabase()
{
if (File.Exists(_fileName))
{
return Connection();
return;
}

protected abstract void CreateDb(IDbConnection dbConnection);
FileStream fileStream = File.Create(_fileName);
fileStream.Close();

private void InitializeDatabase()
using var conn = Connection();
conn.Open();
try
{
CreateDb(conn);
}
finally
{
if (File.Exists(_fileName))
{
return;
}

FileStream fileStream = File.Create(_fileName);
fileStream.Close();

using var conn = Connection();
conn.Open();
try
{
CreateDb(conn);
}
finally
{
conn.Close();
}
conn.Close();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Data;

namespace Dapper.CustomTypeHandlers.Tests.DbConnection
namespace Dapper.CustomTypeHandlers.Tests.DbConnection;

internal interface IDbConnectionFactory
{
internal interface IDbConnectionFactory
{
IDbConnection Connection();
IDbConnection Connection();

IDbConnection Connection(string name);
}
}
IDbConnection Connection(string name);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
using System;
using System.Data;

namespace Dapper.CustomTypeHandlers.Tests.DbConnection
namespace Dapper.CustomTypeHandlers.Tests.DbConnection;

internal class SqliteConnectionFactory : BaseSqliteConnectionFactory
{
internal class SqliteConnectionFactory : BaseSqliteConnectionFactory
public SqliteConnectionFactory() : base($"TestDb_{Guid.NewGuid()}.sqlite")
{
public SqliteConnectionFactory() : base($"TestDb_{Guid.NewGuid()}.sqlite")
{

}
}

protected override void CreateDb(IDbConnection dbConnection)
{
dbConnection.Execute(
@"CREATE TABLE Test_Objects
protected override void CreateDb(IDbConnection dbConnection)
{
dbConnection.Execute(
@"CREATE TABLE Test_Objects
(
ID integer primary key AUTOINCREMENT,
FirstName varchar(100) not null,
LastName varchar(100) not null,
StartWork datetime not null,
Content TEXT
)");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System;
using System.Data;

namespace Dapper.CustomTypeHandlers.Tests.DbConnection
namespace Dapper.CustomTypeHandlers.Tests.DbConnection;

internal class SqliteConnectionFactoryForGuid : BaseSqliteConnectionFactory
{
internal class SqliteConnectionFactoryForGuid : BaseSqliteConnectionFactory
public SqliteConnectionFactoryForGuid() : base($"TestGuidDb_{Guid.NewGuid()}.sqlite")
{
public SqliteConnectionFactoryForGuid() : base($"TestGuidDb_{Guid.NewGuid()}.sqlite")
{
}
}

protected override void CreateDb(IDbConnection dbConnection)
=>
dbConnection.Execute(
@"CREATE TABLE Test_Objects
protected override void CreateDb(IDbConnection dbConnection)
=>
dbConnection.Execute(
@"CREATE TABLE Test_Objects
(
ID integer primary key AUTOINCREMENT,
GuidId varchar(36)
)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,40 @@
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

namespace Dapper.CustomTypeHandlers.Tests
namespace Dapper.CustomTypeHandlers.Tests;

public class GuidTypeHandlerTests
{
public class GuidTypeHandlerTests
[Test]
public async Task Guid_Data_Saved_In_DataBase_Should_Be_Properly_Restored()
{
[Test]
public async Task Guid_Data_Saved_In_DataBase_Should_Be_Properly_Restored()
{
ServiceCollection services =
new ServiceCollectionBuilder().PrepareServiceCollectionForGuidTests(s =>
{
s.ResetDapperCustomTypeHandlers();
s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly());
});
ServiceCollection services =
new ServiceCollectionBuilder().PrepareServiceCollectionForGuidTests(s =>
{
s.ResetDapperCustomTypeHandlers();
s.RegisterDapperCustomTypeHandlers(Assembly.GetExecutingAssembly());
});

ServiceProvider serviceProvider = services.BuildServiceProvider();
ServiceProvider serviceProvider = services.BuildServiceProvider();

using IServiceScope scope = serviceProvider.CreateScope();
using IServiceScope scope = serviceProvider.CreateScope();

var scopedServices = scope.ServiceProvider;
var scopedServices = scope.ServiceProvider;

ITestGuidRepository testGuidRepository = scopedServices.GetRequiredService<ITestGuidRepository>();
var testGuidRepository = scopedServices.GetRequiredService<ITestGuidRepository>();

TestGuidObject testGuidObject = new TestGuidObject
{
GuidId = Guid.NewGuid()
};
var testGuidObject = new TestGuidObject
{
GuidId = Guid.NewGuid()
};

// Act
await testGuidRepository.SaveTestGuidObject(testGuidObject);
TestGuidObject retrievedTestGuidObject = await testGuidRepository.GetTestGuidObject(testGuidObject.Id);
// Act
await testGuidRepository.SaveTestGuidObject(testGuidObject);
var retrievedTestGuidObject = await testGuidRepository.GetTestGuidObject(testGuidObject.Id);

// Assert
retrievedTestGuidObject.Should().NotBeNull();
retrievedTestGuidObject.Should().BeEquivalentTo(testGuidObject);
retrievedTestGuidObject.GuidId.Should().Be(testGuidObject.GuidId);
}
// Assert
retrievedTestGuidObject.Should().NotBeNull();
retrievedTestGuidObject.Should().BeEquivalentTo(testGuidObject);
retrievedTestGuidObject.GuidId.Should().Be(testGuidObject.GuidId);
}
}
Loading

0 comments on commit f0ad33c

Please sign in to comment.