Skip to content

Commit e04cb4d

Browse files
refactor tests around supported query annotations (#163)
1 parent 6b8d862 commit e04cb4d

34 files changed

+637
-723
lines changed

.github/workflows/legacy-tests.yml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,34 @@ jobs:
5959
- name: Install MySQL and restore test projects in parallel
6060
shell: pwsh
6161
run: |
62-
$mysql_job = Start-Job -ScriptBlock {
62+
$mysqlJob = Start-Job -ScriptBlock {
6363
choco install mysql --no-progress --version=8.0.31 -y --params "/serviceName:MySQL"
64+
return $LASTEXITCODE
6465
}
65-
$restore_job = Start-Job -ScriptBlock {
66+
67+
$restoreJob = Start-Job -ScriptBlock {
6668
Get-ChildItem -Path examples -Recurse -Filter *.csproj |
6769
Where-Object { $_.FullName -like '*Legacy*' } |
6870
ForEach-Object { nuget restore $_.FullName }
71+
nuget restore ./EndToEndCommon/EndToEndCommon.csproj
6972
nuget restore ./LegacyEndToEndTests/LegacyEndToEndTests.csproj
7073
msbuild.exe ./LegacyEndToEndTests/LegacyEndToEndTests.csproj -p:Configuration=Release -p:FrameworkVersion=v4.7.2
74+
return $LASTEXITCODE
75+
}
76+
77+
$mysqlOutput = Receive-Job -Job $mysqlJob -Wait
78+
$mysqlExitCode = @($mysqlOutput)[-1]
79+
if ($mysqlExitCode -ne 0) {
80+
Write-Error "MySQL install failed with exit code: $mysqlExitCode"
81+
return $mysqlExitCode
7182
}
7283
73-
Wait-Job -Job $mysql_job, $restore_job
74-
Receive-Job -Job $mysql_job
75-
Receive-Job -Job $restore_job
84+
$restoreOutput = Receive-Job -Job $restoreJob -Wait
85+
$restoreExitCode = @($restoreOutput)[-1]
86+
if ($restoreExitCode -ne 0) {
87+
Write-Error "Restore/build failed with exit code: $restoreExitCode"
88+
return $restoreExitCode
89+
}
7690
7791
- name: Init MySQL Schema
7892
shell: powershell

Drivers/DbDriver.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ public string GetColumnReader(Column column, int ordinal)
8686

8787
public abstract string CreateSqlCommand(string sqlTextConstant);
8888

89-
public abstract MemberDeclarationSyntax OneDeclare(string sqlTextConstant, string argInterface, string returnInterface, Query query);
89+
public abstract MemberDeclarationSyntax OneDeclare(string sqlTextConstant, string argInterface,
90+
string returnInterface, Query query);
9091

91-
public abstract MemberDeclarationSyntax ManyDeclare(string sqlTextConstant, string argInterface, string returnInterface, Query query);
92+
public abstract MemberDeclarationSyntax ManyDeclare(string sqlTextConstant, string argInterface,
93+
string returnInterface, Query query);
9294

9395
public abstract MemberDeclarationSyntax ExecDeclare(string text, string argInterface, Query query);
9496

Drivers/MySqlConnectorDriver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.CodeAnalysis.CSharp.Syntax;
22
using Plugin;
33
using SqlcGenCsharp.Drivers.Generators;
4-
using System;
54
using System.Collections.Generic;
65
using System.Linq;
76
using System.Text.RegularExpressions;
@@ -10,7 +9,7 @@
109

1110
namespace SqlcGenCsharp.Drivers;
1211

13-
public partial class MySqlConnectorDriver(Options options) : DbDriver(options), IExecLastId, IExecRows
12+
public partial class MySqlConnectorDriver(Options options) : DbDriver(options), IOne, IMany, IExec, IExecLastId, IExecRows
1413
{
1514
protected override List<ColumnMapping> ColumnMappings { get; } =
1615
[

Drivers/NpgsqlDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace SqlcGenCsharp.Drivers;
1010

11-
public class NpgsqlDriver : DbDriver, ICopyFrom, IExecRows
11+
public class NpgsqlDriver : DbDriver, IOne, IMany, IExec, ICopyFrom, IExecRows
1212
{
1313
public NpgsqlDriver(Options options) : base(options)
1414
{

Drivers/QueryAnnotations.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33

44
namespace SqlcGenCsharp.Drivers;
55

6+
public interface IOne
7+
{
8+
MemberDeclarationSyntax OneDeclare(string queryTextConstant, string argInterface, string returnInterface, Query query);
9+
}
10+
public interface IMany
11+
{
12+
MemberDeclarationSyntax ManyDeclare(string queryTextConstant, string argInterface, string returnInterface, Query query);
13+
}
14+
public interface IExec
15+
{
16+
MemberDeclarationSyntax ExecDeclare(string queryTextConstant, string argInterface, Query query);
17+
}
18+
619
public interface IExecLastId
720
{
821
MemberDeclarationSyntax ExecLastIdDeclare(string queryTextConstant, string argInterface, Query query);

Drivers/SqliteDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace SqlcGenCsharp.Drivers;
1010

11-
public partial class SqliteDriver(Options options) : DbDriver(options), IExecRows
11+
public partial class SqliteDriver(Options options) : DbDriver(options), IOne, IMany, IExec, IExecRows
1212
{
1313
protected override List<ColumnMapping> ColumnMappings { get; } = [
1414
new("byte[]", ordinal => $"Utils.GetBytes(reader, {ordinal})",

EndToEndCommon/EndToEndCommon.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using dotenv.net;
2+
using Microsoft.Data.Sqlite;
3+
using System;
4+
using System.IO;
5+
using System.Text.RegularExpressions;
6+
7+
namespace SqlcGenCsharpTests
8+
{
9+
public static class EndToEndCommon
10+
{
11+
private const string EnvFile = ".env";
12+
private const string SchemaFile = "sqlite.schema.sql";
13+
14+
public const string PostgresConnectionStringEnv = "POSTGRES_CONNECTION_STRING";
15+
public const string MySqlConnectionStringEnv = "MYSQL_CONNECTION_STRING";
16+
public const string SqliteConnectionStringEnv = "SQLITE_CONNECTION_STRING";
17+
18+
public static void SetUp()
19+
{
20+
if (File.Exists(EnvFile))
21+
DotEnv.Load(options: new DotEnvOptions(envFilePaths: new[] { EnvFile }));
22+
RemoveExistingSqliteDb();
23+
if (File.Exists(SchemaFile))
24+
InitSqliteDb();
25+
}
26+
27+
public static void TearDown()
28+
{
29+
RemoveExistingSqliteDb();
30+
}
31+
32+
private static void RemoveExistingSqliteDb()
33+
{
34+
var connectionString = Environment.GetEnvironmentVariable(SqliteConnectionStringEnv);
35+
if (connectionString == null) return;
36+
37+
var dbFilename = SqliteFilenameRegex.Match(connectionString).Groups[1].Value;
38+
Console.WriteLine($"Removing sqlite db from {dbFilename}");
39+
if (File.Exists(dbFilename))
40+
File.Delete(dbFilename);
41+
}
42+
private static void InitSqliteDb()
43+
{
44+
var schemaSql = File.ReadAllText(SchemaFile);
45+
var connectionString = Environment.GetEnvironmentVariable(EndToEndCommon.SqliteConnectionStringEnv);
46+
using (var connection = new SqliteConnection(connectionString))
47+
{
48+
connection.Open();
49+
using (var command = connection.CreateCommand())
50+
{
51+
command.CommandText = schemaSql;
52+
command.ExecuteNonQuery();
53+
}
54+
}
55+
}
56+
57+
private static readonly Regex SqliteFilenameRegex = new Regex(@"Data Source=([\w\.\/\-]+\.db);", RegexOptions.Compiled);
58+
}
59+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>.netstandard2.0</TargetFramework>
5+
<RootNamespace>SqlcGenCsharpTests</RootNamespace>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Content Include="../.env">
10+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
11+
</Content>
12+
<Content Include="../examples/config/sqlite/schema.sql">
13+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
14+
<TargetPath>sqlite.schema.sql</TargetPath>
15+
</Content>
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="dotenv.net" Version="3.2.1" />
20+
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0"/>
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0"/>
22+
</ItemGroup>
23+
24+
</Project>

EndToEndCommon/TestInterfaces.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Threading.Tasks;
2+
3+
namespace SqlcGenCsharpTests
4+
{
5+
public interface IOneTester
6+
{
7+
Task TestOne();
8+
}
9+
10+
public interface IManyTester
11+
{
12+
Task TestMany();
13+
}
14+
15+
public interface IExecTester
16+
{
17+
Task TestExec();
18+
}
19+
20+
public interface IExecRowsTester
21+
{
22+
Task TestExecRows();
23+
}
24+
25+
public interface ICopyFromTester
26+
{
27+
Task TestCopyFrom();
28+
}
29+
}

0 commit comments

Comments
 (0)