Skip to content

Commit

Permalink
Some fixes after PR 405
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbra committed Dec 17, 2023
1 parent e439dee commit 99666bd
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 112 deletions.
6 changes: 6 additions & 0 deletions unittests/Basic_tests/Basic_tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@
<ProjectReference Include="..\TestCommon\TestCommon.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
67 changes: 37 additions & 30 deletions unittests/Basic_tests/CommandLineParsing/FolderConfiguration_.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.CommandLine.Parsing;
using System.Runtime.CompilerServices;
using FluentAssertions;
using grate.Commands;
using grate.Configuration;
using Xunit;

namespace Basic_tests.CommandLineParsing;

Expand Down Expand Up @@ -39,45 +39,38 @@ public async Task Default_With_Overrides(string commandLine, KnownFolderNames fo

[Theory]
[MemberData(nameof(FullyCustomFoldersCommandLines))]
public async Task Fully_Customised(string root, string foldersArgument, IFoldersConfiguration expected)
public async Task Fully_Customised(string name, string root, string foldersArgument, IFoldersConfiguration expected)
{
var s = name;
var cfg = await ParseGrateConfiguration("--sqlfilesdirectory=" + root, foldersArgument);
var actual = cfg?.Folders;

AssertEquivalent(expected.Values, actual?.Values);
}

public static IEnumerable<object?[]> FoldersCommandLines()
{
yield return new object?[] { "--folders=up=tables", KnownFolderNames.Default with { Up = "tables" } };
yield return new object?[] { "--folders=up=tables;views=projections", KnownFolderNames.Default with { Up = "tables", Views = "projections" } };
}

public static TheoryData<string, KnownFolderNames?> FoldersCommandLines() =>
new()
{
{ "--folders=up=tables", Wrap(KnownFolderNames.Default with { Up = "tables" }) },
{ "--folders=up=tables;views=projections", Wrap(KnownFolderNames.Default with { Up = "tables", Views = "projections" }) },
};

public static IEnumerable<object?[]> FullyCustomFoldersCommandLines()
{
yield return new object?[]{
"/tmp/jalla",
@"--folders=folder1=type:Once;folder2=type:EveryTime;folder3=type:AnyTime",
new FoldersConfiguration(
new MigrationsFolder("folder1", MigrationType.Once),
new MigrationsFolder("folder2", MigrationType.EveryTime),
new MigrationsFolder("folder3", MigrationType.AnyTime)
)};
}

// private static TestCaseData GetTestCase(
// string folderArg,
// Func<KnownFolderNames, KnownFolderNames> expectedOverrides,
// [CallerArgumentExpression(nameof(expectedOverrides))] string overridesText = ""
// ) => new TestCaseData(folderArg, expectedOverrides).SetArgDisplayNames(folderArg, overridesText);
public static TheoryData<string, string, string, IFoldersConfiguration> FullyCustomFoldersCommandLines() =>
new()
{
{
"Mostly defaults",
"/tmp/jalla",
@"--folders=folder1=type:Once;folder2=type:EveryTime;folder3=type:AnyTime",
new FoldersConfiguration(
new MigrationsFolder("folder1", MigrationType.Once),
new MigrationsFolder("folder2", MigrationType.EveryTime),
new MigrationsFolder("folder3", MigrationType.AnyTime)
)
}
};

// private static TestCaseData GetCustomisedTestCase(
// string caseName,
// string root,
// string folderArg,
// IFoldersConfiguration expected
// ) => new TestCaseData(root, folderArg, expected).SetArgDisplayNames(caseName, folderArg);

private static void AssertEquivalent(
IEnumerable<MigrationsFolder?> expected,
Expand Down Expand Up @@ -120,4 +113,18 @@ private static void AssertEquivalent(MigrationsFolder? expected, MigrationsFolde

return cfg;
}

private static KnownFolderNamesWithDescription? Wrap(KnownFolderNames? names, [CallerArgumentExpression(nameof(names))] string description = "") =>
names is { } ? new KnownFolderNamesWithDescription(names) { Description = description} : null;

public record KnownFolderNamesWithDescription : KnownFolderNames
{
public KnownFolderNamesWithDescription(KnownFolderNames folder) : base(folder)
{
}

public string Description { get; init; } = null!;
public override string ToString() => Description;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
using grate.Configuration;
using grate.Migration;
using TestCommon.TestInfrastructure;
using Xunit;
using static grate.Configuration.KnownFolderKeys;
using static grate.Configuration.MigrationType;
using static grate.Migration.ConnectionType;
using static TestCommon.TestInfrastructure.DescriptiveTestObjects;

namespace Basic_tests.Infrastructure.FolderConfiguration;

Expand Down Expand Up @@ -43,20 +43,20 @@ public void Returns_folders_in_same_order_as_default()
[MemberData(nameof(ExpectedKnownFolderNames))]
public void Has_expected_folder_configuration(
MigrationsFolder folder,
string expectedName,
MigrationType expectedType,
ConnectionType expectedConnectionType,
TransactionHandling transactionHandling
string name,
MigrationType type,
ConnectionType conn,
TransactionHandling tran
)
{
var root = Root.ToString();

Assert.Multiple(() =>
{
folder.Path.Should().Be(expectedName);
folder.Type.Should().Be(expectedType);
folder.ConnectionType.Should().Be(expectedConnectionType);
folder.TransactionHandling.Should().Be(transactionHandling);
folder.Path.Should().Be(name);
folder.Type.Should().Be(type);
folder.ConnectionType.Should().Be(conn);
folder.TransactionHandling.Should().Be(tran);
});
}

Expand All @@ -81,22 +81,26 @@ TransactionHandling transactionHandling

private static readonly DirectoryInfo Root = TestConfig.CreateRandomTempDirectory();
private static readonly IFoldersConfiguration Folders = FoldersConfiguration.Default(OverriddenFolderNames);

public static IEnumerable<object?[]> ExpectedKnownFolderNames()
public static TheoryData<MigrationsFolderWithDescription?, string, MigrationType, ConnectionType, TransactionHandling> ExpectedKnownFolderNames()
{
yield return new object?[] { Folders[BeforeMigration], OverriddenFolderNames.BeforeMigration, EveryTime, Default, TransactionHandling.Autonomous };
yield return new object?[] { Folders[AlterDatabase], OverriddenFolderNames.AlterDatabase, AnyTime, Admin, TransactionHandling.Autonomous };
yield return new object?[] { Folders[RunAfterCreateDatabase], OverriddenFolderNames.RunAfterCreateDatabase, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[RunBeforeUp], OverriddenFolderNames.RunBeforeUp, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[Up], OverriddenFolderNames.Up, Once, Default, TransactionHandling.Default };
yield return new object?[] { Folders[RunFirstAfterUp], OverriddenFolderNames.RunFirstAfterUp, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[Functions], OverriddenFolderNames.Functions, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[Views], OverriddenFolderNames.Views, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[Sprocs], OverriddenFolderNames.Sprocs, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[Triggers], OverriddenFolderNames.Triggers, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[Indexes], OverriddenFolderNames.Indexes, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[RunAfterOtherAnyTimeScripts], OverriddenFolderNames.RunAfterOtherAnyTimeScripts, AnyTime, Default, TransactionHandling.Default };
yield return new object?[] { Folders[Permissions], OverriddenFolderNames.Permissions, EveryTime, Default, TransactionHandling.Autonomous };
yield return new object?[] { Folders[AfterMigration], OverriddenFolderNames.AfterMigration, EveryTime, Default, TransactionHandling.Autonomous };
var data = new TheoryData<MigrationsFolderWithDescription?, string, MigrationType, ConnectionType, TransactionHandling>
{
{ Describe(Folders[BeforeMigration]), OverriddenFolderNames.BeforeMigration, EveryTime, Default, TransactionHandling.Autonomous },
{ Describe(Folders[AlterDatabase]), OverriddenFolderNames.AlterDatabase, AnyTime, Admin, TransactionHandling.Autonomous },
{ Describe(Folders[RunAfterCreateDatabase]), OverriddenFolderNames.RunAfterCreateDatabase, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[RunBeforeUp]), OverriddenFolderNames.RunBeforeUp, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[Up]), OverriddenFolderNames.Up, Once, Default, TransactionHandling.Default },
{ Describe(Folders[RunFirstAfterUp]), OverriddenFolderNames.RunFirstAfterUp, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[Functions]), OverriddenFolderNames.Functions, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[Views]), OverriddenFolderNames.Views, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[Sprocs]), OverriddenFolderNames.Sprocs, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[Triggers]), OverriddenFolderNames.Triggers, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[Indexes]), OverriddenFolderNames.Indexes, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[RunAfterOtherAnyTimeScripts]), OverriddenFolderNames.RunAfterOtherAnyTimeScripts, AnyTime, Default, TransactionHandling.Default },
{ Describe(Folders[Permissions]), OverriddenFolderNames.Permissions, EveryTime, Default, TransactionHandling.Autonomous },
{ Describe(Folders[AfterMigration]), OverriddenFolderNames.AfterMigration, EveryTime, Default, TransactionHandling.Autonomous }
};
return data;
}
}
5 changes: 5 additions & 0 deletions unittests/Basic_tests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
6 changes: 6 additions & 0 deletions unittests/MariaDB/MariaDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<ProjectReference Include="..\TestCommon\TestCommon.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>



</Project>
5 changes: 5 additions & 0 deletions unittests/MariaDB/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
6 changes: 6 additions & 0 deletions unittests/Oracle/Oracle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
<ProjectReference Include="..\TestCommon\TestCommon.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


</Project>
5 changes: 5 additions & 0 deletions unittests/Oracle/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
6 changes: 6 additions & 0 deletions unittests/PostgreSQL/PostgreSQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
<ProjectReference Include="..\TestCommon\TestCommon.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>




Expand Down
5 changes: 5 additions & 0 deletions unittests/PostgreSQL/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
6 changes: 6 additions & 0 deletions unittests/SqlServer/SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
<ProjectReference Include="..\TestCommon\TestCommon.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


</Project>
12 changes: 5 additions & 7 deletions unittests/SqlServer/TestInfrastructure/SqlServerTestContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
namespace TestCommon.TestInfrastructure;
public class SqlServerTestContainer : ContainerFixture
{
// on arm64 (M1), the standard mssql/server image is not available
public string? DockerImage => RuntimeInformation.ProcessArchitecture switch
{
Arm64 => "mcr.microsoft.com/azure-sql-edge:latest",
X64 => "mcr.microsoft.com/mssql/server:2019-latest",
var other => throw new PlatformNotSupportedException("Unsupported platform for running tests: " + other)
};

// Run with linux/amd86 on ARM architectures too, the docker emulation is good enough
public string? DockerImage => "mcr.microsoft.com/mssql/server:2019-latest";

public int Port = 1433;
public SqlServerTestContainer()
{
TestContainer = new MsSqlBuilder()
.WithImage(DockerImage)
.WithEnvironment("DOCKER_DEFAULT_PLATFORM", "linux/amd64")
.WithPassword(AdminPassword)
.WithPortBinding(Port, true)
.WithEnvironment("MSSQL_COLLATION", "Danish_Norwegian_CI_AS")
Expand Down
5 changes: 5 additions & 0 deletions unittests/SqlServer/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
<ProjectReference Include="..\TestCommon\TestCommon.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


</Project>
5 changes: 5 additions & 0 deletions unittests/SqlServerCaseSensitive/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
6 changes: 6 additions & 0 deletions unittests/Sqlite/Sqlite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@
<ProjectReference Include="..\TestCommon\TestCommon.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


</Project>
5 changes: 5 additions & 0 deletions unittests/Sqlite/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
41 changes: 23 additions & 18 deletions unittests/TestCommon/Generic/GenericMigrationTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,33 @@ public async Task Is_created_even_if_scripts_fail(string tableName)
}
scripts.Should().NotBeNull();
}

// [Theory]
// [InlineData("ScriptsRun")]
// [InlineData("ScriptsRunErrors")]
// [InlineData("Version")]
// public async Task Migration_does_not_fail_if_table_already_exists(string tableName)
// {
// var db = "MonoBonoJono";

// var parent = TestConfig.CreateRandomTempDirectory();
// var knownFolders = FoldersConfiguration.Default(null);

// await using (var migrator = Context.GetMigrator(db, parent, knownFolders))
// {
// await migrator.Migrate();
// }

// // Run migration again - make sure it does not throw an exception
// await using (var migrator = Context.GetMigrator(db, parent, knownFolders))
// {
// Assert.DoesNotThrowAsync(() => migrator.Migrate());
// }
// }
//public async Task Migration_does_not_fail_if_table_already_exists(string tableName)
[Fact]
public async Task Migration_does_not_fail_if_table_already_exists()
{
var db = "MonoBonoJono";

var parent = TestConfig.CreateRandomTempDirectory();
var knownFolders = FoldersConfiguration.Default(null);

await using (var migrator = Context.GetMigrator(db, parent, knownFolders))
{
await migrator.Migrate();
}

// Run migration again - make sure it does not throw an exception
await using (var migrator = Context.GetMigrator(db, parent, knownFolders))
{
var exception = await Record.ExceptionAsync(() => migrator.Migrate());
Assert.Null(exception);
}
}

[Theory]
[InlineData("version")]
[InlineData("vErSiON")]
Expand Down
Loading

0 comments on commit 99666bd

Please sign in to comment.