Skip to content

Commit

Permalink
refactor tests around supported query annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
SockworkOrange committed Dec 31, 2024
1 parent 6b8d862 commit c3142a8
Show file tree
Hide file tree
Showing 34 changed files with 637 additions and 723 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/legacy-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions Drivers/DbDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions Drivers/MySqlConnectorDriver.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ColumnMapping> ColumnMappings { get; } =
[
Expand Down
2 changes: 1 addition & 1 deletion Drivers/NpgsqlDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
13 changes: 13 additions & 0 deletions Drivers/QueryAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Drivers/SqliteDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ColumnMapping> ColumnMappings { get; } = [
new("byte[]", ordinal => $"Utils.GetBytes(reader, {ordinal})",
Expand Down
File renamed without changes.
59 changes: 59 additions & 0 deletions EndToEndCommon/EndToEndCommon.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 24 additions & 0 deletions EndToEndCommon/EndToEndCommon.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>.netstandard2.0</TargetFramework>
<RootNamespace>SqlcGenCsharpTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Content Include="../.env">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="../examples/config/sqlite/schema.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<TargetPath>sqlite.schema.sql</TargetPath>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="dotenv.net" Version="3.2.1" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0"/>
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions EndToEndCommon/TestInterfaces.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
14 changes: 0 additions & 14 deletions EndToEndTests/DataGenerator.cs

This file was deleted.

1 change: 1 addition & 0 deletions EndToEndTests/EndToEndTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EndToEndCommon\EndToEndCommon.csproj" />
<ProjectReference Include="..\examples\MySqlConnectorDapperExample\MySqlConnectorDapperExample.csproj" />
<ProjectReference Include="..\examples\MySqlConnectorExample\MySqlConnectorExample.csproj" />
<ProjectReference Include="..\examples\NpgsqlDapperExample\NpgsqlDapperExample.csproj" />
Expand Down
65 changes: 14 additions & 51 deletions EndToEndTests/GlobalSetup.cs
Original file line number Diff line number Diff line change
@@ -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();
}
Loading

0 comments on commit c3142a8

Please sign in to comment.