NULL column validation when updating cells (#274)

* Adding validation that NULL is allowed when setting cell values

* Adding unit tests
This commit is contained in:
Benjamin Russell
2017-03-09 16:14:25 -08:00
committed by GitHub
parent 8f3e83b519
commit f2afa07a93
6 changed files with 50 additions and 8 deletions

View File

@@ -31,11 +31,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
}
[Fact]
public void NullStringTest()
public void NullStringAllowedTest()
{
// If: I attempt to create a CellUpdate to set it to NULL (with mixed cases)
// If: I attempt to create a CellUpdate to set it to NULL
const string nullString = "NULL";
DbColumnWrapper col = GetWrapper<string>("ntext");
DbColumnWrapper col = GetWrapper<string>("ntext", true);
CellUpdate cu = new CellUpdate(col, nullString);
// Then: The value should be a DBNull and the string value should be the same as what
@@ -46,6 +46,14 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
Assert.Equal(col, cu.Column);
}
[Fact]
public void NullStringNotAllowedTest()
{
// If: I attempt to create a cell update to set to null when its not allowed
// Then: I should get an exception thrown
Assert.Throws<InvalidOperationException>(() => new CellUpdate(GetWrapper<string>("ntext", false), "NULL"));
}
[Fact]
public void NullTextStringTest()
{
@@ -197,15 +205,16 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
}
}
private static DbColumnWrapper GetWrapper<T>(string dataTypeName)
private static DbColumnWrapper GetWrapper<T>(string dataTypeName, bool allowNull = true)
{
return new DbColumnWrapper(new CellUpdateTestDbColumn(typeof(T), dataTypeName));
return new DbColumnWrapper(new CellUpdateTestDbColumn(typeof(T), dataTypeName, allowNull));
}
private class CellUpdateTestDbColumn : DbColumn
{
public CellUpdateTestDbColumn(Type dataType, string dataTypeName)
public CellUpdateTestDbColumn(Type dataType, string dataTypeName, bool allowNull = true)
{
AllowDBNull = allowNull;
DataType = dataType;
DataTypeName = dataTypeName;
}