Skip to content

Commit d446f0e

Browse files
authored
Set types when create Column definitions
Add a MockColumn class to manage ColumnName and ColumnType. Add WithColumns method : `MockTable.WithColumns(("Col1", typeof(int?)));`
1 parent 455866b commit d446f0e

File tree

11 files changed

+240
-24
lines changed

11 files changed

+240
-24
lines changed

DbMocker.Tests/DatabaseCommandTests.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void Mock_ExecuteTable_Test()
148148
.When(null)
149149
.ReturnsTable(new MockTable()
150150
{
151-
Columns = new[] { "Col1", "Col2", "Col3" },
151+
Columns = Columns.WithNames("Col1", "Col2", "Col3"),
152152
Rows = new object[,]
153153
{
154154
{ 0, 1, 2 },
@@ -167,11 +167,46 @@ public void Mock_ExecuteTable_Test()
167167
Col3 = 0
168168
});
169169

170-
Assert.AreEqual(3, result.Count()); // 2 rows
170+
Assert.AreEqual(3, result.Count()); // 3 rows
171171
Assert.AreEqual(1, result.First().Col2); // First row / Col2
172172
}
173173

174174
}
175175

176+
[TestMethod]
177+
public void Mock_ExecuteTable_WithNullInFirstRow_Test()
178+
{
179+
var conn = new MockDbConnection();
180+
181+
conn.Mocks
182+
.When(null)
183+
.ReturnsTable(new MockTable()
184+
{
185+
Columns = Columns.WithNames("Col1", "Col2", "Col3"),
186+
Rows = new object[,]
187+
{
188+
{ null, 1, 2 },
189+
{ null, 8, 7 },
190+
{ 4, 5, 6 },
191+
}
192+
});
193+
194+
using (var cmd = new DatabaseCommand(conn))
195+
{
196+
cmd.CommandText.AppendLine("SELECT ...");
197+
var result = cmd.ExecuteTable(new
198+
{
199+
Col1 = (int?)0,
200+
Col2 = 0,
201+
Col3 = 0
202+
});
203+
204+
Assert.AreEqual(null, result.ElementAt(0).Col1);
205+
Assert.AreEqual(null, result.ElementAt(1).Col1);
206+
Assert.AreEqual(4, result.ElementAt(2).Col1);
207+
}
208+
209+
}
210+
176211
}
177212
}

DbMocker.Tests/DbMockTableTests.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,57 @@ public void Mock_ReturnsSimple_MockTable_Test()
5353
Assert.AreEqual(14, result.GetInt32(1));
5454
}
5555

56+
[TestMethod]
57+
public void Mock_ReturnsSimple_MockTableWithNull_Test()
58+
{
59+
var conn = new MockDbConnection();
60+
61+
var table = MockTable.WithColumns("Col1", "Col2")
62+
.AddRow(null, 12)
63+
.AddRow(13, 14);
64+
conn.Mocks
65+
.WhenAny()
66+
.ReturnsTable(table);
67+
68+
var cmd = conn.CreateCommand();
69+
var result = cmd.ExecuteReader();
70+
71+
result.Read();
72+
73+
Assert.AreEqual(null, result.GetValue(0));
74+
Assert.IsTrue(result.GetFieldType(0) == typeof(object));
75+
76+
result.Read();
77+
78+
Assert.AreEqual(13, result.GetValue(0));
79+
Assert.IsTrue(result.GetFieldType(0) == typeof(object));
80+
}
81+
82+
[TestMethod]
83+
public void Mock_ReturnsSimple_MockTypedTable_Test()
84+
{
85+
var conn = new MockDbConnection();
86+
87+
var table = MockTable.WithColumns(("Col1", typeof(int?)),
88+
("Col2", typeof(int)))
89+
.AddRow(null, 12)
90+
.AddRow(13, 14);
91+
conn.Mocks
92+
.WhenAny()
93+
.ReturnsTable(table);
94+
95+
var cmd = conn.CreateCommand();
96+
var result = cmd.ExecuteReader();
97+
98+
result.Read();
99+
100+
Assert.AreEqual(null, result.GetValue(0));
101+
Assert.IsTrue(result.GetFieldType(0) == typeof(int?));
102+
103+
Assert.AreEqual(12, result.GetValue(1));
104+
Assert.IsTrue(result.GetFieldType(1) == typeof(int));
105+
}
106+
56107
[TestMethod]
57108
public void Mock_ReturnsSimple_DBNull_Test()
58109
{
@@ -160,8 +211,8 @@ 2 Bill 1972-01-12
160211

161212
var table = MockTable.FromCsv(csv);
162213

163-
Assert.AreEqual("Id", table.Columns[0]);
164-
Assert.AreEqual("Name", table.Columns[1]);
214+
Assert.AreEqual("Id", table.Columns[0].Name);
215+
Assert.AreEqual("Name", table.Columns[1].Name);
165216

166217
Assert.AreEqual(3, table.Rows.RowsCount());
167218

DbMocker.Tests/DbMockTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void Mock_ReturnsMockTable_Properties_Test()
8888
.When(c => c.CommandText.Contains("SELECT"))
8989
.ReturnsTable(new MockTable()
9090
{
91-
Columns = new[] { "X" },
91+
Columns = Columns.WithNames("X"),
9292
Rows = new object[,]
9393
{
9494
{ 14 }

DbMocker.Tests/Samples.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public int GetNumberOfEmployees(DbConnection connection)
1717
{
1818
cmd.CommandText = "SELECT COUNT(*) FROM Employees";
1919
return Convert.ToInt32(cmd.ExecuteScalar());
20-
}
20+
}
2121
}
2222

2323
// Sample method from your DataService
2424
public object[][] GetEmployees(DbConnection connection)
25-
{
25+
{
2626
using (var cmd = connection.CreateCommand())
2727
{
2828
cmd.CommandText = "SELECT ID, Name FROM Employees";
@@ -35,8 +35,9 @@ public object[][] GetEmployees(DbConnection connection)
3535
reader.GetValues(row);
3636
data.Add(row);
3737

38-
int id = reader.GetInt32(reader.GetOrdinal("ID"));
39-
string name = reader.GetString(reader.GetOrdinal("NAME"));
38+
int id = reader.GetValue(0) == null ? 0 : reader.GetInt32(0);
39+
string name = reader.GetString(1);
40+
4041
}
4142

4243
return data.ToArray();
@@ -137,6 +138,23 @@ public void UnitTest6()
137138
Assert.AreEqual(14, count);
138139
}
139140

141+
[TestMethod]
142+
public void UnitTest7()
143+
{
144+
var conn = new MockDbConnection();
145+
146+
conn.Mocks
147+
.WhenAny()
148+
.ReturnsTable(MockTable.WithColumns(("ID", typeof(int?)),
149+
("Name", typeof(string)))
150+
.AddRow(null, "Scott")
151+
.AddRow(2, "Bill"));
152+
153+
var data = GetEmployees(conn);
154+
155+
Assert.AreEqual(null, data[0][0]);
156+
Assert.AreEqual("Scott", data[0][1]);
157+
}
140158

141159
}
142160
}

DbMocker/Data/MockDbDataReader.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace Apps72.Dev.Data.DbMocker.Data
66
{
77
public class MockDbDataReader : DbDataReader
88
{
9-
private string[] _columns;
9+
private MockColumn[] _columns;
1010
private object[,] _rows;
1111
private int _currentRowIndex = -1;
1212

1313
internal MockDbDataReader(MockTable table)
1414
{
15-
_columns = table.Columns ?? Array.Empty<string>();
15+
_columns = table.Columns ?? Array.Empty<MockColumn>();
1616
_rows = table.Rows ?? new object[,] { };
1717
}
1818

@@ -59,7 +59,7 @@ public override long GetChars(int ordinal, long dataOffset, char[] buffer, int b
5959

6060
public override string GetDataTypeName(int ordinal)
6161
{
62-
return _columns[ordinal].GetType().Name;
62+
return _columns[ordinal].Type.Name;
6363
}
6464

6565
public override DateTime GetDateTime(int ordinal)
@@ -84,7 +84,10 @@ public override IEnumerator GetEnumerator()
8484

8585
public override Type GetFieldType(int ordinal)
8686
{
87-
return GetValue(ordinal).GetType();
87+
if (ordinal < _columns.Length)
88+
return _columns[ordinal].Type;
89+
else
90+
return GetValue(ordinal).GetType();
8891
}
8992

9093
public override float GetFloat(int ordinal)
@@ -114,14 +117,14 @@ public override long GetInt64(int ordinal)
114117

115118
public override string GetName(int ordinal)
116119
{
117-
return _columns[ordinal];
120+
return _columns[ordinal].Name;
118121
}
119122

120123
public override int GetOrdinal(string name)
121124
{
122125
for (int i = 0; i < _columns.Length; i++)
123126
{
124-
if (String.Compare(_columns[i], name, ignoreCase: true) == 0)
127+
if (String.Compare(_columns[i].Name, name, ignoreCase: true) == 0)
125128
return i;
126129
}
127130
return -1;

DbMocker/DbMocker.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netcoreapp2.1</TargetFramework>
55
<AssemblyName>Apps72.Dev.Data.DbMocker</AssemblyName>
66
<RootNamespace>Apps72.Dev.Data.DbMocker</RootNamespace>
7-
<Version>1.4.0</Version>
7+
<Version>1.5.0</Version>
88
<PackageId>DbMocker</PackageId>
99
<Authors>Denis Voituron</Authors>
1010
<Product>DbMocker</Product>
@@ -18,7 +18,7 @@ conn.Mocks.WhenAny()..ReturnsScalar(14);</Description>
1818
<PackageTags>DbMocker, Mocker, SQLServer, Oracle, Sqlite, EntityFramework, EF, Dapper, UnitTest</PackageTags>
1919
<PackageReleaseNotes>https://github.com/Apps72/DbMocker</PackageReleaseNotes>
2020
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
21-
<AssemblyVersion>1.4.0.0</AssemblyVersion>
21+
<AssemblyVersion>1.5.0.0</AssemblyVersion>
2222
<PackageProjectUrl>https://github.com/Apps72/DbMocker</PackageProjectUrl>
2323
<RepositoryUrl>https://github.com/Apps72/DbMocker</RepositoryUrl>
2424
</PropertyGroup>

DbMocker/MockColumn.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Linq;
4+
5+
namespace Apps72.Dev.Data.DbMocker
6+
{
7+
/// <summary />
8+
[DebuggerDisplay("Name")]
9+
public class MockColumn
10+
{
11+
/// <summary />
12+
public MockColumn() : this(string.Empty, typeof(object))
13+
{
14+
}
15+
16+
/// <summary />
17+
public MockColumn(string name) : this(name, typeof(object))
18+
{
19+
}
20+
21+
/// <summary />
22+
public MockColumn(string name, Type type)
23+
{
24+
this.Name = name;
25+
this.Type = type;
26+
}
27+
28+
/// <summary />
29+
public string Name { get; set; }
30+
31+
/// <summary />
32+
public Type Type { get; set; }
33+
34+
/// <summary />
35+
public static implicit operator string(MockColumn column)
36+
{
37+
return column.Name;
38+
}
39+
40+
/// <summary />
41+
public static implicit operator MockColumn(string name)
42+
{
43+
return new MockColumn()
44+
{
45+
Name = name,
46+
Type = typeof(object)
47+
};
48+
}
49+
50+
/// <summary />
51+
public static implicit operator (string Name, Type Type)(MockColumn column)
52+
{
53+
return (column.Name, column.Type);
54+
}
55+
56+
/// <summary />
57+
public static implicit operator MockColumn((string Name, Type Type) column)
58+
{
59+
return new MockColumn()
60+
{
61+
Name = column.Name,
62+
Type = column.Type
63+
};
64+
}
65+
}
66+
67+
public static class Columns
68+
{
69+
public static MockColumn[] WithNames(params string[] names)
70+
{
71+
return names.Select(name => new MockColumn(name)).ToArray();
72+
}
73+
74+
public static MockColumn[] WithNames(params (string Name, Type Type)[] columns)
75+
{
76+
return columns.Select(i => new MockColumn(i.Name, i.Type)).ToArray();
77+
}
78+
}
79+
}

DbMocker/MockReturns.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private MockTable ConvertToMockTable<T>(T returns)
8282
{
8383
return new MockTable()
8484
{
85-
Columns = new[] { String.Empty },
85+
Columns = Columns.WithNames(string.Empty),
8686
Rows = new object[,]
8787
{
8888
{ GetValueOrDbNull(returns) }

DbMocker/MockTable.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ public partial class MockTable
1313
/// <summary />
1414
public MockTable()
1515
{
16-
this.Columns = Array.Empty<string>();
16+
this.Columns = Array.Empty<MockColumn>();
1717
this.Rows = null;
1818
}
1919

2020
/// <summary />
2121
public MockTable(string[] columns, object[,] rows)
2222
{
23-
this.Columns = columns;
23+
this.Columns = columns.Select(name => new MockColumn(name)).ToArray();
2424
this.Rows = rows;
2525
}
2626

2727
/// <summary />
28-
public string[] Columns { get; set; }
28+
public MockColumn[] Columns { get; set; }
2929

3030
/// <summary />
3131
public object[,] Rows
@@ -53,7 +53,14 @@ public MockTable AddRow(params object[] values)
5353
/// <summary />
5454
public MockTable AddColumns(params string[] columns)
5555
{
56-
this.Columns = this.Columns.Concat(columns).ToArray();
56+
this.Columns = this.Columns.Concat(DbMocker.Columns.WithNames(columns)).ToArray();
57+
return this;
58+
}
59+
60+
/// <summary />
61+
public MockTable AddColumns(params (string Name, Type Type)[] columns)
62+
{
63+
this.Columns = this.Columns.Concat(DbMocker.Columns.WithNames(columns)).ToArray();
5764
return this;
5865
}
5966

0 commit comments

Comments
 (0)