Fixed correct value types when saving columns to a json file (#691)

This commit is contained in:
AlexFsmn
2018-09-25 23:24:39 +02:00
committed by Karl Burtram
parent d31ce985ce
commit 41d0ef1814
3 changed files with 104 additions and 1 deletions

View File

@@ -3,6 +3,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -142,5 +143,59 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
}
}
}
[Fact]
public void WriteRowWithSpecialTypesSuccess()
{
// Setup:
// ... Create a request params that has three different types of value
// ... Create a set of data to write
// ... Create storage for the output
SaveResultsAsJsonRequestParams saveParams = new SaveResultsAsJsonRequestParams();
List<DbCellValue> data = new List<DbCellValue>
{
new DbCellValue {DisplayValue = "1", RawObject = 1},
new DbCellValue {DisplayValue = "1.234", RawObject = 1.234},
new DbCellValue {DisplayValue = "2017-07-08T00:00:00", RawObject = new DateTime(2017, 07, 08)},
};
List<DbColumnWrapper> columns = new List<DbColumnWrapper>
{
new DbColumnWrapper(new TestDbColumn("numberCol", typeof(int))),
new DbColumnWrapper(new TestDbColumn("decimalCol", typeof(decimal))),
new DbColumnWrapper(new TestDbColumn("datetimeCol", typeof(DateTime)))
};
byte[] output = new byte[8192];
// If:
// ... I write two rows
var jsonWriter = new SaveAsJsonFileStreamWriter(new MemoryStream(output), saveParams);
using (jsonWriter)
{
jsonWriter.WriteRow(data, columns);
jsonWriter.WriteRow(data, columns);
}
// Then:
// ... Upon deserialization to an array of dictionaries
string outputString = Encoding.UTF8.GetString(output).TrimEnd('\0');
Dictionary<string, string>[] outputObject =
JsonConvert.DeserializeObject<Dictionary<string, string>[]>(outputString);
// ... There should be 2 items in the array,
// ... The item should have three fields, and three values, assigned appropriately
// ... The deserialized values should match the display value
Assert.Equal(2, outputObject.Length);
foreach (var item in outputObject)
{
Assert.Equal(3, item.Count);
for (int i = 0; i < columns.Count; i++)
{
Assert.True(item.ContainsKey(columns[i].ColumnName));
Assert.Equal(data[i].RawObject == null ? null : data[i].DisplayValue, item[columns[i].ColumnName]);
}
}
}
}
}