Skip to content

Commit

Permalink
Set types when create Column definitions
Browse files Browse the repository at this point in the history
Add a MockColumn class to manage ColumnName and ColumnType.
Add WithColumns method : `MockTable.WithColumns(("Col1", typeof(int?)));`
  • Loading branch information
dvoituron authored Sep 2, 2018
1 parent 455866b commit d446f0e
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 24 deletions.
39 changes: 37 additions & 2 deletions DbMocker.Tests/DatabaseCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void Mock_ExecuteTable_Test()
.When(null)
.ReturnsTable(new MockTable()
{
Columns = new[] { "Col1", "Col2", "Col3" },
Columns = Columns.WithNames("Col1", "Col2", "Col3"),
Rows = new object[,]
{
{ 0, 1, 2 },
Expand All @@ -167,11 +167,46 @@ public void Mock_ExecuteTable_Test()
Col3 = 0
});

Assert.AreEqual(3, result.Count()); // 2 rows
Assert.AreEqual(3, result.Count()); // 3 rows
Assert.AreEqual(1, result.First().Col2); // First row / Col2
}

}

[TestMethod]
public void Mock_ExecuteTable_WithNullInFirstRow_Test()
{
var conn = new MockDbConnection();

conn.Mocks
.When(null)
.ReturnsTable(new MockTable()
{
Columns = Columns.WithNames("Col1", "Col2", "Col3"),
Rows = new object[,]
{
{ null, 1, 2 },
{ null, 8, 7 },
{ 4, 5, 6 },
}
});

using (var cmd = new DatabaseCommand(conn))
{
cmd.CommandText.AppendLine("SELECT ...");
var result = cmd.ExecuteTable(new
{
Col1 = (int?)0,
Col2 = 0,
Col3 = 0
});

Assert.AreEqual(null, result.ElementAt(0).Col1);
Assert.AreEqual(null, result.ElementAt(1).Col1);
Assert.AreEqual(4, result.ElementAt(2).Col1);
}

}

}
}
55 changes: 53 additions & 2 deletions DbMocker.Tests/DbMockTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,57 @@ public void Mock_ReturnsSimple_MockTable_Test()
Assert.AreEqual(14, result.GetInt32(1));
}

[TestMethod]
public void Mock_ReturnsSimple_MockTableWithNull_Test()
{
var conn = new MockDbConnection();

var table = MockTable.WithColumns("Col1", "Col2")
.AddRow(null, 12)
.AddRow(13, 14);
conn.Mocks
.WhenAny()
.ReturnsTable(table);

var cmd = conn.CreateCommand();
var result = cmd.ExecuteReader();

result.Read();

Assert.AreEqual(null, result.GetValue(0));
Assert.IsTrue(result.GetFieldType(0) == typeof(object));

result.Read();

Assert.AreEqual(13, result.GetValue(0));
Assert.IsTrue(result.GetFieldType(0) == typeof(object));
}

[TestMethod]
public void Mock_ReturnsSimple_MockTypedTable_Test()
{
var conn = new MockDbConnection();

var table = MockTable.WithColumns(("Col1", typeof(int?)),
("Col2", typeof(int)))
.AddRow(null, 12)
.AddRow(13, 14);
conn.Mocks
.WhenAny()
.ReturnsTable(table);

var cmd = conn.CreateCommand();
var result = cmd.ExecuteReader();

result.Read();

Assert.AreEqual(null, result.GetValue(0));
Assert.IsTrue(result.GetFieldType(0) == typeof(int?));

Assert.AreEqual(12, result.GetValue(1));
Assert.IsTrue(result.GetFieldType(1) == typeof(int));
}

[TestMethod]
public void Mock_ReturnsSimple_DBNull_Test()
{
Expand Down Expand Up @@ -160,8 +211,8 @@ 2 Bill 1972-01-12

var table = MockTable.FromCsv(csv);

Assert.AreEqual("Id", table.Columns[0]);
Assert.AreEqual("Name", table.Columns[1]);
Assert.AreEqual("Id", table.Columns[0].Name);
Assert.AreEqual("Name", table.Columns[1].Name);

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

Expand Down
2 changes: 1 addition & 1 deletion DbMocker.Tests/DbMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void Mock_ReturnsMockTable_Properties_Test()
.When(c => c.CommandText.Contains("SELECT"))
.ReturnsTable(new MockTable()
{
Columns = new[] { "X" },
Columns = Columns.WithNames("X"),
Rows = new object[,]
{
{ 14 }
Expand Down
26 changes: 22 additions & 4 deletions DbMocker.Tests/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public int GetNumberOfEmployees(DbConnection connection)
{
cmd.CommandText = "SELECT COUNT(*) FROM Employees";
return Convert.ToInt32(cmd.ExecuteScalar());
}
}
}

// Sample method from your DataService
public object[][] GetEmployees(DbConnection connection)
{
{
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "SELECT ID, Name FROM Employees";
Expand All @@ -35,8 +35,9 @@ public object[][] GetEmployees(DbConnection connection)
reader.GetValues(row);
data.Add(row);

int id = reader.GetInt32(reader.GetOrdinal("ID"));
string name = reader.GetString(reader.GetOrdinal("NAME"));
int id = reader.GetValue(0) == null ? 0 : reader.GetInt32(0);
string name = reader.GetString(1);

}

return data.ToArray();
Expand Down Expand Up @@ -137,6 +138,23 @@ public void UnitTest6()
Assert.AreEqual(14, count);
}

[TestMethod]
public void UnitTest7()
{
var conn = new MockDbConnection();

conn.Mocks
.WhenAny()
.ReturnsTable(MockTable.WithColumns(("ID", typeof(int?)),
("Name", typeof(string)))
.AddRow(null, "Scott")
.AddRow(2, "Bill"));

var data = GetEmployees(conn);

Assert.AreEqual(null, data[0][0]);
Assert.AreEqual("Scott", data[0][1]);
}

}
}
15 changes: 9 additions & 6 deletions DbMocker/Data/MockDbDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace Apps72.Dev.Data.DbMocker.Data
{
public class MockDbDataReader : DbDataReader
{
private string[] _columns;
private MockColumn[] _columns;
private object[,] _rows;
private int _currentRowIndex = -1;

internal MockDbDataReader(MockTable table)
{
_columns = table.Columns ?? Array.Empty<string>();
_columns = table.Columns ?? Array.Empty<MockColumn>();
_rows = table.Rows ?? new object[,] { };
}

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

public override string GetDataTypeName(int ordinal)
{
return _columns[ordinal].GetType().Name;
return _columns[ordinal].Type.Name;
}

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

public override Type GetFieldType(int ordinal)
{
return GetValue(ordinal).GetType();
if (ordinal < _columns.Length)
return _columns[ordinal].Type;
else
return GetValue(ordinal).GetType();
}

public override float GetFloat(int ordinal)
Expand Down Expand Up @@ -114,14 +117,14 @@ public override long GetInt64(int ordinal)

public override string GetName(int ordinal)
{
return _columns[ordinal];
return _columns[ordinal].Name;
}

public override int GetOrdinal(string name)
{
for (int i = 0; i < _columns.Length; i++)
{
if (String.Compare(_columns[i], name, ignoreCase: true) == 0)
if (String.Compare(_columns[i].Name, name, ignoreCase: true) == 0)
return i;
}
return -1;
Expand Down
4 changes: 2 additions & 2 deletions DbMocker/DbMocker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
<AssemblyName>Apps72.Dev.Data.DbMocker</AssemblyName>
<RootNamespace>Apps72.Dev.Data.DbMocker</RootNamespace>
<Version>1.4.0</Version>
<Version>1.5.0</Version>
<PackageId>DbMocker</PackageId>
<Authors>Denis Voituron</Authors>
<Product>DbMocker</Product>
Expand All @@ -18,7 +18,7 @@ conn.Mocks.WhenAny()..ReturnsScalar(14);</Description>
<PackageTags>DbMocker, Mocker, SQLServer, Oracle, Sqlite, EntityFramework, EF, Dapper, UnitTest</PackageTags>
<PackageReleaseNotes>https://github.com/Apps72/DbMocker</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<AssemblyVersion>1.5.0.0</AssemblyVersion>
<PackageProjectUrl>https://github.com/Apps72/DbMocker</PackageProjectUrl>
<RepositoryUrl>https://github.com/Apps72/DbMocker</RepositoryUrl>
</PropertyGroup>
Expand Down
79 changes: 79 additions & 0 deletions DbMocker/MockColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Diagnostics;
using System.Linq;

namespace Apps72.Dev.Data.DbMocker
{
/// <summary />
[DebuggerDisplay("Name")]
public class MockColumn
{
/// <summary />
public MockColumn() : this(string.Empty, typeof(object))
{
}

/// <summary />
public MockColumn(string name) : this(name, typeof(object))
{
}

/// <summary />
public MockColumn(string name, Type type)
{
this.Name = name;
this.Type = type;
}

/// <summary />
public string Name { get; set; }

/// <summary />
public Type Type { get; set; }

/// <summary />
public static implicit operator string(MockColumn column)
{
return column.Name;
}

/// <summary />
public static implicit operator MockColumn(string name)
{
return new MockColumn()
{
Name = name,
Type = typeof(object)
};
}

/// <summary />
public static implicit operator (string Name, Type Type)(MockColumn column)
{
return (column.Name, column.Type);
}

/// <summary />
public static implicit operator MockColumn((string Name, Type Type) column)
{
return new MockColumn()
{
Name = column.Name,
Type = column.Type
};
}
}

public static class Columns
{
public static MockColumn[] WithNames(params string[] names)
{
return names.Select(name => new MockColumn(name)).ToArray();
}

public static MockColumn[] WithNames(params (string Name, Type Type)[] columns)
{
return columns.Select(i => new MockColumn(i.Name, i.Type)).ToArray();
}
}
}
2 changes: 1 addition & 1 deletion DbMocker/MockReturns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private MockTable ConvertToMockTable<T>(T returns)
{
return new MockTable()
{
Columns = new[] { String.Empty },
Columns = Columns.WithNames(string.Empty),
Rows = new object[,]
{
{ GetValueOrDbNull(returns) }
Expand Down
15 changes: 11 additions & 4 deletions DbMocker/MockTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ public partial class MockTable
/// <summary />
public MockTable()
{
this.Columns = Array.Empty<string>();
this.Columns = Array.Empty<MockColumn>();
this.Rows = null;
}

/// <summary />
public MockTable(string[] columns, object[,] rows)
{
this.Columns = columns;
this.Columns = columns.Select(name => new MockColumn(name)).ToArray();
this.Rows = rows;
}

/// <summary />
public string[] Columns { get; set; }
public MockColumn[] Columns { get; set; }

/// <summary />
public object[,] Rows
Expand Down Expand Up @@ -53,7 +53,14 @@ public MockTable AddRow(params object[] values)
/// <summary />
public MockTable AddColumns(params string[] columns)
{
this.Columns = this.Columns.Concat(columns).ToArray();
this.Columns = this.Columns.Concat(DbMocker.Columns.WithNames(columns)).ToArray();
return this;
}

/// <summary />
public MockTable AddColumns(params (string Name, Type Type)[] columns)
{
this.Columns = this.Columns.Concat(DbMocker.Columns.WithNames(columns)).ToArray();
return this;
}

Expand Down
Loading

0 comments on commit d446f0e

Please sign in to comment.