Skip to content

Commit

Permalink
Simplify and improve db API
Browse files Browse the repository at this point in the history
  • Loading branch information
asherber committed Apr 19, 2019
1 parent 3f71661 commit cc63424
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 223 deletions.
67 changes: 32 additions & 35 deletions PetaPoco.SqlKata.Tests/AutoGenerateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,48 +269,45 @@ public void Different_Default_Mappers(IMapper mapper, string tableName, params s
new object[] { new ConventionMapper(), "MyOtherClass", "OtherID", "OtherName" },
new object[] { null, "MyOtherClass", "OtherID", "OtherName" },
};
}




public class MyClass
{
public int ID { get; set; }
public string Name { get; set; }
}
public class MyClass
{
public int ID { get; set; }
public string Name { get; set; }
}

public class NoFields
{
}
public class NoFields
{
}

[TableName("TableName")]
public class MyClassWithName
{
public int ID { get; set; }
public string Name { get; set; }
}
[TableName("TableName")]
public class MyClassWithName
{
public int ID { get; set; }
public string Name { get; set; }
}

public class MyClassWithColumnNames
{
[Column("ID_FIELD")]
public int ID { get; set; }
[Column("NAME_FIELD")]
public string Name { get; set; }
}
public class MyClassWithColumnNames
{
[Column("ID_FIELD")]
public int ID { get; set; }
[Column("NAME_FIELD")]
public string Name { get; set; }
}

public class UnderscoreMapper : ConventionMapper
{
public UnderscoreMapper()
public class UnderscoreMapper : ConventionMapper
{
InflectColumnName = (i, cn) => i.Underscore(cn);
InflectTableName = (i, tn) => i.Underscore(tn);
public UnderscoreMapper()
{
InflectColumnName = (i, cn) => i.Underscore(cn);
InflectTableName = (i, tn) => i.Underscore(tn);
}
}
}

public class MyOtherClass
{
public int OtherID { get; set; }
public string OtherName { get; set; }
public class MyOtherClass
{
public int OtherID { get; set; }
public string OtherName { get; set; }
}
}
}
8 changes: 8 additions & 0 deletions PetaPoco.SqlKata.Tests/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]
99 changes: 99 additions & 0 deletions PetaPoco.SqlKata.Tests/DatabaseExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using FluentAssertions;
using Moq;
using PetaPoco.Providers;
using SqlKata;
using PetaPoco.Extensions;
using PetaPoco.Core;

namespace PetaPoco.SqlKata.Tests
{
public class DatabaseExtensionTests
{
private Mock<IDatabase> _mockDb;
private Sql _lastSql;

public class UnderscoreMapper : ConventionMapper
{
public UnderscoreMapper()
{
InflectColumnName = (i, cn) => i.Underscore(cn);
InflectTableName = (i, tn) => i.Underscore(tn);
}
}

public class SomeClass
{
public int SomeID { get; set; }
public string SomeName { get; set; }
}

public DatabaseExtensionTests()
{
Mappers.RevokeAll(); // to clear cache
_mockDb = new Mock<IDatabase>();
_mockDb.Setup(m => m.Query<SomeClass>(It.IsAny<Sql>()))
.Returns(new List<SomeClass>())
.Callback<Sql>(s => _lastSql = s);
_mockDb.Setup(m => m.Execute(It.IsAny<Sql>()))
.Returns(0)
.Callback<Sql>(s => _lastSql = s);
}

[Theory]
[MemberData(nameof(QuerySetups))]
public void Query_Uses_MapperAndProvider(IProvider provider, IMapper mapper, string selectText)
{
_mockDb.Setup(m => m.Provider).Returns(provider);
_mockDb.Setup(m => m.DefaultMapper).Returns(mapper);

var q = new Query().Where("Foo", "Bar");
var output = _mockDb.Object.Query<SomeClass>(q);
var expected = new Sql(selectText, "Bar");

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

public static IEnumerable<object[]> QuerySetups => new[]
{
new object[] { new MySqlDatabaseProvider(), new UnderscoreMapper(),
"SELECT `some_id`, `some_name` FROM `some_class` WHERE `Foo` = @0" },
new object[] { new SqlServerDatabaseProvider(), new ConventionMapper(),
"SELECT [SomeID], [SomeName] FROM [SomeClass] WHERE [Foo] = @0"},
};

[Fact]
public void First_Should_Throw()
{
Action act = () => _mockDb.Object.First<SomeClass>(new Query());
act.Should().Throw<InvalidOperationException>().WithMessage("Sequence contains no elements");
}

[Theory]
[MemberData(nameof(ExecuteSetups))]
public void Execute_Uses_Provider(IProvider provider, string expectedSql)
{
_mockDb.Setup(m => m.Provider).Returns(provider);

var q = new Query("Foo").AsUpdate(new { Bar = "Fizzbin" }).Where("Bar", "Baz");
var output = _mockDb.Object.Execute(q);
var expected = new Sql(expectedSql, "Fizzbin", "Baz");

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

public static IEnumerable<object[]> ExecuteSetups => new[]
{
new object[] { new MySqlDatabaseProvider(), "UPDATE `Foo` SET `Bar` = @0 WHERE `Bar` = @1" },
new object[] { new SqlServerDatabaseProvider(), "UPDATE [Foo] SET [Bar] = @0 WHERE [Bar] = @1"},
};
}
}
1 change: 1 addition & 0 deletions PetaPoco.SqlKata.Tests/PetaPoco.SqlKata.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="PetaPoco.Compiled" Version="6.0.316" />
<PackageReference Include="SqlKata" Version="1.1.7" />
<PackageReference Include="xunit" Version="2.3.1" />
Expand Down
Loading

0 comments on commit cc63424

Please sign in to comment.