Path: redb/redb.Tests.Integration/Tests/MsSql/MsSqlExportBulkCopyOptionsTests.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/MsSql/MsSqlExportBulkCopyOptionsTests.cs
----
using System.Data;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using redb.Export.Providers;
namespace redb.Tests.Integration.Tests.MsSql;
///
/// The bulk load of the SQL Server export provider writes the rows exactly as given: a key written into an IDENTITY
/// column is kept, not replaced by a generated one, and a null written into a column with a default stays null.
///
public class MsSqlExportBulkCopyOptionsTests : IAsyncLifetime
{
private string _server = null!;
private string _database = null!;
public async Task InitializeAsync()
{
_server = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetConnectionString("MSSql")!;
_database = $"redb_export_bulk_{Guid.NewGuid().ToString("N")[..12]}";
await ExecuteAsync(Master(), $"CREATE DATABASE [{_database}]");
await ExecuteAsync(Target(), """
CREATE TABLE dbo.bulk_probe (
_id BIGINT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
_note NVARCHAR(50) NULL DEFAULT N'from default')
""");
}
public async Task DisposeAsync()
{
SqlConnection.ClearAllPools();
await ExecuteAsync(Master(),
$"IF DB_ID('{_database}') IS NOT NULL BEGIN ALTER DATABASE [{_database}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{_database}] END");
}
[Fact]
public async Task ABulkLoad_KeepsWrittenKeysAndNulls()
{
var data = new DataTable();
data.Columns.Add("_id", typeof(long));
data.Columns.Add("_note", typeof(string));
data.Rows.Add(42L, DBNull.Value);
data.Rows.Add(7L, "written");
await using (var provider = ProviderFactory.Create("mssql"))
{
await provider.OpenAsync(Target());
await provider.BulkInsertAsync("dbo.bulk_probe", data);
}
(await RowsAsync()).Should().Equal("7=written", "42=");
}
private async Task> RowsAsync()
{
var rows = new List();
await using var connection = new SqlConnection(Target());
await connection.OpenAsync();
await using var command = new SqlCommand("SELECT _id, _note FROM dbo.bulk_probe ORDER BY _id", connection);
await using var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
rows.Add($"{reader.GetInt64(0)}={(reader.IsDBNull(1) ? "" : reader.GetString(1))}");
return rows;
}
private string Master() => new SqlConnectionStringBuilder(_server) { InitialCatalog = "master" }.ConnectionString;
private string Target() => new SqlConnectionStringBuilder(_server) { InitialCatalog = _database }.ConnectionString;
private static async Task ExecuteAsync(string cs, string sql)
{
await using var connection = new SqlConnection(cs);
await connection.OpenAsync();
await using var command = new SqlCommand(sql, connection);
await command.ExecuteNonQueryAsync();
}
}