mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-22 09:35:38 -05:00
Address warnings and (some) nullables (#2013)
This commit is contained in:
@@ -105,8 +105,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
yield return new object[] {"0x000", new byte[] {0x00, 0x00}, "0x0000"}; // Base16, odd
|
||||
|
||||
// Single byte tests
|
||||
yield return new object[] {"50", new byte[] {0x32}, "0x32"}; // Base10
|
||||
yield return new object[] {"050", new byte[] {0x32}, "0x32"}; // Base10, leading zeros
|
||||
yield return new object[] {"50", "2"u8.ToArray(), "0x32"}; // Base10
|
||||
yield return new object[] {"050", "2"u8.ToArray(), "0x32"}; // Base10, leading zeros
|
||||
yield return new object[] {"0xF0", new byte[] {0xF0}, "0xF0"}; // Base16
|
||||
yield return new object[] {"0x0F", new byte[] {0x0F}, "0x0F"}; // Base16, leading zeros
|
||||
yield return new object[] {"0xF", new byte[] {0x0F}, "0x0F"}; // Base16, odd
|
||||
@@ -296,7 +296,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
return new DbColumnWrapper(new CellUpdateTestDbColumn(typeof(T), dataTypeName, allowNull, colSize));
|
||||
}
|
||||
|
||||
private class CellUpdateTestDbColumn : DbColumn
|
||||
private sealed class CellUpdateTestDbColumn : DbColumn
|
||||
{
|
||||
public CellUpdateTestDbColumn(Type dataType, string dataTypeName, bool allowNull = true, int? colSize = null)
|
||||
{
|
||||
|
||||
@@ -21,18 +21,18 @@ using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
public class RowCreateTests
|
||||
public partial class RowCreateTests
|
||||
{
|
||||
[Test]
|
||||
public async Task RowCreateConstruction()
|
||||
{
|
||||
// Setup: Create the values to store
|
||||
const long rowId = 100;
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
|
||||
// If: I create a RowCreate instance
|
||||
RowCreate rc = new RowCreate(rowId, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(rowId, rs, data.TableMetadata);
|
||||
|
||||
// Then: The values I provided should be available
|
||||
Assert.AreEqual(rowId, rc.RowId);
|
||||
@@ -67,7 +67,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// Setup: Generate the parameters for the row create
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, defaultCols, nullableCols);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, includeIdentity);
|
||||
RowCreate rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
|
||||
// If: I ask for a script to be generated without setting any values
|
||||
// Then: An exception should be thrown for missing cells
|
||||
@@ -116,11 +116,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Generate the parameters for the row create
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, colsWithDefaultConstraints, colsThatAllowNull);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, colsWithDefaultConstraints, colsThatAllowNull);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, includeIdentity);
|
||||
|
||||
// ... Create a row create and set the appropriate number of cells
|
||||
RowCreate rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
Common.AddCells(rc, valuesToSkipSetting);
|
||||
|
||||
// If: I ask for the script for the row insert
|
||||
@@ -139,8 +139,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
if (expectedOutput == null)
|
||||
{
|
||||
// If expected output was null make sure we match the default values reges
|
||||
Regex r = new Regex(@"INSERT INTO (.+) DEFAULT VALUES");
|
||||
Match m = r.Match(sql);
|
||||
Match m = GetInsert1Regex().Match(sql);
|
||||
Assert.True(m.Success);
|
||||
|
||||
// Table name matches
|
||||
@@ -149,8 +148,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
else
|
||||
{
|
||||
// Do the whole validation
|
||||
Regex r = new Regex(@"INSERT INTO (.+)\((.+)\) VALUES \((.+)\)");
|
||||
Match m = r.Match(sql);
|
||||
Match m = GetInsert2Regex().Match(sql);
|
||||
Assert.True(m.Success);
|
||||
|
||||
// Table name matches
|
||||
@@ -174,14 +172,14 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// Setup:
|
||||
// ... Generate the parameters for the row create
|
||||
const long rowId = 100;
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, 0, 0);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, 0, 0);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, includeIdentity);
|
||||
|
||||
// ... Setup a db reader for the result of an insert
|
||||
var newRowReader = Common.GetNewRowDataReader(data.DbColumns, includeIdentity);
|
||||
|
||||
// If: I ask for the change to be applied
|
||||
RowCreate rc = new RowCreate(rowId, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(rowId, rs, data.TableMetadata);
|
||||
await rc.ApplyChanges(newRowReader);
|
||||
|
||||
// Then: The result set should have an additional row in it
|
||||
@@ -226,9 +224,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Generate the row create object
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, defaultCols, nullableCols);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, defaultCols, nullableCols);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, includeIdentity);
|
||||
RowCreate rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
|
||||
// ... Create a mock db connection for building the command
|
||||
var mockConn = new TestSqlConnection();
|
||||
@@ -280,14 +278,14 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Generate the parameters for the row create
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, defaultCols, nullableCols);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, defaultCols, nullableCols);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, includeIdentity);
|
||||
|
||||
// ... Mock db connection for building the command
|
||||
var mockConn = new TestSqlConnection(null);
|
||||
|
||||
// ... Create a row create and set the appropriate number of cells
|
||||
RowCreate rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
Common.AddCells(rc, valuesToSkip);
|
||||
|
||||
// If: I ask for the command for the row insert
|
||||
@@ -311,8 +309,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
Assert.AreEqual(3, splitSql.Length);
|
||||
|
||||
// Check the declare statement first
|
||||
Regex declareRegex = new Regex(@"^DECLARE @(.+) TABLE \((.+)\)$");
|
||||
Match declareMatch = declareRegex.Match(splitSql[0]);
|
||||
Match declareMatch = GetDeclareRegex().Match(splitSql[0]);
|
||||
Assert.True(declareMatch.Success);
|
||||
|
||||
// Declared table name matches
|
||||
@@ -327,7 +324,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
if (expectedOutput.ExpectedInColumns == 0 || expectedOutput.ExpectedInValues == 0)
|
||||
{
|
||||
// If expected output was null make sure we match the default values reges
|
||||
Regex insertRegex = new Regex(@"^INSERT INTO (.+) OUTPUT (.+) INTO @(.+) DEFAULT VALUES$");
|
||||
var insertRegex = GetInsertRegex();
|
||||
Match insertMatch = insertRegex.Match(splitSql[1]);
|
||||
Assert.True(insertMatch.Success);
|
||||
|
||||
@@ -347,7 +344,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
else
|
||||
{
|
||||
// Do the whole validation
|
||||
Regex insertRegex = new Regex(@"^INSERT INTO (.+)\((.+)\) OUTPUT (.+) INTO @(.+) VALUES \((.+)\)$");
|
||||
var insertRegex = GetInsertFullRegex();
|
||||
Match insertMatch = insertRegex.Match(splitSql[1]);
|
||||
Assert.True(insertMatch.Success);
|
||||
|
||||
@@ -380,7 +377,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
}
|
||||
|
||||
// Check the select statement last
|
||||
Regex selectRegex = new Regex(@"^SELECT (.+) FROM @(.+)$");
|
||||
var selectRegex = GetSelectRegex();
|
||||
Match selectMatch = selectRegex.Match(splitSql[2]);
|
||||
Assert.True(selectMatch.Success);
|
||||
|
||||
@@ -425,9 +422,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup: Generate a row create with default values
|
||||
const long rowId = 100;
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, 3, 0);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, 3, 0);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
RowCreate rc = new RowCreate(rowId, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(rowId, rs, data.TableMetadata);
|
||||
|
||||
// If: I request an edit row from the row create
|
||||
EditRow er = rc.GetEditRow(null);
|
||||
@@ -451,9 +448,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup: Generate a row create with an identity column
|
||||
const long rowId = 100;
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, true, 0, 0);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, true, 0, 0);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, true);
|
||||
RowCreate rc = new RowCreate(rowId, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(rowId, rs, data.TableMetadata);
|
||||
|
||||
// If: I request an edit row from the row created
|
||||
EditRow er = rc.GetEditRow(null);
|
||||
@@ -573,7 +570,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
var etm = Common.GetCustomEditTableMetadata(cols);
|
||||
|
||||
// ... Create the row create
|
||||
RowCreate rc = new RowCreate(100, rs, etm);
|
||||
var rc = new RowCreate(100, rs, etm);
|
||||
|
||||
// If: I set a cell in the newly created row to something that will be corrected
|
||||
EditUpdateCellResult eucr = rc.SetCell(0, "1000");
|
||||
@@ -644,9 +641,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Generate the parameters for the row create
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, defaultCols, 0);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, defaultCols, 0);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
RowCreate rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
|
||||
// If: I attempt to revert a cell that has not been set
|
||||
EditRevertCellResult result = rc.RevertCell(0);
|
||||
@@ -673,9 +670,9 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Generate the parameters for the row create
|
||||
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, defaultCols, 0);
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, defaultCols, 0);
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
RowCreate rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
var rc = new RowCreate(100, rs, data.TableMetadata);
|
||||
rc.SetCell(0, "1");
|
||||
|
||||
// If: I attempt to revert a cell that was set
|
||||
@@ -720,5 +717,25 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
public int ExpectedInValues { get; }
|
||||
public int ExpectedOutColumns { get; }
|
||||
}
|
||||
|
||||
#region Generated Regex
|
||||
[GeneratedRegex("INSERT INTO (.+) DEFAULT VALUES")]
|
||||
private static partial Regex GetInsert1Regex();
|
||||
|
||||
[GeneratedRegex("INSERT INTO (.+)\\((.+)\\) VALUES \\((.+)\\)")]
|
||||
private static partial Regex GetInsert2Regex();
|
||||
|
||||
[GeneratedRegex("^DECLARE @(.+) TABLE \\((.+)\\)$")]
|
||||
private static partial Regex GetDeclareRegex();
|
||||
|
||||
[GeneratedRegex("^INSERT INTO (.+) OUTPUT (.+) INTO @(.+) DEFAULT VALUES$")]
|
||||
private static partial Regex GetInsertRegex();
|
||||
|
||||
[GeneratedRegex("^INSERT INTO (.+)\\((.+)\\) OUTPUT (.+) INTO @(.+) VALUES \\((.+)\\)$")]
|
||||
private static partial Regex GetInsertFullRegex();
|
||||
|
||||
[GeneratedRegex("^SELECT (.+) FROM @(.+)$")]
|
||||
private static partial Regex GetSelectRegex();
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,7 +251,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
private class RowEditTester : RowEditBase
|
||||
private sealed class RowEditTester : RowEditBase
|
||||
{
|
||||
public RowEditTester(ResultSet rs, EditTableMetadata meta) : base(0, rs, meta) { }
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
public class RowUpdateTests
|
||||
public partial class RowUpdateTests
|
||||
{
|
||||
[Test]
|
||||
public async Task RowUpdateConstruction()
|
||||
@@ -32,7 +32,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
var rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
|
||||
// If: I create a RowUpdate instance
|
||||
RowUpdate rc = new RowUpdate(rowId, rs, data.TableMetadata);
|
||||
var rc = new RowUpdate(rowId, rs, data.TableMetadata);
|
||||
|
||||
// Then: The values I provided should be available
|
||||
Assert.AreEqual(rowId, rc.RowId);
|
||||
@@ -61,7 +61,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
|
||||
// If:
|
||||
// ... I add updates to all the cells in the row
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
Common.AddCells(ru, 1);
|
||||
|
||||
// ... Then I update a cell back to it's old value
|
||||
@@ -83,8 +83,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
Assert.True(eucr.IsRowDirty);
|
||||
|
||||
// ... It should be formatted as an update script
|
||||
Regex r = new Regex(@"UPDATE .+ SET (.*) WHERE");
|
||||
var m = r.Match(ru.GetScript());
|
||||
var m = GetUpdateRegex().Match(ru.GetScript());
|
||||
|
||||
// ... It should have 2 updates
|
||||
string updates = m.Groups[1].Value;
|
||||
@@ -102,7 +101,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
|
||||
// If:
|
||||
// ... I add updates to one cell in the row
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
ru.SetCell(1, "qqq");
|
||||
|
||||
// ... Then I update the cell to its original value
|
||||
@@ -149,7 +148,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
var etm = Common.GetCustomEditTableMetadata(cols);
|
||||
|
||||
// ... Create the row update
|
||||
RowUpdate ru = new RowUpdate(0, rs, etm);
|
||||
var ru = new RowUpdate(0, rs, etm);
|
||||
|
||||
// If: I set a cell in the newly created row to something that will be corrected
|
||||
EditUpdateCellResult eucr = ru.SetCell(0, "1000");
|
||||
@@ -214,7 +213,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
ResultSet rs = await Common.GetResultSet(data.DbColumns, true);
|
||||
|
||||
// If: I ask for a script to be generated for update
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
Common.AddCells(ru, 1);
|
||||
string script = ru.GetScript();
|
||||
|
||||
@@ -226,7 +225,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
string regexString = isMemoryOptimized
|
||||
? @"UPDATE (.+) WITH \(SNAPSHOT\) SET (.*) WHERE .+"
|
||||
: @"UPDATE (.+) SET (.*) WHERE .+";
|
||||
Regex r = new Regex(regexString);
|
||||
var r = new Regex(regexString);
|
||||
var m = r.Match(script);
|
||||
Assert.True(m.Success);
|
||||
|
||||
@@ -248,7 +247,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a row update with cell updates
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(isMemoryOptimized, includeIdentity, 0, 0);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, includeIdentity);
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
Common.AddCells(ru, includeIdentity ? 1 : 0);
|
||||
|
||||
// ... Mock db connection for building the command
|
||||
@@ -267,8 +266,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
Assert.True(splitSql.Length >= 3);
|
||||
|
||||
// Check the declare statement first
|
||||
Regex declareRegex = new Regex(@"^DECLARE @(.+) TABLE \((.+)\)$");
|
||||
Match declareMatch = declareRegex.Match(splitSql[0]);
|
||||
Match declareMatch = GetDeclareTableRegex().Match(splitSql[0]);
|
||||
Assert.True(declareMatch.Success);
|
||||
|
||||
// Declared table name matches
|
||||
@@ -283,7 +281,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
string regex = isMemoryOptimized
|
||||
? @"^UPDATE (.+) WITH \(SNAPSHOT\) SET (.+) OUTPUT (.+) INTO @(.+) WHERE .+$"
|
||||
: @"^UPDATE (.+) SET (.+) OUTPUT (.+) INTO @(.+) WHERE .+$";
|
||||
Regex updateRegex = new Regex(regex);
|
||||
var updateRegex = new Regex(regex);
|
||||
Match updateMatch = updateRegex.Match(splitSql[10]);
|
||||
Assert.True(updateMatch.Success);
|
||||
|
||||
@@ -304,8 +302,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
Assert.That(updateMatch.Groups[4].Value, Does.EndWith("Output"));
|
||||
|
||||
// Check the select statement last
|
||||
Regex selectRegex = new Regex(@"^SELECT (.+) FROM @(.+)$");
|
||||
Match selectMatch = selectRegex.Match(splitSql[11]);
|
||||
Match selectMatch = GetSelectRegex().Match(splitSql[11]);
|
||||
Assert.True(selectMatch.Success);
|
||||
|
||||
// Correct number of columns in select statement
|
||||
@@ -343,7 +340,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// Setup: Create a row update with a cell set
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
ru.SetCell(0, "foo");
|
||||
|
||||
// If: I attempt to get an edit row
|
||||
@@ -399,7 +396,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a row update (no cell updates needed)
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, 0, 0);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, includeIdentity);
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
long oldBytesWritten = rs.totalBytesWritten;
|
||||
|
||||
// ... Setup a db reader for the result of an update
|
||||
@@ -421,7 +418,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a row update (no cell updates needed)
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, true, 0, 0);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, true);
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
|
||||
// If: I ask for the changes to be applied with a null db reader
|
||||
// Then: I should get an exception
|
||||
@@ -439,7 +436,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a row update (no cell updates needed)
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
|
||||
// If: I attempt to revert a cell that is out of range
|
||||
// Then: I should get an exception
|
||||
@@ -453,7 +450,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a row update (no cell updates needed)
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, true, 0, 0);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, true);
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
|
||||
// If: I attempt to revert a cell that has not been set
|
||||
EditRevertCellResult result = ru.RevertCell(0);
|
||||
@@ -480,7 +477,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a row update
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
ru.SetCell(0, "qqq");
|
||||
ru.SetCell(1, "qqq");
|
||||
|
||||
@@ -509,7 +506,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a row update
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
|
||||
var rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
var ru = new RowUpdate(0, rs, data.TableMetadata);
|
||||
ru.SetCell(0, "qqq");
|
||||
|
||||
// If: I attempt to revert a cell that was set
|
||||
@@ -538,5 +535,14 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
var rs = await Common.GetResultSet(data.DbColumns, false);
|
||||
return new RowUpdate(0, rs, data.TableMetadata);
|
||||
}
|
||||
|
||||
[GeneratedRegex("^DECLARE @(.+) TABLE \\((.+)\\)$")]
|
||||
private static partial Regex GetDeclareTableRegex();
|
||||
|
||||
[GeneratedRegex("^SELECT (.+) FROM @(.+)$")]
|
||||
private static partial Regex GetSelectRegex();
|
||||
|
||||
[GeneratedRegex("UPDATE .+ SET (.*) WHERE")]
|
||||
private static partial Regex GetUpdateRegex();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
public class SessionTests
|
||||
public partial class SessionTests
|
||||
{
|
||||
#region Construction Tests
|
||||
|
||||
@@ -42,8 +42,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
public void SessionConstructionValid()
|
||||
{
|
||||
// If: I create a session object with a proper arguments
|
||||
Mock<IEditMetadataFactory> mockFactory = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(mockFactory.Object);
|
||||
var mockFactory = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(mockFactory.Object);
|
||||
|
||||
// Then:
|
||||
// ... The edit cache should not exist
|
||||
@@ -87,7 +87,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
ConnectionService.Instance.OwnerToConnectionMap[ci.OwnerUri] = ci;
|
||||
|
||||
var fsf = MemoryFileSystem.GetFileStreamFactory();
|
||||
Query query = new Query(Constants.StandardQuery, ci, new QueryExecutionSettings(), fsf);
|
||||
var query = new Query(Constants.StandardQuery, ci, new QueryExecutionSettings(), fsf);
|
||||
query.Execute();
|
||||
query.ExecutionTask.Wait();
|
||||
|
||||
@@ -116,8 +116,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session without initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// If: I ask to create a row without initializing
|
||||
// Then: I should get an exception
|
||||
@@ -292,8 +292,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session and fake that it has been initialized
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object) {IsInitialized = true};
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object) {IsInitialized = true};
|
||||
|
||||
// If: I initialize it
|
||||
// Then: I should get an exception
|
||||
@@ -305,8 +305,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session and fake that it is in progress of initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object) {InitializeTask = new Task(() => {})};
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object) {InitializeTask = new Task(() => {})};
|
||||
|
||||
// If: I initialize it
|
||||
// Then: I should get an exception
|
||||
@@ -320,8 +320,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session that hasn't been initialized
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
Assert.Catch<ArgumentException>(() => s.Initialize(initParams, c, qr, sh, fh), "I initialize it with a missing parameter. It should throw an exception");
|
||||
}
|
||||
@@ -381,12 +381,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a metadata factory that throws
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
emf.Setup(f => f.GetObjectMetadata(It.IsAny<DbConnection>(), It.IsAny<string[]>(), It.IsAny<string>()))
|
||||
.Throws<Exception>();
|
||||
|
||||
// ... Create a session that hasn't been initialized
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// ... Create a mock for verifying the failure handler will be called
|
||||
var successHandler = DoNothingSuccessMock;
|
||||
@@ -414,15 +414,15 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a metadata factory that will return some generic column information
|
||||
var b = QueryExecution.Common.GetBasicExecutedBatch();
|
||||
var etm = Common.GetCustomEditTableMetadata(b.ResultSets[0].Columns.Cast<DbColumn>().ToArray());
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
emf.Setup(f => f.GetObjectMetadata(It.IsAny<DbConnection>(), It.IsAny<string[]>(), It.IsAny<string>()))
|
||||
.Returns(etm);
|
||||
|
||||
// ... Create a session that hasn't been initialized
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// ... Create a query runner that will fail via exception
|
||||
Mock<EditSession.QueryRunner> qr = new Mock<EditSession.QueryRunner>();
|
||||
var qr = new Mock<EditSession.QueryRunner>();
|
||||
qr.Setup(r => r(It.IsAny<string>())).Throws(new Exception("qqq"));
|
||||
|
||||
// ... Create a mock for verifying the failure handler will be called
|
||||
@@ -451,15 +451,15 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
// ... Create a metadata factory that will return some generic column information
|
||||
var b = QueryExecution.Common.GetBasicExecutedBatch();
|
||||
var etm = Common.GetCustomEditTableMetadata(b.ResultSets[0].Columns.Cast<DbColumn>().ToArray());
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
emf.Setup(f => f.GetObjectMetadata(It.IsAny<DbConnection>(), It.IsAny<string[]>(), It.IsAny<string>()))
|
||||
.Returns(etm);
|
||||
|
||||
// ... Create a session that hasn't been initialized
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// ... Create a query runner that will fail via returning a null query
|
||||
Mock<EditSession.QueryRunner> qr = new Mock<EditSession.QueryRunner>();
|
||||
var qr = new Mock<EditSession.QueryRunner>();
|
||||
qr.Setup(r => r(It.IsAny<string>()))
|
||||
.Returns(Task.FromResult(new EditSession.EditSessionQueryExecutionState(null, message)));
|
||||
|
||||
@@ -490,15 +490,15 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
var q = QueryExecution.Common.GetBasicExecutedQuery();
|
||||
var rs = q.Batches[0].ResultSets[0];
|
||||
var etm = Common.GetCustomEditTableMetadata(rs.Columns.Cast<DbColumn>().ToArray());
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
emf.Setup(f => f.GetObjectMetadata(It.IsAny<DbConnection>(), It.IsAny<string[]>(), It.IsAny<string>()))
|
||||
.Returns(etm);
|
||||
|
||||
// ... Create a session that hasn't been initialized
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// ... Create a query runner that will return a successful query
|
||||
Mock<EditSession.QueryRunner> qr = new Mock<EditSession.QueryRunner>();
|
||||
var qr = new Mock<EditSession.QueryRunner>();
|
||||
qr.Setup(r => r(It.IsAny<string>()))
|
||||
.Returns(Task.FromResult(new EditSession.EditSessionQueryExecutionState(q)));
|
||||
|
||||
@@ -533,8 +533,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session without initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// If: I ask to delete a row without initializing
|
||||
// Then: I should get an exception
|
||||
@@ -583,8 +583,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session without initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// If: I ask to revert a row without initializing
|
||||
// Then: I should get an exception
|
||||
@@ -683,8 +683,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session without initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// If: I ask to revert a cell without initializing
|
||||
// Then: I should get an exception
|
||||
@@ -724,8 +724,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session without initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// If: I ask to update a cell without initializing
|
||||
// Then: I should get an exception
|
||||
@@ -807,8 +807,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session without initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// If: I ask to update a cell without initializing
|
||||
// Then: I should get an exception
|
||||
@@ -974,8 +974,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session without initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// If: I ask to script edits without initializing
|
||||
// Then: I should get an exception
|
||||
@@ -1001,12 +1001,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
EditSession s = await GetBasicSession();
|
||||
|
||||
// ... Add two mock edits that will generate a script
|
||||
Mock<RowEditBase> edit = new Mock<RowEditBase>();
|
||||
var edit = new Mock<RowEditBase>();
|
||||
edit.Setup(e => e.GetScript()).Returns("test");
|
||||
s.EditCache[0] = edit.Object;
|
||||
s.EditCache[1] = edit.Object;
|
||||
|
||||
using (SelfCleaningTempFile file = new SelfCleaningTempFile())
|
||||
using (var file = new SelfCleaningTempFile())
|
||||
{
|
||||
// If: I script the edit cache to a local output path
|
||||
string outputPath = s.ScriptEdits(file.FilePath);
|
||||
@@ -1029,8 +1029,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a session without initializing
|
||||
Mock<IEditMetadataFactory> emf = new Mock<IEditMetadataFactory>();
|
||||
EditSession s = new EditSession(emf.Object);
|
||||
var emf = new Mock<IEditMetadataFactory>();
|
||||
var s = new EditSession(emf.Object);
|
||||
|
||||
// If: I ask to script edits without initializing
|
||||
// Then: I should get an exception
|
||||
@@ -1088,7 +1088,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
DbConnection conn = new TestSqlConnection(null);
|
||||
|
||||
// ... Mock a task that has not completed
|
||||
Task notCompleted = new Task(() => {});
|
||||
var notCompleted = new Task(() => {});
|
||||
s.CommitTask = notCompleted;
|
||||
|
||||
// If: I attempt to commit while a task is in progress
|
||||
@@ -1106,7 +1106,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
DbConnection conn = new TestSqlConnection(null);
|
||||
|
||||
// ... Add a mock commands for fun
|
||||
Mock<RowEditBase> edit = new Mock<RowEditBase>();
|
||||
var edit = new Mock<RowEditBase>();
|
||||
edit.Setup(e => e.GetCommand(It.IsAny<DbConnection>())).Returns<DbConnection>(dbc => dbc.CreateCommand());
|
||||
edit.Setup(e => e.ApplyChanges(It.IsAny<DbDataReader>())).Returns(Task.FromResult(0));
|
||||
s.EditCache[0] = edit.Object;
|
||||
@@ -1150,7 +1150,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
DbConnection conn = new TestSqlConnection(null);
|
||||
|
||||
// ... Add a mock edit that will explode on generating a command
|
||||
Mock<RowEditBase> edit = new Mock<RowEditBase>();
|
||||
var edit = new Mock<RowEditBase>();
|
||||
edit.Setup(e => e.GetCommand(It.IsAny<DbConnection>())).Throws<Exception>();
|
||||
s.EditCache[0] = edit.Object;
|
||||
|
||||
@@ -1194,7 +1194,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
|
||||
|
||||
// If: I generate a query for selecting rows without a limit
|
||||
EditInitializeFiltering eif = new EditInitializeFiltering
|
||||
var eif = new EditInitializeFiltering
|
||||
{
|
||||
LimitResults = null
|
||||
};
|
||||
@@ -1202,7 +1202,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
|
||||
// Then:
|
||||
// ... The query should look like a select statement
|
||||
Regex selectRegex = new Regex("SELECT (.+) FROM (.+)", RegexOptions.IgnoreCase);
|
||||
Regex selectRegex = GetSelectRegex();
|
||||
var match = selectRegex.Match(query);
|
||||
Assert.True(match.Success);
|
||||
|
||||
@@ -1223,7 +1223,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
|
||||
// If: I generate a query for selecting rows with a negative limit
|
||||
// Then: An exception should be thrown
|
||||
EditInitializeFiltering eif = new EditInitializeFiltering
|
||||
var eif = new EditInitializeFiltering
|
||||
{
|
||||
LimitResults = -1
|
||||
};
|
||||
@@ -1237,7 +1237,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
|
||||
|
||||
// If: I generate a query for selecting rows without a limit
|
||||
EditInitializeFiltering eif = new EditInitializeFiltering
|
||||
var eif = new EditInitializeFiltering
|
||||
{
|
||||
LimitResults = limit
|
||||
};
|
||||
@@ -1245,7 +1245,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
|
||||
// Then:
|
||||
// ... The query should look like a select statement
|
||||
Regex selectRegex = new Regex(@"SELECT TOP (\d+) (.+) FROM (.+)", RegexOptions.IgnoreCase);
|
||||
var selectRegex = GetSelectTopRegex();
|
||||
var match = selectRegex.Match(query);
|
||||
Assert.True(match.Success);
|
||||
|
||||
@@ -1286,7 +1286,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
private static Mock<Func<Task>> DoNothingSuccessMock
|
||||
{
|
||||
get {
|
||||
Mock<Func<Task>> successHandler = new Mock<Func<Task>>();
|
||||
var successHandler = new Mock<Func<Task>>();
|
||||
successHandler.Setup(f => f()).Returns(Task.FromResult(0));
|
||||
return successHandler;
|
||||
}
|
||||
@@ -1296,7 +1296,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
{
|
||||
get
|
||||
{
|
||||
Mock<Func<Exception, Task>> failureHandler = new Mock<Func<Exception, Task>>();
|
||||
var failureHandler = new Mock<Func<Exception, Task>>();
|
||||
failureHandler.Setup(f => f(It.IsAny<Exception>())).Returns(Task.FromResult(0));
|
||||
return failureHandler;
|
||||
}
|
||||
@@ -1309,5 +1309,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
|
||||
EditTableMetadata etm = Common.GetCustomEditTableMetadata(rs.Columns.Cast<DbColumn>().ToArray());
|
||||
return await Common.GetCustomSession(q, etm);
|
||||
}
|
||||
|
||||
[GeneratedRegex("SELECT (.+) FROM (.+)", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex GetSelectRegex();
|
||||
|
||||
[GeneratedRegex("SELECT TOP (\\d+) (.+) FROM (.+)", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex GetSelectTopRegex();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user