mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Fixed correct value types when saving columns to a json file (#691)
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user