diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index c4f433c..c65a019 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -7,11 +7,18 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Setup dotnet uses: actions/setup-dotnet@v3 + + - name: Docker Setup Buildx + uses: docker/setup-buildx-action@v3.0.0 + - name: Install dependencies run: dotnet restore + - name: Build run: dotnet build + - name: Test run: dotnet test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c1af4be..12a2c53 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,12 +12,19 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + - name: Setup .NET SDK uses: actions/setup-dotnet@v1 + + - name: Docker Setup Buildx + uses: docker/setup-buildx-action@v3.0.0 + - name: Build run: dotnet build -c Release + - name: Test run: dotnet test -c Release --no-build + - name: Publish NuGet packacges env: NUGET_API_KEY: ${{ secrets.nuget_api_key }} diff --git a/Directory.Packages.props b/Directory.Packages.props index 2237e42..8e66685 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -6,8 +6,9 @@ - + + diff --git a/TransactionContext.sln b/TransactionContext.sln index 37713b4..d4f4791 100644 --- a/TransactionContext.sln +++ b/TransactionContext.sln @@ -9,6 +9,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransactionContext", "sourc EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransactionContext.Postgres", "source\TransactionContext.Postgres\TransactionContext.Postgres.csproj", "{DD81C9E5-5DB6-4032-BD2C-F039300A63DD}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{5A6575AC-81E5-4E97-909F-BBE634A2BD14}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransactionContext.Postgres.Tests", "tests\TransactionContext.Postgres.Tests\TransactionContext.Postgres.Tests.csproj", "{A7CC7D41-46AF-45D7-95C9-681253CCB885}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,6 +27,10 @@ Global {DD81C9E5-5DB6-4032-BD2C-F039300A63DD}.Debug|Any CPU.Build.0 = Debug|Any CPU {DD81C9E5-5DB6-4032-BD2C-F039300A63DD}.Release|Any CPU.ActiveCfg = Release|Any CPU {DD81C9E5-5DB6-4032-BD2C-F039300A63DD}.Release|Any CPU.Build.0 = Release|Any CPU + {A7CC7D41-46AF-45D7-95C9-681253CCB885}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A7CC7D41-46AF-45D7-95C9-681253CCB885}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A7CC7D41-46AF-45D7-95C9-681253CCB885}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A7CC7D41-46AF-45D7-95C9-681253CCB885}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -30,6 +38,7 @@ Global GlobalSection(NestedProjects) = preSolution {536A7C7E-F987-4DDC-BEA8-846A3771F918} = {6004A5FC-0B64-464C-977A-5E721E8567B7} {DD81C9E5-5DB6-4032-BD2C-F039300A63DD} = {6004A5FC-0B64-464C-977A-5E721E8567B7} + {A7CC7D41-46AF-45D7-95C9-681253CCB885} = {5A6575AC-81E5-4E97-909F-BBE634A2BD14} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {48C38B93-C350-4CC9-BCDD-CE256D01DD7E} diff --git a/tests/TransactionContext.Postgres.Tests/SeedWork/Customer.cs b/tests/TransactionContext.Postgres.Tests/SeedWork/Customer.cs new file mode 100644 index 0000000..19dcb71 --- /dev/null +++ b/tests/TransactionContext.Postgres.Tests/SeedWork/Customer.cs @@ -0,0 +1,4 @@ +namespace TransactionContext.Postgres.Tests.SeedWork +{ + public readonly record struct Customer(Guid Id, string Name); +} diff --git a/tests/TransactionContext.Postgres.Tests/SeedWork/PostgreSqlContainerTest.cs b/tests/TransactionContext.Postgres.Tests/SeedWork/PostgreSqlContainerTest.cs new file mode 100644 index 0000000..d8c2387 --- /dev/null +++ b/tests/TransactionContext.Postgres.Tests/SeedWork/PostgreSqlContainerTest.cs @@ -0,0 +1,47 @@ +using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; +using Xunit; + +namespace TransactionContext.Postgres.Tests.SeedWork +{ + public abstract class PostgreSqlContainerTest : IAsyncLifetime + { + private readonly PostgreSqlContainer _postgreSqlContainer = new PostgreSqlBuilder().Build(); + + protected ITransactionContext TransactionContext { get; private set; } + + protected IDbConnectionFactory DbConnectionFactory { get; private set; } + + public async Task InitializeAsync() + { + await _postgreSqlContainer.StartAsync(); + + var connectionString = _postgreSqlContainer.GetConnectionString(); + + IServiceCollection services = new ServiceCollection(); + + services.AddTransactionContext(x => x.UsePostgres(connectionString)); + + var provider = services.BuildServiceProvider(); + + TransactionContext = provider.GetRequiredService(); + DbConnectionFactory = provider.GetRequiredService(); + + await CreateDb(); + } + + public Task DisposeAsync() + { + return _postgreSqlContainer.DisposeAsync().AsTask(); + } + + private Task CreateDb() + { + const string sql = "CREATE TABLE IF NOT EXISTS customers (id UUID NOT NULL, name VARCHAR NOT NULL, PRIMARY KEY (id))"; + + TransactionContext.Add(sql); + + return TransactionContext.Commit(); + } + } +} diff --git a/tests/TransactionContext.Postgres.Tests/TransactionContext.Postgres.Tests.csproj b/tests/TransactionContext.Postgres.Tests/TransactionContext.Postgres.Tests.csproj new file mode 100644 index 0000000..f0f114b --- /dev/null +++ b/tests/TransactionContext.Postgres.Tests/TransactionContext.Postgres.Tests.csproj @@ -0,0 +1,32 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/tests/TransactionContext.Postgres.Tests/TransactionContextTests.cs b/tests/TransactionContext.Postgres.Tests/TransactionContextTests.cs new file mode 100644 index 0000000..d07c2c6 --- /dev/null +++ b/tests/TransactionContext.Postgres.Tests/TransactionContextTests.cs @@ -0,0 +1,32 @@ +using Dapper; +using TransactionContext.Postgres.Tests.SeedWork; +using Xunit; + +namespace TransactionContext.Postgres.Tests +{ + public sealed class TransactionContextTests : PostgreSqlContainerTest + { + [Fact] + public async Task BasicTest() + { + // Arrange + const int numberOfCustomers = 100; + const string sql = "INSERT INTO customers (id, name) VALUES(@id, @name)"; + + for (var i = 0; i < numberOfCustomers; i++) + { + TransactionContext.Add(sql, new { id = Guid.NewGuid(), Name = $"name{i}" }); + } + + // Act + await TransactionContext.Commit(); + + // Assert + const string selectSql = "SELECT id, name FROM customers"; + using var connection = DbConnectionFactory.Create(); + var customers = await connection.QueryAsync(selectSql); + + Assert.Equal(numberOfCustomers, customers.Count()); + } + } +}