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

Unit test with testcontainers #405

Merged
merged 11 commits into from
Dec 17, 2023
10 changes: 7 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
Note: the following references are exclusively used in the test projects:
-->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="NUnit" Version="4.0.1" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageVersion Include="NUnit.Analyzers" Version="3.10.0" />
<PackageVersion Include="xunit" Version="2.5.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="XunitXml.TestLogger" Version="3.1.17" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="Testcontainers" Version="3.6.0" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.6.0" />
<PackageVersion Include="Testcontainers.MariaDb" Version="3.6.0" />
<PackageVersion Include="Testcontainers.Oracle" Version="3.6.0" />
<PackageVersion Include="Testcontainers.MsSql" Version="3.6.0" />
</ItemGroup>
</Project>
20 changes: 10 additions & 10 deletions grate/Commands/FoldersCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public static IFoldersConfiguration Parse(string? arg)
if (IsFile(arg))
{
arg = File.Exists(arg) ? File.ReadAllText(arg) : "";

// Makes more sense to supply an empty config than the default if you actually supply a file,
// but that file is either non-existant or empty
if (string.IsNullOrEmpty(arg))
{
return FoldersConfiguration.Empty;
}
}

string? content = arg switch
{
{ Length: 0 } => null,
Expand Down Expand Up @@ -77,7 +77,7 @@ private static IFoldersConfiguration ParseNewCustomFoldersConfiguration(string s
}
ApplyConfig(folder, config);
}

return foldersConfiguration;
}

Expand Down Expand Up @@ -109,7 +109,7 @@ private static void ApplyConfig(MigrationsFolder folder, string folderConfig)

return;
}


var tokens = folderConfig.Split(',');
foreach (var token in tokens)
Expand All @@ -124,8 +124,8 @@ private static void ApplyConfig(MigrationsFolder folder, string folderConfig)
throw new InvalidFolderConfiguration(folderConfig, key);
}

var parsed = (propertyType?.IsEnum ?? false)
? Enum.Parse(propertyType, value)
var parsed = (propertyType?.IsEnum ?? false)
? Enum.Parse(propertyType, value)
: value;

setter.Invoke(folder, new[] { parsed });
Expand All @@ -147,13 +147,13 @@ private static (MethodInfo? setter, Type? propertyType) GetProperty(string prope

private static (string key, string value) SplitInTwo(string s, char separator)
{
var keyAndValue = s.Split(separator, 2);
var (key, value) = (keyAndValue.First(), keyAndValue.Last());
return (key, value);
var keyAndValue = s.Split(separator, 2);
var (key, value) = (keyAndValue.First(), keyAndValue.Last());
return (key, value);
}

private static bool IsFile(string? s)
{
return s is not null && (File.Exists(s) || s.StartsWith("/")) ;
return s is not null && (File.Exists(s) || s.StartsWith("/"));
}
}
2 changes: 1 addition & 1 deletion grate/Configuration/ApplicationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public static class ApplicationInfo
{
public static string Name => "grate";
public static string Version => typeof(ApplicationInfo).Assembly.GetName().Version?.ToString() ?? "0.0.0.1";
}
}
2 changes: 1 addition & 1 deletion grate/Configuration/ArgumentParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class ArgumentParsers
{
return new GrateEnvironment(result.Tokens[0].Value);
}

result.ErrorMessage = $"Arg specified multiple times.";

return default;
Expand Down
20 changes: 10 additions & 10 deletions grate/Configuration/FoldersConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@

namespace grate.Configuration;

public class FoldersConfiguration: Dictionary<string, MigrationsFolder?>, IFoldersConfiguration
public class FoldersConfiguration : Dictionary<string, MigrationsFolder?>, IFoldersConfiguration
{
public FoldersConfiguration(IEnumerable<MigrationsFolder> folders) :
base(folders.ToDictionary(folder => folder.Name, folder => (MigrationsFolder?) folder))
base(folders.ToDictionary(folder => folder.Name, folder => (MigrationsFolder?)folder))
{
}

public FoldersConfiguration(params MigrationsFolder[] folders) :
this(folders.AsEnumerable())
{ }
public FoldersConfiguration(IDictionary<string, MigrationsFolder> source)
: base(source.ToDictionary(item => item.Key, item => (MigrationsFolder?) item.Value))

public FoldersConfiguration(IDictionary<string, MigrationsFolder> source)
: base(source.ToDictionary(item => item.Key, item => (MigrationsFolder?)item.Value))
{ }

public FoldersConfiguration()

public FoldersConfiguration()
{ }

public MigrationsFolder? CreateDatabase { get; set; }
public MigrationsFolder? DropDatabase { get; set; }

public static FoldersConfiguration Empty => new();

public static IFoldersConfiguration Default(IKnownFolderNames? folderNames = null)
{
folderNames ??= KnownFolderNames.Default;
Expand All @@ -52,7 +52,7 @@ public static IFoldersConfiguration Default(IKnownFolderNames? folderNames = nul
};
foldersConfiguration.CreateDatabase = new MigrationsFolder("CreateDatabase", folderNames.CreateDatabase, AnyTime, ConnectionType.Admin, TransactionHandling.Autonomous);
foldersConfiguration.DropDatabase = new MigrationsFolder("DropDatabase", folderNames.DropDatabase, AnyTime, ConnectionType.Admin, TransactionHandling.Autonomous);

return foldersConfiguration;
}

Expand Down
2 changes: 1 addition & 1 deletion grate/Configuration/IFoldersConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace grate.Configuration;

public interface IFoldersConfiguration: IDictionary<string, MigrationsFolder?>
public interface IFoldersConfiguration : IDictionary<string, MigrationsFolder?>
erikbra marked this conversation as resolved.
Show resolved Hide resolved
{
MigrationsFolder? CreateDatabase { get; set; }
MigrationsFolder? DropDatabase { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion grate/Configuration/KnownFolderNames.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace grate.Configuration;

public record KnownFolderNames: IKnownFolderNames
public record KnownFolderNames : IKnownFolderNames
{
public string BeforeMigration { get; init; } = "beforeMigration";
public string CreateDatabase { get; init; } = "createDatabase";
Expand Down
4 changes: 2 additions & 2 deletions grate/Configuration/MigrationType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace grate.Configuration;
namespace grate.Configuration;

public enum MigrationType
{
Once,
AnyTime,
EveryTime
}
}
4 changes: 2 additions & 2 deletions grate/Exceptions/InvalidFolderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace grate.Exceptions;

public class InvalidFolderConfiguration: Exception
public class InvalidFolderConfiguration : Exception
{
public InvalidFolderConfiguration(
string? folderConfiguration,
string? propertyName): base("Invalid property name: " + propertyName + ". Folder configuration: " + folderConfiguration)
string? propertyName) : base("Invalid property name: " + propertyName + ". Folder configuration: " + folderConfiguration)
{ }
}
4 changes: 2 additions & 2 deletions grate/Exceptions/MigrationFailed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

namespace grate.Exceptions;

public class MigrationFailed: AggregateException
public class MigrationFailed : AggregateException
{
private const string ErrorMessage = "Migration failed due to errors";

public MigrationFailed(IEnumerable<Exception> exceptions)
: base(ErrorMessage, exceptions)
{
}

public MigrationFailed(params Exception[] exceptions)
: base(ErrorMessage, exceptions)
{ }
Expand Down
2 changes: 1 addition & 1 deletion grate/Exceptions/UnknownConnectionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace grate.Exceptions;

public class UnknownConnectionType: ArgumentOutOfRangeException
public class UnknownConnectionType : ArgumentOutOfRangeException
{
public UnknownConnectionType(object? connectionType,
[CallerArgumentExpression(nameof(connectionType))] string argumentName = "")
Expand Down
2 changes: 1 addition & 1 deletion grate/Exceptions/UnknownTransactionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace grate.Exceptions;

public class UnknownTransactionHandling: ArgumentOutOfRangeException
public class UnknownTransactionHandling : ArgumentOutOfRangeException
{
public UnknownTransactionHandling(object? transactionHandling,
[CallerArgumentExpression(nameof(transactionHandling))] string argumentName = "")
Expand Down
2 changes: 1 addition & 1 deletion grate/Infrastructure/BatchSplitterReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ private string ReplaceBatchSeparator(Match match)
var replacement = groups["BATCHSPLITTER"].Success ? Replacement : string.Empty;
return groups["KEEP1"].Value + replacement + groups["KEEP2"].Value;
}
}
}
2 changes: 1 addition & 1 deletion grate/Infrastructure/CliCommandsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public static IServiceCollection AddCliCommands(this IServiceCollection services

return services;
}
}
}
2 changes: 1 addition & 1 deletion grate/Infrastructure/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public static class EnumerableExtensions
{
public static IEnumerable<T> Safe<T>(this T[]? source) => source ?? Enumerable.Empty<T>();
public static IEnumerable<T> Safe<T>(this IEnumerable<T>? source) => source ?? Enumerable.Empty<T>();
}
}
4 changes: 2 additions & 2 deletions grate/Infrastructure/Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public void AddService<TKey, TValue>(TKey name)
public TValue GetService<TKey, TValue>(TKey key)
{
if (key == null) throw new ArgumentNullException(nameof(key));
return (TValue) _provider.GetRequiredService(_services[key]);
return (TValue)_provider.GetRequiredService(_services[key]);
}
}
}
14 changes: 7 additions & 7 deletions grate/Infrastructure/GrateConsoleColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private GrateConsoleColor(string ansiColorCode, ConsoleColor fallback)
AnsiColorCode = ansiColorCode;
Fallback = fallback;
}

public static class Foreground
{
private static class AnsiColors
Expand All @@ -34,12 +34,12 @@ private static class AnsiColors
internal const string Magenta = "\x1B[1m\x1B[35m";
internal const string Cyan = "\x1B[1m\x1B[36m";
internal const string White = "\x1B[1m\x1B[37m";

internal static string Rgb(int r, int g, int b) => $"\x1b[38;2;{r};{g};{b}m";
// ReSharper restore MemberHidesStaticFromOuterClass
}
public static readonly GrateConsoleColor Default = new(AnsiColors.Default, ConsoleColor.White);

public static readonly GrateConsoleColor Default = new(AnsiColors.Default, ConsoleColor.White);
public static readonly GrateConsoleColor Info = Rgb(23, 162, 184);
public static readonly GrateConsoleColor Black = new(AnsiColors.Black, ConsoleColor.Black);
public static readonly GrateConsoleColor DarkRed = new(AnsiColors.DarkRed, ConsoleColor.DarkRed);
Expand All @@ -49,7 +49,7 @@ private static class AnsiColors
public static readonly GrateConsoleColor DarkMagenta = new(AnsiColors.DarkMagenta, ConsoleColor.DarkMagenta);
public static readonly GrateConsoleColor DarkCyan = new(AnsiColors.DarkCyan, ConsoleColor.DarkCyan);
public static readonly GrateConsoleColor Gray = new(AnsiColors.Gray, ConsoleColor.Gray);
public static readonly GrateConsoleColor DarkGray = Rgb(192,192,192);
public static readonly GrateConsoleColor DarkGray = Rgb(192, 192, 192);
public static readonly GrateConsoleColor Red = new(AnsiColors.Red, ConsoleColor.Red);
public static readonly GrateConsoleColor Green = new(AnsiColors.Green, ConsoleColor.Green);
public static readonly GrateConsoleColor Yellow = new(AnsiColors.Yellow, ConsoleColor.Yellow);
Expand Down Expand Up @@ -81,8 +81,8 @@ private static class AnsiColors
internal static string Rgb(int r, int g, int b) => $"\x1b[48;2;{r};{g};{b}m";
// ReSharper restore MemberHidesStaticFromOuterClass
}
public static readonly GrateConsoleColor Default = new(AnsiColors.Default, ConsoleColor.Black);

public static readonly GrateConsoleColor Default = new(AnsiColors.Default, ConsoleColor.Black);
public static readonly GrateConsoleColor Info = Rgb(23, 162, 184);
public static readonly GrateConsoleColor Black = new(AnsiColors.Black, ConsoleColor.Black);
public static readonly GrateConsoleColor DarkRed = new(AnsiColors.DarkRed, ConsoleColor.DarkRed);
Expand Down
2 changes: 1 addition & 1 deletion grate/Infrastructure/GrateEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GrateEnvironment
private bool IsForCurrentEnvironment(string path) =>
FileName(path).StartsWith($"{Current}.", InvariantCultureIgnoreCase) ||
FileName(path).Contains($".{Current}.", InvariantCultureIgnoreCase);

public static bool IsEnvironmentFile(string fileName) => fileName.Contains(EnvironmentMarker, InvariantCultureIgnoreCase);
private static string FileName(string path) => new FileInfo(path).Name;

Expand Down
4 changes: 2 additions & 2 deletions grate/Infrastructure/IFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace grate.Infrastructure;
namespace grate.Infrastructure;

public interface IFactory
{
public TValue GetService<TKey, TValue>(TKey key);
}
}
2 changes: 1 addition & 1 deletion grate/Infrastructure/IHashGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
public interface IHashGenerator
{
string Hash(string text);
}
}
2 changes: 1 addition & 1 deletion grate/Infrastructure/ISyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public interface ISyntax
string Quote(string text);
string PrimaryKeyColumn(string columnName);
string PrimaryKeyConstraint(string tableName, string column);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace grate.Infrastructure.Npgsql;

public class DummyNpgsqlParameterCollection: Dictionary<string, DummyNpgsqlParameter>
public class DummyNpgsqlParameterCollection : Dictionary<string, DummyNpgsqlParameter>
{
public void Add(DummyNpgsqlParameter parameter) => Add(parameter.ParameterName, parameter);
public DummyPlaceholderType PlaceholderType => DummyPlaceholderType.Whatever;
Expand Down
6 changes: 3 additions & 3 deletions grate/Infrastructure/Npgsql/NativeSqlQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace grate.Infrastructure.Npgsql;
// This makes updating the class easier if it should change in the future.
using NpgsqlBatchCommand = DummyBatchCommand;
using NpgsqlCommand = DummyNpgsqlCommand;
using NpgsqlParameterCollection = DummyNpgsqlParameterCollection;
using NpgsqlParameter = DummyNpgsqlParameter;
using ThrowHelper = DummyThrowHelper;
using NpgsqlParameterCollection = DummyNpgsqlParameterCollection;
using PlaceholderType = DummyPlaceholderType;
using ThrowHelper = DummyThrowHelper;


/// <summary>
Expand All @@ -29,7 +29,7 @@ public static IEnumerable<string> Split(string sql)
return statements;
}


/// <summary>
///
/// Method "stolen" from Unit-test in Npgsql: https://github.com/npgsql/npgsql/blob/main/test/Npgsql.Tests/SqlQueryParserTests.cs#L196
Expand Down
2 changes: 1 addition & 1 deletion grate/Infrastructure/Npgsql/ReflectionNpgsqlQueryParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down
Loading