public DatabaseFixture(IMessageSink messageSink)
        {
            var options       = new SqlServerDockerDatabaseOptions(dockerSqlServerHostPort: 1455);
            var logger        = new TestLogger(m => messageSink.OnMessage(new DiagnosticMessage(m)));
            var loggerFactory = new TestLoggerFactory(logger);
            var sqlServerDockerDatabaseLogger = loggerFactory.CreateLogger <SqlServerDockerDatabase>();

            _databaseServer = new SqlServerDockerDatabase(options, sqlServerDockerDatabaseLogger);
        }
Esempio n. 2
0
        public BaseServerTest()
        {
            // Set up in-memory database
            _effortDatabaseStrategy = CreateTestStrategy();
            _context = _effortDatabaseStrategy.CreateContext();

            // Set up in-memory web server
            _server = TestServer.Create(builder => new TestStartup(_context)
                                        .Configuration(builder));
        }
Esempio n. 3
0
        public DatabaseFixture()
        {
            var connectionString = "User ID=postgres;Password=4310;Server=localhost;Database=cSharpTest;";

            testDatabase = new TestDatabaseBuilder().WithConnectionString(connectionString).Build();
            testDatabase.Create();
            //testDatabase.RunScripts("./DatabaseScripts");
            var builder = new DbContextOptionsBuilder <cSharpContext>();

            builder.UseNpgsql(testDatabase.ConnectionString);
            DbContext = new cSharpContext(builder.Options);
            DbContext.Database.EnsureCreated();
        }
Esempio n. 4
0
        public static IntegrationContext CreateAndGetContext()
        {
            var connectionString = "host=localhost;port=5432;Username=postgres;Password=048365;Database=Test";

            testDatabase = new TestDatabaseBuilder().WithConnectionString(connectionString).Build();

            testDatabase.Create();

            var builder = new DbContextOptionsBuilder <IntegrationContext>();

            builder.UseNpgsql(testDatabase.ConnectionString);

            var context = new IntegrationContext(builder.Options);

            context.Database.EnsureCreated();

            return(context);
        }
        public async Task InitializeAsync()
        {
            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.Test.json")
                                .Build();

            // Remember to create integration_test_user in PostgreSQL. User need to be able to create DB
            // Get into docker container: docker exec -it {ContainerID} psql -U {adminUser} postgres
            //  e.g: CREATE USER yourUsername WITH PASSWORD 'yourPassword' CREATEDB;
            var connectionString = configuration["ConnectionStrings:MyPostgreSQLConnection"];

            tempDatabase = new TestDatabaseBuilder()
                           .WithConnectionString(connectionString)
                           .Build();
            tempDatabase.Create();

            if (tempDatabase != null)
            {
                configuration["ConnectionStrings:MyPostgreSQLConnection"] = tempDatabase.ConnectionString.ToString();
            }

            Host = await new HostBuilder()
                   .ConfigureWebHost(webBuilder =>
            {
                webBuilder
                .UseEnvironment(Environments.Staging)
                .UseStartup <Startup>()
                .UseTestServer()
                .UseConfiguration(configuration);
            })
                   .StartAsync();

            using (var scope = Host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var context  = services.GetService <ApplicationDbContext>();
                if (tempDatabase != null)
                {
                    context.Database.EnsureCreated();
                }
            }
        }
        /// <summary>
        /// Get or create an instance of <see cref="ITestDatabase"/>
        /// </summary>
        /// <remarks>
        /// There must only be ONE instance shared between all tests in a session
        /// </remarks>
        private static ITestDatabase GetOrCreateDatabase(string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
        {
            lock (s_dbLocker)
            {
                if (s_dbInstance != null)
                {
                    return(s_dbInstance);
                }

                // TODO: pull from IConfiguration
                var settings = new TestDatabaseSettings
                {
                    PrepareThreadCount  = 4,
                    EmptyDatabasesCount = 2,
                    SchemaDatabaseCount = 4
                };

                s_dbInstance = TestDatabaseFactory.Create(settings, filesPath, loggerFactory, dbFactory);

                return(s_dbInstance);
            }
        }
Esempio n. 7
0
        public DatabaseFixture()
        {
            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.Test.json")
                                .Build();

            // Remember to create integration_test_user in PostgreSQL. User need to be able to create DB
            //  e.g: CREATE USER yourUsername WITH PASSWORD 'yourPassword' CREATEDB;
            var connectionString = configuration["ConnectionStrings:MyPostgreSQLConnection"];

            tempDatabase = new TestDatabaseBuilder()
                           .WithConnectionString(connectionString)
                           .Build();

            tempDatabase.Create();

            var builder = new DbContextOptionsBuilder <ApplicationDbContext>();

            builder.UseNpgsql(tempDatabase.ConnectionString);
            DbContext = new ApplicationDbContext(builder.Options);
            DbContext.Database.EnsureCreated();
        }
Esempio n. 8
0
        public TestFixture()
        {
            var config = new ConfigurationBuilder()
                         .AddJsonFile("appsettings.json")
                         .Build();

            _database = new DacpacTestDatabase(config.GetValue <string>("ConnectionStrings:DBConnection"))
                        .ConfigureOptions(option =>
            {
                var dacpacPath = Path.GetFullPath(
                    @"..\..\..\..\..\WeatherApp\DB\bin\Debug\db.dacpac");
                option.DacpacPath = dacpacPath;
#if DEBUG
                option.AlwayCreate = false;
                option.AlwayDrop   = false;
#else
                option.AlwayCreate = false;
                option.AlwayDrop   = true;
#endif
            })
                        .Build()
                        .RunScriptFile(@"Scripts\clean-all-stations.sql")
                        .RunScriptFile(@"Scripts\add-ap-stations.sql")
                        .RunScriptFile(@"Scripts\add-ka-stations.sql")
                        .RunScriptFile(@"Scripts\add-ks-stations.sql")
                        .RunScriptFile(@"Scripts\add-tn-stations.sql")
                        .RunScriptFile(@"Scripts\add-ts-stations.sql");

            var builder = new WebHostBuilder()
                          .UseConfiguration(config)
                          .UseStartup <Startup>();

            _server = new TestServer(builder);

            Client             = _server.CreateClient();
            Client.BaseAddress = new Uri("http://local.sandbox");
        }
Esempio n. 9
0
 /// <summary>
 /// Получить базовый сервис получения данных из базы
 /// </summary>
 public static ITestDatabaseService GetTestDatabaseService(ITestDatabase testDatabase, ITestTable testTable,
                                                           ITestDatabaseValidateService testDatabaseValidateService,
                                                           ITestEntityConverter testConverter) =>
 new TestDatabaseService(testDatabase, testTable, testDatabaseValidateService, testConverter);
        /// <summary>
        /// Creates a LocalDb instance to use for the test
        /// </summary>
        private void SetupTestDatabase(
            TestUmbracoDatabaseFactoryProvider testUmbracoDatabaseFactoryProvider,
            IUmbracoDatabaseFactory databaseFactory,
            ILoggerFactory loggerFactory,
            IRuntimeState runtimeState,
            string workingDirectory)
        {
            if (TestOptions.Database == UmbracoTestOptions.Database.None)
            {
                return;
            }

            // need to manually register this factory
            DbProviderFactories.RegisterFactory(Constants.DbProviderNames.SqlServer, SqlClientFactory.Instance);

            string dbFilePath = Path.Combine(workingDirectory, "LocalDb");

            ITestDatabase db = GetOrCreateDatabase(dbFilePath, loggerFactory, testUmbracoDatabaseFactoryProvider);

            switch (TestOptions.Database)
            {
            case UmbracoTestOptions.Database.NewSchemaPerTest:

                // New DB + Schema
                TestDbMeta newSchemaDbMeta = db.AttachSchema();

                // Add teardown callback
                OnTestTearDown(() => db.Detach(newSchemaDbMeta));

                ConfigureTestDatabaseFactory(newSchemaDbMeta, databaseFactory, runtimeState);

                Assert.AreEqual(RuntimeLevel.Run, runtimeState.Level);

                break;

            case UmbracoTestOptions.Database.NewEmptyPerTest:
                TestDbMeta newEmptyDbMeta = db.AttachEmpty();

                // Add teardown callback
                OnTestTearDown(() => db.Detach(newEmptyDbMeta));

                ConfigureTestDatabaseFactory(newEmptyDbMeta, databaseFactory, runtimeState);

                Assert.AreEqual(RuntimeLevel.Install, runtimeState.Level);

                break;

            case UmbracoTestOptions.Database.NewSchemaPerFixture:
                // Only attach schema once per fixture
                // Doing it more than once will block the process since the old db hasn't been detached
                // and it would be the same as NewSchemaPerTest even if it didn't block
                if (_firstTestInFixture)
                {
                    // New DB + Schema
                    TestDbMeta newSchemaFixtureDbMeta = db.AttachSchema();
                    s_fixtureDbMeta = newSchemaFixtureDbMeta;

                    // Add teardown callback
                    OnFixtureTearDown(() => db.Detach(newSchemaFixtureDbMeta));
                }

                ConfigureTestDatabaseFactory(s_fixtureDbMeta, databaseFactory, runtimeState);

                break;

            case UmbracoTestOptions.Database.NewEmptyPerFixture:
                // Only attach schema once per fixture
                // Doing it more than once will block the process since the old db hasn't been detached
                // and it would be the same as NewSchemaPerTest even if it didn't block
                if (_firstTestInFixture)
                {
                    // New DB + Schema
                    TestDbMeta newEmptyFixtureDbMeta = db.AttachEmpty();
                    s_fixtureDbMeta = newEmptyFixtureDbMeta;

                    // Add teardown callback
                    OnFixtureTearDown(() => db.Detach(newEmptyFixtureDbMeta));
                }

                ConfigureTestDatabaseFactory(s_fixtureDbMeta, databaseFactory, runtimeState);

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(TestOptions), TestOptions, null);
            }
        }