Skip to content

Commit 9bfd35d

Browse files
author
Denis Voituron
committed
Add Guid type in mock samples and add a custom message when a conversion fails
1 parent 365144d commit 9bfd35d

File tree

7 files changed

+83
-22
lines changed

7 files changed

+83
-22
lines changed

DbMocker.Tests/DbMockResourceTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ public void MockTable_DateTime_Test()
9393
Assert.AreEqual(new DateTime(2020, 01, 15), table.Rows[0, 0]);
9494
}
9595

96+
[TestMethod]
97+
[ExpectedException(typeof(InvalidCastException))]
98+
public void MockTable_InvalidDate_Test()
99+
{
100+
string content = "Col1 \n" +
101+
"(date) \n" +
102+
"2020-01-32 ";
103+
104+
var table = MockTable.FromFixed(content);
105+
}
106+
96107
[TestMethod]
97108
public void MockTable_Boolean_Test()
98109
{
@@ -132,6 +143,30 @@ public void MockTable_StringGuillemets_Test()
132143
Assert.AreEqual("ABC DEF", table.Rows[0, 0]);
133144
}
134145

146+
[TestMethod]
147+
public void MockTable_Guid_Test()
148+
{
149+
string content = "Col1 \n" +
150+
"(guid) \n" +
151+
"b5acc392-3b9d-4059-9851-3d7344db6e91 ";
152+
153+
var table = MockTable.FromFixed(content);
154+
155+
Assert.AreEqual(typeof(Guid), table.Columns[0].Type);
156+
Assert.AreEqual(new Guid("b5acc392-3b9d-4059-9851-3d7344db6e91"), table.Rows[0, 0]);
157+
}
158+
159+
[TestMethod]
160+
[ExpectedException(typeof(InvalidCastException))]
161+
public void MockTable_InvalidGuid_Test()
162+
{
163+
string content = "Col1 \n" +
164+
"(guid) \n" +
165+
"b5acc392-XXXX-XXXX-XXXX-3d7344db6e91 ";
166+
167+
var table = MockTable.FromFixed(content);
168+
}
169+
135170
[TestMethod]
136171
public void MockTable_FixedColumns_MissingColumn_Test()
137172
{

DbMocker.Tests/Samples_EFTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public void EF_AllEmployees_Test()
5050

5151
conn.Mocks
5252
.WhenTag("ALL_EMPLOYEES")
53-
.ReturnsTable(new MockTable().AddColumns("Id", "Name")
54-
.AddRow(1, "Scott")
55-
.AddRow(2, "Bill"));
53+
.ReturnsTable(new MockTable().AddColumns("Id", "Code", "Name")
54+
.AddRow(1, "802f2ae2-fd99-4f67-b01a-ccc5a72b4f72", "Scott")
55+
.AddRow(2, "649d095b-0dda-4cb6-a8c9-aafc56bab78f", "Bill"));
5656

5757
using (var context = new CompanyContext(conn))
5858
{
@@ -88,7 +88,8 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
8888
public class Employee
8989
{
9090
public int Id { get; set; }
91-
public string Name { get; set; }
91+
public Guid Code { get; set; }
92+
public string Name { get; set; }
9293
}
9394

9495
#endregion

DbMocker/Data/MockDbDataReader.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ public override float GetFloat(int ordinal)
9999

100100
public override Guid GetGuid(int ordinal)
101101
{
102-
return (Guid)GetValue(ordinal);
102+
object value = GetValue(ordinal);
103+
if (value.GetType() == typeof(string))
104+
{
105+
return new Guid(Convert.ToString(value));
106+
}
107+
else
108+
{
109+
return (Guid)value;
110+
}
103111
}
104112

105113
public override short GetInt16(int ordinal)

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.13.0</Version>
7+
<Version>1.14.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.13.0.0</AssemblyVersion>
28+
<AssemblyVersion>1.14.0.0</AssemblyVersion>
2929
<PackageProjectUrl>https://github.com/Apps72/DbMocker</PackageProjectUrl>
3030
<RepositoryUrl>https://github.com/Apps72/DbMocker</RepositoryUrl>
31-
<FileVersion>1.13.0.0</FileVersion>
31+
<FileVersion>1.14.0.0</FileVersion>
3232
</PropertyGroup>
3333

3434
<ItemGroup>

DbMocker/Helpers/TypeExtension.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public static Type NameToType(this string value)
5959
case "timespan":
6060
return typeof(TimeSpan);
6161

62+
case "guid":
63+
case "id":
64+
case "uniqueidentifier":
65+
case "unique":
66+
return typeof(Guid);
67+
6268
case "decimal":
6369
return typeof(decimal);
6470

DbMocker/MockTableImportFixed.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,31 @@ private IEnumerable<object[]> GetRowsConverted(IEnumerable<string[]> rows)
104104
var rowConverted = new object[fieldsCount];
105105
for (int i = 0; i < fieldsCount; i++)
106106
{
107-
// If not a string => String.Empty = NULL
108-
if (fieldsType[i] != typeof(string) && String.IsNullOrEmpty(row[i]))
107+
try
109108
{
110-
row[i] = "NULL";
111-
}
112-
113-
if (fieldsType[i] != null && String.Compare(row[i], "NULL", ignoreCase: true) != 0)
114-
{
115-
// Convert
116-
var converter = System.ComponentModel.TypeDescriptor.GetConverter(fieldsType[i]);
117-
rowConverted[i] = Convert.ChangeType(converter.ConvertFromInvariantString(row[i]), fieldsType[i]);
109+
// If not a string => String.Empty = NULL
110+
if (fieldsType[i] != typeof(string) && String.IsNullOrEmpty(row[i]))
111+
{
112+
row[i] = "NULL";
113+
}
118114

119-
// Guillemets
120-
if (fieldsType[i] == typeof(string) && row[i].Length >= 2 && row[i][0] == '"' && row[i][row[i].Length -1] == '"')
115+
if (fieldsType[i] != null && String.Compare(row[i], "NULL", ignoreCase: true) != 0)
121116
{
122-
rowConverted[i] = row[i].Substring(1, row[i].Length - 2);
117+
// Convert
118+
var converter = System.ComponentModel.TypeDescriptor.GetConverter(fieldsType[i]);
119+
rowConverted[i] = Convert.ChangeType(converter.ConvertFromInvariantString(row[i]), fieldsType[i]);
120+
121+
// Guillemets
122+
if (fieldsType[i] == typeof(string) && row[i].Length >= 2 && row[i][0] == '"' && row[i][row[i].Length - 1] == '"')
123+
{
124+
rowConverted[i] = row[i].Substring(1, row[i].Length - 2);
125+
}
123126
}
124127
}
128+
catch (Exception)
129+
{
130+
throw new InvalidCastException($"Invalid conversion of \"{row[i]}\" to \"{fieldsType[i].Name}\", for column \"{Fields.ElementAt(i).Name}\".");
131+
}
125132
}
126133
yield return rowConverted;
127134
}
@@ -206,7 +213,7 @@ private static string ReadResourceFile(Assembly assembly, string resourceName)
206213
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
207214
using (StreamReader reader = new StreamReader(stream))
208215
{
209-
return reader.ReadToEnd();
216+
return reader.ReadToEnd();
210217
}
211218
}
212219

ReadMe.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ var conn = new MockDbConnection()
228228

229229
## Releases
230230

231+
## Version 1.14
232+
- Add 'Guid' type in `MockTable.FromFixed` and resource sample files.
233+
- Add a custom message when a sample conversion fails. Ex: `Invalid conversion of "2020-01-32" to "DateTime", for column "Col1"`.
234+
231235
## Version 1.13
232236
- Set output value for DbParameter, using `MockResturns.SetParameterValue` method.
233237
Thanks [unby](https://github.com/unby).

0 commit comments

Comments
 (0)