示例#1
0
        public RepositoryCachingTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
        {
#if NETFULL
            contextType = ContextProviderType.EntityFramework;
#else
            contextType = ContextProviderType.EntityFrameworkCore;
#endif
        }
示例#2
0
        protected void ForRepositoryFactoryWithAllCachingProviders(ContextProviderType contextProvider, Action <IRepositoryFactory, CachingProviderType> action)
        {
            CachingProviders().ToList().ForEach(cachingProvider =>
            {
                var builder = GetRepositoryOptionsBuilder(contextProvider);

                ApplyCachingProvider(cachingProvider, builder);

                action(new RepositoryFactory(builder.Options), cachingProvider);
            });
        }
示例#3
0
        protected void ForRepositoryFactoryWithAllCachingProvidersAsync(ContextProviderType contextProvider, Func <IRepositoryFactory, CachingProviderType, Task> action)
        => CachingProviders()
        .ToList()
        .ForEach(async cachingProvider =>
        {
            var builder = GetRepositoryOptionsBuilder(contextProvider);

            ApplyCachingProvider(cachingProvider, builder);

            await HandleExceptionAsync(() => action(
                                           new RepositoryFactory(builder.Options),
                                           cachingProvider));
        });
        protected RepositoryOptionsBuilder GetRepositoryOptionsBuilder(ContextProviderType provider)
        {
            var builder = new RepositoryOptionsBuilder();

            switch (provider)
            {
            case ContextProviderType.InMemory:
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
                break;
            }

            case ContextProviderType.EntityFrameworkCore:
            {
                builder.UseEntityFrameworkCore <TestEfCoreDbContext>(options =>
                    {
                        options
                        .UseInMemoryDatabase(Guid.NewGuid().ToString())
                        .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning));
                    });
                break;
            }

#if NETCORE
            case ContextProviderType.AzureStorageBlob:
            {
                builder.UseAzureStorageBlob(
                    connectionString: "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;",
                    createIfNotExists: true);
                break;
            }
#endif
#if NETFULL
            case ContextProviderType.EntityFramework:
            {
                builder.UseEntityFramework <TestEfDbContext>(Helpers.DbConnectionHelper.CreateConnection());
                break;
            }
#endif
            default:
                throw new ArgumentOutOfRangeException(nameof(provider));
            }

            builder.UseLoggerProvider(TestXUnitLoggerProvider);

            return(builder);
        }
示例#5
0
        protected void ForRepositoryFactoryWithAllCachingProvidersAsync(ContextProviderType contextProvider, Func <IRepositoryFactory, CachingProviderType, Task> action)
        {
            CachingProviders().ToList().ForEach(async cachingProvider =>
            {
                var builder = GetRepositoryOptionsBuilder(contextProvider);

                ApplyCachingProvider(cachingProvider, builder);

                // Perform test
                var task = Record.ExceptionAsync(() => action(new RepositoryFactory(builder.Options), cachingProvider));

                // Checks to see if we have any un-handled exception
                if (task != null)
                {
                    var ex = await task;

                    Assert.Null(ex);
                }
            });
        }
        protected IRepositoryOptions BuildOptions(ContextProviderType provider)
        {
            var builder = new RepositoryOptionsBuilder();

            switch (provider)
            {
            case ContextProviderType.InMemory:
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
                break;
            }

#if NETFULL
            case ContextProviderType.EntityFramework:
            {
                builder.UseEntityFramework <EfDbContext>(_connection);
                break;
            }
#else
            case ContextProviderType.EntityFrameworkCore:
            {
                builder.UseEntityFrameworkCore <EfCoreDbContext>(x => x.UseSqlServer(ConnectionString));
                break;
            }

            case ContextProviderType.AzureStorageBlob:
            {
                builder.UseAzureStorageBlob(
                    connectionString: "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;",
                    createIfNotExists: true);
                break;
            }
#endif
            default:
                throw new ArgumentOutOfRangeException(nameof(provider));
            }

            return(builder.Options);
        }
        protected IRepositoryOptions BuildOptions(ContextProviderType provider)
        {
            var builder = new RepositoryOptionsBuilder();

            switch (provider)
            {
            case ContextProviderType.InMemory:
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
                break;
            }

            case ContextProviderType.Json:
            {
                builder.UseJsonDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.Xml:
            {
                builder.UseJsonDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.AdoNet:
            {
                builder.UseAdoNet(_connection);
                break;
            }

            case ContextProviderType.NHibernate:
            {
                builder.UseNHibernate(cfg =>
                    {
                        cfg.DataBaseIntegration(x =>
                        {
                            x.Dialect <MsSql2012Dialect>();
                            x.Driver <SqlClientDriver>();
                            x.ConnectionString = ConnectionString;
                            x.LogSqlInConsole  = true;
                            x.LogFormattedSql  = true;
                        });

                        var mapper = new ModelMapper();

                        mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());

                        var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

                        cfg.AddMapping(mapping);

                        var exporter = new SchemaExport(cfg);

                        exporter.Execute(true, true, false);
                    });

                break;
            }

            case ContextProviderType.EntityFramework:
            {
                builder.UseEntityFramework <EfDbContext>(_connection);
                break;
            }

            case ContextProviderType.EntityFrameworkCore:
            {
                builder.UseEntityFrameworkCore <EfCoreDbContext>(x => x.UseSqlServer(ConnectionString));
                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(provider));
            }

            return(builder.Options);
        }
示例#8
0
 protected void ForRepositoryFactoryWithAllCachingProvidersAsync(ContextProviderType contextProvider, Func <IRepositoryFactory, Task> action)
 => ForRepositoryFactoryWithAllCachingProvidersAsync(contextProvider, (factory, type) => action(factory));
示例#9
0
 protected void ForRepositoryFactoryWithAllCachingProviders(ContextProviderType contextProvider, Action <IRepositoryFactory> action)
 => ForRepositoryFactoryWithAllCachingProviders(contextProvider, (factory, type) => action(factory));
示例#10
0
 protected IRepositoryOptions BuildOptions(ContextProviderType provider) => GetRepositoryOptionsBuilder(provider).Options;
示例#11
0
        protected RepositoryOptionsBuilder GetRepositoryOptionsBuilder(ContextProviderType provider)
        {
            var builder = new RepositoryOptionsBuilder();

            switch (provider)
            {
            case ContextProviderType.InMemory:
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
                break;
            }

            case ContextProviderType.Json:
            {
                builder.UseJsonDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.Xml:
            {
                builder.UseXmlDatabase(Path.GetTempPath() + Guid.NewGuid().ToString("N"));
                break;
            }

            case ContextProviderType.AdoNet:
            {
                builder.UseAdoNet(TestDbConnectionHelper.CreateConnection(), ensureDatabaseCreated: true);
                break;
            }

            case ContextProviderType.NHibernate:
            {
                builder.UseNHibernate(cfg =>
                    {
                        var currentFile      = TestPathHelper.GetTempFileName();
                        var connectionString = $"Data Source={currentFile};Persist Security Info=False";

                        cfg.DataBaseIntegration(x =>
                        {
                            x.Dialect <TestFixedMsSqlCe40Dialect>();
                            x.Driver <SqlServerCeDriver>();
                            x.ConnectionString = connectionString;
                            x.LogSqlInConsole  = true;
                            x.LogFormattedSql  = true;
                        });

                        var mapper = new ModelMapper();

                        mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());

                        var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

                        cfg.AddMapping(mapping);

                        var exporter = new SchemaExport(cfg);

                        exporter.Execute(true, true, false);
                    });

                break;
            }

            case ContextProviderType.EntityFramework:
            {
                builder.UseEntityFramework <TestEfDbContext>(TestDbConnectionHelper.CreateConnection());
                break;
            }

            case ContextProviderType.EntityFrameworkCore:
            {
                builder.UseEntityFrameworkCore <TestEfCoreDbContext>(options =>
                    {
                        options
                        .UseInMemoryDatabase(Guid.NewGuid().ToString())
                        .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning));
                    });
                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(provider));
            }

            builder.UseLoggerProvider(TestXUnitLoggerProvider);

            return(builder);
        }
示例#12
0
 private static bool SupportsTransactions(ContextProviderType x)
 {
     return(SqlServerContextProviders().Contains(x));
 }
示例#13
0
        private static void TestExistWithNavigationProperty_OneToOneRelationship(IRepositoryFactory repoFactory, ContextProviderType providerType)
        {
            var addressRepo  = repoFactory.Create <CustomerAddress>();
            var customerRepo = repoFactory.Create <Customer>();

            var queryOptions = new QueryOptions <Customer>();

            var entity = new Customer
            {
                Name = "Random Name"
            };

            customerRepo.Add(entity);

            var address = new CustomerAddress
            {
                Street     = "Street",
                City       = "New City",
                State      = "ST",
                CustomerId = entity.Id
            };

            // The customer is required here, otherwise it will throw an exception for entity framework
            if (providerType == ContextProviderType.EntityFramework)
            {
                address.Customer = entity;
            }

            Assert.False(customerRepo.Exists(queryOptions.WithFilter(x => x.Id == entity.Id && (x.Address != null && x.Address.Street.Equals(address.Street)))));
            Assert.False(customerRepo.Exists(x => x.Id == entity.Id && (x.Address != null && x.Address.Street.Equals(address.Street))));

            addressRepo.Add(address);

            // for one to one, the navigation properties will be included automatically (no need to fetch)
            Assert.True(customerRepo.Exists(queryOptions.WithFilter(x => x.Id == entity.Id && (x.Address != null && x.Address.Street.Equals(address.Street)))));
            Assert.True(customerRepo.Exists(x => x.Id == entity.Id && (x.Address != null && x.Address.Street.Equals(address.Street))));
        }
 protected virtual void AfterTest(ContextProviderType contextProvider)
 {
 }
 protected virtual void BeforeTest(ContextProviderType contextProvider)
 {
 }