public void TestCaching() { ICache cache = new NonExpiringCache(); context.ObjectFactory.RegisterSingleton("inventors", cache); ProxyFactory pf = new ProxyFactory(new InventorStore()); pf.AddAdvisors(cacheAspect); IInventorStore store = (IInventorStore)pf.GetProxy(); Assert.AreEqual(0, cache.Count); IList inventors = store.GetAll(); Assert.AreEqual(2, cache.Count); store.Delete((Inventor)inventors[0]); Assert.AreEqual(1, cache.Count); Inventor tesla = store.Load("Nikola Tesla"); Assert.AreEqual(2, cache.Count); store.Save(tesla); Assert.AreEqual(2, cache.Count); Assert.AreEqual("Serbian", ((Inventor)cache.Get("Nikola Tesla")).Nationality); store.DeleteAll(); Assert.AreEqual(0, cache.Count); }
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_GivenNullQuery_ThrowsException() { // arrange var sut = new NonExpiringCache(Mock.Of<IBackingStore>()); // assert Assert.Throws<ArgumentNullException>(() => sut.GetDataReader(new SqlCommand(), null)); }
public void NoCacheKeySpecified() { ICache cache = new NonExpiringCache(); context.ObjectFactory.RegisterSingleton("inventors", cache); ProxyFactory pf = new ProxyFactory(new InventorStore()); pf.AddAdvisors(cacheAspect); IInventorStore store = (IInventorStore)pf.GetProxy(); Assert.Throws <ArgumentNullException>(() => store.GetAllNoCacheKey()); }
public void NoCacheKeySpecified() { ICache cache = new NonExpiringCache(); context.ObjectFactory.RegisterSingleton("inventors", cache); ProxyFactory pf = new ProxyFactory(new InventorStore()); pf.AddAdvisors(cacheAspect); IInventorStore store = (IInventorStore)pf.GetProxy(); IList items = store.GetAllNoCacheKey(); Assert.IsNotNull(items); }
public void TestCacheResultDoesNotReturnInvalidType() { ICache cache = new NonExpiringCache(); context.ObjectFactory.RegisterSingleton("inventors", cache); ProxyFactory pf = new ProxyFactory(new InventorStore()); pf.AddAdvisors(cacheAspect); IInventorStore store = (IInventorStore)pf.GetProxy(); cache.Insert("Nikola Tesla", "WrongTypeForMethodSignature"); Inventor value = store.Load("Nikola Tesla"); Assert.That(value, Is.AssignableTo(typeof(Inventor)), "CacheAspect returned a cached type that is incompatible with the method signature return type"); }
public void GetSqlDataReader_GivenCommand_ChecksBackingStore() { // arrange var storeMock = new Mock<IBackingStore>(); storeMock .Setup(x => x.ContainsKey(It.IsAny<string>())) .Returns(true); var sut = new NonExpiringCache(storeMock.Object); // act sut.GetDataReader(new SqlCommand(), () => new MockDataReader()); // assert storeMock.Verify( x => x.ContainsKey(It.IsAny<string>()), Times.Once()); }
public void UseMethodInfoForKeyGeneration() { ICache cache = new NonExpiringCache(); context.ObjectFactory.RegisterSingleton("defaultCache", cache); ProxyFactory pf = new ProxyFactory(new GenericDao <string, int>()); pf.AddAdvisors(cacheAspect); IGenericDao <string, int> dao = (IGenericDao <string, int>)pf.GetProxy(); Assert.AreEqual(0, cache.Count); dao.Load(1); Assert.AreEqual(1, cache.Count); // actually, it should be null, because default(string) = null // but it returns the NullValue marker created by the CacheResultAttribute Assert.IsNotNull(cache.Get("String_1")); }
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_WhenBackingStoreThrows_ReThrowsException() { // arrange var expected = new ApplicationException(); 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>())).Throws(expected); var sut = new NonExpiringCache(storeMock.Object); // assert Assert.Throws(Is.SameAs(expected), () => sut.GetDataReader(new SqlCommand(), () => new MockDataReader())); }
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()); }