public async Task GetCategories_Uses_Cache_When_Data_Available() { var cacheService = new MockCacheService(); // cacheService.DataExistsAndIsValidAsyncDelegate = s => Task.FromResult(true); var categories = new List<Category> { new Category{ Id = 1}, new Category{ Id = 2} }; cacheService.GetDataDelegate = (string s) => { if (s == "Categories-0-0") return new ReadOnlyCollection<Category>(categories); return new ReadOnlyCollection<Category>(null); }; var productCatalogService = new MockProductCatalogService { GetSubcategoriesAsyncDelegate = (parentId, maxProducts) => Task.FromResult<ICollection<Category>>( new Collection<Category>(null)) }; var target = new ProductCatalogRepository(productCatalogService, cacheService); var returnedCategories = (await target.GetRootCategoriesAsync(0)).ToList(); Assert.AreEqual(2, returnedCategories.Count); Assert.AreEqual(1, returnedCategories[0].Id); Assert.AreEqual(2, returnedCategories[1].Id); }
public async Task GetCategories_Calls_Service_When_Cache_Miss() { var cacheService = new MockCacheService { GetDataDelegate = s => { throw new FileNotFoundException(); }, SaveDataAsyncDelegate = (s, c) => Task.FromResult(new Uri("http://test.org")) }; var productCatalogService = new MockProductCatalogService(); var categories = new List<Category> { new Category { Id = 1 }, new Category { Id = 2 } }; productCatalogService.GetSubcategoriesAsyncDelegate = (parentId, maxProducts) => Task.FromResult((ICollection<Category>)(new Collection<Category>(categories))); var target = new ProductCatalogRepository(productCatalogService, cacheService); var returnedCategories = (await target.GetRootCategoriesAsync(0)).ToList(); Assert.AreEqual(2, returnedCategories.Count); Assert.AreEqual(1, returnedCategories[0].Id); Assert.AreEqual(2, returnedCategories[1].Id); }
public async Task GetCategories_Saves_Data_To_Cache() { var cacheService = new MockCacheService { GetDataDelegate = s => { throw new FileNotFoundException(); }, SaveDataAsyncDelegate = (s, o) => { var collection = (Collection<Category>)o; Assert.AreEqual("Categories-0-0", s); Assert.AreEqual(2, collection.Count); Assert.AreEqual(1, collection[0].Id); Assert.AreEqual(2, collection[1].Id); return Task.FromResult(new Uri("http://test.org")); } }; var productCatalogService = new MockProductCatalogService(); var categories = new List<Category> { new Category{ Id = 1}, new Category{ Id = 2} }; productCatalogService.GetSubcategoriesAsyncDelegate = (parentId, maxProducts) => Task.FromResult<ICollection<Category>>(new Collection<Category>(categories)); var target = new ProductCatalogRepository(productCatalogService, cacheService); await target.GetRootCategoriesAsync(0); }