Example #1
0
            public WhenExecutingTwoQueriesAndTheSqlDialectUsedSupportsBatching()
            {
                var mockSqlDialect = new Mock <ISqlDialect>();

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

                this.mockDbDriver.Setup(x => x.Combine(It.IsNotNull <SqlQuery>(), It.IsNotNull <SqlQuery>())).Returns(new SqlQuery(""));

                var mockReader = new Mock <IDataReader>();

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

                var mockCommand = new Mock <IDbCommand>();

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

                var mockConnection = new Mock <IDbConnection>();

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

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

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

                var includeCustomer = session.Include.Single <Customer>(2);
                var customer        = session.SingleAsync <Customer>(1).Result;
            }
Example #2
0
        public void SingleSqlQueryExecutesAndReturnsResult()
        {
            var mockSqlDialect = new Mock <ISqlDialect>();

            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(It.IsAny <CommandBehavior>())).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(new MockDbConnectionWrapper(mockConnection.Object));

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

            var customer = session.SingleAsync <Customer>(new SqlQuery("")).Result;

            Assert.NotNull(customer);

            mockReader.VerifyAll();
            mockCommand.VerifyAll();
            mockConnection.VerifyAll();
        }
Example #3
0
        public void SingleSqlQueryThrowsObjectDisposedExceptionIfDisposed()
        {
            var session = new ReadOnlySession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object);

            using (session)
            {
            }

            Assert.Throws <ObjectDisposedException>(() => session.SingleAsync <Customer>(new SqlQuery("")).Result);
        }
Example #4
0
        public void SingleSqlQueryThrowsArgumentNullExceptionForNullSqlQuery()
        {
            var session = new ReadOnlySession(
                ConnectionScope.PerTransaction,
                new Mock <ISqlDialect>().Object,
                new Mock <IDbDriver>().Object);

            SqlQuery sqlQuery = null;

            var exception = Assert.Throws <ArgumentNullException>(
                () => session.SingleAsync <Customer>(sqlQuery).Result);

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

            object identifier = null;

            var exception = Assert.Throws <ArgumentNullException>(
                () => session.SingleAsync <Customer>(identifier).Result);

            Assert.Equal("identifier", exception.ParamName);
        }
Example #6
0
        public void SingleIdentifierExecutesAndReturnsNull()
        {
            object identifier = 100;

            var mockSqlDialect = new Mock <ISqlDialect>();

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

            var mockReader = new Mock <IDataReader>();

            mockReader.Setup(x => x.Read()).Returns(false);

            var mockCommand = new Mock <IDbCommand>();

            mockCommand.Setup(x => x.ExecuteReader(It.IsAny <CommandBehavior>())).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(new MockDbConnectionWrapper(mockConnection.Object));

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

            var customer = session.SingleAsync <Customer>(identifier).Result;

            Assert.Null(customer);

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