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 GetCommandNullConnection() { // Setup: Create a row create RowCreate rc = await GetStandardRowCreate(); // If: I attempt to create a command with a null connection // Then: It should throw an exception Assert.Throws <ArgumentNullException>(() => rc.GetCommand(null)); }
public async Task GetCommandMissingCell() { // Setup: Generate the parameters for the row create RowCreate rc = await GetStandardRowCreate(); var mockConn = new TestSqlConnection(null); // 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.GetCommand(mockConn)); }
public async Task GetCommand(bool includeIdentity) { // Setup: // ... Create a row create with cell updates const long rowId = 100; var columns = Common.GetColumns(includeIdentity); var rs = await Common.GetResultSet(columns, includeIdentity); var etm = Common.GetStandardMetadata(columns); RowCreate rc = new RowCreate(rowId, rs, etm); Common.AddCells(rc, includeIdentity); // ... Mock db connection for building the command var mockConn = new TestSqlConnection(null); // If: I attempt to get a command for the edit DbCommand cmd = rc.GetCommand(mockConn); // Then: // ... The command should not be null Assert.NotNull(cmd); // ... There should be parameters in it Assert.Equal(3, cmd.Parameters.Count); // ... It should be formatted into an insert script with output Regex r = new Regex(@"INSERT INTO (.+)\((.+)\) OUTPUT (.+) VALUES \((.+)\)"); var m = r.Match(cmd.CommandText); Assert.True(m.Success); // ... There should be a table string tbl = m.Groups[1].Value; Assert.Equal(etm.EscapedMultipartName, tbl); // ... There should be 3 columns for input string inCols = m.Groups[2].Value; Assert.Equal(3, inCols.Split(',').Length); // ... There should be 3 OR 4 columns for output that are inserted. 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 3 parameters string[] param = m.Groups[4].Value.Split(','); Assert.Equal(3, param.Length); Assert.All(param, s => Assert.StartsWith("@Value", s.Trim())); }
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)); }