Warning for multiple delete (#931)

* Fix for multiple delete bug

* minor space removal

* removed catch error in RowDelete

* small optimizations

* space adding

* WIP on creating test

* Some cleanup

* removed spaces

* Fix for verifytext

* Added check for command format correctness.

* tidying up

* added working test for command exception

* simplification of TestDbDataReader getint

* Corrections made

* spacing and naming issues

* minor space

* one more space issue
This commit is contained in:
Alex Ma
2020-08-06 13:18:24 -07:00
committed by Karl Burtram
parent 839acf67cd
commit 81b4bb7753
4 changed files with 144 additions and 6 deletions

View File

@@ -193,6 +193,56 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
Assert.Throws<InvalidOperationException>(() => rd.RevertCell(0));
}
[Fact]
public async Task GetVerifyQuery()
{
// Setup: Create a row update and set the first row cell to have values
// ... other than "1" for testing purposes (simulated select query result).
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
var rs = await Common.GetResultSet(data.DbColumns, false);
RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);
object[][] rows =
{
new object[] {"2", "0", "0"},
};
var testResultSet = new TestResultSet(data.DbColumns, rows);
var newRowReader = new TestDbDataReader(new[] { testResultSet }, false);
await ru.ApplyChanges(newRowReader);
// ... Create a row delete.
RowDelete rd = new RowDelete(0, rs, data.TableMetadata);
int expectedKeys = 3;
// If: I generate a verify command
String verifyCommand = rd.GetVerifyScript();
// Then:
// ... The command should not be null
Assert.NotNull(verifyCommand);
// ... It should be formatted into an where script
string regexTest = @"SELECT COUNT \(\*\) FROM (.+) WHERE (.+)";
Regex r = new Regex(regexTest);
var m = r.Match(verifyCommand);
Assert.True(m.Success);
// ... There should be a table
string tbl = m.Groups[1].Value;
Assert.Equal(data.TableMetadata.EscapedMultipartName, tbl);
// ... There should be as many where components as there are keys
string[] whereComponents = m.Groups[2].Value.Split(new[] { "AND" }, StringSplitOptions.None);
Assert.Equal(expectedKeys, whereComponents.Length);
// ... Mock db connection for building the command
var mockConn = new TestSqlConnection(new[] { testResultSet });
// If: I attempt to get a command for a simulated delete of a row with duplicates.
// Then: The Command will throw an exception as it detects there are
// ... 2 or more rows with the same value in the simulated query results data.
Assert.Throws<EditDataDeleteException>(() => rd.GetCommand(mockConn));
}
private async Task<RowDelete> GetStandardRowDelete()
{
Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);