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();
                        }
                }
        }
Beispiel #2
0
        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));
                    }
                }
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        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 SimpleExample()
        {
            // This is the result we want to return when execute reader is executed
            var fakeDataReader = new FakeDataReader(0, "UserId", "Name");

            fakeDataReader.AddRow(1, "Smith");
            fakeDataReader.AddRow(2, "John");

            var result = new List <User>();

            using (var connection = new FakeDbConnection("ConnectionString", dbCommand => fakeDataReader))
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT * FROM Users";

                    IDbDataParameter dbDataParameter = command.CreateParameter();
                    dbDataParameter.ParameterName = "ParameterName";
                    dbDataParameter.DbType        = DbType.Int32;
                    dbDataParameter.Value         = 0;

                    command.Parameters.Add(dbDataParameter);
                    using (IDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            result.Add(new User {
                                UserId = reader.GetInt32(0), Name = reader.GetString(1)
                            });
                        }
                    }
                }
            }

            Assert.That(result.Count, Is.EqualTo(2));

            Assert.That(result.ElementAt(0).UserId, Is.EqualTo(1));
            Assert.That(result.ElementAt(0).Name, Is.EqualTo("Smith"));

            Assert.That(result.ElementAt(1).UserId, Is.EqualTo(2));
            Assert.That(result.ElementAt(1).Name, Is.EqualTo("John"));
        }
        public void SimpleExample()
        {
            // This is the result we want to return when execute reader is executed
            var fakeDataReader = new FakeDataReader(0,"UserId","Name");
            fakeDataReader.AddRow(1, "Smith");
            fakeDataReader.AddRow(2, "John");

            var result = new List<User>();
            using (var connection = new FakeDbConnection("ConnectionString", dbCommand => fakeDataReader))
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT * FROM Users";

                    IDbDataParameter dbDataParameter = command.CreateParameter();
                    dbDataParameter.ParameterName = "ParameterName";
                    dbDataParameter.DbType = DbType.Int32;
                    dbDataParameter.Value = 0;

                    command.Parameters.Add(dbDataParameter);
                    using (IDataReader reader = command.ExecuteReader())
                    {

                        while (reader.Read())
                        {
                            result.Add(new User { UserId = reader.GetInt32(0), Name = reader.GetString(1)});
                        }
                    }
                }
            }

            Assert.That(result.Count, Is.EqualTo(2));

            Assert.That(result.ElementAt(0).UserId, Is.EqualTo(1));
            Assert.That(result.ElementAt(0).Name, Is.EqualTo("Smith"));

            Assert.That(result.ElementAt(1).UserId, Is.EqualTo(2));
            Assert.That(result.ElementAt(1).Name, Is.EqualTo("John"));
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        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);
        }