Exemplo n.º 1
0
        public void ValidateArguments_TakesValidArguments_DoesNothing()
        {
            var mockConnection = new Mock <IDbConnection>();

            Assert.DoesNotThrow(() => SmallDb.ValidateArguments(mockConnection.Object, "TestProcedure"),
                                "Unit Test threw an exception that was not expected!");
        }
Exemplo n.º 2
0
        public void ValidateArguments_TakesEmptyStoredProcedureName_ThrowsException()
        {
            var mockConnection = new Mock <IDbConnection>();

            Assert.Throws <ArgumentNullException>(() => SmallDb.ValidateArguments(mockConnection.Object, string.Empty),
                                                  "Unit test expected an ArgumentNullException to be thrown!");
        }
Exemplo n.º 3
0
        public void ExecuteQueryWithFunc_TakesNoParameters_ReturnsValidObject()
        {
            var mockReader = new Mock <IDataReader>();

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.SetupSet(x => x.Connection = It.IsAny <IDbConnection>());
            mockCommand.Setup(x => x.ExecuteReader()).Returns(mockReader.Object);

            var mockConnection = new Mock <IDbConnection>();

            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            mockCommand.SetupGet(x => x.Connection).Returns(mockConnection.Object);

            var mockLogger = new Mock <ILogWrapper>();

            var helper = new SmallDb(mockLogger.Object);

            var result = helper.ExecuteQuery(mockConnection.Object, "TestProcedure", CreateFakeObject);

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.InstanceOf <FakeObject>());
            Assert.That(FakeObject.Name, Is.EqualTo("Fake"));
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        public void ExecuteNonQuery_TakesNoParameters_Executes()
        {
            var mockCommand = new Mock <IDbCommand>();

            mockCommand.SetupSet(x => x.Connection = It.IsAny <IDbConnection>());

            var mockConnection = new Mock <IDbConnection>();

            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            mockCommand.SetupGet(x => x.Connection).Returns(mockConnection.Object);

            var mockLogger = new Mock <ILogWrapper>();

            var helper = new SmallDb(mockLogger.Object);

            Assert.DoesNotThrow(() => helper.ExecuteNonQuery(mockConnection.Object, "TestProcedure"),
                                "Unit Test threw an exception that was not expected!");
        }
Exemplo n.º 6
0
        public void ExecuteScalar_TakesNoParameters_ReturnsValidResult()
        {
            const string expected = "This is a test result";

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteScalar()).Returns(expected);
            mockCommand.SetupSet(x => x.Connection = It.IsAny <IDbConnection>());

            var mockConnection = new Mock <IDbConnection>();

            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            mockCommand.SetupGet(x => x.Connection).Returns(mockConnection.Object);

            var mockLogger = new Mock <ILogWrapper>();

            var helper       = new SmallDb(mockLogger.Object);
            var actualResult = helper.ExecuteScalar(mockConnection.Object, "TestProcedure");

            Assert.That(expected, Is.EqualTo(actualResult));
        }
Exemplo n.º 7
0
        public void ExecuteQuery_TakesNoParameters_ReturnsValidDataTable()
        {
            var mockReader = new Mock <IDataReader>();

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.SetupSet(x => x.Connection = It.IsAny <IDbConnection>());
            mockCommand.Setup(x => x.ExecuteReader()).Returns(mockReader.Object);

            var mockConnection = new Mock <IDbConnection>();

            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            mockCommand.SetupGet(x => x.Connection).Returns(mockConnection.Object);

            var mockLogger = new Mock <ILogWrapper>();

            var helper = new SmallDb(mockLogger.Object);
            var result = helper.ExecuteQuery(mockConnection.Object, "TestProcedure");

            Assert.That(result, Is.Not.Null);
        }
Exemplo n.º 8
0
 public void ValidateArguments_TakesNullConnection_ThrowsException()
 {
     Assert.Throws <ArgumentNullException>(() => SmallDb.ValidateArguments(null, "TestProcedure"),
                                           "Unit test expected an ArgumentNullException to be thrown!");
 }
Exemplo n.º 9
0
 public void IsInternalSql(string sql, bool expectedValue)
 {
     Assert.That(SmallDb.IsInternalSql(sql), Is.EqualTo(expectedValue));
 }