Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
adimiko committed Feb 5, 2024
1 parent 5408485 commit ebe00eb
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageVersion Include="Dapper" Version="2.0.143" />
<PackageVersion Include="Dapper" Version="2.1.28" />
<PackageVersion Include="Npgsql" Version="8.0.1" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.7.0" />
<!-- Test references -->
<PackageVersion Include="xunit" Version="2.5.0" />
<PackageVersion Include="xunit.assert" Version="2.4.2" />
Expand Down
9 changes: 9 additions & 0 deletions TransactionContext.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,13 +27,18 @@ 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
EndGlobalSection
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}
Expand Down
4 changes: 4 additions & 0 deletions tests/TransactionContext.Postgres.Tests/SeedWork/Customer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace TransactionContext.Postgres.Tests.SeedWork
{
public readonly record struct Customer(Guid Id, string Name);
}
Original file line number Diff line number Diff line change
@@ -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; }

Check warning on line 11 in tests/TransactionContext.Postgres.Tests/SeedWork/PostgreSqlContainerTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TransactionContext' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in tests/TransactionContext.Postgres.Tests/SeedWork/PostgreSqlContainerTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TransactionContext' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

protected IDbConnectionFactory DbConnectionFactory { get; private set; }

Check warning on line 13 in tests/TransactionContext.Postgres.Tests/SeedWork/PostgreSqlContainerTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'DbConnectionFactory' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 13 in tests/TransactionContext.Postgres.Tests/SeedWork/PostgreSqlContainerTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'DbConnectionFactory' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

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<ITransactionContext>();
DbConnectionFactory = provider.GetRequiredService<IDbConnectionFactory>();

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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<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.NET.Test.Sdk" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Testcontainers.PostgreSql" />
<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.Postgres\TransactionContext.Postgres.csproj" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions tests/TransactionContext.Postgres.Tests/TransactionContextTests.cs
Original file line number Diff line number Diff line change
@@ -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<Customer>(selectSql);

Assert.Equal(numberOfCustomers, customers.Count());
}
}
}

0 comments on commit ebe00eb

Please sign in to comment.