Skip to content

Commit c2649fc

Browse files
committed
Add samples and publish 1.16
1 parent 007b189 commit c2649fc

File tree

5 files changed

+103
-21
lines changed

5 files changed

+103
-21
lines changed

DbMocker.Tests/DbMocker.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<None Remove="123-EMPLOYEES.txt" />
1011
<None Remove="Samples\SampleTable1.txt" />
1112
<None Remove="Samples\SampleTable2.txt" />
1213
<None Remove="Samples\XX-SampleTable1.txt" />
@@ -16,6 +17,7 @@
1617
<EmbeddedResource Include="Samples\XX-SampleTable1.txt" />
1718
<EmbeddedResource Include="Samples\SampleTable1.txt" />
1819
<EmbeddedResource Include="Samples\SampleTable2.txt" />
20+
<EmbeddedResource Include="Samples\123-EMPLOYEES.txt" />
1921
</ItemGroup>
2022

2123
<ItemGroup>

DbMocker.Tests/Samples.cs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,35 @@ namespace DbMocker.Tests
1010
[TestClass]
1111
public class Samples
1212
{
13+
[TestMethod]
14+
public void UnitTest0()
15+
{
16+
var conn = new MockDbConnection();
17+
18+
// The text file "123-EMPLOYEES.txt" is embedded in this project.
19+
// See the Samples folder.
20+
// - 123 is an identifier (as you want)
21+
// - EMPLOYEES is the CommandText Tag
22+
// See https://docs.microsoft.com/en-us/ef/core/querying/tags
23+
conn.Mocks.LoadTagsFromResources("123-EMPLOYEES");
24+
25+
// Call your "classic" methods to tests
26+
var data = GetEmployees(conn);
27+
28+
// DbMocker read the embedded file
29+
// and associated the content to the tag
30+
Assert.AreEqual(2, data.Length);
31+
Assert.AreEqual("Scott", data[0][1]);
32+
Assert.AreEqual("Bill", data[1][1]);
33+
}
34+
1335
// Sample method from your DataService
1436
public int GetNumberOfEmployees(DbConnection connection)
1537
{
1638
using (var cmd = connection.CreateCommand())
1739
{
18-
cmd.CommandText = "SELECT COUNT(*) FROM Employees";
40+
cmd.CommandText = @"-- COUNT_EMPLOYEES
41+
SELECT COUNT(*) FROM Employees";
1942
return Convert.ToInt32(cmd.ExecuteScalar());
2043
}
2144
}
@@ -25,7 +48,8 @@ public object[][] GetEmployees(DbConnection connection)
2548
{
2649
using (var cmd = connection.CreateCommand())
2750
{
28-
cmd.CommandText = "SELECT ID, Name FROM Employees";
51+
cmd.CommandText = @"-- EMPLOYEES
52+
SELECT ID, Name FROM Employees";
2953
var reader = cmd.ExecuteReader();
3054

3155
var data = new List<object[]>();
@@ -53,7 +77,7 @@ public void UnitTest1()
5377
// Don't execute the query to your SQL Server,
5478
// But returns this MockTable.
5579
conn.Mocks
56-
.When(cmd => cmd.CommandText.StartsWith("SELECT COUNT(*)") &&
80+
.When(cmd => cmd.CommandText.Contains("COUNT_EMPLOYEES") &&
5781
cmd.Parameters.Count() == 0)
5882
.ReturnsTable(MockTable.WithColumns("Count")
5983
.AddRow(14));
@@ -70,7 +94,7 @@ public void UnitTest2()
7094
var conn = new MockDbConnection();
7195

7296
conn.Mocks
73-
.WhenAny()
97+
.WhenTag("EMPLOYEES")
7498
.ReturnsTable(new MockTable().AddColumns("ID", "Name")
7599
.AddRow(1, "Scott")
76100
.AddRow(2, "Bill"));
@@ -86,7 +110,7 @@ public void UnitTest3()
86110
var conn = new MockDbConnection();
87111

88112
conn.Mocks
89-
.WhenAny()
113+
.WhenTag("EMPLOYEES")
90114
.ReturnsTable(MockTable.Empty()
91115
.AddColumns("ID", "Name")
92116
.AddRow(1, "Scott"));
@@ -102,7 +126,7 @@ public void UnitTest4()
102126
var conn = new MockDbConnection();
103127

104128
conn.Mocks
105-
.WhenAny()
129+
.WhenTag("COUNT_EMPLOYEES")
106130
.ReturnsTable(MockTable.SingleCell("Count", 14));
107131

108132
int count = GetNumberOfEmployees(conn);
@@ -116,7 +140,7 @@ public void UnitTest5()
116140
var conn = new MockDbConnection();
117141

118142
conn.Mocks
119-
.WhenAny()
143+
.WhenTag("COUNT_EMPLOYEES")
120144
.ReturnsTable(cmd => MockTable.SingleCell("Count", cmd.Parameters.Count()));
121145

122146
int count = GetNumberOfEmployees(conn);
@@ -130,7 +154,7 @@ public void UnitTest6()
130154
var conn = new MockDbConnection();
131155

132156
conn.Mocks
133-
.WhenAny()
157+
.WhenTag("COUNT_EMPLOYEES")
134158
.ReturnsScalar<int>(14);
135159

136160
int count = GetNumberOfEmployees(conn);
@@ -156,5 +180,29 @@ public void UnitTest7()
156180
Assert.AreEqual("Scott", data[0][1]);
157181
}
158182

183+
[TestMethod]
184+
public void UnitTest8()
185+
{
186+
var conn = new MockDbConnection();
187+
188+
// Sample data rows
189+
var samples = new[]
190+
{
191+
new { ID = 0, Name = "Scott" },
192+
new { ID = 1, Name = "Denis" },
193+
};
194+
195+
conn.Mocks
196+
.WhenTag("EMPLOYEES")
197+
.ReturnsTable(MockTable.FromType(samples));
198+
199+
var data = GetEmployees(conn);
200+
201+
Assert.AreEqual(2, data.Length);
202+
Assert.AreEqual(0, data[0][0]);
203+
Assert.AreEqual("Scott", data[0][1]);
204+
}
205+
206+
159207
}
160208
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Id Name Age
2+
(int) (string) (int?)
3+
4+
10 Scott 21
5+
20 Bill NULL

DbMocker/DbMocker.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<AssemblyName>Apps72.Dev.Data.DbMocker</AssemblyName>
66
<RootNamespace>Apps72.Dev.Data.DbMocker</RootNamespace>
7-
<Version>1.15.0</Version>
7+
<Version>1.16.0</Version>
88
<PackageId>DbMocker</PackageId>
99
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1010
<Authors>Denis Voituron</Authors>
@@ -25,10 +25,10 @@ conn.Mocks
2525
<PackageTags>DbMocker, Mocker, SQLServer, Oracle, Sqlite, EntityFramework, EF, Dapper, UnitTest</PackageTags>
2626
<PackageReleaseNotes>https://github.com/Apps72/DbMocker</PackageReleaseNotes>
2727
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
28-
<AssemblyVersion>1.15.0.0</AssemblyVersion>
28+
<AssemblyVersion>1.16.0.0</AssemblyVersion>
2929
<PackageProjectUrl>https://github.com/Apps72/DbMocker</PackageProjectUrl>
3030
<RepositoryUrl>https://github.com/Apps72/DbMocker</RepositoryUrl>
31-
<FileVersion>1.15.0.0</FileVersion>
31+
<FileVersion>1.16.0.0</FileVersion>
3232
</PropertyGroup>
3333

3434
<ItemGroup>

ReadMe.md

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ public int GetNumberOfEmployees(DbConnection connection)
2222
}
2323
}
2424

25+
/* Create a text file "123-EMPLOYEES.txt" with this content
26+
And set the build property to "Embedded resource".
27+
Id Name Age
28+
(int) (string) (int?)
29+
30+
10 Scott 21
31+
20 Bill NULL
32+
*/
33+
34+
[TestMethod]
35+
public void UnitTest0()
36+
{
37+
var conn = new MockDbConnection();
38+
39+
// The text file "123-EMPLOYEES.txt" is embedded in this project.
40+
// See the Samples folder.
41+
// - 123 is an identifier (as you want)
42+
// - EMPLOYEES is the CommandText Tag
43+
// See https://docs.microsoft.com/en-us/ef/core/querying/tags
44+
conn.Mocks.LoadTagsFromResources("123-EMPLOYEES");
45+
46+
// Call your "classic" methods to tests
47+
var data = GetEmployees(conn);
48+
49+
// DbMocker read the embedded file
50+
// and associated the content to the tag
51+
Assert.AreEqual("Scott", data[0][1]);
52+
Assert.AreEqual("Bill", data[1][1]);
53+
}
54+
2555
[TestMethod]
2656
public void UnitTest1()
2757
{
@@ -45,18 +75,11 @@ public void UnitTest1()
4575

4676
See [https://apps72.com](https://apps72.com) for more information.
4777

48-
Tip: To easily detect SQL queries to intercept, we advise you to add a comment that identifies the SQL query
49-
and use it in DBMocker.
5078

51-
```CSharp
52-
cmd.CommandText = " -- [Request to Update Employees] ...";
53-
...
54-
conn.Mocks
55-
.When(cmd => cmd.CommandText.Contains("[Request to Update Employees]")
56-
.ReturnsScalar(14);
57-
```
5879

59-
Since DBMocker 1.6, use the `WhenTag` method (see below).
80+
81+
82+
6083

6184
## Conditions
6285

@@ -228,6 +251,10 @@ var conn = new MockDbConnection()
228251

229252
## Releases
230253

254+
## Version 1.16
255+
- Add `MockTable.FromType<T>` method to fill a mock table using existing .NET objects.
256+
Thanks [martinsmith1968](https://github.com/martinsmith1968).
257+
231258
## Version 1.15
232259
- Fix `MockDbDataReader.IsDBNull` when the value is null.
233260

0 commit comments

Comments
 (0)