-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MySqlConnector; | ||
using System.Data.Common; | ||
using TransactionContext.Configurators; | ||
using TransactionContext.MySql.Internals; | ||
|
||
namespace TransactionContext.MySql | ||
{ | ||
public static class Extensions | ||
{ | ||
public static void UseMySql( | ||
this IDbProviderConfigurator configurator, | ||
string connectionString) | ||
{ | ||
configurator.Services.AddSingleton<IDbConnectionFactory>(new MySqlConnectionFactory(connectionString)); | ||
configurator.Services.AddScoped<DbBatch, MySqlBatch>(); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
source/TransactionContext.MySql/Internals/MySqlConnectionFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Data.Common; | ||
using MySqlConnector; | ||
|
||
namespace TransactionContext.MySql.Internals | ||
{ | ||
internal sealed class MySqlConnectionFactory : IDbConnectionFactory | ||
{ | ||
private readonly string _connectionString; | ||
|
||
public MySqlConnectionFactory(string connectionString) => _connectionString = connectionString; | ||
|
||
public DbConnection Create() => new MySqlConnection(_connectionString); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
source/TransactionContext.MySql/TransactionContext.MySql.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MySqlConnector" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TransactionContext\TransactionContext.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
49 changes: 49 additions & 0 deletions
49
tests/TransactionContext.MySql.Tests/MySqlTransactionContextTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Dapper; | ||
using TransactionContext.MySql.Tests.SeedWork; | ||
using TransactionContext.Tests.SeedWork; | ||
using Xunit; | ||
|
||
namespace TransactionContext.MySql.Tests | ||
{ | ||
public sealed class MySqlTransactionContextTests : MySqlContainerTest | ||
{ | ||
[Fact] | ||
public async Task BasicTest() | ||
{ | ||
// Arrange | ||
const int numberOfCustomers = 100; | ||
|
||
for (var i = 0; i < numberOfCustomers; i++) | ||
{ | ||
var customerId = Guid.NewGuid(); | ||
var orderId = Guid.NewGuid(); | ||
|
||
TransactionContext.Add(CustomerSQL.Insert, new { CustomerId = customerId, Name = $"name{i}" }); | ||
TransactionContext.Add(OrderSQL.Insert, new { OrderId = orderId, CustomerId = customerId }); | ||
|
||
TransactionContext.Add(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 }); | ||
TransactionContext.Add(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 }); | ||
TransactionContext.Add(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 }); | ||
TransactionContext.Add(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 }); | ||
} | ||
|
||
// Act | ||
await TransactionContext.Commit(); | ||
|
||
// Assert | ||
const string selectCustomersSql = "SELECT CustomerId, Name FROM Customers"; | ||
using var connection = DbConnectionFactory.Create(); | ||
var customers = await connection.QueryAsync<Customer>(selectCustomersSql); | ||
|
||
const string selectOrdersSql = "SELECT OrderId, CustomerId FROM Orders"; | ||
var orders = await connection.QueryAsync<Order>(selectOrdersSql); | ||
|
||
const string selectOrderItemssSql = "SELECT OrderItemId, OrderId, Name, Amount FROM OrderItems"; | ||
var orderItems = await connection.QueryAsync<Order>(selectOrderItemssSql); | ||
|
||
Assert.Equal(numberOfCustomers, customers.Count()); | ||
Assert.Equal(numberOfCustomers, orders.Count()); | ||
Assert.Equal(4 * numberOfCustomers, orderItems.Count()); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/TransactionContext.MySql.Tests/SeedWork/MySqlContainerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Dapper; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using Testcontainers.MySql; | ||
using Xunit; | ||
|
||
namespace TransactionContext.MySql.Tests.SeedWork | ||
{ | ||
public abstract class MySqlContainerTest : IAsyncLifetime | ||
{ | ||
private readonly MySqlContainer _container = new MySqlBuilder().Build(); | ||
|
||
protected ITransactionContext TransactionContext { get; private set; } | ||
Check warning on line 13 in tests/TransactionContext.MySql.Tests/SeedWork/MySqlContainerTest.cs GitHub Actions / build
|
||
|
||
protected IDbConnectionFactory DbConnectionFactory { get; private set; } | ||
Check warning on line 15 in tests/TransactionContext.MySql.Tests/SeedWork/MySqlContainerTest.cs GitHub Actions / build
|
||
|
||
public async Task InitializeAsync() | ||
{ | ||
await _container.StartAsync(); | ||
|
||
var connectionString = _container.GetConnectionString(); | ||
|
||
IServiceCollection services = new ServiceCollection(); | ||
|
||
services.AddTransactionContext(x => x.UseMySql(connectionString)); | ||
|
||
var provider = services.BuildServiceProvider(); | ||
|
||
TransactionContext = provider.GetRequiredService<ITransactionContext>(); | ||
DbConnectionFactory = provider.GetRequiredService<IDbConnectionFactory>(); | ||
|
||
await CreateDb(); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return _container.DisposeAsync().AsTask(); | ||
} | ||
|
||
private Task CreateDb() | ||
{ | ||
const string customerSql = "CREATE TABLE IF NOT EXISTS Customers(CustomerId CHAR(36), Name VARCHAR(100))"; | ||
const string orderSql = "CREATE TABLE IF NOT EXISTS Orders(OrderId CHAR(36), CustomerId CHAR(36))"; | ||
const string orderItemSql = "CREATE TABLE IF NOT EXISTS OrderItems(OrderItemId CHAR(36), OrderId CHAR(36), Name VARCHAR(100), Amount INT)"; | ||
|
||
TransactionContext.Add(customerSql); | ||
TransactionContext.Add(orderSql); | ||
TransactionContext.Add(orderItemSql); | ||
|
||
return TransactionContext.Commit(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/TransactionContext.MySql.Tests/TransactionContext.MySql.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<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="Dapper" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="Testcontainers.MySql" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\source\TransactionContext.MySql\TransactionContext.MySql.csproj" /> | ||
<ProjectReference Include="..\TransactionContext.Tests.SeedWork\TransactionContext.Tests.SeedWork.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |