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

@@ -38,8 +38,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
// Check for null
if (valueAsString == NullString)
{
Value = DBNull.Value;
ValueAsString = valueAsString;
ProcessNullValue();
}
else if (columnType == typeof(byte[]))
{
@@ -197,6 +196,18 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement
ValueAsString = Value.ToString();
}
private void ProcessNullValue()
{
// Make sure that nulls are allowed if we set it to null
if (!Column.AllowDBNull.HasTrue())
{
throw new InvalidOperationException(SR.EditDataNullNotAllowed);
}
Value = DBNull.Value;
ValueAsString = NullString;
}
#endregion
}
}

View File

@@ -525,6 +525,14 @@ namespace Microsoft.SqlTools.ServiceLayer
}
}
public static string EditDataNullNotAllowed
{
get
{
return Keys.GetString(Keys.EditDataNullNotAllowed);
}
}
public static string EE_BatchSqlMessageNoProcedureInfo
{
get
@@ -1079,6 +1087,9 @@ namespace Microsoft.SqlTools.ServiceLayer
public const string EditDataInitializeInProgress = "EditDataInitializeInProgress";
public const string EditDataNullNotAllowed = "EditDataNullNotAllowed";
public const string EE_BatchSqlMessageNoProcedureInfo = "EE_BatchSqlMessageNoProcedureInfo";

View File

@@ -435,6 +435,10 @@
<value>Another edit data initialize is in progress for this owner URI. Please wait for completion.</value>
<comment></comment>
</data>
<data name="EditDataNullNotAllowed" xml:space="preserve">
<value>NULL is not allowed for this column</value>
<comment></comment>
</data>
<data name="EE_BatchSqlMessageNoProcedureInfo" xml:space="preserve">
<value>Msg {0}, Level {1}, State {2}, Line {3}</value>
<comment></comment>

View File

@@ -206,6 +206,8 @@ EditDataComputedColumnPlaceholder = <TBD>
EditDataInitializeInProgress = Another edit data initialize is in progress for this owner URI. Please wait for completion.
EditDataNullNotAllowed = NULL is not allowed for this column
############################################################################
# DacFx Resources

View File

@@ -551,6 +551,11 @@
<target state="new">Cannot add row to result buffer, data reader does not contain rows</target>
<note></note>
</trans-unit>
<trans-unit id="EditDataNullNotAllowed">
<source>NULL is not allowed for this column</source>
<target state="new">NULL is not allowed for this column</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>

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;
}