feature/edit/subset (#283)

* Changing query/subset API to only use Result on success, Error on error

* Creating an interservice API for getting query result subsets

* Updates to subset API

* RowStartIndex is now long
* Output of query/subset is a 2D array of DbCellValue
* Adding LongSkip method to LongList to allow skipping ahead by longs
* Moving LongList back to ServiceLayer utilities. Move refactoring

* Stubbing out request for edit/subset

* Initial implementation of getting edit rows

* Unit tests for RowEdit and RowDelete .GetEditRow

* Fixing major bugs in LongList implementation, adding much more thorough tests

* Adding some more unit tests and fixes to make unit tests pass

* Fixing comment
This commit is contained in:
Benjamin Russell
2017-03-21 15:14:04 -07:00
committed by GitHub
parent 9e576dea92
commit d7ecfb1a87
26 changed files with 1165 additions and 165 deletions

View File

@@ -216,6 +216,33 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void AsDbCellValue(bool isNull)
{
// Setup: Create a cell update
var value = isNull ? "NULL" : "foo";
var col = GetWrapper<string>("NTEXT");
CellUpdate cu = new CellUpdate(col, value);
// If: I convert it to a DbCellvalue
DbCellValue dbc = cu.AsDbCellValue;
// Then:
// ... It should not be null
Assert.NotNull(dbc);
// ... The display value should be the same as the value we supplied
Assert.Equal(value, dbc.DisplayValue);
// ... The null-ness of the value should be the same as what we supplied
Assert.Equal(isNull, dbc.IsNull);
// ... We don't care *too* much about the raw value, but we'll check it anyhow
Assert.Equal(isNull ? (object)DBNull.Value : value, dbc.RawObject);
}
private static DbColumnWrapper GetWrapper<T>(string dataTypeName, bool allowNull = true)
{
return new DbColumnWrapper(new CellUpdateTestDbColumn(typeof(T), dataTypeName, allowNull));

View File

@@ -12,6 +12,7 @@ using Microsoft.SqlTools.ServiceLayer.EditData;
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
using Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.Test.Common;
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
using Xunit;
@@ -181,6 +182,64 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
Assert.Throws<InvalidOperationException>(() => rc.GetCommand(mockConn));
}
[Fact]
public void GetEditRowNoAdditions()
{
// Setup: Generate a standard row create
RowCreate rc = GetStandardRowCreate();
// If: I request an edit row from the row create
EditRow er = rc.GetEditRow(null);
// Then:
// ... The row should not be null
Assert.NotNull(er);
// ... The row should not be clean
Assert.True(er.IsDirty);
Assert.Equal(EditRow.EditRowState.DirtyInsert, er.State);
// ... The row should have a bunch of empty cells (equal to number of columns)
Assert.Equal(rc.newCells.Length, er.Cells.Length);
Assert.All(er.Cells, dbc =>
{
Assert.Equal(string.Empty, dbc.DisplayValue);
Assert.False(dbc.IsNull);
});
}
[Fact]
public void GetEditRowWithAdditions()
{
// Setp: Generate a row create with a cell added to it
RowCreate rc = GetStandardRowCreate();
rc.SetCell(0, "foo");
// If: I request an edit row from the row create
EditRow er = rc.GetEditRow(null);
// Then:
// ... The row should not be null and contain the same number of cells as columns
Assert.NotNull(er);
Assert.Equal(EditRow.EditRowState.DirtyInsert, er.State);
// ... The row should not be clean
Assert.True(er.IsDirty);
Assert.Equal(EditRow.EditRowState.DirtyInsert, er.State);
// ... The row should have a single non-empty cell at the beginning
Assert.Equal("foo", er.Cells[0].DisplayValue);
Assert.False(er.Cells[0].IsNull);
// ... The rest of the cells should be blank
for (int i = 1; i < er.Cells.Length; i++)
{
DbCellValue dbc = er.Cells[i];
Assert.Equal(string.Empty, dbc.DisplayValue);
Assert.False(dbc.IsNull);
}
}
[Theory]
[InlineData(-1)] // Negative
[InlineData(3)] // At edge of acceptable values

View File

@@ -5,11 +5,14 @@
using System;
using System.Data.Common;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.EditData;
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
using Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
using Xunit;
@@ -21,15 +24,15 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
public void RowDeleteConstruction()
{
// Setup: Create the values to store
const long rowId = 100;
ResultSet rs = QueryExecution.Common.GetBasicExecutedBatch().ResultSets[0];
IEditTableMetadata etm = Common.GetStandardMetadata(rs.Columns);
DbColumn[] columns = Common.GetColumns(true);
ResultSet rs = Common.GetResultSet(columns, true);
IEditTableMetadata etm = Common.GetStandardMetadata(columns, false);
// If: I create a RowCreate instance
RowCreate rc = new RowCreate(rowId, rs, etm);
RowDelete rc = new RowDelete(100, rs, etm);
// Then: The values I provided should be available
Assert.Equal(rowId, rc.RowId);
Assert.Equal(100, rc.RowId);
Assert.Equal(rs, rc.AssociatedResultSet);
Assert.Equal(etm, rc.AssociatedObjectMetadata);
}
@@ -64,14 +67,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
public async Task ApplyChanges()
{
// Setup: Generate the parameters for the row delete object
// We don't care about the values besides the row ID
const long rowId = 0;
var columns = Common.GetColumns(false);
var rs = Common.GetResultSet(columns, false);
var etm = Common.GetStandardMetadata(columns);
// If: I ask for the change to be applied
RowDelete rd = new RowDelete(rowId, rs, etm);
RowDelete rd = new RowDelete(0, rs, etm);
await rd.ApplyChanges(null); // Reader not used, can be null
// Then : The result set should have one less row in it
@@ -87,11 +88,10 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
{
// Setup:
// ... Create a row delete
const long rowId = 0;
var columns = Common.GetColumns(includeIdentity);
var rs = Common.GetResultSet(columns, includeIdentity);
var etm = Common.GetStandardMetadata(columns, !includeIdentity, isMemoryOptimized);
RowDelete rd = new RowDelete(rowId, rs, etm);
RowDelete rd = new RowDelete(0, rs, etm);
// ... Mock db connection for building the command
var mockConn = new TestSqlConnection(null);
@@ -131,26 +131,66 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
public void GetCommandNullConnection()
{
// Setup: Create a row delete
var columns = Common.GetColumns(false);
var rs = Common.GetResultSet(columns, false);
var etm = Common.GetStandardMetadata(columns);
RowDelete rd = new RowDelete(0, rs, etm);
RowDelete rd = GetStandardRowDelete();
// If: I attempt to create a command with a null connection
// Then: It should throw an exception
Assert.Throws<ArgumentNullException>(() => rd.GetCommand(null));
}
[Fact]
public void GetEditRow()
{
// Setup: Create a row delete
var columns = Common.GetColumns(false);
var rs = Common.GetResultSet(columns, false);
var etm = Common.GetStandardMetadata(columns);
RowDelete rd = new RowDelete(0, rs, etm);
// If: I attempt to get an edit row
DbCellValue[] cells = rs.GetRow(0).ToArray();
EditRow er = rd.GetEditRow(cells);
// Then:
// ... The state should be dirty
Assert.True(er.IsDirty);
Assert.Equal(EditRow.EditRowState.DirtyDelete, er.State);
// ... The ID should be the same as the one provided
Assert.Equal(0, er.Id);
// ... The row should match the cells that were given
Assert.Equal(cells.Length, er.Cells.Length);
for (int i = 0; i < cells.Length; i++)
{
DbCellValue originalCell = cells[i];
DbCellValue outputCell = er.Cells[i];
Assert.Equal(originalCell.DisplayValue, outputCell.DisplayValue);
Assert.Equal(originalCell.IsNull, outputCell.IsNull);
// Note: No real need to check the RawObject property
}
}
[Fact]
public void GetEditNullRow()
{
// Setup: Create a row delete
RowDelete rd = GetStandardRowDelete();
// If: I attempt to get an edit row with a null cached row
// Then: I should get an exception
Assert.Throws<ArgumentNullException>(() => rd.GetEditRow(null));
}
[Fact]
public void SetCell()
{
DbColumn[] columns = Common.GetColumns(true);
ResultSet rs = Common.GetResultSet(columns, true);
IEditTableMetadata etm = Common.GetStandardMetadata(columns, false);
// Setup: Create a row delete
RowDelete rd = GetStandardRowDelete();
// If: I set a cell on a delete row edit
// Then: It should throw as invalid operation
RowDelete rd = new RowDelete(0, rs, etm);
Assert.Throws<InvalidOperationException>(() => rd.SetCell(0, null));
}
@@ -158,14 +198,19 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
public void RevertCell()
{
// Setup: Create a row delete
DbColumn[] cols = Common.GetColumns(false);
ResultSet rs = Common.GetResultSet(cols, false);
IEditTableMetadata etm = Common.GetStandardMetadata(cols);
RowDelete rd = new RowDelete(0, rs, etm);
RowDelete rd = GetStandardRowDelete();
// If: I revert a cell on a delete row edit
// Then: It should throw
Assert.Throws<InvalidOperationException>(() => rd.RevertCell(0));
}
private RowDelete GetStandardRowDelete()
{
var cols = Common.GetColumns(false);
var rs = Common.GetResultSet(cols, false);
var etm = Common.GetStandardMetadata(cols);
return new RowDelete(0, rs, etm);
}
}
}

View File

@@ -14,6 +14,7 @@ using Microsoft.SqlTools.ServiceLayer.EditData;
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
using Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.Test.Common;
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
using Xunit;
@@ -267,6 +268,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
throw new NotImplementedException();
}
public override EditRow GetEditRow(DbCellValue[] cells)
{
throw new NotImplementedException();
}
public override string RevertCell(int columnId)
{
throw new NotImplementedException();

View File

@@ -5,6 +5,7 @@
using System;
using System.Data.Common;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
@@ -12,6 +13,7 @@ using Microsoft.SqlTools.ServiceLayer.EditData;
using Microsoft.SqlTools.ServiceLayer.EditData.Contracts;
using Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.Test.Common;
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
using Xunit;
@@ -41,10 +43,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
public void SetCell()
{
// Setup: Create a row update
var columns = Common.GetColumns(false);
var rs = Common.GetResultSet(columns, false);
var etm = Common.GetStandardMetadata(columns);
RowUpdate ru = new RowUpdate(0, rs, etm);
RowUpdate ru = GetStandardRowUpdate();
// If: I set a cell that can be updated
EditUpdateCellResult eucr = ru.SetCell(0, "col1");
@@ -234,15 +233,63 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
[Fact]
public void GetCommandNullConnection()
{
// Setup: Create a row create
var columns = Common.GetColumns(false);
var rs = Common.GetResultSet(columns, false);
var etm = Common.GetStandardMetadata(columns);
RowUpdate rc = new RowUpdate(0, rs, etm);
// Setup: Create a row update
RowUpdate ru = GetStandardRowUpdate();
// If: I attempt to create a command with a null connection
// Then: It should throw an exception
Assert.Throws<ArgumentNullException>(() => rc.GetCommand(null));
Assert.Throws<ArgumentNullException>(() => ru.GetCommand(null));
}
[Fact]
public void GetEditRow()
{
// Setup: Create a row update with a cell set
var columns = Common.GetColumns(false);
var rs = Common.GetResultSet(columns, false);
var etm = Common.GetStandardMetadata(columns);
RowUpdate ru = new RowUpdate(0, rs, etm);
ru.SetCell(0, "foo");
// If: I attempt to get an edit row
DbCellValue[] cells = rs.GetRow(0).ToArray();
EditRow er = ru.GetEditRow(cells);
// Then:
// ... The state should be dirty
Assert.True(er.IsDirty);
Assert.Equal(EditRow.EditRowState.DirtyUpdate, er.State);
// ... The ID should be the same as the one provided
Assert.Equal(0, er.Id);
// ... The row should match the cells that were given, except for the updated cell
Assert.Equal(cells.Length, er.Cells.Length);
for (int i = 1; i < cells.Length; i++)
{
DbCellValue originalCell = cells[i];
DbCellValue outputCell = er.Cells[i];
Assert.Equal(originalCell.DisplayValue, outputCell.DisplayValue);
Assert.Equal(originalCell.IsNull, outputCell.IsNull);
// Note: No real need to check the RawObject property
}
// ... The updated cell should match what it was set to
DbCellValue newCell = er.Cells[0];
Assert.Equal(newCell.DisplayValue, "foo");
Assert.Equal(newCell.IsNull, false);
}
[Fact]
public void GetEditNullRow()
{
// Setup: Create a row update
RowUpdate ru = GetStandardRowUpdate();
// If: I attempt to get an edit row with a null cached row
// Then: I should get an exception
Assert.Throws<ArgumentNullException>(() => ru.GetEditRow(null));
}
[Theory]
@@ -344,5 +391,13 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
// ... The cell should no longer be set
Assert.DoesNotContain(0, ru.cellUpdates.Keys);
}
private RowUpdate GetStandardRowUpdate()
{
var columns = Common.GetColumns(false);
var rs = Common.GetResultSet(columns, false);
var etm = Common.GetStandardMetadata(columns);
return new RowUpdate(0, rs, etm);
}
}
}

View File

@@ -64,6 +64,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
efv.Validate();
}
#endregion
#region Dispose Tests
@@ -215,6 +217,36 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
edit.Verify(e => e.SetCell(It.IsAny<int>(), It.IsAny<string>()), Times.Once);
}
[Fact]
public async Task GetRowsSuccess()
{
// Setup: Create an edit data service with a session
// Setup: Create an edit data service with a session
var eds = new EditDataService(null, null, null);
var session = GetDefaultSession();
eds.ActiveSessions[Constants.OwnerUri] = session;
// If: I validly ask for rows
var efv = new EventFlowValidator<EditSubsetResult>()
.AddResultValidation(esr =>
{
Assert.NotNull(esr);
Assert.NotEmpty(esr.Subset);
Assert.NotEqual(0, esr.RowCount);
})
.Complete();
await eds.HandleSubsetRequest(new EditSubsetParams
{
OwnerUri = Constants.OwnerUri,
RowCount = 10,
RowStartIndex = 0
}, efv.Object);
// Then:
// ... It should be successful
efv.Validate();
}
[Theory]
[InlineData(null, "table", "table")] // Null owner URI
[InlineData(Common.OwnerUri, null, "table")] // Null object name

View File

@@ -7,6 +7,7 @@ using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.EditData;
@@ -392,6 +393,172 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData
#endregion
#region SubSet Tests
[Fact]
public async Task GetRowsNoEdits()
{
// Setup: Create a session with a proper query and metadata
Query q = QueryExecution.Common.GetBasicExecutedQuery();
ResultSet rs = q.Batches[0].ResultSets[0];
IEditTableMetadata etm = Common.GetStandardMetadata(rs.Columns);
EditSession s = new EditSession(rs, etm);
// If: I ask for 3 rows from session skipping the first
EditRow[] rows = await s.GetRows(1, 3);
// Then:
// ... I should get back 3 rows
Assert.Equal(3, rows.Length);
// ... Each row should...
for (int i = 0; i < rows.Length; i++)
{
EditRow er = rows[i];
// ... Have properly set IDs
Assert.Equal(i + 1, er.Id);
// ... Have cells equal to the cells in the result set
DbCellValue[] cachedRow = rs.GetRow(i + 1).ToArray();
Assert.Equal(cachedRow.Length, er.Cells.Length);
for (int j = 0; j < cachedRow.Length; j++)
{
Assert.Equal(cachedRow[j].DisplayValue, er.Cells[j].DisplayValue);
Assert.Equal(cachedRow[j].IsNull, er.Cells[j].IsNull);
}
// ... Be clean, since we didn't apply any updates
Assert.Equal(EditRow.EditRowState.Clean, er.State);
Assert.False(er.IsDirty);
}
}
[Fact]
public async Task GetRowsPendingUpdate()
{
// Setup:
// ... Create a session with a proper query and metadata
Query q = QueryExecution.Common.GetBasicExecutedQuery();
ResultSet rs = q.Batches[0].ResultSets[0];
IEditTableMetadata etm = Common.GetStandardMetadata(rs.Columns);
EditSession s = new EditSession(rs, etm);
// ... Add a cell update to it
s.UpdateCell(1, 0, "foo");
// If: I ask for 3 rows from the session, skipping the first, including the updated one
EditRow[] rows = await s.GetRows(1, 3);
// Then:
// ... I should get back 3 rows
Assert.Equal(3, rows.Length);
// ... The first row should reflect that there is an update pending
// (More in depth testing is done in the RowUpdate class tests)
var updatedRow = rows[0];
Assert.Equal(EditRow.EditRowState.DirtyUpdate, updatedRow.State);
Assert.Equal("foo", updatedRow.Cells[0].DisplayValue);
// ... The other rows should be clean
for (int i = 1; i < rows.Length; i++)
{
Assert.Equal(EditRow.EditRowState.Clean, rows[i].State);
}
}
[Fact]
public async Task GetRowsPendingDeletion()
{
// Setup:
// ... Create a session with a proper query and metadata
Query q = QueryExecution.Common.GetBasicExecutedQuery();
ResultSet rs = q.Batches[0].ResultSets[0];
IEditTableMetadata etm = Common.GetStandardMetadata(rs.Columns);
EditSession s = new EditSession(rs, etm);
// ... Add a row deletion
s.DeleteRow(1);
// If: I ask for 3 rows from the session, skipping the first, including the updated one
EditRow[] rows = await s.GetRows(1, 3);
// Then:
// ... I should get back 3 rows
Assert.Equal(3, rows.Length);
// ... The first row should reflect that there is an update pending
// (More in depth testing is done in the RowUpdate class tests)
var updatedRow = rows[0];
Assert.Equal(EditRow.EditRowState.DirtyDelete, updatedRow.State);
Assert.NotEmpty(updatedRow.Cells[0].DisplayValue);
// ... The other rows should be clean
for (int i = 1; i < rows.Length; i++)
{
Assert.Equal(EditRow.EditRowState.Clean, rows[i].State);
}
}
[Fact]
public async Task GetRowsPendingInsertion()
{
// Setup:
// ... Create a session with a proper query and metadata
Query q = QueryExecution.Common.GetBasicExecutedQuery();
ResultSet rs = q.Batches[0].ResultSets[0];
IEditTableMetadata etm = Common.GetStandardMetadata(rs.Columns);
EditSession s = new EditSession(rs, etm);
// ... Add a row creation
s.CreateRow();
// If: I ask for the rows including the new rows
EditRow[] rows = await s.GetRows(0, 6);
// Then:
// ... I should get back 6 rows
Assert.Equal(6, rows.Length);
// ... The last row should reflect that there's a new row
var updatedRow = rows[5];
Assert.Equal(EditRow.EditRowState.DirtyInsert, updatedRow.State);
// ... The other rows should be clean
for (int i = 0; i < rows.Length - 1; i++)
{
Assert.Equal(EditRow.EditRowState.Clean, rows[i].State);
}
}
[Fact]
public async Task GetRowsAllNew()
{
// Setup:
// ... Create a session with a query and metadata
Query q = QueryExecution.Common.GetBasicExecutedQuery();
ResultSet rs = q.Batches[0].ResultSets[0];
IEditTableMetadata etm = Common.GetStandardMetadata(rs.Columns);
EditSession s = new EditSession(rs, etm);
// ... Add a few row creations
s.CreateRow();
s.CreateRow();
s.CreateRow();
// If: I ask for the rows included the new rows
EditRow[] rows = await s.GetRows(5, 5);
// Then:
// ... I should get back 3 rows back
Assert.Equal(3, rows.Length);
// ... All the rows should be new
Assert.All(rows, r => Assert.Equal(EditRow.EditRowState.DirtyInsert, r.State));
}
#endregion
#region Script Edits Tests
[Theory]