public void Create_CommandParameterProviderThrowsException_ExceptionIsPropogated()
        {
            var person = Person.Faker.Generate(1).Single();
            var record = new
            {
                person.FirstName,
                person.LastName,
                person.MiddleInitial,
                person.Age,
                person.DateOfBirth,
                person.Gender
            };
            var staticCommandParameters = DatabaseWriterCommandFactoryTests.CreateStaticParameters(record);

            var commandParameterProvider = MockRepository.GenerateMock <IDatabaseWriterCommandParameterProvider>();

            commandParameterProvider.Stub(x => x.GetParameters(Arg <object> .Is.Equal(record))).Throw(new InternalTestFailureException());

            var databaseWriterCommandFactory = new DatabaseWriterCommandFactory(commandParameterProvider)
            {
                CommandText    = "dbo.InsertPerson",
                CommandType    = CommandType.StoredProcedure,
                CommandTimeout = TimeSpan.FromMinutes(2)
            };

            databaseWriterCommandFactory.Create(record);
        }
        public void Create_StaticCommandParameters_CommandIsCreatedWithStaticParameters()
        {
            var person = Person.Faker.Generate(1).Single();
            var record = new
            {
                person.FirstName,
                person.LastName,
                person.MiddleInitial,
                person.Age,
                person.DateOfBirth,
                person.Gender
            };
            var commandType                  = CommandType.StoredProcedure;
            var commandText                  = "dbo.InsertPerson";
            var staticCommandParameters      = DatabaseWriterCommandFactoryTests.CreateStaticParameters(record);
            var databaseWriterCommandFactory = new DatabaseWriterCommandFactory(staticCommandParameters, commandType, commandText);

            using (var command = databaseWriterCommandFactory.Create(record))
            {
                Assert.AreEqual(databaseWriterCommandFactory.CommandText, command.CommandText);
                Assert.AreEqual(databaseWriterCommandFactory.CommandType, command.CommandType);
                Assert.AreEqual(databaseWriterCommandFactory.CommandTimeout, TimeSpan.FromMilliseconds(command.CommandTimeout));

                DatabaseWriterCommandFactoryTests.AssertCommandParametersAreCreatedAsExpected(command, record);
            }
        }
        public void Constructor_CommandParameterProvider_CommandParameterProviderIsUsed()
        {
            var record = Person.Faker.Generate(1).Single();
            var commandParameterProvider = MockRepository.GenerateMock <IDatabaseWriterCommandParameterProvider>();

            commandParameterProvider.Expect(x => x.GetParameters(Arg <object> .Is.Equal(record))).Return(new SqlParameter[0]).Repeat.Once();

            var databaseWriterCommandFactory = new DatabaseWriterCommandFactory(commandParameterProvider);

            using (var command = databaseWriterCommandFactory.Create(record))
            {
                commandParameterProvider.VerifyAllExpectations();
            }
        }
        public void Create_DefaultConstructorAndRecordWithSqlParameterAttributes_CommandIsCreatedAsExpected()
        {
            var record = Person.Faker.Generate(1).Single();
            var databaseWriterCommandFactory = new DatabaseWriterCommandFactory()
            {
                CommandText    = "dbo.InsertPerson",
                CommandType    = CommandType.StoredProcedure,
                CommandTimeout = TimeSpan.FromMinutes(2)
            };

            using (var command = databaseWriterCommandFactory.Create(record))
            {
                Assert.AreEqual(databaseWriterCommandFactory.CommandText, command.CommandText);
                Assert.AreEqual(databaseWriterCommandFactory.CommandType, command.CommandType);
                Assert.AreEqual(databaseWriterCommandFactory.CommandTimeout, TimeSpan.FromMilliseconds(command.CommandTimeout));

                DatabaseWriterCommandFactoryTests.AssertCommandParametersAreCreatedAsExpected(command, record);
            }
        }
        public void Constructor_CommandParameterProviderAndCommandText_CommandParameterProviderIsUsedAndCommandTextPropertyIsSet()
        {
            var commandText = "dbo.InsertPerson";
            var record      = Person.Faker.Generate(1).Single();
            var commandParameterProvider = MockRepository.GenerateMock <IDatabaseWriterCommandParameterProvider>();

            commandParameterProvider.Expect(x => x.GetParameters(Arg <object> .Is.Equal(record))).Return(new SqlParameter[0]).Repeat.Once();

            var databaseWriterCommandFactory = new DatabaseWriterCommandFactory(commandParameterProvider, commandText);

            using (var command = databaseWriterCommandFactory.Create(record))
            {
                commandParameterProvider.VerifyAllExpectations();
            }

            Assert.AreEqual(commandText, databaseWriterCommandFactory.CommandText);
            Assert.AreEqual(CommandType.StoredProcedure, databaseWriterCommandFactory.CommandType);
            Assert.AreEqual(TimeSpan.FromSeconds(30), databaseWriterCommandFactory.CommandTimeout);
        }
        public void Create_CustomCommandParameterProvider_CommandIsCreatedAsExpected()
        {
            var person = Person.Faker.Generate(1).Single();
            var record = new
            {
                person.FirstName,
                person.LastName,
                person.MiddleInitial,
                person.Age,
                person.DateOfBirth,
                person.Gender
            };
            var staticCommandParameters = DatabaseWriterCommandFactoryTests.CreateStaticParameters(record);

            var commandParameterProvider = MockRepository.GenerateMock <IDatabaseWriterCommandParameterProvider>();

            commandParameterProvider.Expect(x => x.GetParameters(Arg <object> .Is.Equal(record))).Return(staticCommandParameters).Repeat.Once();

            var databaseWriterCommandFactory = new DatabaseWriterCommandFactory(commandParameterProvider)
            {
                CommandText    = "dbo.InsertPerson",
                CommandType    = CommandType.StoredProcedure,
                CommandTimeout = TimeSpan.FromMinutes(2)
            };

            using (var command = databaseWriterCommandFactory.Create(record))
            {
                commandParameterProvider.VerifyAllExpectations();

                Assert.AreEqual(databaseWriterCommandFactory.CommandText, command.CommandText);
                Assert.AreEqual(databaseWriterCommandFactory.CommandType, command.CommandType);
                Assert.AreEqual(databaseWriterCommandFactory.CommandTimeout, TimeSpan.FromMilliseconds(command.CommandTimeout));

                DatabaseWriterCommandFactoryTests.AssertCommandParametersAreCreatedAsExpected(command, record);
            }
        }