mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-24 01:25:42 -05:00
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:
@@ -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));
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -142,8 +142,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
|
||||
var subsetRequest = new EventFlowValidator<SubsetResult>()
|
||||
.AddResultValidation(r =>
|
||||
{
|
||||
// Then: Messages should be null and subset should not be null
|
||||
Assert.Null(r.Message);
|
||||
// Then: Subset should not be null
|
||||
Assert.NotNull(r.ResultSubset);
|
||||
}).Complete();
|
||||
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
|
||||
@@ -159,12 +158,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
|
||||
var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
|
||||
var subsetParams = new SubsetParams { OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
|
||||
var subsetRequest = new EventFlowValidator<SubsetResult>()
|
||||
.AddResultValidation(r =>
|
||||
{
|
||||
// Then: Messages should not be null and the subset should be null
|
||||
Assert.NotNull(r.Message);
|
||||
Assert.Null(r.ResultSubset);
|
||||
}).Complete();
|
||||
.AddErrorValidation<string>(Assert.NotEmpty)
|
||||
.Complete();
|
||||
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
|
||||
subsetRequest.Validate();
|
||||
}
|
||||
@@ -185,12 +180,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
|
||||
// ... And I then ask for a valid set of results from it
|
||||
var subsetParams = new SubsetParams { OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
|
||||
var subsetRequest = new EventFlowValidator<SubsetResult>()
|
||||
.AddResultValidation(r =>
|
||||
{
|
||||
// Then: There should not be a subset and message should not be null
|
||||
Assert.NotNull(r.Message);
|
||||
Assert.Null(r.ResultSubset);
|
||||
}).Complete();
|
||||
.AddErrorValidation<string>(Assert.NotEmpty)
|
||||
.Complete();
|
||||
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
|
||||
subsetRequest.Validate();
|
||||
}
|
||||
@@ -210,12 +201,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution
|
||||
// ... And I then ask for a set of results from it
|
||||
var subsetParams = new SubsetParams { OwnerUri = Constants.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
|
||||
var subsetRequest = new EventFlowValidator<SubsetResult>()
|
||||
.AddResultValidation(r =>
|
||||
{
|
||||
// Then: There should be an error message and no subset
|
||||
Assert.NotNull(r.Message);
|
||||
Assert.Null(r.ResultSubset);
|
||||
}).Complete();
|
||||
.AddErrorValidation<string>(Assert.NotEmpty)
|
||||
.Complete();
|
||||
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
|
||||
subsetRequest.Validate();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
@@ -13,27 +15,368 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
|
||||
/// </summary>
|
||||
public class LongListTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Add and remove and item in a LongList
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void LongListTest()
|
||||
public void LongListConstruction()
|
||||
{
|
||||
var longList = new LongList<char>();
|
||||
longList.Add('.');
|
||||
Assert.True(longList.Count == 1);
|
||||
longList.RemoveAt(0);
|
||||
Assert.True(longList.Count == 0);
|
||||
// If: I construct a new long list
|
||||
LongList<char> ll = new LongList<char>();
|
||||
|
||||
// Then:
|
||||
// ... There should be no values in the list
|
||||
Assert.Equal(0, ll.Count);
|
||||
}
|
||||
|
||||
#region GetItem / Add Tests
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1L)] // Negative index
|
||||
[InlineData(0L)] // Index equal to count of elements
|
||||
[InlineData(100L)] // Index larger than elements
|
||||
public void GetItemOutOfRange(long index)
|
||||
{
|
||||
// If: I construct a new long list
|
||||
LongList<char> ll = new LongList<char>();
|
||||
|
||||
// Then:
|
||||
// ... There should be no values in the list
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => ll[index]);
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => ll.GetItem(index));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)] // Element at beginning
|
||||
[InlineData(1)] // Element in middle
|
||||
[InlineData(2)] // Element at end
|
||||
public void GetItemNotExpanded(long index)
|
||||
{
|
||||
// If: I construct a new long list with a couple items in it
|
||||
LongList<int> ll = new LongList<int> {0, 1, 2};
|
||||
|
||||
// Then: I can read back the value from the list
|
||||
Assert.Equal(3, ll.Count);
|
||||
Assert.Equal(index, ll[index]);
|
||||
Assert.Equal(index, ll.GetItem(index));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetItemExanded()
|
||||
{
|
||||
// If: I construct a new long list that is guaranteed to have been expanded
|
||||
LongList<int> ll = new LongList<int> {ExpandListSize = 2};
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// Then:
|
||||
// ... All the added values should be accessible
|
||||
Assert.Equal(10, ll.Count);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Assert.Equal(i, ll[i]);
|
||||
Assert.Equal(i, ll.GetItem(i));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetItem Tests
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1L)] // Negative index
|
||||
[InlineData(0L)] // Index equal to count of elements
|
||||
[InlineData(100L)] // Index larger than elements
|
||||
public void SetItemOutOfRange(long index)
|
||||
{
|
||||
// If: I construct a new long list
|
||||
LongList<int> ll = new LongList<int>();
|
||||
|
||||
// Then:
|
||||
// ... There should be no values in the list
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => ll[index] = 8);
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => ll.SetItem(index, 8));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetItemNotExpanded()
|
||||
{
|
||||
// If:
|
||||
// ... I construct a new long list with a few items in it
|
||||
// ... And I set all values to new values
|
||||
LongList<int> ll = new LongList<int> {0, 1, 2};
|
||||
for (int i = 0; i < ll.Count; i++)
|
||||
{
|
||||
ll.SetItem(i, 8);
|
||||
}
|
||||
|
||||
// Then: All values in the list should be 8
|
||||
Assert.All(ll, i => Assert.Equal(8, i));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetItemIndexerNotExpanded()
|
||||
{
|
||||
// If:
|
||||
// ... I construct a new long list with a few items in it
|
||||
// ... And I set all values to new values
|
||||
LongList<int> ll = new LongList<int> {0, 1, 2};
|
||||
for (int i = 0; i < ll.Count; i++)
|
||||
{
|
||||
ll[i] = 8;
|
||||
}
|
||||
|
||||
// Then: All values in the list should be 8
|
||||
Assert.All(ll, i => Assert.Equal(8, i));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetItemExpanded()
|
||||
{
|
||||
// If:
|
||||
// ... I construct a new long list that is guaranteed to have been expanded
|
||||
LongList<int> ll = new LongList<int> {ExpandListSize = 2};
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// ... And reset all the values to 8
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ll.SetItem(i, 8);
|
||||
}
|
||||
|
||||
// Then: All values in the list should be 8
|
||||
Assert.All(ll, i => Assert.Equal(8, i));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetItemIndexerExpanded()
|
||||
{
|
||||
// If:
|
||||
// ... I construct a new long list that is guaranteed to have been expanded
|
||||
LongList<int> ll = new LongList<int> {ExpandListSize = 2};
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// ... And reset all the values to 8
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ll[i] = 8;
|
||||
}
|
||||
|
||||
// Then: All values in the list should be 8
|
||||
Assert.All(ll, i => Assert.Equal(8, i));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RemoveAt Tests
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1L)] // Negative index
|
||||
[InlineData(0L)] // Index equal to count of elements
|
||||
[InlineData(100L)] // Index larger than elements
|
||||
public void RemoveOutOfRange(long index)
|
||||
{
|
||||
// If: I construct a new long list
|
||||
LongList<char> ll = new LongList<char>();
|
||||
|
||||
// Then:
|
||||
// ... There should be no values in the list
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => ll.RemoveAt(index));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)] // Remove at beginning of list
|
||||
[InlineData(2)] // Remove from middle of list
|
||||
[InlineData(4)] // Remove at end of list
|
||||
public void RemoveAtNotExpanded(long index)
|
||||
{
|
||||
// If:
|
||||
// ... I create a long list with a few elements in it (and one element that will be removed)
|
||||
LongList<int> ll = new LongList<int>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
ll.Add(i == index ? 1 : 8);
|
||||
}
|
||||
|
||||
// ... And I delete an element
|
||||
ll.RemoveAt(index);
|
||||
|
||||
// Then:
|
||||
// ... The count should have subtracted
|
||||
Assert.Equal(4, ll.Count);
|
||||
|
||||
// ... All values should be 8 since we removed the 1
|
||||
Assert.All(ll, i => Assert.Equal(8, i));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAtExpanded()
|
||||
{
|
||||
// If:
|
||||
// ... I create a long list that is guaranteed to be expanded
|
||||
// (Created with 2x the values, evaluate the )
|
||||
LongList<int> ll = new LongList<int> {ExpandListSize = 2};
|
||||
for (int j = 0; j < 2; j++)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
}
|
||||
|
||||
// ... And I delete all of the first half of values
|
||||
// (we're doing this backwards to make sure remove works at different points in the list)
|
||||
for (int i = 9; i >= 0; i--)
|
||||
{
|
||||
ll.RemoveAt(i);
|
||||
}
|
||||
|
||||
// Then:
|
||||
// ... The second half of the values should still remain
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Assert.Equal(i, ll[i]);
|
||||
}
|
||||
|
||||
// If:
|
||||
// ... I then proceed to add elements onto the end again
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// Then: All the elements should be there, in order
|
||||
for (int j = 0; j < 2; j++)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
int index = j * 10 + i;
|
||||
Assert.Equal(i, ll[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable Tests
|
||||
|
||||
[Fact]
|
||||
public void GetEnumerator()
|
||||
{
|
||||
// Setup: Create a long list with a handful of elements
|
||||
LongList<int> ll = new LongList<int>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// If: I get iterate over the list via GetEnumerator
|
||||
// Then: All the elements should be returned, in order
|
||||
int val = 0;
|
||||
foreach (int element in ll)
|
||||
{
|
||||
Assert.Equal(val++, element);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetEnumeratorExpanded()
|
||||
{
|
||||
// Setup: Create a long list with a handful of elements
|
||||
LongList<int> ll = new LongList<int> {ExpandListSize = 2};
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// If: I get iterate over the list via GetEnumerator
|
||||
// Then: All the elements should be returned, in order
|
||||
int val = 0;
|
||||
foreach (int element in ll)
|
||||
{
|
||||
Assert.Equal(val++, element);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1)] // Negative
|
||||
[InlineData(5)] // Equal to count
|
||||
[InlineData(100)] // Far too large
|
||||
public void LongSkipOutOfRange(long index)
|
||||
{
|
||||
// Setup: Create a long list with a handful of elements
|
||||
LongList<int> ll = new LongList<int> {ExpandListSize = 2};
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// If: I attempt to skip ahead by a value that is out of range
|
||||
// Then: I should get an exception
|
||||
// NOTE: We must do the .ToList in order to evaluate the LongSkip since it is implemented
|
||||
// with a yield return
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => ll.LongSkip(index).ToArray());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)] // Don't actually skip anything
|
||||
[InlineData(2)] // Skip within the short list
|
||||
public void LongSkip(long index)
|
||||
{
|
||||
// Setup: Create a long list with a handful of elements
|
||||
LongList<int> ll = new LongList<int>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// If: I skip ahead by a few elements and get all elements in an array
|
||||
int[] values = ll.LongSkip(index).ToArray();
|
||||
|
||||
// Then: The elements including the skip start index should be in the output
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
Assert.Equal(ll[i+index], values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)] // Don't actually skip anything
|
||||
[InlineData(1)] // Skip within the short list
|
||||
[InlineData(3)] // Skip across expanded lists
|
||||
public void LongSkipExpanded(long index)
|
||||
{
|
||||
// Setup: Create a long list with a handful of elements
|
||||
LongList<int> ll = new LongList<int> {ExpandListSize = 2};
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
ll.Add(i);
|
||||
}
|
||||
|
||||
// If: I skip ahead by a few elements and get all elements in an array
|
||||
int[] values = ll.LongSkip(index).ToArray();
|
||||
|
||||
// Then: The elements including the skip start index should be in the output
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
Assert.Equal(ll[i+index], values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Add and remove and item in a LongList causing an expansion
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void LongListExpandTest()
|
||||
{
|
||||
var longList = new LongList<int>();
|
||||
longList.ExpandListSize = 3;
|
||||
var longList = new LongList<int> {ExpandListSize = 3};
|
||||
for (int i = 0; i < 6; ++i)
|
||||
{
|
||||
longList.Add(i);
|
||||
|
||||
Reference in New Issue
Block a user