示例#1
0
        public void UpdateInstanceReturnsFalseIfNoRecordsUpdated()
        {
            var customer = new Customer
            {
                Id = 187224
            };

            var mockSqlDialect = new Mock <ISqlDialect>();

            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(It.IsNotNull <IObjectInfo>(), customer)).Returns(new SqlQuery(""));

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(0);

            var mockConnection = new Mock <IDbConnection>();

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

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new SessionListeners());

            Assert.False(session.UpdateAsync(customer).Result);

            mockSqlDialect.VerifyAll();
            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
示例#2
0
        public void UpdateObjectDeltaReturnsTrueIfRecordUpdated()
        {
            var objectDelta = new ObjectDelta(typeof(Customer), 1234);

            objectDelta.AddChange("Name", "Fred Flintstone");

            var mockSqlDialect = new Mock <ISqlDialect>();

            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(objectDelta)).Returns(new SqlQuery(""));

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(1);

            var mockConnection = new Mock <IDbConnection>();

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

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new SessionListeners());

            Assert.True(session.UpdateAsync(objectDelta).Result);

            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
示例#3
0
        public void UpdateObjectDeltaThrowsArgumentNullExceptionForNullObjectDelta()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object,
                new SessionListeners());

            var exception = Assert.Throws <AggregateException>(
                () => session.UpdateAsync((ObjectDelta)null).Result);

            Assert.Equal("objectDelta", ((ArgumentNullException)exception.InnerException).ParamName);
        }
示例#4
0
        public void UpdateInstanceInvokesListeners()
        {
            var customer = new Customer
            {
                Id = 187224
            };

            var rowsAffected = 1;

            var mockSqlDialect = new Mock <ISqlDialect>();

            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(It.IsNotNull <IObjectInfo>(), customer)).Returns(new SqlQuery(""));

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(rowsAffected);

            var mockConnection = new Mock <IDbConnection>();

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

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            int counter = 0;

            var mockListener1 = new Mock <IUpdateListener>();

            mockListener1.Setup(x => x.AfterUpdate(customer, 1)).Callback(() => Assert.Equal(4, ++counter));
            mockListener1.Setup(x => x.BeforeUpdate(customer)).Callback(() => Assert.Equal(1, ++counter));

            var mockListener2 = new Mock <IUpdateListener>();

            mockListener2.Setup(x => x.AfterUpdate(customer, 1)).Callback(() => Assert.Equal(3, ++counter));
            mockListener2.Setup(x => x.BeforeUpdate(customer)).Callback(() => Assert.Equal(2, ++counter));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new SessionListeners(
                    new IDeleteListener[0],
                    new IInsertListener[0],
                    new[] { mockListener1.Object, mockListener2.Object }));

            session.UpdateAsync(customer).Wait();

            mockListener1.VerifyAll();
            mockListener2.VerifyAll();
        }
示例#5
0
        public void UpdateInstanceThrowsArgumentNullExceptionForNullInstance()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws <AggregateException>(
                () => session.UpdateAsync((Customer)null).Result);

            Assert.Equal("instance", ((ArgumentNullException)exception.InnerException).ParamName);
        }
示例#6
0
        public void UpdateObjectDeltaThrowsMicroLiteExceptionIfItContainsNoChanges()
        {
            var objectDelta = new ObjectDelta(typeof(Customer), 1234);

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object,
                new SessionListeners());

            var exception = Assert.Throws <AggregateException>(
                () => session.UpdateAsync(objectDelta).Result);

            Assert.IsType <MicroLiteException>(exception.InnerException);
            Assert.Equal(ExceptionMessages.ObjectDelta_MustContainAtLeastOneChange, exception.InnerException.Message);
        }
示例#7
0
        public void UpdateObjectDeltaThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object,
                new SessionListeners());

            using (session)
            {
            }

            var exception = Assert.Throws <AggregateException>(
                () => session.UpdateAsync(new ObjectDelta(typeof(Customer), 1234)).Result);

            Assert.IsType <ObjectDisposedException>(exception.InnerException);
        }
示例#8
0
        public void UpdateInstanceThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            using (session)
            {
            }

            var exception = Assert.Throws <AggregateException>(
                () => session.UpdateAsync(new Customer()).Result);

            Assert.IsType <ObjectDisposedException>(exception.InnerException);
        }
示例#9
0
        public void UpdateInstanceThrowsMicroLiteExceptionIfExecuteNonQueryThrowsException()
        {
            var customer = new Customer
            {
                Id = 187224
            };

            var mockSqlDialect = new Mock <ISqlDialect>();

            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(It.IsNotNull <IObjectInfo>(), customer)).Returns(new SqlQuery(""));

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteNonQuery()).Throws <InvalidOperationException>();

            var mockConnection = new Mock <IDbConnection>();

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

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws <AggregateException>(() => session.UpdateAsync(customer).Wait());

            Assert.IsType <MicroLiteException>(exception.InnerException);
            Assert.IsType <InvalidOperationException>(exception.InnerException.InnerException);
            Assert.Equal(exception.InnerException.InnerException.Message, exception.InnerException.Message);

            // Command should still be disposed.
            mockCommand.VerifyAll();
        }
示例#10
0
        public void UpdateInstanceBuildsAndExecutesQuery()
        {
            var customer = new Customer
            {
                Id = 187224
            };

            var rowsAffected = 1;

            var mockSqlDialect = new Mock <ISqlDialect>();

            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(It.IsNotNull <IObjectInfo>(), customer)).Returns(new SqlQuery(""));

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(rowsAffected);

            var mockConnection = new Mock <IDbConnection>();

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

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            session.UpdateAsync(customer).Wait();

            mockSqlDialect.VerifyAll();
            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
示例#11
0
        public void UpdateInstanceThrowsMicroLiteExceptionIfIdentifierNotSet()
        {
            var customer = new Customer
            {
                Id = 0
            };

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(new Mock <IDbConnection>().Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                mockDbDriver.Object,
                new SessionListeners());

            var exception = Assert.Throws <AggregateException>(() => session.UpdateAsync(customer).Wait());

            Assert.IsType <MicroLiteException>(exception.InnerException);
            Assert.Equal(ExceptionMessages.Session_IdentifierNotSetForUpdate, exception.InnerException.Message);
        }
        public void UpdateObjectDeltaThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            using (session)
            {
            }

            var exception = Assert.Throws<AggregateException>(
                () => session.UpdateAsync(new ObjectDelta(typeof(Customer), 1234)).Result);

            Assert.IsType<ObjectDisposedException>(exception.InnerException);
        }
        public void UpdateObjectDeltaThrowsMicroLiteExceptionIfItContainsNoChanges()
        {
            var objectDelta = new ObjectDelta(typeof(Customer), 1234);

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws<AggregateException>(
                () => session.UpdateAsync(objectDelta).Result);

            Assert.IsType<MicroLiteException>(exception.InnerException);
            Assert.Equal(ExceptionMessages.ObjectDelta_MustContainAtLeastOneChange, exception.InnerException.Message);
        }
        public void UpdateObjectDeltaThrowsArgumentNullExceptionForNullObjectDelta()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws<AggregateException>(
                () => session.UpdateAsync((ObjectDelta)null).Result);

            Assert.Equal("objectDelta", ((ArgumentNullException)exception.InnerException).ParamName);
        }
        public void UpdateObjectDeltaReturnsTrueIfRecordUpdated()
        {
            var objectDelta = new ObjectDelta(typeof(Customer), 1234);
            objectDelta.AddChange("Name", "Fred Flintstone");

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(objectDelta)).Returns(new SqlQuery(""));

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(1);

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            Assert.True(session.UpdateAsync(objectDelta).Result);

            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
        public void UpdateInstanceThrowsMicroLiteExceptionIfIdentifierNotSet()
        {
            var customer = new Customer
            {
                Id = 0
            };

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(new Mock<IDbConnection>().Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws<AggregateException>(() => session.UpdateAsync(customer).Wait());

            Assert.IsType<MicroLiteException>(exception.InnerException);
            Assert.Equal(ExceptionMessages.Session_IdentifierNotSetForUpdate, exception.InnerException.Message);
        }
        public void UpdateInstanceThrowsMicroLiteExceptionIfExecuteNonQueryThrowsException()
        {
            var customer = new Customer
            {
                Id = 187224
            };

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(new SqlQuery(""));

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteNonQuery()).Throws<InvalidOperationException>();

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            var exception = Assert.Throws<AggregateException>(() => session.UpdateAsync(customer).Wait());

            Assert.IsType<MicroLiteException>(exception.InnerException);
            Assert.IsType<InvalidOperationException>(exception.InnerException.InnerException);
            Assert.Equal(exception.InnerException.InnerException.Message, exception.InnerException.Message);

            // Command should still be disposed.
            mockCommand.VerifyAll();
        }
示例#18
0
        public void UpdateInstanceThrowsArgumentNullExceptionForNullInstance()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new SessionListeners());

            var exception = Assert.Throws<AggregateException>(
                () => session.UpdateAsync((Customer)null).Result);

            Assert.Equal("instance", ((ArgumentNullException)exception.InnerException).ParamName);
        }
示例#19
0
        public void UpdateInstanceBuildsAndExecutesQuery()
        {
            var customer = new Customer
            {
                Id = 187224
            };

            var rowsAffected = 1;

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(new SqlQuery(""));

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(rowsAffected);

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new SessionListeners());

            session.UpdateAsync(customer).Wait();

            mockSqlDialect.VerifyAll();
            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
        public void UpdateInstanceInvokesListeners()
        {
            var customer = new Customer
            {
                Id = 187224
            };

            var rowsAffected = 1;

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(new SqlQuery(""));

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(rowsAffected);

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            int counter = 0;

            var mockListener1 = new Mock<IUpdateListener>();
            mockListener1.Setup(x => x.AfterUpdate(customer, 1)).Callback(() => Assert.Equal(4, ++counter));
            mockListener1.Setup(x => x.BeforeUpdate(customer)).Callback(() => Assert.Equal(1, ++counter));

            var mockListener2 = new Mock<IUpdateListener>();
            mockListener2.Setup(x => x.AfterUpdate(customer, 1)).Callback(() => Assert.Equal(3, ++counter));
            mockListener2.Setup(x => x.BeforeUpdate(customer)).Callback(() => Assert.Equal(2, ++counter));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new[] { mockListener1.Object, mockListener2.Object });

            session.UpdateAsync(customer).Wait();

            mockListener1.VerifyAll();
            mockListener2.VerifyAll();
        }
        public void UpdateInstanceReturnsTrueIfRecordUpdated()
        {
            var customer = new Customer
            {
                Id = 187224
            };

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(It.IsNotNull<IObjectInfo>(), customer)).Returns(new SqlQuery(""));

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(1);

            var mockConnection = new Mock<IDbConnection>();
            mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object));

            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                mockSqlDialect.Object,
                mockDbDriver.Object,
                new IDeleteListener[0],
                new IInsertListener[0],
                new IUpdateListener[0]);

            Assert.True(session.UpdateAsync(customer).Result);

            mockSqlDialect.VerifyAll();
            mockDbDriver.VerifyAll();
            mockCommand.VerifyAll();
        }
示例#22
0
        public void UpdateInstanceThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new AsyncSession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object,
                new SessionListeners());

            using (session)
            {
            }

            var exception = Assert.Throws<AggregateException>(
                () => session.UpdateAsync(new Customer()).Result);

            Assert.IsType<ObjectDisposedException>(exception.InnerException);
        }