Skip to content

Commit

Permalink
postgres integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Nov 15, 2023
1 parent fa468a5 commit b8d5dd0
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@
<PackageVersion Include="Dapper" Version="2.1.21" />
<PackageVersion Include="Dapper.AOT" Version="0.5.0-beta.139" />
<PackageVersion Include="Dapper.StrongName" Version="2.1.21" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.6.133" />
<PackageVersion Include="Npgsql" Version="7.0.6" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageVersion Include="Microsoft.Build" Version="17.7.2" />
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.7.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.1" />
<!-- 4.8 would be nice, but: let's try to offer down-level compiler support -->
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="[4.4.0]" />
<PackageVersion Include="Microsoft.CodeAnalysis.VisualBasic.Analyzer.Testing.XUnit" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.VisualBasic.CodeFix.Testing.XUnit" Version="1.1.1" />
<!-- 4.8 would be nice, but: let's try to offer down-level compiler support -->
<PackageVersion Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="[4.4.0]" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.1.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.SqlServer.TransactSql.ScriptDom" Version="161.8910.0" />
<PackageVersion Include="Oracle.ManagedDataAccess" Version="21.12.0" />
<PackageVersion Include="Oracle.ManagedDataAccess.Core" Version="3.21.120" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
<PackageVersion Include="System.Data.Common" Version="4.3.0" />
<PackageVersion Include="System.Data.SqlClient" Version="4.8.5" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.6.0" />
<!-- these are bound by Microsoft.CodeAnalysis.CSharp.*.Testing.XUnit -->
<PackageVersion Include="xunit" Version="[2.3.0]" />
<PackageVersion Include="xunit.runner.visualstudio" Version="[2.3.0]" />
<PackageVersion Include="Microsoft.SqlServer.TransactSql.ScriptDom" Version="161.8910.0" />

<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.VisualBasic.Analyzer.Testing.XUnit" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.VisualBasic.CodeFix.Testing.XUnit" Version="1.1.1" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions test/Dapper.AOT.Test/Dapper.AOT.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="Microsoft.Build" Condition="'$(TargetFramework)'!='net6.0'" />
<PackageReference Include="Microsoft.Build" VersionOverride="[17.3.2]" Condition="'$(TargetFramework)'=='net6.0'" />
<PackageReference Include="Microsoft.Build.Utilities.Core" />
<PackageReference Include="Npgsql" />
<PackageReference Include="Oracle.ManagedDataAccess" Condition="'$(TargetFramework)'=='net48'" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Condition="'$(TargetFramework)'!='net48'" />
<PackageReference Include="System.Data.Common" />
Expand All @@ -52,5 +53,6 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Analyzer.Testing.XUnit" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.CodeFix.Testing.XUnit" />
<PackageReference Include="Testcontainers.PostgreSql" />
</ItemGroup>
</Project>
74 changes: 74 additions & 0 deletions test/Dapper.AOT.Test/Integration/BatchPostgresql.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Data;
using System.Linq;
using Xunit;

namespace Dapper.AOT.Test.Integration;

[Collection(SharedPostgresqlClient.Collection)]
public class BatchPostgresql
{
private PostgresqlFixture _fixture;

public BatchPostgresql(PostgresqlFixture fixture)
{
_fixture = fixture;
fixture.NpgsqlConnection.Execute("""
CREATE TABLE IF NOT EXISTS batch_exec(
id integer PRIMARY KEY,
name varchar(40) NOT NULL CHECK (name <> '')
);
TRUNCATE batch_exec;
""");
}

[Fact]
public void ExecuteMulti()
{
var items = Enumerable.Range(0, 100).Select(i => (i, $"item {i}")).ToList();
var count = _fixture.NpgsqlConnection.Command("""
INSERT INTO batch_exec(id, name)
VALUES (@x, @y);
""", CommandType.Text, handler: CustomHandler.Instance).Execute(items);

Assert.Equal(100, count);

var actual = _fixture.NpgsqlConnection.QuerySingle<int>("""
select count(1) from batch_exec
""");
Assert.Equal(100, actual);
}

private class CustomHandler : CommandFactory<(int x, string y)>
{
public static readonly CustomHandler Instance = new();
private CustomHandler() { }

public override void AddParameters(in UnifiedCommand command, (int x, string y) args)
{
var ps = command.Parameters;
var p = command.CreateParameter();
p.DbType = DbType.Int32;
p.Direction = ParameterDirection.Input;
p.Value = AsValue(args.x);
p.ParameterName = "x";
ps.Add(p);

p = command.CreateParameter();
p.DbType = DbType.AnsiString;
p.Size = 40;
p.Direction = ParameterDirection.Input;
p.Value = AsValue(args.y);
p.ParameterName = "y";
ps.Add(p);
}

public override void UpdateParameters(in UnifiedCommand command, (int x, string y) args)
{
var ps = command.Parameters;
ps[0].Value = AsValue(args.x);
ps[1].Value = AsValue(args.y);
}

public override bool CanPrepare => true;
}
}
52 changes: 52 additions & 0 deletions test/Dapper.AOT.Test/Integration/PostgresqlFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Npgsql;
using System.Threading.Tasks;
using Testcontainers.PostgreSql;
using Xunit;

namespace Dapper.AOT.Test.Integration;

[CollectionDefinition(Collection)]
public class SharedPostgresqlClient : ICollectionFixture<PostgresqlFixture>
{
public const string Collection = nameof(SharedPostgresqlClient);
}

public sealed class PostgresqlFixture : IAsyncLifetime
{
private readonly PostgreSqlContainer _postgresContainer = new PostgreSqlBuilder()
.WithImage("postgres:15-alpine")
.Build();

public string ConnectionString { get; private set; } = null!;

private NpgsqlConnection? _sharedConnection;

public NpgsqlConnection NpgsqlConnection
=> _sharedConnection ??= CreateOpenConnection();

public NpgsqlConnection CreateOpenConnection()
{
var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
return conn;
}

async Task IAsyncLifetime.InitializeAsync()
{
await _postgresContainer.StartAsync();
ConnectionString = _postgresContainer.GetConnectionString();
}

async Task IAsyncLifetime.DisposeAsync()
{
await using (_postgresContainer)
{
var tmp = _sharedConnection;
_sharedConnection = null;
if (tmp is not null)
{
await tmp.DisposeAsync();
}
}
}
}

0 comments on commit b8d5dd0

Please sign in to comment.