Skip to content

Commit 964dbed

Browse files
committed
create test infrastructure classes
1 parent 461cc54 commit 964dbed

4 files changed

Lines changed: 155 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Reflection;
3+
using SqlKata.Compilers;
4+
5+
namespace SqlKata.Tests.Infrastructure
6+
{
7+
/// <summary>
8+
/// A test class to expose private methods
9+
/// </summary>
10+
class TestCompiler : Compiler
11+
{
12+
public override string EngineCode { get; } = "test";
13+
14+
public virtual MethodInfo Call_FindCompilerMethodInfo(Type clauseType, string methodName)
15+
{
16+
return FindCompilerMethodInfo(clauseType, methodName);
17+
}
18+
}
19+
20+
class TestSqlServerCompiler : SqlServerCompiler
21+
{
22+
public virtual MethodInfo Call_FindCompilerMethodInfo(Type clauseType, string methodName)
23+
{
24+
return FindCompilerMethodInfo(clauseType, methodName);
25+
}
26+
}
27+
28+
class TestMySqlCompiler : MySqlCompiler
29+
{
30+
public virtual MethodInfo Call_FindCompilerMethodInfo(Type clauseType, string methodName)
31+
{
32+
return FindCompilerMethodInfo(clauseType, methodName);
33+
}
34+
}
35+
36+
class TestPostgresCompiler : PostgresCompiler
37+
{
38+
public virtual MethodInfo Call_FindCompilerMethodInfo(Type clauseType, string methodName)
39+
{
40+
return FindCompilerMethodInfo(clauseType, methodName);
41+
}
42+
}
43+
44+
class TestFirebirdCompiler : FirebirdCompiler
45+
{
46+
public virtual MethodInfo Call_FindCompilerMethodInfo(Type clauseType, string methodName)
47+
{
48+
return FindCompilerMethodInfo(clauseType, methodName);
49+
}
50+
}
51+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using SqlKata.Compilers;
5+
6+
namespace SqlKata.Tests.Infrastructure
7+
{
8+
public class TestCompilersContainer
9+
{
10+
protected readonly IDictionary<string, Compiler> Compilers = new Dictionary<string, Compiler>
11+
{
12+
[EngineCodes.Firebird] = new FirebirdCompiler(),
13+
[EngineCodes.MySql] = new MySqlCompiler(),
14+
[EngineCodes.Oracle] = new OracleCompiler(),
15+
[EngineCodes.PostgreSql] = new PostgresCompiler(),
16+
[EngineCodes.Sqlite] = new SqliteCompiler(),
17+
[EngineCodes.SqlServer] = new SqlServerCompiler()
18+
};
19+
20+
/// <summary>
21+
/// Returns the compiler instance for the given <param name="engineCode"></param>
22+
/// </summary>
23+
/// <param name="engineCode"></param>
24+
/// <returns></returns>
25+
public Compiler Get(string engineCode)
26+
{
27+
if (!Compilers.ContainsKey(engineCode))
28+
{
29+
throw new InvalidOperationException($"Engine code '{engineCode}' is not valid");
30+
}
31+
32+
return Compilers[engineCode];
33+
}
34+
35+
/// <summary>
36+
/// Convenience method <seealso cref="Get"/>
37+
/// </summary>
38+
/// <remarks>Does not validate generic type against engine code before cast</remarks>
39+
/// <typeparam name="TCompiler"></typeparam>
40+
/// <param name="engineCode"></param>
41+
/// <returns></returns>
42+
public TCompiler Get<TCompiler>(string engineCode) where TCompiler : Compiler
43+
{
44+
return (TCompiler) Get(engineCode);
45+
}
46+
47+
/// <summary>
48+
/// Compiles the query for the given <param name="engineCode"></param>
49+
/// </summary>
50+
/// <param name="engineCode"></param>
51+
/// <param name="query"></param>
52+
/// <returns></returns>
53+
public SqlResult CompileFor(string engineCode, Query query)
54+
{
55+
var compiler = Get(engineCode);
56+
return compiler.Compile(query);
57+
}
58+
59+
/// <summary>
60+
/// Compiles the query with all enabled compilers.
61+
/// </summary>
62+
/// <param name="query"></param>
63+
/// <returns></returns>
64+
public TestSqlResultContainer Compile(Query query)
65+
{
66+
var resultKeyValues = Compilers
67+
.ToDictionary(k => k.Key, v => v.Value.Compile(query.Clone()));
68+
return new TestSqlResultContainer(resultKeyValues);
69+
}
70+
}
71+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
3+
4+
namespace SqlKata.Tests.Infrastructure
5+
{
6+
public class TestSqlResultContainer : ReadOnlyDictionary<string, SqlResult>
7+
{
8+
public TestSqlResultContainer(IDictionary<string, SqlResult> dictionary) : base(dictionary)
9+
{
10+
11+
}
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace SqlKata.Tests.Infrastructure
5+
{
6+
public abstract class TestSupport
7+
{
8+
protected readonly TestCompilersContainer Compilers = new TestCompilersContainer();
9+
10+
/// <summary>
11+
/// For legacy test support
12+
/// </summary>
13+
/// <param name="query"></param>
14+
/// <returns></returns>
15+
protected IReadOnlyDictionary<string, string> Compile(Query query)
16+
{
17+
return Compilers.Compile(query).ToDictionary(s => s.Key, v => v.Value.ToString());
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)