public void AllCreatesASelectAllQueryExecutesAndReturnsResults()
        {
            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.SqlCharacters).Returns(SqlCharacters.Empty);

            var mockReader = new Mock<IDataReader>();
            mockReader.Setup(x => x.Read()).Returns(new Queue<bool>(new[] { true, false }).Dequeue);
            mockReader.As<IDisposable>().Setup(x => x.Dispose());

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteReader()).Returns(mockReader.Object);

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

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

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

            var customers = session.Include.All<Customer>();

            session.ExecutePendingQueries();

            Assert.Equal(1, customers.Values.Count);

            mockReader.VerifyAll();
            mockCommand.VerifyAll();
            mockConnection.VerifyAll();
            mockSqlDialect.VerifyAll();
        }
        public void MicroLiteExceptionsCaughtByExecutePendingQueriesShouldNotBeWrappedInAnotherMicroLiteException()
        {
            var mockSqlDialect = new Mock <ISqlDialect>();

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.BuildCommand(It.IsNotNull <IDbCommand>(), It.IsNotNull <SqlQuery>())).Throws <MicroLiteException>();

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

            // We need at least 1 queued query otherwise we will get an exception when doing queries.Dequeue() instead.
            session.Include.Scalar <int>(new SqlQuery(""));

            var exception = Assert.Throws <MicroLiteException>(() => session.ExecutePendingQueries());

            Assert.IsNotType <MicroLiteException>(exception.InnerException);
        }
예제 #3
0
        public void IncludeScalarSqlQueryExecutesAndReturnsResult()
        {
            var sqlQuery = new SqlQuery("");

            var mockReader = new Mock <IDataReader>();

            mockReader.Setup(x => x.FieldCount).Returns(1);
            mockReader.Setup(x => x[0]).Returns(10);
            mockReader.Setup(x => x.Read()).Returns(new Queue <bool>(new[] { true, false }).Dequeue);
            mockReader.As <IDisposable>().Setup(x => x.Dispose());

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteReader()).Returns(mockReader.Object);
            mockCommand.As <IDisposable>().Setup(x => x.Dispose());

            var mockConnection = new Mock <IDbConnection>();

            var mockSqlDialect = new Mock <ISqlDialect>();

            var mockDbDriver = new Mock <IDbDriver>();

            mockDbDriver.Setup(x => x.CreateConnection()).Returns(mockConnection.Object);
            mockDbDriver.Setup(x => x.BuildCommand(sqlQuery)).Returns(mockCommand.Object);

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

            var includeScalar = session.Include.Scalar <int>(sqlQuery);

            session.ExecutePendingQueries();

            Assert.Equal(10, includeScalar.Value);

            mockReader.VerifyAll();
            mockCommand.VerifyAll();
            mockConnection.VerifyAll();
            mockSqlDialect.VerifyAll();
        }
        public void AllCreatesASelectAllQueryExecutesAndReturnsResults()
        {
            var mockSqlDialect = new Mock <ISqlDialect>();

            mockSqlDialect.Setup(x => x.SqlCharacters).Returns(SqlCharacters.Empty);

            var mockReader = new Mock <IDataReader>();

            mockReader.Setup(x => x.Read()).Returns(new Queue <bool>(new[] { true, false }).Dequeue);
            mockReader.As <IDisposable>().Setup(x => x.Dispose());

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteReader()).Returns(mockReader.Object);

            var mockConnection = new Mock <IDbConnection>();

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

            var mockDbDriver = new Mock <IDbDriver>();

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

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

            var customers = session.Include.All <Customer>();

            session.ExecutePendingQueries();

            Assert.Equal(1, customers.Values.Count);

            mockReader.VerifyAll();
            mockCommand.VerifyAll();
            mockConnection.VerifyAll();
            mockSqlDialect.VerifyAll();
        }
        public void MicroLiteExceptionsCaughtByExecutePendingQueriesShouldNotBeWrappedInAnotherMicroLiteException()
        {
            var mockSqlDialect = new Mock<ISqlDialect>();

            var mockDbDriver = new Mock<IDbDriver>();
            mockDbDriver.Setup(x => x.BuildCommand(It.IsNotNull<IDbCommand>(), It.IsNotNull<SqlQuery>())).Throws<MicroLiteException>();

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

            // We need at least 1 queued query otherwise we will get an exception when doing queries.Dequeue() instead.
            session.Include.Scalar<int>(new SqlQuery(""));

            var exception = Assert.Throws<MicroLiteException>(() => session.ExecutePendingQueries());

            Assert.IsNotType<MicroLiteException>(exception.InnerException);
        }
        public void IncludeScalarSqlQueryExecutesAndReturnsResult()
        {
            var mockSqlDialect = new Mock<ISqlDialect>();

            var mockReader = new Mock<IDataReader>();
            mockReader.Setup(x => x.FieldCount).Returns(1);
            mockReader.Setup(x => x[0]).Returns(10);
            mockReader.Setup(x => x.Read()).Returns(new Queue<bool>(new[] { true, false }).Dequeue);
            mockReader.As<IDisposable>().Setup(x => x.Dispose());

            var mockCommand = new Mock<IDbCommand>();
            mockCommand.Setup(x => x.ExecuteReader()).Returns(mockReader.Object);

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

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

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

            var includeScalar = session.Include.Scalar<int>(new SqlQuery(""));

            session.ExecutePendingQueries();

            Assert.Equal(10, includeScalar.Value);

            mockReader.VerifyAll();
            mockCommand.VerifyAll();
            mockConnection.VerifyAll();
            mockSqlDialect.VerifyAll();
        }