Skip to content

Commit 9fe53e1

Browse files
committed
Redo tests with mocks
1 parent 15734c3 commit 9fe53e1

File tree

4 files changed

+41
-50
lines changed

4 files changed

+41
-50
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using PetaPoco.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Data.Common;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace StaTypPocoQueries.PetaPoco.Tests
10+
{
11+
public class AngleDatabaseProvider : DatabaseProvider
12+
{
13+
public override DbProviderFactory GetFactory() => null;
14+
public override string EscapeSqlIdentifier(string sqlIdentifier) => $"<{sqlIdentifier}>";
15+
}
16+
}

StaTypPocoQueries.PetaPoco.Tests/QueryTests.cs

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
using PetaPoco.Providers;
55
using PetaPoco;
66
using System.Data;
7-
using SQLDatabase.Net.SQLDatabaseClient;
87
using StaTypPocoQueries.PetaPoco;
98
using FluentAssertions;
109
using System.Data.Common;
10+
using Moq;
11+
using System.Collections.Generic;
12+
using AutoFixture.Xunit2;
1113

1214
namespace StaTypPocoQueries.PetaPoco.Tests
1315
{
@@ -20,62 +22,40 @@ namespace StaTypPocoQueries.PetaPoco.Tests
2022
* just to test that. Delete needs to be tested separately.
2123
*/
2224

23-
public class QueryTests: IDisposable
25+
public class QueryTests
2426
{
2527
private class MyClass
2628
{
2729
public int ID { get; set; }
2830
public string Name { get; set; }
2931
}
3032

31-
private class SqlDatabaseProvider : DatabaseProvider
32-
{
33-
public override DbProviderFactory GetFactory() => null;
34-
}
35-
36-
private IDbConnection _conn;
33+
private Mock<IDatabase> _mockDb;
34+
private Sql _lastSql;
3735

3836
public QueryTests()
3937
{
40-
DatabaseProvider.RegisterCustomProvider<SqlDatabaseProvider>("SQLD");
41-
42-
_conn = new SqlDatabaseConnection("SchemaName=PetaPoco;uri=@memory");
43-
_conn.Open();
44-
45-
// Create an empty table, since we don't care about the results
46-
var cmd = _conn.CreateCommand();
47-
cmd.CommandType = CommandType.Text;
48-
cmd.CommandText = "CREATE TABLE MYCLASS (ID INTEGER INTEGER NOT NULL, NAME TEXT)";
49-
cmd.ExecuteNonQuery();
38+
_mockDb = new Mock<IDatabase>();
39+
_mockDb.Setup(m => m.Query<MyClass>(It.IsAny<Sql>()))
40+
.Returns(new List<MyClass>())
41+
.Callback<Sql>(s => _lastSql = s);
42+
_mockDb.Setup(m => m.Delete<MyClass>(It.IsAny<Sql>()))
43+
.Callback<Sql>(s => _lastSql = s);
44+
_mockDb.Setup(m => m.Provider).Returns(new AngleDatabaseProvider());
5045
}
5146

52-
public void Dispose()
53-
{
54-
_conn.Dispose();
47+
[Theory, AutoData]
48+
public void Query(int id)
49+
{
50+
_mockDb.Object.Query<MyClass>(c => c.ID == id);
51+
_lastSql.Should().BeEquivalentTo(new Sql("WHERE <ID> = @0", id));
5552
}
5653

57-
[Fact]
58-
public void Query()
54+
[Theory, AutoData]
55+
public void Delete(string name)
5956
{
60-
using (var db = new Database(_conn))
61-
{
62-
db.Fetch<MyClass>(c => c.ID == 4);
63-
64-
db.LastSQL.Should().Be("SELECT [MyClass].[ID], [MyClass].[Name] FROM [MyClass] WHERE [ID] = @0");
65-
db.LastArgs.Should().BeEquivalentTo(4);
66-
}
67-
}
68-
69-
[Fact]
70-
public void Delete()
71-
{
72-
using (var db = new Database(_conn))
73-
{
74-
db.Delete<MyClass>(c => c.Name == "Bob");
75-
76-
db.LastSQL.Should().Be("DELETE FROM [MyClass]\nWHERE [Name] = @0");
77-
db.LastArgs.Should().BeEquivalentTo(new[] { "Bob" });
78-
}
57+
_mockDb.Object.Delete<MyClass>(c => c.Name == name);
58+
_lastSql.Should().BeEquivalentTo(new Sql("WHERE <Name> = @0", name));
7959
}
8060
}
8161
}

StaTypPocoQueries.PetaPoco.Tests/QuoterTests.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@
77
using FluentAssertions;
88
using System.Data.Common;
99
using PetaPoco.Core;
10-
using SQLDatabase.Net.SQLDatabaseClient;
1110
using PetaPoco;
1211

1312
namespace StaTypPocoQueries.PetaPoco.Tests
1413
{
1514
public class QuoterTests
1615
{
17-
private class AngleDatabaseProvider : DatabaseProvider
18-
{
19-
public override DbProviderFactory GetFactory() => null;
20-
public override string EscapeSqlIdentifier(string sqlIdentifier) => $"<{sqlIdentifier}>";
21-
}
22-
2316
[Fact]
2417
public void Quoter_Should_UseCorrectChar()
2518
{

StaTypPocoQueries.PetaPoco.Tests/StaTypPocoQueries.PetaPoco.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="AutoFixture" Version="4.8.0" />
11+
<PackageReference Include="AutoFixture.Xunit2" Version="4.8.0" />
1012
<PackageReference Include="FluentAssertions" Version="5.5.3" />
1113
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
14+
<PackageReference Include="Moq" Version="4.10.1" />
1215
<PackageReference Include="PetaPoco.Compiled" Version="6.0.353" />
13-
<PackageReference Include="SQLDatabase.Net" Version="2.6.0" />
1416
<PackageReference Include="xunit" Version="2.4.0" />
1517
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
1618
</ItemGroup>

0 commit comments

Comments
 (0)