public void FakeDbCommandBuilder_should_work() { List <TestTable> randomDataSource = RandomDataGenerator.CreateRandomTables(seed: 1234, tableCount: 5, /*allowZeroRowsInTablesByIdx: */ 1, 3); using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { connection.Open(); using (FakeDbDataAdapter adpt = new FakeDbDataAdapter(selectCommand)) using (FakeDbCommandBuilder cmdBuilder = adpt.CreateCommandBuilder()) { cmdBuilder.DataAdapter.ShouldBe(adpt); FakeDbCommand deleteCommand1 = cmdBuilder.GetDeleteCommand(); // Same as ` useColumnsForParameterNames: false );` FakeDbCommand updateCommand1 = cmdBuilder.GetUpdateCommand(); FakeDbCommand insertCommand1 = cmdBuilder.GetInsertCommand(); FakeDbCommand deleteCommand2 = cmdBuilder.GetDeleteCommand(useColumnsForParameterNames: true); FakeDbCommand updateCommand2 = cmdBuilder.GetUpdateCommand(useColumnsForParameterNames: true); FakeDbCommand insertCommand2 = cmdBuilder.GetInsertCommand(useColumnsForParameterNames: true); _ = deleteCommand1.ShouldNotBeNull(); _ = updateCommand1.ShouldNotBeNull(); _ = insertCommand1.ShouldNotBeNull(); _ = deleteCommand2.ShouldNotBeNull(); _ = updateCommand2.ShouldNotBeNull(); _ = insertCommand2.ShouldNotBeNull(); } } }
private static void FiddleWithPropertiesAsIDbDataAdapter(IDbDataAdapter adapter) { { FakeDbCommand cmd = (FakeDbCommand)adapter.SelectCommand; adapter.SelectCommand = null; adapter.SelectCommand = cmd; } { FakeDbCommand cmd = (FakeDbCommand)adapter.InsertCommand; adapter.InsertCommand = null; adapter.InsertCommand = cmd; } { FakeDbCommand cmd = (FakeDbCommand)adapter.DeleteCommand; adapter.DeleteCommand = null; adapter.DeleteCommand = cmd; } { FakeDbCommand cmd = (FakeDbCommand)adapter.UpdateCommand; adapter.UpdateCommand = null; adapter.UpdateCommand = cmd; } }
public void FakeDbDataAdapter_properties_should_not_have_infinite_loops_and_stack_overflows() { List <TestTable> randomDataSource = RandomDataGenerator.CreateRandomTables(seed: 1, tableCount: 2, /*allowZeroRowsInTablesByIdx: */ 1, 3); // Test that .Dispose() works (DbDataAdapter clears mutable properties in its disposal method) using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) using (FakeDbDataAdapter adapter = new FakeDbDataAdapter(selectCommand)) { } using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand1 = connection.CreateCommand(testTables: randomDataSource)) using (FakeDbCommand selectCommand2 = connection.CreateCommand(testTables: randomDataSource)) using (FakeDbDataAdapter adapter = new FakeDbDataAdapter(selectCommand1)) { using (FakeProxiedDbDataAdapter prox = new FakeProxiedDbDataAdapter(selectCommand1)) { FiddleWithPropertiesAsFakeProxiedDbDataAdapter(prox); FiddleWithPropertiesAsFakeDbDataAdapter(prox); FiddleWithPropertiesAsDbDataAdapter(prox); FiddleWithPropertiesAsIDbDataAdapter(prox); } FiddleWithPropertiesAsFakeDbDataAdapter(adapter); FiddleWithPropertiesAsDbDataAdapter(adapter); FiddleWithPropertiesAsIDbDataAdapter(adapter); } }
private FakeDbCommand CreateDbCommandForHappyPath(string expectedResponse) { expectedResponse ??= new Fixture().Create <string>(); var dbCommand = new FakeDbCommand(); dbCommand.ConfigureExecuteScalarAsync = expectedResponse; return(dbCommand); }
protected TResult DoRunProxiedDbDataAdapter(Int32 seed, Int32 tableCount) { List <TestTable> randomDataSource = RandomDataGenerator.CreateRandomTables(seed: seed, tableCount: tableCount); using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { connection.Open(); using (FakeProxiedDbDataAdapter adapter = new FakeProxiedDbDataAdapter(selectCommand)) { return(this.RunProxiedDbDataAdapter(randomDataSource, adapter)); } } }
protected async Task <TResult> DoRunBatchingProxiedDbDataAdapterAsync(Int32 seed, Int32 tableCount) { List <TestTable> randomDataSource = RandomDataGenerator.CreateRandomTables(seed: seed, tableCount: tableCount); using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AwaitAsync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { await connection.OpenAsync(); using (BatchingFakeProxiedDbDataAdapter adapter = new BatchingFakeProxiedDbDataAdapter(selectCommand)) { return(await this.RunBatchingProxiedDbDataAdapterAsync(randomDataSource, adapter)); } } }
public void SetupDbCommand_TakesParameters_PopulatesCommand() { var connection = A.Fake <IDbConnection>(); var param1 = new SqlParameter("TestParam1", SqlDbType.Int); var param2 = new SqlParameter("TestParam2", SqlDbType.VarChar); var parameterList = new List <IDataParameter> { param1, param2 }; var fakeCommand = new FakeDbCommand(); DatabaseHelper.SetupDbCommand(connection, fakeCommand, "TestProcedure", parameterList); fakeCommand.Connection.Should().Be(connection); fakeCommand.CommandText.Should().Be("TestProcedure"); fakeCommand.Parameters.Count.Should().Be(2); }
//Todo добавить проверку на Exceptions private FakeDbCommand CreateDbCommandForHappyPath(string expectedResponse = null, int?expectedId = int.MinValue) { expectedResponse ??= new Fixture().Create <string>(); if (expectedId == int.MinValue) { expectedId = new Fixture().Create <int>(); } var dbCommand = new FakeDbCommand(); dbCommand.ConfigureExecuteScalarAsync = expectedResponse; dbCommand.SetOutputParameterValueAfterExecute($"@Id", expectedId); return(dbCommand); }
public async Task Proxy_FillAsync_should_work_identically_to_DbDataReader_Fill() { List <TestTable> randomDataSource = RandomDataGenerator.CreateRandomTables(seed: 1234, tableCount: 5, /*allowZeroRowsInTablesByIdx: */ 1, 3); // Part 1: Use proxy DataSet dataSetFromProxy; { using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AwaitAsync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { await connection.OpenAsync(); using (BatchingFakeProxiedDbDataAdapter adpt = new BatchingFakeProxiedDbDataAdapter(selectCommand)) { dataSetFromProxy = new DataSet(); // `.Fill` returns the number of rows in the first table, not any subsequent tables. Yes, that's silly. Int32 rowsInFirstTable = await adpt.FillAsync(dataSetFromProxy); rowsInFirstTable.ShouldBe(40); } } } // Part 2: Use real DataSet dataSetFromReal; { using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { connection.Open(); using (FakeDbDataAdapter adpt = new FakeDbDataAdapter(selectCommand)) { dataSetFromReal = new DataSet(); Int32 rowsInFirstTable = adpt.Fill(dataSetFromReal); rowsInFirstTable.ShouldBe(40); } } } // Assert equality: DataTableMethods.DataSetEquals(dataSetFromProxy, dataSetFromReal, out String diffs).ShouldBeTrue(customMessage: diffs); }
public void Proxy_FillSchema_should_work_identically_to_FillSchema_SchemaType(SchemaType schemaType) { List <TestTable> randomDataSource = RandomDataGenerator.CreateRandomTables(seed: 1234, tableCount: 5, /*allowZeroRowsInTablesByIdx: */ 1, 3); // Part 1: Use proxy DataTable[] whatIsThisFromProxy; DataSet schemaFromProxy; { using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { connection.Open(); using (BatchingFakeProxiedDbDataAdapter adpt = new BatchingFakeProxiedDbDataAdapter(selectCommand)) { schemaFromProxy = new DataSet(); whatIsThisFromProxy = adpt.FillSchema(schemaFromProxy, schemaType); } } } // Part 2: Use real DataTable[] whatIsThisFromReal; DataSet schemaFromReal; { using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { connection.Open(); using (FakeDbDataAdapter adpt = new FakeDbDataAdapter(selectCommand)) { schemaFromReal = new DataSet(); whatIsThisFromReal = adpt.FillSchema(schemaFromReal, schemaType); } } } // Assert equality: DataTableMethods.DataSetEquals(schemaFromProxy, schemaFromReal, out String diffs).ShouldBeTrue(customMessage: diffs); }
public void SetupDbCommand_TakesParameters_PopulatesCommand() { var parameterList = new List <IDataParameter> { new SqlParameter("TestParam1", SqlDbType.Int), new SqlParameter("TestParam2", SqlDbType.VarChar) }; var fakeCommand = new FakeDbCommand(); var mockConnection = new Mock <IDbConnection>(); SmallDb.SetupDbCommand(mockConnection.Object, fakeCommand, "TestProcedure", parameterList); Assert.That(fakeCommand.Connection, Is.EqualTo(mockConnection.Object)); Assert.That(fakeCommand.CommandText, Is.EqualTo("TestProcedure")); Assert.That(fakeCommand.Parameters.Count == 2, Is.True); }
public async Task FakeDbDataReader_Async_should_behave() { FakeDbCommand cmd = new FakeDbCommand(); FakeDbDataReader rdr = new FakeDbDataReader(cmd); // List <TestTable> tables = RandomDataGenerator.CreateRandomTables(seed: 1234, tableCount: 5); rdr.ResetAndLoadTestData(tables); rdr.AllTables.Count.ShouldBe(5); // rdr.AsyncMode = AsyncMode.AwaitAsync; // Table 0: { Int32 i = 0; while (await rdr.ReadAsync()) { i++; } i.ShouldBe(tables[0].Rows.Count); } // Table 1: (await rdr.NextResultAsync()).ShouldBeTrue(); { Int32 i = 0; while (await rdr.ReadAsync()) { i++; } i.ShouldBe(tables[1].Rows.Count); } // Table 2: (await rdr.NextResultAsync()).ShouldBeTrue(); { Int32 i = 0; while (await rdr.ReadAsync()) { i++; } i.ShouldBe(tables[2].Rows.Count); } // Table 3: (await rdr.NextResultAsync()).ShouldBeTrue(); { Int32 i = 0; while (await rdr.ReadAsync()) { i++; } i.ShouldBe(tables[3].Rows.Count); } // Table 4: (await rdr.NextResultAsync()).ShouldBeTrue(); { Int32 i = 0; while (await rdr.ReadAsync()) { i++; } i.ShouldBe(tables[4].Rows.Count); } (await rdr.NextResultAsync()).ShouldBeFalse(); }
public void FakeDbDataReader_Sync_should_behave() { FakeDbCommand cmd = new FakeDbCommand(); FakeDbDataReader rdr = new FakeDbDataReader(cmd); List <TestTable> tables = RandomDataGenerator.CreateRandomTables(seed: 1234, tableCount: 5); rdr.ResetAndLoadTestData(tables); rdr.AllTables.Count.ShouldBe(5); // The RNG is rather fickle, so don't test these. See the `RandomDataGenerator_seed_values_should_produce_expected_results` test above instead. // rdr.AllTables[0].Rows.Count.ShouldBe( 40 ); // rdr.AllTables[1].Rows.Count.ShouldBe( 52 ); // rdr.AllTables[2].Rows.Count.ShouldBe( 79 ); // rdr.AllTables[3].Rows.Count.ShouldBe( 37 ); // rdr.AllTables[4].Rows.Count.ShouldBe( 31 ); // rdr.AsyncMode = AsyncMode.AllowSync; // Table 0: { Int32 i = 0; while (rdr.Read()) { i++; } i.ShouldBe(tables[0].Rows.Count); } // Table 1: rdr.NextResult().ShouldBeTrue(); { Int32 i = 0; while (rdr.Read()) { i++; } i.ShouldBe(tables[1].Rows.Count); } // Table 2: rdr.NextResult().ShouldBeTrue(); { Int32 i = 0; while (rdr.Read()) { i++; } i.ShouldBe(tables[2].Rows.Count); } // Table 3: rdr.NextResult().ShouldBeTrue(); { Int32 i = 0; while (rdr.Read()) { i++; } i.ShouldBe(tables[3].Rows.Count); } // Table 4: rdr.NextResult().ShouldBeTrue(); { Int32 i = 0; while (rdr.Read()) { i++; } i.ShouldBe(tables[4].Rows.Count); } rdr.NextResult().ShouldBeFalse(); }
public Task <DbDataReader> ExecuteReaderAsync(FakeDbCommand command, CommandBehavior behavior, CancellationToken cancellationToken) => _executeReaderAsync(command, behavior, cancellationToken);
public void CreatesCommand() { var command = new FakeDbCommand(); Assert.Equal(command, new TransactedDbConnection(new FakeDbConnection(command)).CreateCommand()); }
public Task <int> ExecuteNonQueryAsync(FakeDbCommand command, CancellationToken cancellationToken) => _executeNonQueryAsync(command, cancellationToken);
public Task <object> ExecuteScalarAsync(FakeDbCommand command, CancellationToken cancellationToken) => _executeScalarAsync(command, cancellationToken);
public virtual DbDataReader ExecuteReader(FakeDbCommand command, CommandBehavior behavior) => _executeReader(command, behavior);
public virtual object ExecuteScalar(FakeDbCommand command) => _executeScalar(command);
public virtual int ExecuteNonQuery(FakeDbCommand command) => _executeNonQuery(command);
public async Task Proxy_UpdateAsync_should_work_identically_to_Update() { List <TestTable> randomDataSource = RandomDataGenerator.CreateRandomTables(seed: 1234, tableCount: 5, /*allowZeroRowsInTablesByIdx: */ 1, 3); // Part 1: Use proxy DataSet dataSetFromProxy; { using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AwaitAsync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { await connection.OpenAsync(); using (BatchingFakeProxiedDbDataAdapter adapter = new BatchingFakeProxiedDbDataAdapter(selectCommand)) using (DbCommandBuilder cmdBuilder = await adapter.CreateCommandBuilderAsync().ConfigureAwait(false)) { dataSetFromProxy = new DataSet(); // `.Fill` returns the number of rows in the first table, not any subsequent tables. Yes, that's silly. Int32 rowsInFirstTable = await adapter.FillAsync(dataSetFromProxy); rowsInFirstTable.ShouldBe(40); // Dictionary <String, Int32> rowsModified = DataTableMethods.MutateDataSet(dataSetFromProxy); // adapter.UpdateCommand = (FakeDbCommand)cmdBuilder.GetUpdateCommand(); adapter.UpdateCommand.NonQueryResultRowCountValue = (cmd) => 1; // HACK /* DataTableMethods.GetNonQueryResultRowCountValue( dataSetFromProxy, cmd, rowsModified ); */; // Int32 updatedRows = await adapter.UpdateAsync(dataSetFromProxy); // updatedRows... in first table only? // updatedRows.ShouldBe( rowsModified ); } } } // Part 2: Use real DataSet dataSetFromReal; { using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { connection.Open(); using (FakeDbDataAdapter adapter = new FakeDbDataAdapter(selectCommand)) using (FakeDbCommandBuilder cmdBuilder = adapter.CreateCommandBuilder()) { dataSetFromReal = new DataSet(); // `.Fill` returns the number of rows in the first table, not any subsequent tables. Yes, that's silly. Int32 rowsInFirstTable = adapter.Fill(dataSetFromReal); rowsInFirstTable.ShouldBe(40); // Dictionary <String, Int32> rowsModified = DataTableMethods.MutateDataSet(dataSetFromReal); // adapter.UpdateCommand = cmdBuilder.GetUpdateCommand(); adapter.UpdateCommand.NonQueryResultRowCountValue = (cmd) => 1; // HACK /* DataTableMethods.GetNonQueryResultRowCountValue( dataSetFromProxy, cmd, rowsModified ); */; // Int32 updatedRows = adapter.Update(dataSetFromReal); // updatedRows... in first table only? // updatedRows.ShouldBe( rowsModified ); } } } // Assert equality: DataTableMethods.DataSetEquals(dataSetFromProxy, dataSetFromReal, out String diffs).ShouldBeTrue(customMessage: diffs); }
public void Proxy_Update_should_work_identically_to_Update() { List <TestTable> randomDataSource = RandomDataGenerator.CreateRandomTables(seed: 1234, tableCount: 5, /*allowZeroRowsInTablesByIdx: */ 1, 3); // TODO: Multiple table UPDATE support is... complicated: https://stackoverflow.com/questions/16218856/how-to-update-two-tables-with-one-dataset // Part 1: Use proxy DataSet dataSetFromProxy; { using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { connection.Open(); using (BatchingFakeProxiedDbDataAdapter adapter = new BatchingFakeProxiedDbDataAdapter(selectCommand)) using (FakeDbCommandBuilder cmdBuilder = new FakeDbCommandBuilder(adapter)) { dataSetFromProxy = new DataSet(); // `.Fill` returns the number of rows in the first table, not any subsequent tables. Yes, that's silly. Int32 rowsInFirstTable = adapter.Fill(dataSetFromProxy); rowsInFirstTable.ShouldBe(40); // Dictionary <String, Int32> rowsModified = DataTableMethods.MutateDataSet(dataSetFromProxy); // adapter.UpdateCommand = cmdBuilder.GetUpdateCommand(); adapter.UpdateCommand.NonQueryResultRowCountValue = (cmd) => DataTableMethods.GetUpdateStatementNonQueryResultRowCountValue(expectedTableName: "TODO", adapter, dataSetFromProxy, cmd, rowsModified); Int32 updatedRows = adapter.Update(dataSetFromProxy); // updatedRows... in first table only? // updatedRows.ShouldBe( rowsModified ); } } } // Part 2: Use real DataSet dataSetFromReal; { using (FakeDbConnection connection = new FakeDbConnection(asyncMode: AsyncMode.AllowSync)) using (FakeDbCommand selectCommand = connection.CreateCommand(testTables: randomDataSource)) { connection.Open(); using (FakeDbDataAdapter adapter = new FakeDbDataAdapter(selectCommand)) using (FakeDbCommandBuilder cmdBuilder = adapter.CreateCommandBuilder()) { dataSetFromReal = new DataSet(); // `.Fill` returns the number of rows in the first table, not any subsequent tables. Yes, that's silly. Int32 rowsInFirstTable = adapter.Fill(dataSetFromReal); rowsInFirstTable.ShouldBe(40); // Dictionary <String, Int32> rowsModified = DataTableMethods.MutateDataSet(dataSetFromReal); // adapter.UpdateCommand = cmdBuilder.GetUpdateCommand(); adapter.UpdateCommand.NonQueryResultRowCountValue = (cmd) => DataTableMethods.GetUpdateStatementNonQueryResultRowCountValue(expectedTableName: "TODO", adapter, dataSetFromReal, cmd, rowsModified); Int32 updatedRows = adapter.Update(dataSetFromReal); // updatedRows... in first table only? // updatedRows.ShouldBe( rowsModified ); } } } // Assert equality: DataTableMethods.DataSetEquals(dataSetFromProxy, dataSetFromReal, out String diffs).ShouldBeTrue(customMessage: diffs); }