Skip to content

Commit

Permalink
test is executed targeting docker db
Browse files Browse the repository at this point in the history
  • Loading branch information
DeagleGross committed Jul 5, 2024
1 parent 63fd1a9 commit c5a4fee
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
<ItemGroup>
<Compile Include="..\..\src\Dapper.AOT.Analyzers\AotGridReader.cs" Link="AotGridReader.cs" />

<Compile Remove="InterceptionExecutables/**/*.*.cs" />
<None Include="InterceptionExecutables/**/*.*" />
<None Update="InterceptionExecutables/**/*.*" CopyToOutputDirectory="PreserveNewest" />
<Compile Remove="InterceptionExecutables/*.*.cs" />
<None Include="InterceptionExecutables/*.*" />
<None Update="InterceptionExecutables/*.*" CopyToOutputDirectory="PreserveNewest" />

<None Include="InterceptionExecutables/IncludedTypes/Poco.cs">
<Link>InterceptionExecutables/IncludedTypes/Poco.txt</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

<!-- <Compile Remove="InterceptionExecutables\Template.cs" />-->
<!-- <None Include="InterceptionExecutables\_Template.cs" />-->
Expand Down
22 changes: 16 additions & 6 deletions test/Dapper.AOT.Test.Integration/DbStringTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using System.Diagnostics;
using System.Threading.Tasks;
using Dapper.AOT.Test.Integration.Setup;
using Xunit;
Expand All @@ -12,21 +13,30 @@ public class DbStringTests : InterceptedCodeExecutionTestsBase
public DbStringTests(PostgresqlFixture fixture, ITestOutputHelper log) : base(fixture, log)
{
Fixture.NpgsqlConnection.Execute("""
CREATE TABLE IF NOT EXISTS dbStringTable(
CREATE TABLE IF NOT EXISTS dbStringTestsTable(
id integer PRIMARY KEY,
name varchar(40) NOT NULL CHECK (name <> '')
name varchar(40) NOT NULL
);
TRUNCATE dbStringTable;
TRUNCATE dbStringTestsTable;
""");
}

[Fact]
[DapperAot]
public async Task Test()
{
await Fixture.NpgsqlConnection.ExecuteAsync("""
INSERT INTO dbStringTestsTable(id, name)
VALUES (1, 'me testing!')
""");

var sourceCode = PrepareSourceCodeFromFile("DbString");
var executionResults = BuildAndExecuteInterceptedUserCode<int>(sourceCode, methodName: "ExecuteAsync");

// TODO DO THE CHECK HERE
var executionResults = BuildAndExecuteInterceptedUserCode<InterceptionExecutables.IncludedTypes.Poco>(sourceCode, methodName: "Execute");

var list = Trace.Listeners;
Trace.Write("qwe");

Assert.NotNull(executionResults);
// TODO check that stack trace contains call to `Dapper.Aot.Generated.DbStringHelpers`. probably can be done like here https://stackoverflow.com/a/33939304
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@ namespace InterceptionExecutables
using System.IO;
using Dapper;
using System.Threading.Tasks;

using InterceptionExecutables.IncludedTypes;

[DapperAot] // Enabling Dapper AOT!
public static class Program
{
public static async Task<int> ExecuteAsync(IDbConnection dbConnection)
public static Poco Execute(IDbConnection dbConnection)
=> ExecuteAsync(dbConnection).GetAwaiter().GetResult();

public static async Task<Poco> ExecuteAsync(IDbConnection dbConnection)
{
var res = await dbConnection.QueryAsync<int>("SELECT count(*) FROM dbStringTable");
return res.First();
var results = await dbConnection.QueryAsync<Poco>("select * from dbStringTestsTable where id = @Id and Name = @Name", new
{
Name = new DbString
{
Value = "me testing!",
IsFixedLength = false,
Length = 11
},

Id = 1,
});

return results.First();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace InterceptionExecutables.IncludedTypes;

public class Poco
{
public int Id { get; set; }
public string Name { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
using System.IO;
using Dapper;
using System.Threading.Tasks;
using InterceptionExecutables.IncludedTypes;

// this is just a sample for easy test-writing
[DapperAot] // Enabling Dapper AOT!
public static class Program
{
public static async Task<object> ExecuteAsync(IDbConnection dbConnection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using Dapper.CodeAnalysis;
using Dapper.TestCommon;
using Microsoft.CodeAnalysis;
Expand All @@ -15,7 +15,6 @@
using Microsoft.CodeAnalysis.Text;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Dapper.AOT.Test.Integration.Setup;

Expand All @@ -41,23 +40,40 @@ protected static string PrepareSourceCodeFromFile(string inputFileName, string e
}

protected T BuildAndExecuteInterceptedUserCode<T>(
string userSourceCode,
string sourceCode,
string className = "Program",
string methodName = "ExecuteAsync")
string methodName = "Execute")
{
var inputCompilation = RoslynTestHelpers.CreateCompilation("Assembly", syntaxTrees: [
BuildInterceptorSupportedSyntaxTree(filename: "Program.cs", userSourceCode)
]);
var syntaxTrees = new List<SyntaxTree>
{
RoslynTestHelpers.CreateSyntaxTree(sourceCode, fileName: $"{className}.cs")
};

var diagnosticsOutputStringBuilder = new StringBuilder();
var (compilation, generatorDriverRunResult, diagnostics, errorCount) = Execute<DapperInterceptorGenerator>(inputCompilation, diagnosticsOutputStringBuilder, initializer: g =>
var includedTypesPath = Path.Combine("InterceptionExecutables", "IncludedTypes");
foreach (var file in Directory.GetFiles(includedTypesPath, "*.txt")) // we have copied all same files but in txt format not to have ambiguos references
{
g.Log += message => Log(message);
});
syntaxTrees.Add(RoslynTestHelpers.CreateSyntaxTree(File.ReadAllText(file), fileName: Path.GetFileName(file)));
}

var inputCompilation = RoslynTestHelpers.CreateCompilation(assemblyName: "MyAssembly", syntaxTrees);

var diagnosticsOutputStringBuilder = new StringBuilder();
var (compilation, generatorDriverRunResult, diagnostics, errorCount) = Execute<DapperInterceptorGenerator>(
inputCompilation,
diagnosticsOutputStringBuilder,
initializer: g => { g.Log += message => Log(message); }
);

var results = Assert.Single(generatorDriverRunResult.Results);
Log($$"""
Generated code:
---
{{results.GeneratedSources.First().SourceText}}
---
""");

Assert.NotNull(compilation);
Assert.True(errorCount == 0, "User code should not report errors");
Assert.True(errorCount == 0, $"Compilation errors: {diagnosticsOutputStringBuilder}");

var assembly = Compile(compilation!);
var type = assembly.GetTypes().Single(t => t.FullName == $"InterceptionExecutables.{className}");
Expand All @@ -66,12 +82,9 @@ protected T BuildAndExecuteInterceptedUserCode<T>(

var result = mainMethod!.Invoke(obj: null, [ Fixture.NpgsqlConnection ]);
Assert.NotNull(result);
if (result is not Task<T> taskResult)
{
throw new XunitException($"expected execution result is '{typeof(Task<T>)}' but got {result!.GetType()}");
}

return taskResult.GetAwaiter().GetResult();
var data = JsonSerializer.Serialize(result);
return JsonSerializer.Deserialize<T>(data)!;
}

SyntaxTree BuildInterceptorSupportedSyntaxTree(string filename, string text)
Expand Down Expand Up @@ -123,18 +136,14 @@ static void TryThrowErrors(IEnumerable<Diagnostic> items)

class CompilationException : Exception
{
public IEnumerable<string> Errors { get; private set; }

public CompilationException(IEnumerable<string> errors)
: base(string.Join(Environment.NewLine, errors))
{
this.Errors = errors;
}

public CompilationException(params string[] errors)
: base(string.Join(Environment.NewLine, errors))
{
this.Errors = errors;
}
}
}
3 changes: 2 additions & 1 deletion test/Dapper.AOT.Test/GeneratorTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ protected GeneratorTestBase(ITestOutputHelper? log)

// input from https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#unit-testing-of-generators

protected (Compilation? Compilation, GeneratorDriverRunResult Result, ImmutableArray<Diagnostic> Diagnostics, int ErrorCount) Execute<T>(string source,
protected (Compilation? Compilation, GeneratorDriverRunResult Result, ImmutableArray<Diagnostic> Diagnostics, int ErrorCount) Execute<T>(
string source,
StringBuilder? diagnosticsTo = null,
[CallerMemberName] string? name = null,
string? fileName = null,
Expand Down
52 changes: 16 additions & 36 deletions test/Dapper.AOT.Test/TestCommon/RoslynTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Dapper.TestCommon;
Expand Down Expand Up @@ -43,44 +44,23 @@ public static class RoslynTestHelpers
"RELEASE",
#endif
})
.WithFeatures(new[] { DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespacePair });
.WithFeatures([ DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespacePair ]);

public static SyntaxTree CreateSyntaxTree(string source, string fileName)
=> CSharpSyntaxTree.ParseText(source, ParseOptionsLatestLangVer, encoding: Encoding.UTF8).WithFilePath(fileName);

public static Compilation CreateCompilation(string assemblyName, SyntaxTree[] syntaxTrees, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary)
=> CSharpCompilation.Create(assemblyName,
syntaxTrees: syntaxTrees,
references: new[] {
MetadataReference.CreateFromFile(typeof(Binder).Assembly.Location),
#if !NET48
MetadataReference.CreateFromFile(Assembly.Load("System.Runtime").Location),
MetadataReference.CreateFromFile(Assembly.Load("System.Data").Location),
MetadataReference.CreateFromFile(Assembly.Load("netstandard").Location),
MetadataReference.CreateFromFile(Assembly.Load("System.Collections").Location),
MetadataReference.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.Schema.ColumnAttribute).Assembly.Location),
#endif
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DbConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Data.SqlClient.SqlConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Microsoft.Data.SqlClient.SqlConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(OracleConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(ValueTask<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Component).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DapperAotExtensions).Assembly.Location),
MetadataReference.CreateFromFile(typeof(SqlMapper).Assembly.Location),
MetadataReference.CreateFromFile(typeof(ImmutableList<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(ImmutableArray<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
MetadataReference.CreateFromFile(typeof(IAsyncEnumerable<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Span<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(IgnoreDataMemberAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(SqlMapper).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DynamicAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(IValidatableObject).Assembly.Location),
},
options: new CSharpCompilationOptions(outputKind, allowUnsafe: true));

public static Compilation CreateCompilation(string source, string assemblyName, string fileName)
{
var syntaxTree = CreateSyntaxTree(source, fileName);
return CreateCompilation(assemblyName, syntaxTree);
}

public static Compilation CreateCompilation(string assemblyName, SyntaxTree syntaxTree)
=> CreateCompilation(assemblyName, [syntaxTree]);

public static Compilation CreateCompilation(string assemblyName, IEnumerable<SyntaxTree> syntaxTrees)
=> CSharpCompilation.Create(assemblyName,
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(source, ParseOptionsLatestLangVer).WithFilePath(fileName) },
syntaxTrees: syntaxTrees,
references: new[] {
MetadataReference.CreateFromFile(typeof(Binder).Assembly.Location),
#if !NET48
Expand Down Expand Up @@ -109,5 +89,5 @@ public static Compilation CreateCompilation(string source, string assemblyName,
MetadataReference.CreateFromFile(typeof(DynamicAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(IValidatableObject).Assembly.Location),
},
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication, allowUnsafe: true));
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true));
}

0 comments on commit c5a4fee

Please sign in to comment.