Skip to content

Commit

Permalink
Merge branch 'custom-compiler' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
asherber committed Jun 18, 2021
2 parents 960f3c3 + a307528 commit dd3be3a
Show file tree
Hide file tree
Showing 12 changed files with 1,096 additions and 59 deletions.
35 changes: 33 additions & 2 deletions PetaPoco.SqlKata.Tests/DatabaseExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void Query_Uses_MapperAndProvider(IProvider provider, IMapper mapper, str
var output = _mockDb.Object.Query<SomeClass>(q);
var expected = new Sql(selectText, "Bar");

_mockDb.VerifyGet(db => db.Provider, Times.Once());
_mockDb.VerifyGet(db => db.Provider, Times.Exactly(2));
_mockDb.VerifyGet(db => db.DefaultMapper, Times.Once());
_lastSql.Should().BeEquivalentTo(expected);
}
Expand All @@ -72,6 +72,7 @@ public void Query_Uses_MapperAndProvider(IProvider provider, IMapper mapper, str
[Fact]
public void First_Should_Throw()
{
_mockDb.Setup(m => m.Provider).Returns(new SqlServerDatabaseProvider());
Action act = () => _mockDb.Object.First<SomeClass>(new Query());
act.Should().Throw<InvalidOperationException>().WithMessage("Sequence contains no elements");
}
Expand All @@ -86,7 +87,7 @@ public void Execute_Uses_Provider(IProvider provider, string expectedSql)
var output = _mockDb.Object.Execute(q);
var expected = new Sql(expectedSql, "Fizzbin", "Baz");

_mockDb.VerifyGet(db => db.Provider, Times.Once());
_mockDb.VerifyGet(db => db.Provider, Times.Exactly(2));
_lastSql.Should().BeEquivalentTo(expected);
}

Expand All @@ -95,5 +96,35 @@ public void Execute_Uses_Provider(IProvider provider, string expectedSql)
new object[] { new MySqlDatabaseProvider(), "UPDATE `Foo` SET `Bar` = @0 WHERE `Bar` = @1" },
new object[] { new SqlServerDatabaseProvider(), "UPDATE [Foo] SET [Bar] = @0 WHERE [Bar] = @1"},
};

[Fact]
public void Query_Uses_Explicit_Compiler()
{
var input = new Query("Foo").Select("Bar");
var output = _mockDb.Object.Query<SomeClass>(input, new PercentCompiler());
var expected = "SELECT %%Bar%% FROM %%Foo%%";

_lastSql.Should().BeEquivalentTo(new Sql(expected));
}

[Fact]
public void Query_Uses_Custom_Compiler()
{
try
{
_mockDb.Setup(m => m.Provider).Returns(new MySqlDatabaseProvider());
DefaultCompilers.RegisterFor<MySqlDatabaseProvider>(new PercentCompiler());

var input = new Query("Foo").Select("Bar");
var output = _mockDb.Object.Query<SomeClass>(input);
var expected = "SELECT %%Bar%% FROM %%Foo%%";

_lastSql.Should().BeEquivalentTo(new Sql(expected));
}
finally
{
DefaultCompilers.RegisterFor<MySqlDatabaseProvider>(null);
}
}
}
}
49 changes: 49 additions & 0 deletions PetaPoco.SqlKata.Tests/DefaultCompilersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using FluentAssertions;
using SqlKata.Compilers;
using PetaPoco.Providers;

namespace PetaPoco.SqlKata.Tests
{
public class DefaultCompilersTests
{
[Theory]
[InlineData(CompilerType.MySql, typeof(MySqlCompiler))]
[InlineData(CompilerType.Firebird, typeof(FirebirdCompiler))]
public void Get_Should_Work_For_Known_Types(CompilerType compilerType, Type compiler)
{
var output = DefaultCompilers.Get(compilerType);
output.Should().BeOfType(compiler);
}

[Fact]
public void Get_For_Custom_Should_Throw()
{
Action act = () => DefaultCompilers.Get(CompilerType.Custom);
act.Should().Throw<ArgumentException>();
}

public class MyDatabaseProvider : OracleDatabaseProvider
{ }

[Fact]
public void Adding_And_Removing_Custom_Works()
{
var provider = new MyDatabaseProvider();
DefaultCompilers.TryGetCustom(provider.GetType(), out var compiler).Should().BeFalse();

DefaultCompilers.RegisterFor<MyDatabaseProvider>(new PercentCompiler());

DefaultCompilers.TryGetCustom(provider, out compiler).Should().BeTrue();
compiler.Should().BeOfType<PercentCompiler>();

DefaultCompilers.RegisterFor<MyDatabaseProvider>(null);
DefaultCompilers.TryGetCustom(provider.GetType(), out compiler).Should().BeFalse();
}
}
}
5 changes: 4 additions & 1 deletion PetaPoco.SqlKata.Tests/MultiPocoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Moq;
using SqlKata;
using PetaPoco.Extensions;
using PetaPoco.Providers;
using PetaPoco.Core;

namespace PetaPoco.SqlKata.Tests
{
Expand All @@ -21,7 +23,8 @@ public class A { }

public MultiPocoTests()
{
_mockDb = new Mock<IDatabase>();
_mockDb = new Mock<IDatabase>();
_mockDb.Setup(m => m.Provider).Returns(new SqlServerDatabaseProvider());
}

[Fact]
Expand Down
19 changes: 19 additions & 0 deletions PetaPoco.SqlKata.Tests/PercentCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SqlKata.Compilers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PetaPoco.SqlKata.Tests
{
public class PercentCompiler : Compiler
{
public PercentCompiler()
{
OpeningIdentifier = ClosingIdentifier = "%%";
}
}


}
64 changes: 62 additions & 2 deletions PetaPoco.SqlKata.Tests/ToSqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using Xunit;
using FluentAssertions;
using SqlKata.Compilers;

namespace PetaPoco.SqlKata.Tests
{
Expand Down Expand Up @@ -33,15 +34,15 @@ public void Different_Default_Compilers(CompilerType type, string table)
{
try
{
SqlKataExtensions.DefaultCompiler = type;
SqlKataExtensions.DefaultCompilerType = type;
var input = new Query("Foo");
var expected = new Sql($"SELECT * FROM {table}");
var output = input.ToSql();
output.Should().BeEquivalentTo(expected);
}
finally
{
SqlKataExtensions.DefaultCompiler = CompilerType.SqlServer;
SqlKataExtensions.DefaultCompilerType = CompilerType.SqlServer;
}
}

Expand Down Expand Up @@ -213,5 +214,64 @@ public void NullQuery_ShouldThrow()
Action act = () => query.ToSql();
act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void NullCompiler_ShouldThrow()
{
var input = new Query("Foo");
Compiler compiler = null;
Action act = () => input.ToSql(compiler);
act.Should().Throw<ArgumentNullException>();
}

private void Compile_With_Percents(Func<Query, Sql> compile)
{
var input = new Query("Foo");
var expected = new Sql("SELECT * FROM %%Foo%%");
var output = compile(input);
output.Should().BeEquivalentTo(expected);
}

[Fact]
public void Pass_Compiler_Instance()
{
Compile_With_Percents(q => q.ToSql(new PercentCompiler()));
}

[Fact]
public void Pass_Generic()
{
Compile_With_Percents(q => q.ToSql<PercentCompiler>());
}

[Fact]
public void Set_Custom_Instance()
{
try
{
SqlKataExtensions.CustomCompiler = new PercentCompiler();
Compile_With_Percents(q => q.ToSql());
}
finally
{
SqlKataExtensions.DefaultCompilerType = CompilerType.SqlServer;
}
}

[Fact]
public void Null_Custom_Instance()
{
try
{
SqlKataExtensions.DefaultCompilerType = CompilerType.Custom;
var input = new Query("Foo");
Action act = () => input.ToSql();
act.Should().Throw<InvalidOperationException>();
}
finally
{
SqlKataExtensions.DefaultCompilerType = CompilerType.SqlServer;
}
}
}
}
Loading

0 comments on commit dd3be3a

Please sign in to comment.