Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestCommon: Support other SQL database collations than default #319

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions source/TestCommon/documents/release-notes/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# TestCommon Release notes

## Version 5.2.0

- Changes to `SqlServerDatabaseManager`:
- Extended constructor of `SqlServerDatabaseManager` with parameter `collationName` to support the creation of databases with other collation names than the `DefaultCollationName`.
- Added const `DurableTaskCollationName` to allow easy access to the collation name necessary for the Durable Task SQL Provider.

## Version 5.1.2

- No functional change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public class SqlServerDatabaseManagerTests
[Collection(nameof(SqlServerDatabaseCollectionFixture))]
public class CreateDatabase
{
[Fact]
public async Task When_DbContextIsConfigured_Then_DatabaseCanBeCreatedAsync()
[Theory]
[MemberData(nameof(GetCollationName), MemberType = typeof(SqlServerDatabaseManagerTests))]
public async Task When_DbContextIsConfigured_Then_DatabaseCanBeCreatedAsync(string collationName)
{
// Arrange
var sut = new SqlServerDatabaseManagerSut();
var sut = new SqlServerDatabaseManagerSut(collationName);

// Act
var databaseCreated = await sut.CreateDatabaseAsync();
Expand Down Expand Up @@ -58,11 +59,12 @@ public async Task When_DatabaseExists_Then_NothingHappensAndDatabaseStillExists_
await context.Database.EnsureDeletedAsync();
}

[Fact]
public void When_DbContextIsConfigured_Then_DatabaseCanBeCreated()
[Theory]
[MemberData(nameof(GetCollationName), MemberType = typeof(SqlServerDatabaseManagerTests))]
public void When_DbContextIsConfigured_Then_DatabaseCanBeCreated(string collationName)
{
// Arrange
var sut = new SqlServerDatabaseManagerSut();
var sut = new SqlServerDatabaseManagerSut(collationName);

// Act
var databaseCreated = sut.CreateDatabase();
Expand Down Expand Up @@ -182,11 +184,22 @@ public SqlServerDatabaseManagerSut()
{
}

public SqlServerDatabaseManagerSut(string collationName)
: base(nameof(SqlServerDatabaseManagerSut), collationName)
{
}

public override ExampleDbContext CreateDbContext()
{
var optionsBuilder = new DbContextOptionsBuilder<ExampleDbContext>();
optionsBuilder.UseSqlServer(ConnectionString);
return new ExampleDbContext(optionsBuilder.Options);
}
}

public static IEnumerable<object[]> GetCollationName()
{
yield return new object[] { SqlServerDatabaseManager<ExampleDbContext>.DefaultCollationName };
yield return new object[] { SqlServerDatabaseManager<ExampleDbContext>.DurableTaskCollationName };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,41 @@ namespace Energinet.DataHub.Core.FunctionApp.TestCommon.Database;
public abstract class SqlServerDatabaseManager<TContextImplementation>
where TContextImplementation : DbContext
{
/// <summary>
/// Use this collation when creating a default database for traditional application usage.
/// </summary>
public const string DefaultCollationName = "SQL_Latin1_General_CP1_CI_AS";

/// <summary>
/// Use this collation when creating a database for use with the Durable Task SQL Provider.
/// See https://microsoft.github.io/durabletask-mssql/#/quickstart?id=database-setup
/// </summary>
public const string DurableTaskCollationName = "Latin1_General_100_BIN2_UTF8";

private readonly SqlServerConnectionStringProvider _sqlServerConnectionStringProvider;

protected SqlServerDatabaseManager(string prefixForDatabaseName)
: this(prefixForDatabaseName, new SqlServerConnectionStringProvider(RuntimeEnvironment.Default))
protected SqlServerDatabaseManager(string prefixForDatabaseName, string collationName = DefaultCollationName)
: this(prefixForDatabaseName, new SqlServerConnectionStringProvider(RuntimeEnvironment.Default), collationName)
{ }

protected SqlServerDatabaseManager(
string prefixForDatabaseName,
SqlServerConnectionStringProvider sqlServerConnectionStringProvider)
SqlServerConnectionStringProvider sqlServerConnectionStringProvider,
string collationName = DefaultCollationName)
{
if (string.IsNullOrWhiteSpace(prefixForDatabaseName))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(prefixForDatabaseName));
}
ArgumentException.ThrowIfNullOrWhiteSpace(prefixForDatabaseName);
ArgumentException.ThrowIfNullOrWhiteSpace(collationName);

_sqlServerConnectionStringProvider = sqlServerConnectionStringProvider;
ConnectionString = sqlServerConnectionStringProvider.BuildConnectionStringForDatabaseWithPrefix(prefixForDatabaseName);

CollationName = collationName;
}

public string ConnectionString { get; }

public string CollationName { get; }

/// <summary>
/// IMPORTANT: Dispose the DbContext after use to return the underlying connection to the pool.
/// </summary>
Expand Down Expand Up @@ -195,12 +208,12 @@ private static string GetDatabaseName(TContextImplementation context)
}

/// <summary>
/// Create a clean database using SQL, similar to the PowerShell script described in the Development.md file.
/// Create a clean database using SQL.
/// </summary>
private static string BuildCreateDatabaseCommandText(string databaseName)
private string BuildCreateDatabaseCommandText(string databaseName)
{
return
$"CREATE DATABASE [{databaseName}] COLLATE {DefaultCollationName};" +
$"CREATE DATABASE [{databaseName}] COLLATE {CollationName};" +
$"ALTER DATABASE [{databaseName}] SET READ_COMMITTED_SNAPSHOT ON;";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ limitations under the License.

<PropertyGroup>
<PackageId>Energinet.DataHub.Core.FunctionApp.TestCommon</PackageId>
<PackageVersion>5.1.2$(VersionSuffix)</PackageVersion>
<PackageVersion>5.2.0$(VersionSuffix)</PackageVersion>
<Title>FunctionApp TestCommon library</Title>
<Company>Energinet-DataHub</Company>
<Authors>Energinet-DataHub</Authors>
Expand Down
2 changes: 1 addition & 1 deletion source/TestCommon/source/TestCommon/TestCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ limitations under the License.

<PropertyGroup>
<PackageId>Energinet.DataHub.Core.TestCommon</PackageId>
<PackageVersion>5.1.2$(VersionSuffix)</PackageVersion>
<PackageVersion>5.2.0$(VersionSuffix)</PackageVersion>
<Title>TestCommon library</Title>
<Company>Energinet-DataHub</Company>
<Authors>Energinet-DataHub</Authors>
Expand Down
Loading