mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-01 17:24:42 -05:00
Add support for using generic SQL queries to filter EditData rows. (#605)
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.ServiceLayer.EditData;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
/// <summary>
|
||||
/// When using generic SQL queries to retrieve EditData rows, the columns in the result set may be
|
||||
/// a reordered subset of the columns that are present in the complete table metadata.
|
||||
/// EditTableMetadata.FilterColumnMetadata() filters out unnecessary columns from the retrieved
|
||||
/// table metadata, and reorders the metadata columns so that it matches the same column
|
||||
/// ordering in the result set.
|
||||
/// </summary>
|
||||
public class FilterMetadataTest
|
||||
{
|
||||
[Fact]
|
||||
public void BasicFilterTest()
|
||||
{
|
||||
EditColumnMetadata[] metas = CreateMetadataColumns(new string[] { "[col1]", "[col2]", "[col3]" });
|
||||
DbColumnWrapper[] cols = CreateColumnWrappers(new string[] { metas[0].EscapedName, metas[1].EscapedName, metas[2].EscapedName });
|
||||
|
||||
EditColumnMetadata[] filteredData = EditTableMetadata.FilterColumnMetadata(metas, cols);
|
||||
ValidateFilteredData(filteredData, cols);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReorderedResultsTest()
|
||||
{
|
||||
EditColumnMetadata[] metas = CreateMetadataColumns(new string[] { "[col1]", "[col2]", "[col3]" });
|
||||
DbColumnWrapper[] cols = CreateColumnWrappers(new string[] { metas[1].EscapedName, metas[2].EscapedName, metas[0].EscapedName });
|
||||
|
||||
EditColumnMetadata[] filteredData = EditTableMetadata.FilterColumnMetadata(metas, cols);
|
||||
ValidateFilteredData(filteredData, cols);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LessResultColumnsTest()
|
||||
{
|
||||
EditColumnMetadata[] metas = CreateMetadataColumns(new string[] { "[col1]", "[col2]", "[col3]", "[fillerCol1]", "[fillerCol2]" });
|
||||
DbColumnWrapper[] cols = CreateColumnWrappers(new string[] { metas[0].EscapedName, metas[1].EscapedName, metas[2].EscapedName });
|
||||
|
||||
EditColumnMetadata[] filteredData = EditTableMetadata.FilterColumnMetadata(metas, cols);
|
||||
ValidateFilteredData(filteredData, cols);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmptyDataTest()
|
||||
{
|
||||
EditColumnMetadata[] metas = new EditColumnMetadata[0];
|
||||
DbColumnWrapper[] cols = new DbColumnWrapper[0];
|
||||
|
||||
EditColumnMetadata[] filteredData = EditTableMetadata.FilterColumnMetadata(metas, cols);
|
||||
ValidateFilteredData(filteredData, cols);
|
||||
}
|
||||
|
||||
private DbColumnWrapper[] CreateColumnWrappers(string[] colNames)
|
||||
{
|
||||
DbColumnWrapper[] cols = new DbColumnWrapper[colNames.Length];
|
||||
for (int i = 0; i < cols.Length; i++)
|
||||
{
|
||||
cols[i] = new DbColumnWrapper(new TestDbColumn(colNames[i], i));
|
||||
}
|
||||
return cols;
|
||||
}
|
||||
|
||||
private EditColumnMetadata[] CreateMetadataColumns(string[] colNames)
|
||||
{
|
||||
EditColumnMetadata[] metas = new EditColumnMetadata[colNames.Length];
|
||||
for (int i = 0; i < metas.Length; i++)
|
||||
{
|
||||
metas[i] = new EditColumnMetadata { EscapedName = colNames[i], Ordinal = i };
|
||||
}
|
||||
return metas;
|
||||
}
|
||||
|
||||
private void ValidateFilteredData(EditColumnMetadata[] filteredData, DbColumnWrapper[] cols)
|
||||
{
|
||||
Assert.Equal(cols.Length, filteredData.Length);
|
||||
for (int i = 0; i < cols.Length; i++)
|
||||
{
|
||||
Assert.Equal(cols[i].ColumnName, filteredData[i].EscapedName);
|
||||
if (cols[i].ColumnOrdinal.HasValue)
|
||||
{
|
||||
Assert.Equal(cols[i].ColumnOrdinal, filteredData[i].Ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,14 +194,17 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
new EditColumnMetadata // No default
|
||||
{
|
||||
DefaultValue = null,
|
||||
EscapedName = cols[0].ColumnName,
|
||||
EscapedName = cols[0].ColumnName
|
||||
},
|
||||
new EditColumnMetadata // Has default
|
||||
{
|
||||
DefaultValue = "default",
|
||||
EscapedName = cols[0].ColumnName,
|
||||
EscapedName = cols[1].ColumnName
|
||||
},
|
||||
new EditColumnMetadata()
|
||||
new EditColumnMetadata
|
||||
{
|
||||
EscapedName = cols[2].ColumnName
|
||||
}
|
||||
};
|
||||
var etm = new EditTableMetadata
|
||||
{
|
||||
|
||||
@@ -83,7 +83,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
/// Constructs a basic DbColumn that is an NVARCHAR(128) NULL
|
||||
/// </summary>
|
||||
/// <param name="columnName">Name of the column</param>
|
||||
public TestDbColumn(string columnName)
|
||||
public TestDbColumn(string columnName, int? columnOrdinal = null)
|
||||
{
|
||||
base.IsLong = false;
|
||||
base.ColumnName = columnName;
|
||||
@@ -91,6 +91,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
base.AllowDBNull = true;
|
||||
base.DataType = typeof(string);
|
||||
base.DataTypeName = "nvarchar";
|
||||
base.ColumnOrdinal = columnOrdinal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,5 +75,21 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.UtilityTests
|
||||
{
|
||||
Assert.Equal(output, FromSqlScript.UnwrapLiteral(input));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("[name]", true)]
|
||||
[InlineData("[ name ]", true)]
|
||||
[InlineData("[na[[]me]", true)]
|
||||
[InlineData("[]", true)]
|
||||
[InlineData("name", false)]
|
||||
[InlineData("[name", false)]
|
||||
[InlineData("name]", false)]
|
||||
[InlineData("[]name", false)]
|
||||
[InlineData("name[]", false)]
|
||||
[InlineData("[na]me", false)]
|
||||
public void BracketedIdentifierTest(string input, bool output)
|
||||
{
|
||||
Assert.Equal(output, FromSqlScript.IsIdentifierBracketed(input));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user