示例#1
0
        public void ConstructQueryWithLimit(int limit)
        {
            // Setup: Create a metadata provider for some basic columns
            var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);

            // If: I generate a query for selecting rows without a limit
            EditInitializeFiltering eif = new EditInitializeFiltering
            {
                LimitResults = limit
            };
            string query = EditSession.ConstructInitializeQuery(data.TableMetadata, eif);

            // Then:
            // ... The query should look like a select statement
            Regex selectRegex = new Regex(@"SELECT TOP (\d+) (.+) FROM (.+)", RegexOptions.IgnoreCase);
            var   match       = selectRegex.Match(query);

            Assert.True(match.Success);

            // ... There should be columns in it
            Assert.Equal(data.DbColumns.Length, match.Groups[2].Value.Split(',').Length);

            // ... The table name should be in it
            Assert.Equal(data.TableMetadata.EscapedMultipartName, match.Groups[3].Value);

            // ... The top count should be equal to what we provided
            int limitFromQuery;

            Assert.True(int.TryParse(match.Groups[1].Value, out limitFromQuery));
            Assert.Equal(limit, limitFromQuery);
        }
        public async Task SetCellImplicitRowRevertTests()
        {
            // Setup: Create a fake column to update
            var       data = new Common.TestDbColumnsWithTableMetadata(false, true, 0, 0);
            ResultSet rs   = await Common.GetResultSet(data.DbColumns, true);

            // If:
            // ... I add updates to one cell in the row
            RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);

            ru.SetCell(1, "qqq");

            // ... Then I update the cell to its original value
            var eucr = ru.SetCell(1, (string)rs.GetRow(0)[1].RawObject);

            // Then:
            // ... An edit cell should have been returned
            Assert.NotNull(eucr);
            Assert.NotNull(eucr.Cell);

            // ... The old value should be returned
            Assert.Equal(rs.GetRow(0)[1].DisplayValue, eucr.Cell.DisplayValue);
            Assert.False(eucr.Cell.IsNull);

            // ... The cell should be clean
            Assert.False(eucr.Cell.IsDirty);

            // ... The row should be clean
            Assert.False(eucr.IsRowDirty);

            // TODO: Make sure that the script and command things will return null
        }
        public async Task GetEditRow()
        {
            // Setup: Create a row delete
            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
            var rs = await Common.GetResultSet(data.DbColumns, false);

            RowDelete rd = new RowDelete(0, rs, data.TableMetadata);

            // 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 and should be dirty
            Assert.Equal(cells.Length, er.Cells.Length);
            for (int i = 0; i < cells.Length; i++)
            {
                DbCellValue originalCell = cells[i];
                EditCell    outputCell   = er.Cells[i];

                Assert.Equal(originalCell.DisplayValue, outputCell.DisplayValue);
                Assert.Equal(originalCell.IsNull, outputCell.IsNull);
                Assert.True(outputCell.IsDirty);
                // Note: No real need to check the RawObject property
            }
        }
        public async Task RevertCellRevertsRow()
        {
            // Setup:
            // ... 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);

            ru.SetCell(0, "qqq");

            // If: I attempt to revert a cell that was set
            EditRevertCellResult result = ru.RevertCell(0);

            // Then:
            // ... We should get a result back
            Assert.NotNull(result);

            // ... We should get the original value back
            // @TODO: Check for a default value when we support it
            Assert.NotNull(result.Cell);
            Assert.Equal(rs.GetRow(0)[0].DisplayValue, result.Cell.DisplayValue);

            // ... The row should now be reverted
            Assert.False(result.IsRowDirty);

            // ... The cell should no longer be set
            Assert.DoesNotContain(0, ru.cellUpdates.Keys);
        }
        public async Task GetEditRowWithDefaultValue()
        {
            // Setup: Generate a row create with default values
            const long rowId = 100;

            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, 3, 0);
            ResultSet rs = await Common.GetResultSet(data.DbColumns, false);

            RowCreate rc = new RowCreate(rowId, rs, data.TableMetadata);

            // 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 sould have a bunch of default values (equal to number of columns) and all are dirty
            Assert.Equal(rc.newCells.Length, er.Cells.Length);
            Assert.All(er.Cells, ec =>
            {
                Assert.Equal(Common.DefaultValue, ec.DisplayValue);
                Assert.False(ec.IsNull);    // TODO: Update when we support null default values better
                Assert.True(ec.IsDirty);
            });
        }
        public async Task GetCommand(bool includeIdentity, int defaultCols, int nullableCols, int valuesToSkip, RegexExpectedOutput expectedOutput)
        {
            // Setup:
            // ... Generate the parameters for the row create
            Common.TestDbColumnsWithTableMetadata 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);

            Common.AddCells(rc, valuesToSkip);

            // If: I ask for the command for the row insert
            DbCommand cmd = rc.GetCommand(mockConn);

            // Then:
            // ... The command should not be null
            Assert.NotNull(cmd);

            // ... There should be parameters in it
            Assert.Equal(expectedOutput.ExpectedInValues, cmd.Parameters.Count);

            // ... The script should match the expected regex output
            ValidateCommandAgainstRegex(cmd.CommandText, expectedOutput);
        }
        public async Task SetCellNull()
        {
            // Setup: Generate a row create
            var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 3);
            var rs   = await Common.GetResultSet(data.DbColumns, false);

            var rc = new RowCreate(100, rs, data.TableMetadata);

            // If: I set a cell in the newly created row to null
            const string         nullValue = "NULL";
            EditUpdateCellResult eucr      = rc.SetCell(0, nullValue);

            // Then:
            // ... The returned value should be equal to what we provided
            Assert.NotNull(eucr);
            Assert.NotNull(eucr.Cell);
            Assert.Equal(nullValue, eucr.Cell.DisplayValue);
            Assert.True(eucr.Cell.IsNull);

            // ... The returned value should be dirty
            Assert.NotNull(eucr.Cell.IsDirty);

            // ... The row should still be dirty
            Assert.True(eucr.IsRowDirty);

            // ... There should be a cell update in the cell list
            Assert.NotNull(rc.newCells[0]);
        }
        public async Task GetScriptTest(bool isMemoryOptimized)
        {
            // Setup: Create a fake table to update
            var       data = new Common.TestDbColumnsWithTableMetadata(isMemoryOptimized, true, 0, 0);
            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);

            Common.AddCells(ru, 1);
            string script = ru.GetScript();

            // Then:
            // ... The script should not be null
            Assert.NotNull(script);

            // ... It should be formatted as an update script
            string regexString = isMemoryOptimized
                ? @"UPDATE (.+) WITH \(SNAPSHOT\) SET (.*) WHERE .+"
                : @"UPDATE (.+) SET (.*) WHERE .+";
            Regex r = new Regex(regexString);
            var   m = r.Match(script);

            Assert.True(m.Success);

            // ... It should have 3 updates
            string tbl     = m.Groups[1].Value;
            string updates = m.Groups[2].Value;

            string[] updateSplit = updates.Split(',');
            Assert.Equal(data.TableMetadata.EscapedMultipartName, tbl);
            Assert.Equal(3, updateSplit.Length);
            Assert.All(updateSplit, s => Assert.Equal(2, s.Split('=').Length));
        }
        public async Task RevertCellThatWasSet(int defaultCols)
        {
            // Setup:
            // ... Generate the parameters for the row create
            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, defaultCols, 0);
            ResultSet rs = await Common.GetResultSet(data.DbColumns, false);

            RowCreate rc = new RowCreate(100, rs, data.TableMetadata);

            rc.SetCell(0, "1");

            // If: I attempt to revert a cell that was set
            EditRevertCellResult result = rc.RevertCell(0);

            // Then:
            // ... We should get a result back
            Assert.NotNull(result);

            // ... We should get back an edit cell with a value based on the default value
            string expectedDisplayValue = defaultCols > 0 ? Common.DefaultValue : string.Empty;

            Assert.NotNull(result.Cell);
            Assert.Equal(expectedDisplayValue, result.Cell.DisplayValue);
            Assert.False(result.Cell.IsNull);    // TODO: Modify to support null defaults

            // ... The row should be dirty
            Assert.True(result.IsRowDirty);

            // ... The cell should no longer be set
            Assert.Null(rc.newCells[0]);
        }
示例#10
0
        public void ConstructQueryWithoutLimit()
        {
            // Setup: Create a metadata provider for some basic columns
            var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);

            // If: I generate a query for selecting rows without a limit
            EditInitializeFiltering eif = new EditInitializeFiltering
            {
                LimitResults = null
            };
            string query = EditSession.ConstructInitializeQuery(data.TableMetadata, eif);

            // Then:
            // ... The query should look like a select statement
            Regex selectRegex = new Regex("SELECT (.+) FROM (.+)", RegexOptions.IgnoreCase);
            var   match       = selectRegex.Match(query);

            Assert.True(match.Success);

            // ... There should be columns in it
            Assert.Equal(data.DbColumns.Length, match.Groups[1].Value.Split(',').Length);

            // ... The table name should be in it
            Assert.Equal(data.TableMetadata.EscapedMultipartName, match.Groups[2].Value);

            // ... It should NOT have a TOP clause in it
            Assert.DoesNotContain("TOP", query);
        }
        private static async Task <RowCreate> GetStandardRowCreate()
        {
            var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
            var rs   = await Common.GetResultSet(data.DbColumns, false);

            return(new RowCreate(100, rs, data.TableMetadata));
        }
示例#12
0
        public async Task GetCommand(bool includeIdentity, bool isMemoryOptimized)
        {
            // Setup:
            // ... 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);

            Common.AddCells(ru, includeIdentity ? 1 : 0);

            // ... Mock db connection for building the command
            var mockConn = new TestSqlConnection(null);

            // If: I ask for a command to be generated for update
            DbCommand cmd = ru.GetCommand(mockConn);

            // Then:
            // ... The command should not be null
            Assert.NotNull(cmd);

            // ... There should be an appropriate number of parameters in it
            //     (1 or 3 keys, 3 value parameters)
            int expectedKeys = includeIdentity ? 1 : 3;

            Assert.Equal(expectedKeys + 3, cmd.Parameters.Count);

            // ... It should be formatted into an update script with output
            string regexFormat = isMemoryOptimized
                ? @"UPDATE (.+) WITH \(SNAPSHOT\) SET (.+) OUTPUT (.+) WHERE (.+)"
                : @"UPDATE (.+) SET (.+) OUTPUT(.+) WHERE (.+)";
            Regex r = new Regex(regexFormat);
            var   m = r.Match(cmd.CommandText);

            Assert.True(m.Success);

            // ... There should be a table
            string tbl = m.Groups[1].Value;

            Assert.Equal(data.TableMetadata.EscapedMultipartName, tbl);

            // ... There should be 3 parameters for input
            string[] inCols = m.Groups[2].Value.Split(',');
            Assert.Equal(3, inCols.Length);
            Assert.All(inCols, s => Assert.Matches(@"\[.+\] = @Value\d+", s));

            // ... There should be 3 OR 4 columns for output
            string[] outCols = m.Groups[3].Value.Split(',');
            Assert.Equal(includeIdentity ? 4 : 3, outCols.Length);
            Assert.All(outCols, s => Assert.StartsWith("inserted.", s.Trim()));

            // ... There should be 1 OR 3 columns for where components
            string[] whereComponents = m.Groups[4].Value.Split(new[] { "AND" }, StringSplitOptions.None);
            Assert.Equal(expectedKeys, whereComponents.Length);
            Assert.All(whereComponents, s => Assert.Matches(@"\(.+ = @Param\d+\)", s));
        }
        public async Task GetScriptMissingCell(bool includeIdentity, int defaultCols, int nullableCols, int valuesToSkipSetting)
        {
            // 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);

            // If: I ask for a script to be generated without setting any values
            // Then: An exception should be thrown for missing cells
            Assert.Throws <InvalidOperationException>(() => rc.GetScript());
        }
        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));
        }
        public async Task ApplyChangesNullReader()
        {
            // Setup:
            // ... 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);

            // If: I  ask for the changes to be applied with a null db reader
            // Then: I should get an exception
            await Assert.ThrowsAsync <ArgumentNullException>(() => ru.ApplyChanges(null));
        }
        [InlineData(100)]       // Way too large value
        public async Task RevertCellOutOfRange(int columnId)
        {
            // Setup:
            // ... 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);

            // If: I attempt to revert a cell that is out of range
            // Then: I should get an exception
            Assert.Throws <ArgumentOutOfRangeException>(() => ru.RevertCell(columnId));
        }
        public async Task ApplyChanges()
        {
            // Setup: Generate the parameters for the row delete object
            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
            var rs = await Common.GetResultSet(data.DbColumns, false);

            // If: I ask for the change to be applied
            RowDelete rd = new RowDelete(0, rs, data.TableMetadata);
            await rd.ApplyChanges(null);      // Reader not used, can be null

            // Then : The result set should have one less row in it
            Assert.Equal(0, rs.RowCount);
        }
        public async Task RowDeleteConstruction()
        {
            // Setup: Create the values to store
            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, true, 0, 0);
            ResultSet rs = await Common.GetResultSet(data.DbColumns, true);

            // If: I create a RowCreate instance
            RowDelete rc = new RowDelete(100, rs, data.TableMetadata);

            // Then: The values I provided should be available
            Assert.Equal(100, rc.RowId);
            Assert.Equal(rs, rc.AssociatedResultSet);
            Assert.Equal(data.TableMetadata, rc.AssociatedObjectMetadata);
        }
示例#19
0
        public void ConstructQueryNegativeLimit()
        {
            // Setup: Create a metadata provider for some basic columns
            var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);

            // If: I generate a query for selecting rows with a negative limit
            // Then: An exception should be thrown
            EditInitializeFiltering eif = new EditInitializeFiltering
            {
                LimitResults = -1
            };

            Assert.Throws <ArgumentOutOfRangeException>(() => EditSession.ConstructInitializeQuery(data.TableMetadata, eif));
        }
        public async Task GetCommandMissingCellNoDefault(bool includeIdentity, int defaultCols, int nullableCols,
                                                         int valuesToSkip)
        {
            // Setup:
            // ... Generate the row create object
            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, includeIdentity, defaultCols, nullableCols);
            ResultSet rs = await Common.GetResultSet(data.DbColumns, includeIdentity);

            RowCreate rc = new RowCreate(100, rs, data.TableMetadata);

            // ... Create a mock db connection for building the command
            var mockConn = new TestSqlConnection();

            // If: I ask for a script to be generated without setting all the required values
            // Then: An exception should be thrown for the missing cells
            Assert.Throws <InvalidOperationException>(() => rc.GetCommand(mockConn));
        }
        public async Task GetCommand(bool includeIdentity, bool isMemoryOptimized)
        {
            // Setup:
            // ... Create a row delete
            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(isMemoryOptimized, includeIdentity, 0, 0);
            var rs = await Common.GetResultSet(data.DbColumns, includeIdentity);

            RowDelete rd = new RowDelete(0, rs, data.TableMetadata);

            // ... Mock db connection for building the command
            var mockConn = new TestSqlConnection(null);

            // If: I attempt to get a command for the edit
            DbCommand cmd = rd.GetCommand(mockConn);

            // Then:
            // ... The command should not be null
            Assert.NotNull(cmd);

            // ... Only the keys should be used for parameters
            int expectedKeys = includeIdentity ? 1 : 3;

            Assert.Equal(expectedKeys, cmd.Parameters.Count);

            // ... It should be formatted into an delete script
            string regexTest = isMemoryOptimized
                ? @"DELETE FROM (.+) WITH\(SNAPSHOT\) WHERE (.+)"
                : @"DELETE FROM (.+) WHERE (.+)";
            Regex r = new Regex(regexTest);
            var   m = r.Match(cmd.CommandText);

            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);

            // ... Each component should have be equal to a parameter
            Assert.All(whereComponents, c => Assert.True(Regex.IsMatch(c.Trim(), @"\(.+ = @.+\)")));
        }
        public async Task ApplyChanges(bool includeIdentity)
        {
            // Setup:
            // ... Generate the parameters for the row create
            const long rowId = 100;

            Common.TestDbColumnsWithTableMetadata 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);
            await rc.ApplyChanges(newRowReader);

            // Then: The result set should have an additional row in it
            Assert.Equal(2, rs.RowCount);
        }
示例#23
0
        public async Task SortingByTypeTest()
        {
            // Setup: Create a result set and metadata we can reuse
            var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
            var rs   = await Common.GetResultSet(data.DbColumns, false);

            // If: I request to sort a list of the three different edit operations
            List <RowEditBase> rowEdits = new List <RowEditBase>
            {
                new RowDelete(0, rs, data.TableMetadata),
                new RowUpdate(0, rs, data.TableMetadata),
                new RowCreate(0, rs, data.TableMetadata)
            };

            rowEdits.Sort();

            // Then: Delete should be the last operation to execute
            //       (we don't care about the order of the other two)
            Assert.IsType <RowDelete>(rowEdits.Last());
        }
        public async Task GetEditRow()
        {
            // 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);

            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 and be dirty
            EditCell newCell = er.Cells[0];

            Assert.Equal("foo", newCell.DisplayValue);
            Assert.False(newCell.IsNull);
            Assert.True(newCell.IsDirty);
        }
        public async Task SetCellImplicitRevertTest()
        {
            // Setup: Create a fake table to update
            var       data = new Common.TestDbColumnsWithTableMetadata(false, true, 0, 0);
            ResultSet rs   = await Common.GetResultSet(data.DbColumns, true);

            // If:
            // ... I add updates to all the cells in the row
            RowUpdate ru = new RowUpdate(0, rs, data.TableMetadata);

            Common.AddCells(ru, 1);

            // ... Then I update a cell back to it's old value
            var eucr = ru.SetCell(1, (string)rs.GetRow(0)[1].RawObject);

            // Then:
            // ... A edit cell was returned
            Assert.NotNull(eucr);
            Assert.NotNull(eucr.Cell);

            // ... The new value we provided should be returned
            Assert.Equal(rs.GetRow(0)[1].DisplayValue, eucr.Cell.DisplayValue);
            Assert.False(eucr.Cell.IsNull);

            // ... The cell should be clean
            Assert.False(eucr.Cell.IsDirty);

            // ... The row is still dirty
            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());

            // ... It should have 2 updates
            string updates = m.Groups[1].Value;

            string[] updateSplit = updates.Split(',');
            Assert.Equal(2, updateSplit.Length);
            Assert.All(updateSplit, s => Assert.Equal(2, s.Split('=').Length));
        }
示例#26
0
        public async Task SortingDeletesByRowIdTest()
        {
            // Setup: Create a result set and metadata we can reuse
            var data = new Common.TestDbColumnsWithTableMetadata(false, false, 0, 0);
            var rs   = await Common.GetResultSet(data.DbColumns, false);

            // If: I sort 3 delete operations of the same type
            List <RowEditBase> rowEdits = new List <RowEditBase>
            {
                new RowDelete(1, rs, data.TableMetadata),
                new RowDelete(3, rs, data.TableMetadata),
                new RowDelete(2, rs, data.TableMetadata)
            };

            rowEdits.Sort();

            // Then: They should be in order by row ID DESCENDING
            Assert.Equal(3, rowEdits[0].RowId);
            Assert.Equal(2, rowEdits[1].RowId);
            Assert.Equal(1, rowEdits[2].RowId);
        }
        public async Task ApplyChanges(bool includeIdentity)
        {
            // Setup:
            // ... 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);
            long      oldBytesWritten = rs.totalBytesWritten;

            // ... Setup a db reader for the result of an update
            var newRowReader = Common.GetNewRowDataReader(data.DbColumns, includeIdentity);

            // If: I ask for the change to be applied
            await ru.ApplyChanges(newRowReader);

            // Then:
            // ... The result set should have the same number of rows as before
            Assert.Equal(1, rs.RowCount);
            Assert.True(oldBytesWritten < rs.totalBytesWritten);
        }
        public async Task GetScriptTest(bool isMemoryOptimized)
        {
            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(isMemoryOptimized, true, 0, 0);
            ResultSet rs = await Common.GetResultSet(data.DbColumns, true);

            // If: I ask for a script to be generated for delete
            RowDelete rd     = new RowDelete(0, rs, data.TableMetadata);
            string    script = rd.GetScript();

            // Then:
            // ... The script should not be null
            Assert.NotNull(script);

            // ... It should be formatted as a delete script
            string scriptStart = $"DELETE FROM {data.TableMetadata.EscapedMultipartName}";

            if (isMemoryOptimized)
            {
                scriptStart += " WITH(SNAPSHOT)";
            }
            Assert.StartsWith(scriptStart, script);
        }
        public async Task GetScript(bool includeIdentity, int colsWithDefaultConstraints, int colsThatAllowNull, int valuesToSkipSetting, RegexExpectedOutput expectedOutput)
        {
            // Setup:
            // ... Generate the parameters for the row create
            Common.TestDbColumnsWithTableMetadata 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);

            Common.AddCells(rc, valuesToSkipSetting);

            // If: I ask for the script for the row insert
            string script = rc.GetScript();

            // Then:
            // ... The script should not be null
            Assert.NotNull(script);

            // ... The script should match the expected regex output
            ValidateScriptAgainstRegex(script, expectedOutput);
        }
        public async Task GetEditRowWithCalculatedValue()
        {
            // Setup: Generate a row create with an identity column
            const long rowId = 100;

            Common.TestDbColumnsWithTableMetadata data = new Common.TestDbColumnsWithTableMetadata(false, true, 0, 0);
            ResultSet rs = await Common.GetResultSet(data.DbColumns, true);

            RowCreate rc = new RowCreate(rowId, rs, data.TableMetadata);

            // If: I request an edit row from the row created
            EditRow er = rc.GetEditRow(null);

            // Then:
            // ... The row should not be null
            Assert.NotNull(er);
            Assert.Equal(er.Id, rowId);

            // ... The row should not be clean
            Assert.True(er.IsDirty);
            Assert.Equal(EditRow.EditRowState.DirtyInsert, er.State);

            // ... The row should have a TBD for the identity column
            Assert.Equal(rc.newCells.Length, er.Cells.Length);
            Assert.Equal(SR.EditDataComputedColumnPlaceholder, er.Cells[0].DisplayValue);
            Assert.False(er.Cells[0].IsNull);
            Assert.True(er.Cells[0].IsDirty);

            // ... The rest of the cells should have empty display values
            Assert.All(er.Cells.Skip(1), ec =>
            {
                Assert.Equal(string.Empty, ec.DisplayValue);
                Assert.False(ec.IsNull);
                Assert.True(ec.IsDirty);
            });
        }