diff --git a/src/Microsoft.SqlTools.ServiceLayer/EditData/Contracts/EditCell.cs b/src/Microsoft.SqlTools.ServiceLayer/EditData/Contracts/EditCell.cs index 2fc155fd..239070fc 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/EditData/Contracts/EditCell.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/EditData/Contracts/EditCell.cs @@ -4,6 +4,7 @@ // using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts; +using Microsoft.SqlTools.Utility; namespace Microsoft.SqlTools.ServiceLayer.EditData.Contracts { @@ -24,11 +25,10 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.Contracts /// Whether or not the edit cell is dirty public EditCell(DbCellValue dbCellValue, bool isDirty) { - IsDirty = isDirty; + Validate.IsNotNull(nameof(dbCellValue), dbCellValue); + dbCellValue.CopyTo(this); - DisplayValue = dbCellValue.DisplayValue; - IsNull = dbCellValue.IsNull; - RawObject = dbCellValue.RawObject; + IsDirty = isDirty; } /// diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/DbCellValue.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/DbCellValue.cs index cfd5d95a..258c0548 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/DbCellValue.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/DbCellValue.cs @@ -3,6 +3,8 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // +using Microsoft.SqlTools.Utility; + namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts { /// @@ -24,5 +26,18 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts /// The raw object for the cell, for use internally /// internal object RawObject { get; set; } + + /// + /// Copies the values of this DbCellValue into another DbCellValue (or child object) + /// + /// The DbCellValue (or child) that will receive the values + public virtual void CopyTo(DbCellValue other) + { + Validate.IsNotNull(nameof(other), other); + + other.DisplayValue = DisplayValue; + other.IsNull = IsNull; + other.RawObject = RawObject; + } } } diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/EditData/EditCellTests.cs b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/EditData/EditCellTests.cs new file mode 100644 index 00000000..85fcb452 --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/EditData/EditCellTests.cs @@ -0,0 +1,47 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; +using Microsoft.SqlTools.ServiceLayer.EditData.Contracts; +using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts; +using Xunit; + +namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData +{ + public class EditCellTests + { + [Fact] + public void ConstructNullDbCell() + { + // If: I construct an EditCell with a null DbCellValue + // Then: It should throw + Assert.Throws(() => new EditCell(null, true)); + } + + [Fact] + public void ConstructValid() + { + // Setup: Create a DbCellValue to copy the values from + DbCellValue source = new DbCellValue + { + DisplayValue = "qqq", + IsNull = true, + RawObject = 12 + }; + + // If: I construct an EditCell with a valid DbCellValue + EditCell ec = new EditCell(source, true); + + // Then: + // ... The values I provided in the DbCellValue should be present + Assert.Equal(source.DisplayValue, ec.DisplayValue); + Assert.Equal(source.IsNull, ec.IsNull); + Assert.Equal(source.RawObject, ec.RawObject); + + // ... The is dirty value I set should be present + Assert.True(ec.IsDirty); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/QueryExecution/DbCellValueTests.cs b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/QueryExecution/DbCellValueTests.cs new file mode 100644 index 00000000..87df902e --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/QueryExecution/DbCellValueTests.cs @@ -0,0 +1,53 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; +using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts; +using Xunit; + +namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution +{ + public class DbCellValueTests + { + [Fact] + public void ConstructValid() + { + // If: I construct a new DbCellValue + DbCellValue dbc = new DbCellValue + { + DisplayValue = "qqq", + IsNull = true, + RawObject = 12 + }; + + // Then: It should have the values I specified in it + Assert.Equal("qqq", dbc.DisplayValue); + Assert.Equal(12, dbc.RawObject); + Assert.True(dbc.IsNull); + } + + [Fact] + public void CopyToNullOther() + { + // If: I copy a DbCellValue to null + // Then: I should get an exception + Assert.Throws(() => new DbCellValue().CopyTo(null)); + } + + [Fact] + public void CopyToValid() + { + // If: I copy a DbCellValue to another DbCellValue + DbCellValue source = new DbCellValue {DisplayValue = "qqq", IsNull = true, RawObject = 12}; + DbCellValue dest = new DbCellValue(); + source.CopyTo(dest); + + // Then: The source values should be in the dest + Assert.Equal(source.DisplayValue, dest.DisplayValue); + Assert.Equal(source.IsNull, dest.IsNull); + Assert.Equal(source.RawObject, dest.RawObject); + } + } +} \ No newline at end of file