-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
4 changed files
with
137 additions
and
7 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,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; | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} | ||
} |