public void Delete_Loop_With_Cache_And_Ef() { var cachingStrategy = new StandardCachingStrategy <Contact, string>(cacheProvider); var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; var context = new TestObjectContextCore(options); context.Database.EnsureCreated(); var repository = new EfCoreRepository <Contact, string>(context, cachingStrategy); repository.Add(new Contact() { ContactId = "1", Name = "Contact1", ContactTypeId = 1 }); repository.Add(new Contact() { ContactId = "2", Name = "Contact2", ContactTypeId = 2 }); repository.Add(new Contact() { ContactId = "3", Name = "Contact3", ContactTypeId = 2 }); repository.FindAll(x => x.ContactTypeId == 2); repository = new EfCoreRepository <Contact, string>(new TestObjectContextCore(options), cachingStrategy); repository.Delete(x => x.ContactTypeId == 2); }
public void EfCoreRepositoryFromConfigurationObject() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; var dbContext = new TestObjectContextCore(options); var config = new SharpRepositoryConfiguration(); var coreRepoconfig = new EfCoreRepositoryConfiguration("default", dbContext); coreRepoconfig.Attributes.Add("dbContextType", "SharpRepository.Tests.TestObjects.TestObjectContextCore, SharpRepository.Tests"); config.AddRepository(coreRepoconfig); var repos = RepositoryFactory.GetInstance <Contact, string>(config); if (!(repos is EfCoreRepository <Contact, string>)) { throw new Exception("Not InMemoryRepository"); } if (!(repos.CachingStrategy is NoCachingStrategy <Contact, string>)) { throw new Exception("not NoCachingStrategy"); } }
public void Delete_With_Cache_And_Ef() { var cachingStrategy = new StandardCachingStrategy <Contact, string>(cacheProvider); var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; var context = new TestObjectContextCore(options); context.Database.EnsureCreated(); var repository = new EfCoreRepository <Contact, string>(context, cachingStrategy); repository.Add(new Contact() { ContactId = "1", Name = "Contact1" }); repository = new EfCoreRepository <Contact, string>(context, cachingStrategy); repository.Get("1"); repository.CacheUsed.ShouldBeTrue(); repository.Delete("1"); }
public void EfCoreRepositoryNeedsDbContextType() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; var dbContext = new TestObjectContextCore(options); var config = new SharpRepositoryConfiguration(); var coreRepoconfig = new EfCoreRepositoryConfiguration("default", dbContext); config.AddRepository(coreRepoconfig); try { var repos = RepositoryFactory.GetInstance <Contact, string>(config); } catch (Exception e) { e.Message.ShouldBe("The DbContextOptions passed to the DbContext constructor must be a DbContextOptions<DbContext>. When registering multiple DbContext types make sure that the constructor for each context type has a DbContextOptions<TContext> parameter rather than a non-generic DbContextOptions parameter."); } }
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 EfCorePrimaryKeyTests() { /* var connection = new SqliteConnection("DataSource=:memory:"); * connection.Open();*/ var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseInMemoryDatabase("memory") .Options; // Run the test against one instance of the contextFactory context = new TestObjectContextCore(options); }
public void Setup() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json") .Build(); var sectionName = "sharpRepository"; IConfigurationSection sharpRepoSection = config.GetSection(sectionName); if (sharpRepoSection == null) { throw new ConfigurationErrorsException("Section " + sectionName + " is not found."); } var sharpRepoConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(sharpRepoSection); sharpRepoConfig.DefaultRepository = "efCoreRepos"; var memoryCache = new MemoryCache(new MemoryCacheOptions()); var dbContext = new TestObjectContextCore(options); // structure map container = new Container(x => { x.Scan(_ => { _.TheCallingAssembly(); _.WithDefaultConventions(); }); x.For <DbContext>() .Use(dbContext); x.For <TestObjectContextCore>() .Use(dbContext); x.For <IMemoryCache>().Use(memoryCache); x.ForRepositoriesUseSharpRepository(sharpRepoConfig); }); RepositoryDependencyResolver.SetDependencyResolver(new StructureMapProvider(container)); }
public void Setup() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; // Run the test against one instance of the context context = new TestObjectContextCore(options); context.Database.EnsureCreated(); }
public void Join_GetAll_Should_Return_All_Items_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(); var efCoreRepository = new EfCoreRepository <User, string, int>(context); Join_GetAll_Should_Return_All_Items(efCoreRepository); }
public void SetupRepository() { var dbPath = EfDataDirectoryFactory.Build(); var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; using (dbContext = new TestObjectContextCore(options)) { dbContext.Database.EnsureCreated(); const int totalItems = 5; for (int i = 1; i <= totalItems; i++) { dbContext.Contacts.Add( new Contact { ContactId = i.ToString(), Name = "Test User " + i, EmailAddresses = new List <EmailAddress> { new EmailAddress { ContactId = i.ToString(), EmailAddressId = i, Email = "omar.piani." + i.ToString() + "@email.com", Label = "omar.piani." + i.ToString() } } }); } dbContext.SaveChanges(); } // reistantiate in order to lose caches dbContext = new TestObjectContextCore(options); }
public void LoadEfRepositoryAndCachingFromConfigurationObject() { var config = new SharpRepositoryConfiguration(); var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; var dbContext = new TestObjectContextCore(options); config.AddRepository(new InMemoryRepositoryConfiguration("inMemory", "timeout")); var coreRepoconfig = new EfCoreRepositoryConfiguration("efCore", dbContext, "standard", "inMemoryProvider"); coreRepoconfig.Attributes.Add("dbContextType", "SharpRepository.Tests.TestObjects.TestObjectContextCore, SharpRepository.Tests"); config.AddRepository(coreRepoconfig); config.DefaultRepository = "efCore"; config.AddCachingStrategy(new StandardCachingStrategyConfiguration("standard")); config.AddCachingStrategy(new TimeoutCachingStrategyConfiguration("timeout", 200)); config.AddCachingStrategy(new NoCachingStrategyConfiguration("none")); config.AddCachingProvider(new InMemoryCachingProviderConfiguration("inMemoryProvider", new MemoryCache(new MemoryCacheOptions()))); var repos = RepositoryFactory.GetInstance <Contact, string>(config); if (!(repos is EfCoreRepository <Contact, string>)) { throw new Exception("Not InMemoryRepository"); } if (!(repos.CachingStrategy is StandardCachingStrategy <Contact, string>)) { throw new Exception("not StandardCachingStrategy"); } }
public void Dispose() { context.Dispose(); context = null; }
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)); } } }
public void TearDown() { context.Dispose(); context = null; }