public void OnOverride_GivenContext_CallsCache()
        {
            // arrange
            var context = new InterceptionContext();
            context.Parameters = new List<Parameter>(new[] { new Parameter { Value = 2 } });

            var cacheMock = new Mock<ICache>();
            var expectedReturn = new MockDataReader();

            cacheMock
                .Setup(x => x.GetDataReader(It.IsAny<SqlCommand>(), It.IsAny<Func<DbDataReader>>()))
                .Returns(expectedReturn);

            var sut = new ReaderCachingInterceptor(cacheMock.Object);

            // act
            var actualReturn = sut.OnOverride(context);

            // assert
            Assert.That(actualReturn, Is.SameAs(expectedReturn));

            cacheMock.Verify(
                x => x.GetDataReader(It.IsAny<SqlCommand>(), It.IsAny<Func<DbDataReader>>()),
                Times.Once());
        }
        public void GetSqlDataReader_GivenCachedCommand_ReturnsCachedResult()
        {
            // arrange
            var expected = new MockDataReader();

            var storeMock = new Mock<IBackingStore>();
            storeMock
                .Setup(x => x.ContainsKey(It.IsAny<string>()))
                .Returns(true);

            storeMock
                .Setup(x => x.Get(It.IsAny<string>()))
                .Returns(expected);

            var sut = new NonExpiringCache(storeMock.Object);

            // act
            var actual = sut.GetDataReader(new SqlCommand(), () => new MockDataReader());

            // assert
            Assert.That(actual, Is.SameAs(expected));

            storeMock.Verify(
                x => x.Get(It.IsAny<string>()),
                Times.Once());
        }
        public void GetSqlDataReader_GivenNonCachedCommand_CallsQueryAndReturnsResult()
        {
            // arrange
            var queryWasCalled = false;
            var expected = new MockDataReader();

            Func<DbDataReader> query = () => { queryWasCalled = true; return expected; };

            var storeMock = new Mock<IBackingStore>();
            storeMock
                .Setup(x => x.ContainsKey(It.IsAny<string>()))
                .Returns(false);

            storeMock
                .Setup(x => x.Add(It.IsAny<string>(), It.IsAny<DbDataReader>()));

            var sut = new NonExpiringCache(storeMock.Object);

            // act
            var actual = sut.GetDataReader(new SqlCommand(), query);

            // assert
            Assert.That(actual, Is.InstanceOf<CacheableDataReader>());
            Assert.That(queryWasCalled, Is.True);
        }
        public void GetSqlDataReader_GivenNonCachedCommand_StoresResult()
        {
            // arrange
            var expected = new MockDataReader();
            Func<DbDataReader> query = () => expected;

            var storeMock = new Mock<IBackingStore>();
            storeMock
                .Setup(x => x.ContainsKey(It.IsAny<string>()))
                .Returns(false);

            storeMock
                .Setup(x => x.Add(It.IsAny<string>(), It.IsAny<DbDataReader>()));

            var sut = new NonExpiringCache(storeMock.Object);

            // act
            var actual = sut.GetDataReader(new SqlCommand(), query);

            // assert
            storeMock.Verify(
                x => x.Add(It.IsAny<string>(), It.IsAny<DbDataReader>()),
                Times.Once());
        }