public void AMicroLiteExceptionIsThrown()
            {
                var session = new ReadOnlySession(
                    ConnectionScope.PerTransaction,
                    new Mock <ISqlDialect>().Object,
                    new Mock <IDbDriver>().Object);

                var exception = Assert.Throws <MicroLiteException>(
                    () => session.Paged <Customer>(new SqlQuery(""), PagingOptions.None));

                Assert.Equal(ExceptionMessages.Session_PagingOptionsMustNotBeNone, exception.Message);
            }
        public void PagedThrowsArgumentNullExceptionForNullSqlQuery()
        {
            var session = new ReadOnlySession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object);

            var exception = Assert.Throws <ArgumentNullException>(
                () => session.Paged <Customer>(null, PagingOptions.ForPage(1, 25)));

            Assert.Equal("sqlQuery", exception.ParamName);
        }
        public void PagedThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new ReadOnlySession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object);

            using (session)
            {
            }

            Assert.Throws <ObjectDisposedException>(
                () => session.Paged <Customer>(null, PagingOptions.ForPage(1, 25)));
        }
        public void PagedExecutesAndReturnsResultsForTenthPageWithTwentyFivePerPage()
        {
            var sqlQuery      = new SqlQuery("SELECT * FROM TABLE");
            var countQuery    = new SqlQuery("SELECT COUNT(*) FROM TABLE");
            var pagedQuery    = new SqlQuery("SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM Customers) AS Customers");
            var combinedQuery = new SqlQuery("SELECT COUNT(*) FROM TABLE;SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM Customers) AS Customers");

            var mockSqlDialect = new Mock <ISqlDialect>();

            mockSqlDialect.Setup(x => x.CountQuery(sqlQuery)).Returns(countQuery);
            mockSqlDialect.Setup(x => x.PageQuery(sqlQuery, PagingOptions.ForPage(10, 25))).Returns(pagedQuery);

            var mockReader = new Mock <IDataReader>();

            mockReader.Setup(x => x.FieldCount).Returns(new Queue <int>(new[] { 1, 0 }).Dequeue);
            mockReader.Setup(x => x[0]).Returns(1000); // Simulate 1000 records in the count query
            mockReader.Setup(x => x.NextResult()).Returns(new Queue <bool>(new[] { true, false }).Dequeue);
            mockReader.Setup(x => x.Read()).Returns(new Queue <bool>(new[] { true, false, 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);
            mockDbDriver.Setup(x => x.SupportsBatchedQueries).Returns(true);
            mockDbDriver.Setup(x => x.Combine(countQuery, pagedQuery)).Returns(combinedQuery);

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

            var page = session.Paged <Customer>(sqlQuery, PagingOptions.ForPage(10, 25));

            Assert.Equal(10, page.Page);
            Assert.Equal(1, page.Results.Count);

            mockReader.VerifyAll();
            mockCommand.VerifyAll();
            mockConnection.VerifyAll();
            mockSqlDialect.VerifyAll();
        }
            public void AMicroLiteExceptionIsThrown()
            {
                var session = new ReadOnlySession(
                    ConnectionScope.PerTransaction,
                    new Mock<ISqlDialect>().Object,
                    new Mock<IDbDriver>().Object);

                var exception = Assert.Throws<MicroLiteException>(
                    () => session.Paged<Customer>(new SqlQuery(""), PagingOptions.None));

                Assert.Equal(ExceptionMessages.Session_PagingOptionsMustNotBeNone, exception.Message);
            }
        public void PagedThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new ReadOnlySession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object);

            using (session)
            {
            }

            Assert.Throws<ObjectDisposedException>(
                () => session.Paged<Customer>(null, PagingOptions.ForPage(1, 25)));
        }
        public void PagedThrowsArgumentNullExceptionForNullSqlQuery()
        {
            var session = new ReadOnlySession(
                ConnectionScope.PerTransaction,
                new Mock<ISqlDialect>().Object,
                new Mock<IDbDriver>().Object);

            var exception = Assert.Throws<ArgumentNullException>(
                () => session.Paged<Customer>(null, PagingOptions.ForPage(1, 25)));

            Assert.Equal("sqlQuery", exception.ParamName);
        }
        public void PagedExecutesAndReturnsResultsForTenthPageWithTwentyFivePerPage()
        {
            var sqlQuery = new SqlQuery("SELECT * FROM TABLE");
            var countQuery = new SqlQuery("SELECT COUNT(*) FROM TABLE");
            var pagedQuery = new SqlQuery("SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM Customers) AS Customers");
            var combinedQuery = new SqlQuery("SELECT COUNT(*) FROM TABLE;SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM Customers) AS Customers");

            var mockSqlDialect = new Mock<ISqlDialect>();
            mockSqlDialect.Setup(x => x.CountQuery(sqlQuery)).Returns(countQuery);
            mockSqlDialect.Setup(x => x.PageQuery(sqlQuery, PagingOptions.ForPage(10, 25))).Returns(pagedQuery);

            var mockReader = new Mock<IDataReader>();
            mockReader.Setup(x => x.FieldCount).Returns(new Queue<int>(new[] { 1, 0 }).Dequeue);
            mockReader.Setup(x => x[0]).Returns(1000); // Simulate 1000 records in the count query
            mockReader.Setup(x => x.NextResult()).Returns(new Queue<bool>(new[] { true, false }).Dequeue);
            mockReader.Setup(x => x.Read()).Returns(new Queue<bool>(new[] { true, false, 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);
            mockDbDriver.Setup(x => x.SupportsBatchedQueries).Returns(true);
            mockDbDriver.Setup(x => x.Combine(countQuery, pagedQuery)).Returns(combinedQuery);

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

            var page = session.Paged<Customer>(sqlQuery, PagingOptions.ForPage(10, 25));

            Assert.Equal(10, page.Page);
            Assert.Equal(1, page.Results.Count);

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