public async Task GetOrSet_CorrectlyRetrievesDataWithoutReCaching() { // arrange var fixture = new Fixture(); var cacheKey = fixture.Create <string>(); var expected = fixture.Create <SomeData>(); var setter = A.Fake <Func <Task <SomeData> > >(); A.CallTo(() => setter()).Returns(Task.FromResult(expected)); var sut = new InMemoryCachingProvider(); await sut.GetOrSet(cacheKey, () => Task.FromResult(expected)) .ConfigureAwait(true); // act var actual = await sut.GetOrSet(cacheKey, setter) .ConfigureAwait(true); // assert actual.Should().BeEquivalentTo(expected); A.CallTo(() => setter()).MustNotHaveHappened(); }
public MemoryCachingProviderTest() { var cache = new MemoryCache(new MemoryCacheOptions()); _provider = new InMemoryCachingProvider(cache); _defaultTs = TimeSpan.FromSeconds(30); }
public static IEnumerable <TestCaseData> Build(RepositoryType[] includeType) { if (includeType.Contains(RepositoryType.InMemory)) { yield return(new TestCaseData(new InMemoryRepository <User, string, int>()).SetName("InMemoryRepository Test")); } if (includeType.Contains(RepositoryType.Ef)) { var dbPath = EfDataDirectoryFactory.Build(); yield return(new TestCaseData(new EfRepository <User, string, int>(new TestObjectContext("Data Source=" + dbPath))).SetName("EfRepository Test")); } if (includeType.Contains(RepositoryType.EfCore)) { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; // Create the schema in the database var context = new TestObjectContextCore(options); context.Database.EnsureCreated(); yield return(new TestCaseData(new EfCoreRepository <User, string, int>(context)).SetName("EfCoreRepository Test")); } if (includeType.Contains(RepositoryType.Cache)) { var cachingProvider = new InMemoryCachingProvider(new MemoryCache(new MemoryCacheOptions())); yield return(new TestCaseData(new CacheRepository <User, string, int>(CachePrefixFactory.Build(), cachingProvider)).SetName("CacheRepository Test")); } }
public void Setup() { Cache = new MemoryCache(new MemoryCacheOptions()); var cacheProvider = new InMemoryCachingProvider(Cache); CachingStrategy = new StandardCachingStrategyWithPartition <Contact, int, int>(cacheProvider, c => c.ContactTypeId) { CachePrefix = "#RepoStandardCacheWithPartition" }; }
public void Setup() { // need to clear out the InMemory cache before each test is run so that each is independent and won't effect the next one var provider = new InMemoryCachingProvider(new MemoryCache(new MemoryCacheOptions())); CachingStrategy = new StandardCachingStrategy <Contact, int>(provider) { CachePrefix = "#RepoStandardCache" }; }
public QueryManagerTests() { // need to clear out the InMemory cache before each test is run so that each is independent and won't effect the next one cache = new MemoryCache(new MemoryCacheOptions()); var provider = new InMemoryCachingProvider(cache); QueryManager = new QueryManager <Contact, int>(new StandardCachingStrategy <Contact, int>(provider) { CachePrefix = "#RepoStandardCache" }); }
public void Using_DisableCaching_Should_Disable_Cache_Inside_Using_Block() { var cacheProvider = new InMemoryCachingProvider(new MemoryCache(new MemoryCacheOptions())); var repos = new InMemoryRepository <Contact>(new StandardCachingStrategy <Contact>(cacheProvider)); repos.CachingEnabled.ShouldBeTrue(); using (repos.DisableCaching()) { repos.CachingEnabled.ShouldBeFalse(); } repos.CachingEnabled.ShouldBeTrue(); }
public StationScenariosSteps() { var options = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; this.stationContext = new Context(options); var cacheProvider = new InMemoryCachingProvider(); var repository = new StationRepository(this.stationContext); var cachingRepository = new StationRepositoryCachingDecorator(repository, cacheProvider); var service = new StationService.StationService(cachingRepository); var cachingService = new StationServiceCachingDecorator(service, cacheProvider); this.stationService = cachingService; }
public void TryGetResult_Should_Get_Value_If_Reinstantiated() { var contact = new Contact() { ContactId = 1, Name = "Test User" }; CachingStrategy.SaveGetResult(1, contact); var cacheProvider = new InMemoryCachingProvider(Cache); var localCachingStrategy = new StandardCachingStrategyWithPartition <Contact, int, int>(cacheProvider, c => c.ContactTypeId) { CachePrefix = "#RepoStandardCacheWithPartition" }; localCachingStrategy.TryGetResult(1, out Contact result).ShouldBe(true); result.ContactId.ShouldBe(contact.ContactId); result.Name.ShouldBe(contact.Name); }
public static IEnumerable <TestCaseData> Build(RepositoryType[] includeType, string testName = "Test") { if (includeType.Contains(RepositoryType.InMemory)) { yield return(new TestCaseData(new InMemoryRepository <Contact, string>()).SetName("InMemoryRepository " + testName)); } if (includeType.Contains(RepositoryType.Xml)) { var xmlDataDirectoryPath = XmlDataDirectoryFactory.Build("Contact"); yield return (new TestCaseData(new XmlRepository <Contact, string>(xmlDataDirectoryPath)).SetName("XmlRepository" + testName)); } if (includeType.Contains(RepositoryType.Ef)) { var dbPath = EfDataDirectoryFactory.Build(); yield return (new TestCaseData(new EfRepository <Contact, string>(new TestObjectContext("Data Source=" + dbPath))).SetName("EfRepository" + testName)); } if (includeType.Contains(RepositoryType.EfCore)) { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; // Create the schema in the database var context = new TestObjectContextCore(options); context.Database.EnsureCreated(); yield return(new TestCaseData(new EfCoreRepository <Contact, string>(context)).SetName("EfCoreRepository " + testName)); } if (includeType.Contains(RepositoryType.Dbo4)) { var dbPath = Db4oDataDirectoryFactory.Build("Contact"); yield return(new TestCaseData(new Db4oRepository <Contact, string>(dbPath)).SetName("Db4oRepository " + testName)); } if (includeType.Contains(RepositoryType.MongoDb)) { string connectionString = MongoDbConnectionStringFactory.Build("Contact"); if (MongoDbRepositoryManager.ServerIsRunning(connectionString)) { MongoDbRepositoryManager.DropDatabase(connectionString); // Pre-test cleanup yield return(new TestCaseData(new MongoDbRepository <Contact, string>(connectionString)).SetName("MongoDb " + testName)); } } if (includeType.Contains(RepositoryType.RavenDb)) { var documentStore = new EmbeddableDocumentStore { RunInMemory = true, DataDirectory = "~\\Data\\RavenDb" }; if (IntPtr.Size == 4) { documentStore.Configuration.Storage.Voron.AllowOn32Bits = true; } IDocumentStore x = new EmbeddableDocumentStore(); yield return(new TestCaseData(new RavenDbRepository <Contact, string>(documentStore: documentStore)).SetName("RavenDbRepository " + testName)); } if (includeType.Contains(RepositoryType.Cache)) { var cachingProvider = new InMemoryCachingProvider(new MemoryCache(new MemoryCacheOptions())); yield return(new TestCaseData(new CacheRepository <Contact, string>(CachePrefixFactory.Build(), cachingProvider)).SetName("CacheRepository " + testName)); } if (includeType.Contains(RepositoryType.CouchDb)) { if (CouchDbRepositoryManager.ServerIsRunning(CouchDbUrl.Host, CouchDbUrl.Port)) { var databaseName = CouchDbDatabaseNameFactory.Build("Contact"); CouchDbRepositoryManager.DropDatabase(CouchDbUrl.Host, CouchDbUrl.Port, databaseName); CouchDbRepositoryManager.CreateDatabase(CouchDbUrl.Host, CouchDbUrl.Port, databaseName); yield return(new TestCaseData(new CouchDbRepository <Contact, string>(CouchDbUrl.Host, CouchDbUrl.Port, databaseName)).SetName("CouchDbRepository " + testName)); } } }