From c3142a8564e9f001c5171f0ad8511dceda34dc02 Mon Sep 17 00:00:00 2001 From: Ilan Uzan Date: Tue, 31 Dec 2024 18:27:28 +0100 Subject: [PATCH] refactor tests around supported query annotations --- .github/workflows/legacy-tests.yml | 24 ++- Drivers/DbDriver.cs | 6 +- Drivers/MySqlConnectorDriver.cs | 3 +- Drivers/NpgsqlDriver.cs | 2 +- Drivers/QueryAnnotations.cs | 13 ++ Drivers/SqliteDriver.cs | 2 +- .../DataGenerator.cs | 0 EndToEndCommon/EndToEndCommon.cs | 59 +++++++ EndToEndCommon/EndToEndCommon.csproj | 24 +++ EndToEndCommon/TestInterfaces.cs | 29 ++++ EndToEndTests/DataGenerator.cs | 14 -- EndToEndTests/EndToEndTests.csproj | 1 + EndToEndTests/GlobalSetup.cs | 65 ++------ EndToEndTests/MySqlConnectorDapperTester.cs | 61 +++---- EndToEndTests/MySqlConnectorTester.cs | 56 +++---- EndToEndTests/NpgsqlDapperTester.cs | 143 ++++++++-------- EndToEndTests/NpgsqlTester.cs | 154 ++++++++---------- EndToEndTests/SqliteDapperTester.cs | 51 +++--- EndToEndTests/SqliteTester.cs | 50 +++--- LegacyEndToEndTests/GlobalSetup.cs | 48 +----- .../LegacyEndToEndTests.csproj | 1 + .../MySqlConnectorDapperTester.cs | 42 ++--- LegacyEndToEndTests/MySqlConnectorTester.cs | 42 ++--- LegacyEndToEndTests/NpgsqlDapperTester.cs | 114 +++++-------- LegacyEndToEndTests/NpgsqlTester.cs | 140 ++++++++-------- LegacyEndToEndTests/SqliteDapperTester.cs | 85 +++++----- LegacyEndToEndTests/SqliteTester.cs | 49 +++--- Makefile | 9 +- .../MySqlConnectorDapperExample/QuerySql.cs | 14 -- .../QuerySql.cs | 14 -- examples/MySqlConnectorExample/QuerySql.cs | 14 -- .../MySqlConnectorLegacyExample/QuerySql.cs | 22 --- examples/config/mysql/query.sql | 3 - sqlc-gen-csharp.sln | 6 + 34 files changed, 637 insertions(+), 723 deletions(-) rename {LegacyEndToEndTests => EndToEndCommon}/DataGenerator.cs (100%) create mode 100644 EndToEndCommon/EndToEndCommon.cs create mode 100644 EndToEndCommon/EndToEndCommon.csproj create mode 100644 EndToEndCommon/TestInterfaces.cs delete mode 100644 EndToEndTests/DataGenerator.cs diff --git a/.github/workflows/legacy-tests.yml b/.github/workflows/legacy-tests.yml index 27216b37..3daca148 100644 --- a/.github/workflows/legacy-tests.yml +++ b/.github/workflows/legacy-tests.yml @@ -59,20 +59,34 @@ jobs: - name: Install MySQL and restore test projects in parallel shell: pwsh run: | - $mysql_job = Start-Job -ScriptBlock { + $mysqlJob = Start-Job -ScriptBlock { choco install mysql --no-progress --version=8.0.31 -y --params "/serviceName:MySQL" + return $LASTEXITCODE } - $restore_job = Start-Job -ScriptBlock { + + $restoreJob = Start-Job -ScriptBlock { Get-ChildItem -Path examples -Recurse -Filter *.csproj | Where-Object { $_.FullName -like '*Legacy*' } | ForEach-Object { nuget restore $_.FullName } + nuget restore ./EndToEndCommon/EndToEndCommon.csproj nuget restore ./LegacyEndToEndTests/LegacyEndToEndTests.csproj msbuild.exe ./LegacyEndToEndTests/LegacyEndToEndTests.csproj -p:Configuration=Release -p:FrameworkVersion=v4.7.2 + return $LASTEXITCODE + } + + $mysqlOutput = Receive-Job -Job $mysqlJob -Wait + $mysqlExitCode = @($mysqlOutput)[-1] + if ($mysqlExitCode -ne 0) { + Write-Error "MySQL install failed with exit code: $mysqlExitCode" + return $mysqlExitCode } - Wait-Job -Job $mysql_job, $restore_job - Receive-Job -Job $mysql_job - Receive-Job -Job $restore_job + $restoreOutput = Receive-Job -Job $restoreJob -Wait + $restoreExitCode = @($restoreOutput)[-1] + if ($restoreExitCode -ne 0) { + Write-Error "Restore/build failed with exit code: $restoreExitCode" + return $restoreExitCode + } - name: Init MySQL Schema shell: powershell diff --git a/Drivers/DbDriver.cs b/Drivers/DbDriver.cs index f1e76ad9..13d86b80 100644 --- a/Drivers/DbDriver.cs +++ b/Drivers/DbDriver.cs @@ -86,9 +86,11 @@ public string GetColumnReader(Column column, int ordinal) public abstract string CreateSqlCommand(string sqlTextConstant); - public abstract MemberDeclarationSyntax OneDeclare(string sqlTextConstant, string argInterface, string returnInterface, Query query); + public abstract MemberDeclarationSyntax OneDeclare(string sqlTextConstant, string argInterface, + string returnInterface, Query query); - public abstract MemberDeclarationSyntax ManyDeclare(string sqlTextConstant, string argInterface, string returnInterface, Query query); + public abstract MemberDeclarationSyntax ManyDeclare(string sqlTextConstant, string argInterface, + string returnInterface, Query query); public abstract MemberDeclarationSyntax ExecDeclare(string text, string argInterface, Query query); diff --git a/Drivers/MySqlConnectorDriver.cs b/Drivers/MySqlConnectorDriver.cs index 246099e0..dd18d631 100644 --- a/Drivers/MySqlConnectorDriver.cs +++ b/Drivers/MySqlConnectorDriver.cs @@ -1,7 +1,6 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Plugin; using SqlcGenCsharp.Drivers.Generators; -using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; @@ -10,7 +9,7 @@ namespace SqlcGenCsharp.Drivers; -public partial class MySqlConnectorDriver(Options options) : DbDriver(options), IExecLastId, IExecRows +public partial class MySqlConnectorDriver(Options options) : DbDriver(options), IOne, IMany, IExec, IExecLastId, IExecRows { protected override List ColumnMappings { get; } = [ diff --git a/Drivers/NpgsqlDriver.cs b/Drivers/NpgsqlDriver.cs index 74368d25..47fbfe09 100644 --- a/Drivers/NpgsqlDriver.cs +++ b/Drivers/NpgsqlDriver.cs @@ -8,7 +8,7 @@ namespace SqlcGenCsharp.Drivers; -public class NpgsqlDriver : DbDriver, ICopyFrom, IExecRows +public class NpgsqlDriver : DbDriver, IOne, IMany, IExec, ICopyFrom, IExecRows { public NpgsqlDriver(Options options) : base(options) { diff --git a/Drivers/QueryAnnotations.cs b/Drivers/QueryAnnotations.cs index 669c8159..3ef019e1 100644 --- a/Drivers/QueryAnnotations.cs +++ b/Drivers/QueryAnnotations.cs @@ -3,6 +3,19 @@ namespace SqlcGenCsharp.Drivers; +public interface IOne +{ + MemberDeclarationSyntax OneDeclare(string queryTextConstant, string argInterface, string returnInterface, Query query); +} +public interface IMany +{ + MemberDeclarationSyntax ManyDeclare(string queryTextConstant, string argInterface, string returnInterface, Query query); +} +public interface IExec +{ + MemberDeclarationSyntax ExecDeclare(string queryTextConstant, string argInterface, Query query); +} + public interface IExecLastId { MemberDeclarationSyntax ExecLastIdDeclare(string queryTextConstant, string argInterface, Query query); diff --git a/Drivers/SqliteDriver.cs b/Drivers/SqliteDriver.cs index 74dfac62..3878e2b9 100644 --- a/Drivers/SqliteDriver.cs +++ b/Drivers/SqliteDriver.cs @@ -8,7 +8,7 @@ namespace SqlcGenCsharp.Drivers; -public partial class SqliteDriver(Options options) : DbDriver(options), IExecRows +public partial class SqliteDriver(Options options) : DbDriver(options), IOne, IMany, IExec, IExecRows { protected override List ColumnMappings { get; } = [ new("byte[]", ordinal => $"Utils.GetBytes(reader, {ordinal})", diff --git a/LegacyEndToEndTests/DataGenerator.cs b/EndToEndCommon/DataGenerator.cs similarity index 100% rename from LegacyEndToEndTests/DataGenerator.cs rename to EndToEndCommon/DataGenerator.cs diff --git a/EndToEndCommon/EndToEndCommon.cs b/EndToEndCommon/EndToEndCommon.cs new file mode 100644 index 00000000..fe45af14 --- /dev/null +++ b/EndToEndCommon/EndToEndCommon.cs @@ -0,0 +1,59 @@ +using dotenv.net; +using Microsoft.Data.Sqlite; +using System; +using System.IO; +using System.Text.RegularExpressions; + +namespace SqlcGenCsharpTests +{ + public static class EndToEndCommon + { + private const string EnvFile = ".env"; + private const string SchemaFile = "sqlite.schema.sql"; + + public const string PostgresConnectionStringEnv = "POSTGRES_CONNECTION_STRING"; + public const string MySqlConnectionStringEnv = "MYSQL_CONNECTION_STRING"; + public const string SqliteConnectionStringEnv = "SQLITE_CONNECTION_STRING"; + + public static void SetUp() + { + if (File.Exists(EnvFile)) + DotEnv.Load(options: new DotEnvOptions(envFilePaths: new[] { EnvFile })); + RemoveExistingSqliteDb(); + if (File.Exists(SchemaFile)) + InitSqliteDb(); + } + + public static void TearDown() + { + RemoveExistingSqliteDb(); + } + + private static void RemoveExistingSqliteDb() + { + var connectionString = Environment.GetEnvironmentVariable(SqliteConnectionStringEnv); + if (connectionString == null) return; + + var dbFilename = SqliteFilenameRegex.Match(connectionString).Groups[1].Value; + Console.WriteLine($"Removing sqlite db from {dbFilename}"); + if (File.Exists(dbFilename)) + File.Delete(dbFilename); + } + private static void InitSqliteDb() + { + var schemaSql = File.ReadAllText(SchemaFile); + var connectionString = Environment.GetEnvironmentVariable(EndToEndCommon.SqliteConnectionStringEnv); + using (var connection = new SqliteConnection(connectionString)) + { + connection.Open(); + using (var command = connection.CreateCommand()) + { + command.CommandText = schemaSql; + command.ExecuteNonQuery(); + } + } + } + + private static readonly Regex SqliteFilenameRegex = new Regex(@"Data Source=([\w\.\/\-]+\.db);", RegexOptions.Compiled); + } +} \ No newline at end of file diff --git a/EndToEndCommon/EndToEndCommon.csproj b/EndToEndCommon/EndToEndCommon.csproj new file mode 100644 index 00000000..fd48244e --- /dev/null +++ b/EndToEndCommon/EndToEndCommon.csproj @@ -0,0 +1,24 @@ + + + + .netstandard2.0 + SqlcGenCsharpTests + + + + + Always + + + Always + sqlite.schema.sql + + + + + + + + + + diff --git a/EndToEndCommon/TestInterfaces.cs b/EndToEndCommon/TestInterfaces.cs new file mode 100644 index 00000000..6e49ace9 --- /dev/null +++ b/EndToEndCommon/TestInterfaces.cs @@ -0,0 +1,29 @@ +using System.Threading.Tasks; + +namespace SqlcGenCsharpTests +{ + public interface IOneTester + { + Task TestOne(); + } + + public interface IManyTester + { + Task TestMany(); + } + + public interface IExecTester + { + Task TestExec(); + } + + public interface IExecRowsTester + { + Task TestExecRows(); + } + + public interface ICopyFromTester + { + Task TestCopyFrom(); + } +} \ No newline at end of file diff --git a/EndToEndTests/DataGenerator.cs b/EndToEndTests/DataGenerator.cs deleted file mode 100644 index 354ddb1c..00000000 --- a/EndToEndTests/DataGenerator.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace SqlcGenCsharpTests; - -public static class DataGenerator -{ - public const string BojackAuthor = "Bojack Horseman"; - public const string BojackTheme = "Back in the 90s he was in a very famous TV show"; - - public const string DrSeussAuthor = "Dr. Seuss"; - public const string DrSeussQuote = "You'll miss the best things if you keep your eyes shut"; - - public const string GenericAuthor = "Albert Einstein"; - public const string GenericQuote1 = "Quote that everyone always attribute to Einstein"; - public const string GenericQuote2 = "Only 2 things are infinite, the universe and human stupidity"; -} \ No newline at end of file diff --git a/EndToEndTests/EndToEndTests.csproj b/EndToEndTests/EndToEndTests.csproj index 3bf52e66..73ede0de 100644 --- a/EndToEndTests/EndToEndTests.csproj +++ b/EndToEndTests/EndToEndTests.csproj @@ -25,6 +25,7 @@ + diff --git a/EndToEndTests/GlobalSetup.cs b/EndToEndTests/GlobalSetup.cs index bc46241d..fd60c019 100644 --- a/EndToEndTests/GlobalSetup.cs +++ b/EndToEndTests/GlobalSetup.cs @@ -1,57 +1,20 @@ -using dotenv.net; -using Microsoft.Data.Sqlite; using NUnit.Framework; -using System; -using System.IO; -using System.Text.RegularExpressions; -namespace SqlcGenCsharpTests; - -[SetUpFixture] -public partial class GlobalSetup +namespace SqlcGenCsharpTests { - private const string EnvFile = ".env"; - private const string SchemaFile = "sqlite.schema.sql"; - - public const string PostgresConnectionStringEnv = "POSTGRES_CONNECTION_STRING"; - public const string MySqlConnectionStringEnv = "MYSQL_CONNECTION_STRING"; - public const string SqliteConnectionStringEnv = "SQLITE_CONNECTION_STRING"; - - [OneTimeSetUp] - public void LoadEnvFile() + [SetUpFixture] + public class GlobalSetup { - if (File.Exists(EnvFile)) - DotEnv.Load(options: new DotEnvOptions(envFilePaths: [EnvFile])); - RemoveExistingDb(); - if (File.Exists(SchemaFile)) - InitSqliteDb(); + [OneTimeSetUp] + public void SetUp() + { + EndToEndCommon.SetUp(); + } + + [OneTimeTearDown] + public void TearDown() + { + EndToEndCommon.TearDown(); + } } - - private static void RemoveExistingDb() - { - var connectionString = Environment.GetEnvironmentVariable(SqliteConnectionStringEnv)!; - var dbFilename = SqliteFilenameRegex().Match(connectionString).Groups[1].Value; - if (File.Exists(dbFilename)) - File.Delete(dbFilename); - } - private static void InitSqliteDb() - { - var schemaSql = File.ReadAllText(SchemaFile); - var connectionString = Environment.GetEnvironmentVariable(GlobalSetup.SqliteConnectionStringEnv); - using var connection = new SqliteConnection(connectionString); - connection.Open(); - - using var command = connection.CreateCommand(); - command.CommandText = schemaSql; - command.ExecuteNonQuery(); - } - - [OneTimeTearDown] - public void TearDown() - { - RemoveExistingDb(); - } - - [GeneratedRegex("Data Source=((/|\\w)+.db);")] - private static partial Regex SqliteFilenameRegex(); } \ No newline at end of file diff --git a/EndToEndTests/MySqlConnectorDapperTester.cs b/EndToEndTests/MySqlConnectorDapperTester.cs index 5716964f..a75f53ce 100644 --- a/EndToEndTests/MySqlConnectorDapperTester.cs +++ b/EndToEndTests/MySqlConnectorDapperTester.cs @@ -6,16 +6,10 @@ namespace SqlcGenCsharpTests; -public class MySqlConnectorDapperTester +public class MySqlConnectorDapperTester : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; set; } - - [OneTimeSetUp] - public void SetUp() - { - var connectionString = Environment.GetEnvironmentVariable(GlobalSetup.MySqlConnectionStringEnv); - QuerySql = new QuerySql(connectionString!); - } + private QuerySql QuerySql { get; } = new( + Environment.GetEnvironmentVariable(EndToEndCommon.MySqlConnectionStringEnv)!); [TearDown] public async Task EmptyTestsTable() @@ -24,7 +18,7 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -37,27 +31,20 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actualAuthors = await QuerySql.ListAuthors(); - ClassicAssert.AreEqual(2, actualAuthors.Count); - Assert.That(actualAuthors is [ + var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs + { + Name = DataGenerator.BojackAuthor + }); + ClassicAssert.IsNotNull(actualAuthor); + Assert.That(actualAuthor is { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme - }, - { - Name: DataGenerator.DrSeussAuthor, - Bio: DataGenerator.DrSeussQuote - } - ]); - - // foreach (var a in actualAuthors) - // { - // Assert.That(a.Created >= DateTime.Now.Subtract(TimeSpan.FromSeconds(30)) && a.Created < DateTime.Now); - // } + }); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -70,20 +57,22 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs - { - Name = DataGenerator.BojackAuthor - }); - ClassicAssert.IsNotNull(actualAuthor); - Assert.That(actualAuthor is + var actualAuthors = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actualAuthors.Count); + Assert.That(actualAuthors is [ { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme - }); + }, + { + Name: DataGenerator.DrSeussAuthor, + Bio: DataGenerator.DrSeussQuote + } + ]); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -96,7 +85,9 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor }); + var deleteAuthorArgs = new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor }; + await QuerySql.DeleteAuthor(deleteAuthorArgs); + var authorRows = await QuerySql.ListAuthors(); ClassicAssert.AreEqual(1, authorRows.Count); Assert.That(authorRows[0] is @@ -107,7 +98,7 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { diff --git a/EndToEndTests/MySqlConnectorTester.cs b/EndToEndTests/MySqlConnectorTester.cs index 438546c0..541495de 100644 --- a/EndToEndTests/MySqlConnectorTester.cs +++ b/EndToEndTests/MySqlConnectorTester.cs @@ -6,16 +6,10 @@ namespace SqlcGenCsharpTests; -public class MySqlConnectorTester +public class MySqlConnectorTester : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; set; } - - [OneTimeSetUp] - public void SetUp() - { - var connectionString = Environment.GetEnvironmentVariable(GlobalSetup.MySqlConnectionStringEnv); - QuerySql = new QuerySql(connectionString!); - } + private QuerySql QuerySql { get; } = new( + Environment.GetEnvironmentVariable(EndToEndCommon.MySqlConnectionStringEnv)!); [TearDown] public async Task EmptyTestsTable() @@ -24,7 +18,7 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -37,22 +31,20 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actualAuthors = await QuerySql.ListAuthors(); - ClassicAssert.AreEqual(2, actualAuthors.Count); - Assert.That(actualAuthors is [ + var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs + { + Name = DataGenerator.BojackAuthor + }); + ClassicAssert.IsNotNull(actualAuthor); + Assert.That(actualAuthor is { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme - }, - { - Name: DataGenerator.DrSeussAuthor, - Bio: DataGenerator.DrSeussQuote - } - ]); + }); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -65,20 +57,22 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs - { - Name = DataGenerator.BojackAuthor - }); - ClassicAssert.IsNotNull(actualAuthor); - Assert.That(actualAuthor is + var actualAuthors = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actualAuthors.Count); + Assert.That(actualAuthors is [ { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme - }); + }, + { + Name: DataGenerator.DrSeussAuthor, + Bio: DataGenerator.DrSeussQuote + } + ]); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -91,7 +85,9 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor }); + var deleteAuthorArgs = new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor }; + await QuerySql.DeleteAuthor(deleteAuthorArgs); + var authorRows = await QuerySql.ListAuthors(); ClassicAssert.AreEqual(1, authorRows.Count); Assert.That(authorRows[0] is @@ -102,7 +98,7 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { diff --git a/EndToEndTests/NpgsqlDapperTester.cs b/EndToEndTests/NpgsqlDapperTester.cs index 0fa530b5..67058bfe 100644 --- a/EndToEndTests/NpgsqlDapperTester.cs +++ b/EndToEndTests/NpgsqlDapperTester.cs @@ -6,16 +6,10 @@ namespace SqlcGenCsharpTests; -public class NpgsqlDapperTester +public class NpgsqlDapperTester : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; set; } - - [OneTimeSetUp] - public void SetUp() - { - var connectionString = Environment.GetEnvironmentVariable(GlobalSetup.PostgresConnectionStringEnv); - QuerySql = new QuerySql(connectionString!); - } + private QuerySql QuerySql { get; } = new( + Environment.GetEnvironmentVariable(EndToEndCommon.PostgresConnectionStringEnv)!); [TearDown] public async Task EmptyTestsTable() @@ -24,90 +18,87 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); - var actualAuthors = await QuerySql.ListAuthors(); - ClassicAssert.AreEqual(2, actualAuthors.Count); - Assert.That( - actualAuthors is [ - { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme }, - { Name: DataGenerator.DrSeussAuthor, Bio: DataGenerator.DrSeussQuote } - ] - ); + var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs + { + Name = DataGenerator.BojackAuthor + }); + ClassicAssert.IsNotNull(actualAuthor); + Assert.That(actualAuthor is + { + Name: DataGenerator.BojackAuthor, + Bio: DataGenerator.BojackTheme + }); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); - var actualAuthor = await QuerySql.GetAuthor( - new QuerySql.GetAuthorArgs { Name = DataGenerator.BojackAuthor } - ); - ClassicAssert.IsNotNull(actualAuthor); - Assert.That( - actualAuthor is { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme } - ); + var actualAuthors = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actualAuthors.Count); + Assert.That(actualAuthors is [ + { + Name: DataGenerator.BojackAuthor, + Bio: DataGenerator.BojackTheme + }, + { + Name: DataGenerator.DrSeussAuthor, + Bio: DataGenerator.DrSeussQuote + } + ]); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); + + var deleteAuthorArgs = new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor }; + await QuerySql.DeleteAuthor(deleteAuthorArgs); - await QuerySql.DeleteAuthor( - new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor } - ); var authorRows = await QuerySql.ListAuthors(); ClassicAssert.AreEqual(1, authorRows.Count); - Assert.That( - authorRows[0] is { Name: DataGenerator.DrSeussAuthor, Bio: DataGenerator.DrSeussQuote } - ); + Assert.That(authorRows[0] is + { + Name: DataGenerator.DrSeussAuthor, + Bio: DataGenerator.DrSeussQuote + }); } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { diff --git a/EndToEndTests/NpgsqlTester.cs b/EndToEndTests/NpgsqlTester.cs index b0c5eb31..8854ac1c 100644 --- a/EndToEndTests/NpgsqlTester.cs +++ b/EndToEndTests/NpgsqlTester.cs @@ -7,18 +7,12 @@ namespace SqlcGenCsharpTests; -public class NpgsqlTester +public class NpgsqlTester : IOneTester, IManyTester, IExecTester, IExecRowsTester, ICopyFromTester { private static readonly Random Randomizer = new(); - private QuerySql QuerySql { get; set; } - - [OneTimeSetUp] - public void SetUp() - { - var connectionString = Environment.GetEnvironmentVariable(GlobalSetup.PostgresConnectionStringEnv); - QuerySql = new QuerySql(connectionString!); - } + private QuerySql QuerySql { get; } = new( + Environment.GetEnvironmentVariable(EndToEndCommon.PostgresConnectionStringEnv)!); [TearDown] public async Task EmptyTestsTables() @@ -28,99 +22,87 @@ public async Task EmptyTestsTables() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); - - var actualAuthors = await QuerySql.ListAuthors(); - ClassicAssert.AreEqual(2, actualAuthors.Count); - Assert.That( - actualAuthors - is [ - { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme }, - { Name: DataGenerator.DrSeussAuthor, Bio: DataGenerator.DrSeussQuote } - ] - ); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); - // foreach (var a in actualAuthors) - // { - // Assert.That( - // a.Created >= DateTime.Now.Subtract(TimeSpan.FromSeconds(30)) - // && a.Created < DateTime.Now - // ); - // } + var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs + { + Name = DataGenerator.BojackAuthor + }); + ClassicAssert.IsNotNull(actualAuthor); + Assert.That(actualAuthor is + { + Name: DataGenerator.BojackAuthor, + Bio: DataGenerator.BojackTheme + }); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); - var actualAuthor = await QuerySql.GetAuthor( - new QuerySql.GetAuthorArgs { Name = DataGenerator.BojackAuthor } - ); - ClassicAssert.IsNotNull(actualAuthor); - Assert.That( - actualAuthor is { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme } - ); + var actualAuthors = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actualAuthors.Count); + Assert.That(actualAuthors is [ + { + Name: DataGenerator.BojackAuthor, + Bio: DataGenerator.BojackTheme + }, + { + Name: DataGenerator.DrSeussAuthor, + Bio: DataGenerator.DrSeussQuote + } + ]); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); + + var deleteAuthorArgs = new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor }; + await QuerySql.DeleteAuthor(deleteAuthorArgs); - await QuerySql.DeleteAuthor( - new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor } - ); var authorRows = await QuerySql.ListAuthors(); ClassicAssert.AreEqual(1, authorRows.Count); - Assert.That( - authorRows[0] is { Name: DataGenerator.DrSeussAuthor, Bio: DataGenerator.DrSeussQuote } - ); + Assert.That(authorRows[0] is + { + Name: DataGenerator.DrSeussAuthor, + Bio: DataGenerator.DrSeussQuote + }); } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { @@ -139,7 +121,7 @@ public async Task TestExecRowsFlow() } [Test] - public async Task TestCopyFlow() + public async Task TestCopyFrom() { const int batchSize = 100; var createAuthorBatchArgs = Enumerable.Range(0, batchSize) diff --git a/EndToEndTests/SqliteDapperTester.cs b/EndToEndTests/SqliteDapperTester.cs index 6ec371e4..c7b1a8d3 100644 --- a/EndToEndTests/SqliteDapperTester.cs +++ b/EndToEndTests/SqliteDapperTester.cs @@ -6,17 +6,10 @@ namespace SqlcGenCsharpTests; -public class SqliteDapperTester +public class SqliteDapperTester : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; set; } - - [OneTimeSetUp] - public void SetUp() - { - var connectionString = Environment.GetEnvironmentVariable(GlobalSetup.SqliteConnectionStringEnv); - QuerySql = new QuerySql(connectionString!); - } - + private QuerySql QuerySql { get; } = new( + Environment.GetEnvironmentVariable(EndToEndCommon.SqliteConnectionStringEnv)!); [TearDown] public async Task EmptyTestsTable() { @@ -24,7 +17,7 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -37,21 +30,20 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actualAuthors = await QuerySql.ListAuthors(); - Assert.That(actualAuthors is [ + var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs + { + Name = DataGenerator.BojackAuthor + }); + ClassicAssert.IsNotNull(actualAuthor); + Assert.That(actualAuthor is { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme - }, - { - Name: DataGenerator.DrSeussAuthor, - Bio: DataGenerator.DrSeussQuote - } - ]); + }); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -64,19 +56,22 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs - { - Name = DataGenerator.BojackAuthor - }); - Assert.That(actualAuthor is + var actualAuthors = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actualAuthors.Count); + Assert.That(actualAuthors is [ { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme - }); + }, + { + Name: DataGenerator.DrSeussAuthor, + Bio: DataGenerator.DrSeussQuote + } + ]); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -102,7 +97,7 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { diff --git a/EndToEndTests/SqliteTester.cs b/EndToEndTests/SqliteTester.cs index ec592133..b0ccf150 100644 --- a/EndToEndTests/SqliteTester.cs +++ b/EndToEndTests/SqliteTester.cs @@ -6,16 +6,10 @@ namespace SqlcGenCsharpTests; -public class SqliteTester +public class SqliteTester : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; set; } - - [OneTimeSetUp] - public void SetUp() - { - var connectionString = Environment.GetEnvironmentVariable(GlobalSetup.SqliteConnectionStringEnv); - QuerySql = new QuerySql(connectionString!); - } + private QuerySql QuerySql { get; } = new( + Environment.GetEnvironmentVariable(EndToEndCommon.SqliteConnectionStringEnv)!); [TearDown] public async Task EmptyTestsTable() @@ -24,7 +18,7 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -37,21 +31,20 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actualAuthors = await QuerySql.ListAuthors(); - Assert.That(actualAuthors is [ + var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs + { + Name = DataGenerator.BojackAuthor + }); + ClassicAssert.IsNotNull(actualAuthor); + Assert.That(actualAuthor is { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme - }, - { - Name: DataGenerator.DrSeussAuthor, - Bio: DataGenerator.DrSeussQuote - } - ]); + }); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -64,19 +57,22 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actualAuthor = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs - { - Name = DataGenerator.BojackAuthor - }); - Assert.That(actualAuthor is + var actualAuthors = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actualAuthors.Count); + Assert.That(actualAuthors is [ { Name: DataGenerator.BojackAuthor, Bio: DataGenerator.BojackTheme - }); + }, + { + Name: DataGenerator.DrSeussAuthor, + Bio: DataGenerator.DrSeussQuote + } + ]); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -102,7 +98,7 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { diff --git a/LegacyEndToEndTests/GlobalSetup.cs b/LegacyEndToEndTests/GlobalSetup.cs index 37a89ea9..fd60c019 100644 --- a/LegacyEndToEndTests/GlobalSetup.cs +++ b/LegacyEndToEndTests/GlobalSetup.cs @@ -1,62 +1,20 @@ -using dotenv.net; -using Microsoft.Data.Sqlite; using NUnit.Framework; -using System; -using System.IO; -using System.Text.RegularExpressions; namespace SqlcGenCsharpTests { [SetUpFixture] public class GlobalSetup { - private const string EnvFile = ".env"; - private const string SchemaFile = "sqlite.schema.sql"; - - public const string PostgresConnectionStringEnv = "POSTGRES_CONNECTION_STRING"; - public const string MySqlConnectionStringEnv = "MYSQL_CONNECTION_STRING"; - public const string SqliteConnectionStringEnv = "SQLITE_CONNECTION_STRING"; - [OneTimeSetUp] - public void LoadEnvFile() + public void SetUp() { - if (File.Exists(EnvFile)) - DotEnv.Load(options: new DotEnvOptions(envFilePaths: new[] { EnvFile })); - RemoveExistingDb(); - if (File.Exists(SchemaFile)) - InitSqliteDb(); - } - - private static void RemoveExistingDb() - { - var connectionString = Environment.GetEnvironmentVariable(SqliteConnectionStringEnv); - if (connectionString == null) return; - - var dbFilename = SqliteFilenameRegex.Match(connectionString).Groups[1].Value; - if (File.Exists(dbFilename)) - File.Delete(dbFilename); - } - private static void InitSqliteDb() - { - var schemaSql = File.ReadAllText(SchemaFile); - var connectionString = Environment.GetEnvironmentVariable(GlobalSetup.SqliteConnectionStringEnv); - using (var connection = new SqliteConnection(connectionString)) - { - connection.Open(); - using (var command = connection.CreateCommand()) - { - command.CommandText = schemaSql; - command.ExecuteNonQuery(); - } - } + EndToEndCommon.SetUp(); } [OneTimeTearDown] public void TearDown() { - RemoveExistingDb(); + EndToEndCommon.TearDown(); } - - private static readonly Regex SqliteFilenameRegex = new Regex(@"Data Source=((/|\\w)+\.db);", RegexOptions.Compiled); } } \ No newline at end of file diff --git a/LegacyEndToEndTests/LegacyEndToEndTests.csproj b/LegacyEndToEndTests/LegacyEndToEndTests.csproj index a4e73cad..553e68b7 100644 --- a/LegacyEndToEndTests/LegacyEndToEndTests.csproj +++ b/LegacyEndToEndTests/LegacyEndToEndTests.csproj @@ -24,6 +24,7 @@ + diff --git a/LegacyEndToEndTests/MySqlConnectorDapperTester.cs b/LegacyEndToEndTests/MySqlConnectorDapperTester.cs index fbe411c6..70f432c2 100644 --- a/LegacyEndToEndTests/MySqlConnectorDapperTester.cs +++ b/LegacyEndToEndTests/MySqlConnectorDapperTester.cs @@ -8,9 +8,10 @@ namespace SqlcGenCsharpTests { - public class MySqlConnectorDapperTests + public class MySqlConnectorDapperTests : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; } = new QuerySql(Environment.GetEnvironmentVariable(GlobalSetup.MySqlConnectionStringEnv)); + private QuerySql QuerySql { get; } = new QuerySql( + Environment.GetEnvironmentVariable(EndToEndCommon.MySqlConnectionStringEnv)); [TearDown] public async Task EmptyTestsTable() @@ -19,7 +20,7 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -32,18 +33,20 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actual = await QuerySql.ListAuthors(); - ClassicAssert.AreEqual(2, actual.Count); - var expected = new List + var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs { - new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, - new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } + Name = DataGenerator.BojackAuthor + }); + var expected = new QuerySql.GetAuthorRow + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme }; - Assert.That(SequenceEquals(expected, actual)); + Assert.That(Equals(expected, actual)); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -56,21 +59,18 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs - { - Name = DataGenerator.BojackAuthor - }); - ClassicAssert.IsNotNull(actual); - var expected = new QuerySql.GetAuthorRow + var actual = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actual.Count); + var expected = new List { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme + new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, + new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } }; - Assert.That(Equals(expected, actual)); + Assert.That(SequenceEquals(expected, actual)); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -89,7 +89,7 @@ await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { diff --git a/LegacyEndToEndTests/MySqlConnectorTester.cs b/LegacyEndToEndTests/MySqlConnectorTester.cs index 7c51da5c..1d7d7e4e 100644 --- a/LegacyEndToEndTests/MySqlConnectorTester.cs +++ b/LegacyEndToEndTests/MySqlConnectorTester.cs @@ -8,9 +8,10 @@ namespace SqlcGenCsharpTests { - public class MySqlConnectorTester + public class MySqlConnectorTester : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; } = new QuerySql(Environment.GetEnvironmentVariable(GlobalSetup.MySqlConnectionStringEnv)); + private QuerySql QuerySql { get; } = new QuerySql( + Environment.GetEnvironmentVariable(EndToEndCommon.MySqlConnectionStringEnv)); [TearDown] public async Task EmptyTestsTable() @@ -19,7 +20,7 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -32,18 +33,20 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actual = await QuerySql.ListAuthors(); - ClassicAssert.AreEqual(2, actual.Count); - var expected = new List + var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs { - new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, - new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } + Name = DataGenerator.BojackAuthor + }); + var expected = new QuerySql.GetAuthorRow + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme }; - Assert.That(SequenceEquals(expected, actual)); + Assert.That(Equals(expected, actual)); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -56,21 +59,18 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs - { - Name = DataGenerator.BojackAuthor - }); - ClassicAssert.IsNotNull(actual); - var expected = new QuerySql.GetAuthorRow + var actual = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actual.Count); + var expected = new List { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme + new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, + new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } }; - Assert.That(Equals(expected, actual)); + Assert.That(SequenceEquals(expected, actual)); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -89,7 +89,7 @@ await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { diff --git a/LegacyEndToEndTests/NpgsqlDapperTester.cs b/LegacyEndToEndTests/NpgsqlDapperTester.cs index 18b6bd9d..8ba693d1 100644 --- a/LegacyEndToEndTests/NpgsqlDapperTester.cs +++ b/LegacyEndToEndTests/NpgsqlDapperTester.cs @@ -8,9 +8,10 @@ namespace SqlcGenCsharpTests { - public class NpgsqlDapperTests + public class NpgsqlDapperTests : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; } = new QuerySql(Environment.GetEnvironmentVariable(GlobalSetup.PostgresConnectionStringEnv)); + private QuerySql QuerySql { get; } = new QuerySql( + Environment.GetEnvironmentVariable(EndToEndCommon.PostgresConnectionStringEnv)); [TearDown] public async Task EmptyTestsTable() @@ -19,57 +20,18 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); - - var actual = await QuerySql.ListAuthors(); - var expected = new List + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { - new QuerySql.ListAuthorsRow - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - }, - new QuerySql.ListAuthorsRow - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - }; - Assert.That(SequenceEquals(expected, actual)); - } - - [Test] - public async Task TestGetAuthor() - { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs { @@ -84,23 +46,37 @@ await QuerySql.CreateAuthor( } [Test] - public async Task TestDeleteAuthor() + public async Task TestMany() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); + + var actual = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actual.Count); + var expected = new List + { + new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, + new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } + }; + Assert.That(SequenceEquals(expected, actual)); + } + [Test] + public async Task TestExec() + { + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor @@ -113,7 +89,7 @@ await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { @@ -127,8 +103,8 @@ public async Task TestExecRowsFlow() { Bio = DataGenerator.GenericQuote2 }; - var affected = await QuerySql.UpdateAuthors(updateAuthorsArgs); - Assert.That(affected is 2); + var affectedRows = await QuerySql.UpdateAuthors(updateAuthorsArgs); + ClassicAssert.AreEqual(2, affectedRows); } private static bool Equals(QuerySql.GetAuthorRow x, QuerySql.GetAuthorRow y) diff --git a/LegacyEndToEndTests/NpgsqlTester.cs b/LegacyEndToEndTests/NpgsqlTester.cs index 244a8be9..ee5e769d 100644 --- a/LegacyEndToEndTests/NpgsqlTester.cs +++ b/LegacyEndToEndTests/NpgsqlTester.cs @@ -8,9 +8,12 @@ namespace SqlcGenCsharpTests { - public class NpgsqlTester + public class NpgsqlTester : IOneTester, IManyTester, IExecTester, IExecRowsTester, ICopyFromTester { - private QuerySql QuerySql { get; } = new QuerySql(Environment.GetEnvironmentVariable(GlobalSetup.PostgresConnectionStringEnv)); + private static readonly Random Randomizer = new Random(); + + private QuerySql QuerySql { get; } = new QuerySql( + Environment.GetEnvironmentVariable(EndToEndCommon.PostgresConnectionStringEnv)); [TearDown] public async Task EmptyTestsTable() @@ -19,57 +22,18 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); - - var actual = await QuerySql.ListAuthors(); - var expected = new List + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { - new QuerySql.ListAuthorsRow - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - }, - new QuerySql.ListAuthorsRow - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - }; - Assert.That(SequenceEquals(expected, actual)); - } - - [Test] - public async Task TestGetAuthor() - { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs { @@ -84,23 +48,37 @@ await QuerySql.CreateAuthor( } [Test] - public async Task TestDeleteAuthor() + public async Task TestMany() { - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme - } - ); - await QuerySql.CreateAuthor( - new QuerySql.CreateAuthorArgs - { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote - } - ); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.DrSeussAuthor, + Bio = DataGenerator.DrSeussQuote + }); + + var actual = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actual.Count); + var expected = new List + { + new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, + new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } + }; + Assert.That(SequenceEquals(expected, actual)); + } + [Test] + public async Task TestExec() + { + await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme + }); await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor @@ -113,7 +91,7 @@ await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { @@ -127,8 +105,32 @@ public async Task TestExecRowsFlow() { Bio = DataGenerator.GenericQuote2 }; - var affected = await QuerySql.UpdateAuthors(updateAuthorsArgs); - Assert.That(affected is 2); + var affectedRows = await QuerySql.UpdateAuthors(updateAuthorsArgs); + ClassicAssert.AreEqual(2, affectedRows); + } + + [Test] + public async Task TestCopyFrom() + { + const int batchSize = 100; + var createAuthorBatchArgs = Enumerable.Range(0, batchSize) + .Select(_ => GenerateRandom()) + .ToList(); + await QuerySql.CopyToTests(createAuthorBatchArgs); + var countRows = QuerySql.CountCopyRows().Result.Cnt; + ClassicAssert.AreEqual(batchSize, countRows); + return; + + QuerySql.CopyToTestsArgs GenerateRandom() + { + return new QuerySql.CopyToTestsArgs + { + CVarchar = Randomizer.Next().ToString(), + CInt = Randomizer.Next(), + CDate = DateTime.Now.Subtract(TimeSpan.FromMilliseconds(Randomizer.Next())), + CTimestamp = DateTime.Now.Subtract(TimeSpan.FromMilliseconds(Randomizer.Next())) + }; + } } private static bool Equals(QuerySql.GetAuthorRow x, QuerySql.GetAuthorRow y) diff --git a/LegacyEndToEndTests/SqliteDapperTester.cs b/LegacyEndToEndTests/SqliteDapperTester.cs index 6ac3d03e..fdf14a5c 100644 --- a/LegacyEndToEndTests/SqliteDapperTester.cs +++ b/LegacyEndToEndTests/SqliteDapperTester.cs @@ -8,9 +8,10 @@ namespace SqlcGenCsharpTests { - public class SqliteDapperTests + public class SqliteDapperTests : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; } = new QuerySql(Environment.GetEnvironmentVariable(GlobalSetup.SqliteConnectionStringEnv)); + private QuerySql QuerySql { get; } = new QuerySql( + Environment.GetEnvironmentVariable(EndToEndCommon.SqliteConnectionStringEnv)); [TearDown] public async Task EmptyTestsTable() @@ -19,7 +20,7 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -32,17 +33,20 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actual = await QuerySql.ListAuthors(); - var expected = new List + var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs { - new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, - new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } + Name = DataGenerator.BojackAuthor + }); + var expected = new QuerySql.GetAuthorRow + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme }; - Assert.That(SequenceEquals(expected, actual)); + Assert.That(Equals(expected, actual)); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -55,35 +59,28 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs - { - Name = DataGenerator.BojackAuthor - }); - var expected = new QuerySql.GetAuthorRow + var actual = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actual.Count); + var expected = new List { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme + new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, + new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } }; - Assert.That(Equals(expected, actual)); + Assert.That(SequenceEquals(expected, actual)); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }); - await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote + Name = DataGenerator.BojackAuthor }); - - var deleteAuthorArgs = new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor }; - await QuerySql.DeleteAuthor(deleteAuthorArgs); - var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs { Name = DataGenerator.BojackAuthor @@ -91,26 +88,8 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs ClassicAssert.IsNull(actual); } - private static bool Equals(QuerySql.GetAuthorRow x, QuerySql.GetAuthorRow y) - { - return x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio); - } - - private static bool Equals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y) - { - return x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio); - } - - private static bool SequenceEquals(List x, List y) - { - if (x.Count != y.Count) return false; - x = x.OrderBy(o => o.Name + o.Bio).ToList(); - y = y.OrderBy(o => o.Name + o.Bio).ToList(); - return !x.Where((t, i) => !Equals(t, y[i])).Any(); - } - [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { @@ -127,5 +106,23 @@ public async Task TestExecRowsFlow() var affectedRows = await QuerySql.UpdateAuthors(updateAuthorsArgs); ClassicAssert.AreEqual(2, affectedRows); } + + private static bool Equals(QuerySql.GetAuthorRow x, QuerySql.GetAuthorRow y) + { + return x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio); + } + + private static bool Equals(QuerySql.ListAuthorsRow x, QuerySql.ListAuthorsRow y) + { + return x.Name.Equals(y.Name) && x.Bio.Equals(y.Bio); + } + + private static bool SequenceEquals(List x, List y) + { + if (x.Count != y.Count) return false; + x = x.OrderBy(o => o.Name + o.Bio).ToList(); + y = y.OrderBy(o => o.Name + o.Bio).ToList(); + return !x.Where((t, i) => !Equals(t, y[i])).Any(); + } } } \ No newline at end of file diff --git a/LegacyEndToEndTests/SqliteTester.cs b/LegacyEndToEndTests/SqliteTester.cs index f79c143a..a1c3402e 100644 --- a/LegacyEndToEndTests/SqliteTester.cs +++ b/LegacyEndToEndTests/SqliteTester.cs @@ -8,9 +8,10 @@ namespace SqlcGenCsharpTests { - public class SqliteTester + public class SqliteTester : IOneTester, IManyTester, IExecTester, IExecRowsTester { - private QuerySql QuerySql { get; } = new QuerySql(Environment.GetEnvironmentVariable(GlobalSetup.SqliteConnectionStringEnv)); + private QuerySql QuerySql { get; } = new QuerySql( + Environment.GetEnvironmentVariable(EndToEndCommon.SqliteConnectionStringEnv)); [TearDown] public async Task EmptyTestsTable() @@ -19,7 +20,7 @@ public async Task EmptyTestsTable() } [Test] - public async Task TestCreateAndListAuthors() + public async Task TestOne() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -32,17 +33,20 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actual = await QuerySql.ListAuthors(); - var expected = new List + var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs { - new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, - new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } + Name = DataGenerator.BojackAuthor + }); + var expected = new QuerySql.GetAuthorRow + { + Name = DataGenerator.BojackAuthor, + Bio = DataGenerator.BojackTheme }; - Assert.That(SequenceEquals(expected, actual)); + Assert.That(Equals(expected, actual)); } [Test] - public async Task TestGetAuthor() + public async Task TestMany() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { @@ -55,35 +59,28 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs Bio = DataGenerator.DrSeussQuote }); - var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs - { - Name = DataGenerator.BojackAuthor - }); - var expected = new QuerySql.GetAuthorRow + var actual = await QuerySql.ListAuthors(); + ClassicAssert.AreEqual(2, actual.Count); + var expected = new List { - Name = DataGenerator.BojackAuthor, - Bio = DataGenerator.BojackTheme + new QuerySql.ListAuthorsRow { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }, + new QuerySql.ListAuthorsRow { Name = DataGenerator.DrSeussAuthor, Bio = DataGenerator.DrSeussQuote } }; - Assert.That(Equals(expected, actual)); + Assert.That(SequenceEquals(expected, actual)); } [Test] - public async Task TestDeleteAuthor() + public async Task TestExec() { await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs { Name = DataGenerator.BojackAuthor, Bio = DataGenerator.BojackTheme }); - await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs + await QuerySql.DeleteAuthor(new QuerySql.DeleteAuthorArgs { - Name = DataGenerator.DrSeussAuthor, - Bio = DataGenerator.DrSeussQuote + Name = DataGenerator.BojackAuthor }); - - var deleteAuthorArgs = new QuerySql.DeleteAuthorArgs { Name = DataGenerator.BojackAuthor }; - await QuerySql.DeleteAuthor(deleteAuthorArgs); - var actual = await QuerySql.GetAuthor(new QuerySql.GetAuthorArgs { Name = DataGenerator.BojackAuthor @@ -92,7 +89,7 @@ await QuerySql.CreateAuthor(new QuerySql.CreateAuthorArgs } [Test] - public async Task TestExecRowsFlow() + public async Task TestExecRows() { var bojackCreateAuthorArgs = new QuerySql.CreateAuthorArgs { diff --git a/Makefile b/Makefile index 06006acc..7b57d12b 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,11 @@ dotnet-format: dotnet format --exclude GeneratedProtobuf --exclude examples protobuf-generate: - #./scripts/generate_protobuf.sh + ./scripts/generate_protobuf.sh +unit-tests: + dotnet test SqlcGenCsharpTests + run-end2end-tests: ./scripts/run_end2end_tests.sh @@ -20,7 +23,7 @@ dotnet-publish-process: dotnet-build-process sqlc-generate-process: dotnet-publish-process sqlc -f sqlc.local.yaml generate -test-process-plugin: sqlc-generate-process run-end2end-tests +test-process-plugin: unit-tests sqlc-generate-process run-end2end-tests # WASM type plugin dotnet-publish-wasm: protobuf-generate @@ -33,4 +36,4 @@ update-wasm-plugin: sqlc-generate-wasm: dotnet-publish-wasm update-wasm-plugin SQLCCACHE=./; sqlc -f sqlc.ci.yaml generate -test-wasm-plugin: sqlc-generate-wasm update-wasm-plugin run-end2end-tests \ No newline at end of file +test-wasm-plugin: unit-tests sqlc-generate-wasm update-wasm-plugin run-end2end-tests \ No newline at end of file diff --git a/examples/MySqlConnectorDapperExample/QuerySql.cs b/examples/MySqlConnectorDapperExample/QuerySql.cs index 4233f838..5f0dc419 100644 --- a/examples/MySqlConnectorDapperExample/QuerySql.cs +++ b/examples/MySqlConnectorDapperExample/QuerySql.cs @@ -62,20 +62,6 @@ public async Task CreateAuthor(CreateAuthorArgs args) } } - private const string UpdateAuthorSql = "UPDATE authors SET bio = @bio WHERE id = @id"; - public class UpdateAuthorArgs - { - public string? Bio { get; set; } - public long Id { get; set; } - }; - public async Task UpdateAuthor(UpdateAuthorArgs args) - { - using (var connection = new MySqlConnection(connectionString)) - { - await connection.ExecuteAsync(UpdateAuthorSql, new { bio = args.Bio, id = args.Id }); - } - } - private const string CreateAuthorReturnIdSql = "INSERT INTO authors (name, bio) VALUES (@name, @bio)"; public class CreateAuthorReturnIdArgs { diff --git a/examples/MySqlConnectorDapperLegacyExample/QuerySql.cs b/examples/MySqlConnectorDapperLegacyExample/QuerySql.cs index 61207b80..65b19ef6 100644 --- a/examples/MySqlConnectorDapperLegacyExample/QuerySql.cs +++ b/examples/MySqlConnectorDapperLegacyExample/QuerySql.cs @@ -70,20 +70,6 @@ public async Task CreateAuthor(CreateAuthorArgs args) } } - private const string UpdateAuthorSql = "UPDATE authors SET bio = @bio WHERE id = @id"; - public class UpdateAuthorArgs - { - public string Bio { get; set; } - public long Id { get; set; } - }; - public async Task UpdateAuthor(UpdateAuthorArgs args) - { - using (var connection = new MySqlConnection(ConnectionString)) - { - await connection.ExecuteAsync(UpdateAuthorSql, new { bio = args.Bio, id = args.Id }); - } - } - private const string CreateAuthorReturnIdSql = "INSERT INTO authors (name, bio) VALUES (@name, @bio)"; public class CreateAuthorReturnIdArgs { diff --git a/examples/MySqlConnectorExample/QuerySql.cs b/examples/MySqlConnectorExample/QuerySql.cs index 4ef1073d..bbd5bd10 100644 --- a/examples/MySqlConnectorExample/QuerySql.cs +++ b/examples/MySqlConnectorExample/QuerySql.cs @@ -68,20 +68,6 @@ public async Task CreateAuthor(CreateAuthorArgs args) } } - private const string UpdateAuthorSql = "UPDATE authors SET bio = @bio WHERE id = @id"; - public readonly record struct UpdateAuthorArgs(string? Bio, long Id); - public async Task UpdateAuthor(UpdateAuthorArgs args) - { - { - await using var connection = new MySqlConnection(connectionString); - connection.Open(); - await using var command = new MySqlCommand(UpdateAuthorSql, connection); - command.Parameters.AddWithValue("@bio", args.Bio!); - command.Parameters.AddWithValue("@id", args.Id); - await command.ExecuteScalarAsync(); - } - } - private const string CreateAuthorReturnIdSql = "INSERT INTO authors (name, bio) VALUES (@name, @bio)"; public readonly record struct CreateAuthorReturnIdArgs(string Name, string? Bio); public async Task CreateAuthorReturnId(CreateAuthorReturnIdArgs args) diff --git a/examples/MySqlConnectorLegacyExample/QuerySql.cs b/examples/MySqlConnectorLegacyExample/QuerySql.cs index 76f95f27..eb5d526d 100644 --- a/examples/MySqlConnectorLegacyExample/QuerySql.cs +++ b/examples/MySqlConnectorLegacyExample/QuerySql.cs @@ -111,28 +111,6 @@ public async Task CreateAuthor(CreateAuthorArgs args) } } - private const string UpdateAuthorSql = "UPDATE authors SET bio = @bio WHERE id = @id"; - public class UpdateAuthorArgs - { - public string Bio { get; set; } - public long Id { get; set; } - }; - public async Task UpdateAuthor(UpdateAuthorArgs args) - { - { - using (var connection = new MySqlConnection(ConnectionString)) - { - connection.Open(); - using (var command = new MySqlCommand(UpdateAuthorSql, connection)) - { - command.Parameters.AddWithValue("@bio", args.Bio); - command.Parameters.AddWithValue("@id", args.Id); - await command.ExecuteScalarAsync(); - } - } - } - } - private const string CreateAuthorReturnIdSql = "INSERT INTO authors (name, bio) VALUES (@name, @bio)"; public class CreateAuthorReturnIdArgs { diff --git a/examples/config/mysql/query.sql b/examples/config/mysql/query.sql index 478e50c4..ad549040 100644 --- a/examples/config/mysql/query.sql +++ b/examples/config/mysql/query.sql @@ -7,9 +7,6 @@ SELECT * FROM authors ORDER BY name; -- name: CreateAuthor :exec INSERT INTO authors (name, bio) VALUES (?, ?); --- name: UpdateAuthor :exec -UPDATE authors SET bio = ? WHERE id = ?; - -- name: CreateAuthorReturnId :execlastid INSERT INTO authors (name, bio) VALUES (?, ?); diff --git a/sqlc-gen-csharp.sln b/sqlc-gen-csharp.sln index e7487c3c..4af5f065 100644 --- a/sqlc-gen-csharp.sln +++ b/sqlc-gen-csharp.sln @@ -51,6 +51,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteExample", "examples\S EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlcGenCsharpTests", "SqlcGenCsharpTests\SqlcGenCsharpTests.csproj", "{9FD3C47C-88A5-4C4A-A807-1E36114CBB38}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EndToEndCommon", "EndToEndCommon\EndToEndCommon.csproj", "{FE5AA465-DC0E-4B14-9BD3-16B47813FB97}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -155,6 +157,10 @@ Global {9FD3C47C-88A5-4C4A-A807-1E36114CBB38}.Debug|Any CPU.Build.0 = Debug|Any CPU {9FD3C47C-88A5-4C4A-A807-1E36114CBB38}.Release|Any CPU.ActiveCfg = Release|Any CPU {9FD3C47C-88A5-4C4A-A807-1E36114CBB38}.Release|Any CPU.Build.0 = Release|Any CPU + {FE5AA465-DC0E-4B14-9BD3-16B47813FB97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FE5AA465-DC0E-4B14-9BD3-16B47813FB97}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FE5AA465-DC0E-4B14-9BD3-16B47813FB97}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FE5AA465-DC0E-4B14-9BD3-16B47813FB97}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {096086A5-433C-4682-AF0E-1A42A9EC19B5} = {9404C766-3491-4959-A575-D6015FFB7AE6}