private static string GetConnectionString(ITestOutputHelper outputHelper)
        {
            var options          = new SqlServerDockerDatabaseOptions(dockerSqlServerHostPort: 1458);
            var logger           = new TestLoggerForSqlServerDockerDatabase(outputHelper);
            var testDatabase     = new SqlServerDockerDatabase(options, logger);
            var connectionString = testDatabase.GetConnectionString("Items");

            return(connectionString);
        }
コード例 #2
0
        public async Task ContainerLifecyle()
        {
            const string containerName = "ContainerLifecyle";

            using (var testDatabaseThatDoesntStop = new SqlServerDockerDatabase(
                       new SqlServerDockerDatabaseOptions(stopDockerInstanceOnDispose: false, dockerSqlServerHostPort: 1457,
                                                          dockerContainerName: containerName), _logger))
            {
                await _dockerClient.ShouldReportRunningContainerNamedAsync(containerName);

                await using var connection = new SqlConnection(testDatabaseThatDoesntStop.GetConnectionString());
                await connection.OpenAsync();

                await connection.ShouldReportVersionAsync("Microsoft SQL Server 2019");
            }

            var firstContainersId = await _dockerClient.ShouldReportRunningContainerNamedAsync(containerName);

            using (var testDatabaseThatDoesntRemove = new SqlServerDockerDatabase(
                       new SqlServerDockerDatabaseOptions(removeDockerContainerOnDispose: false, dockerSqlServerHostPort: 1457,
                                                          dockerContainerName: containerName), _logger))
            {
                var secondContainersId = await _dockerClient.ShouldReportRunningContainerNamedAsync(containerName);

                firstContainersId.Should().NotBe(secondContainersId, "because a new container should be created each time");

                await using var connection = new SqlConnection(testDatabaseThatDoesntRemove.GetConnectionString());
                await connection.OpenAsync();

                await connection.ShouldReportVersionAsync("Microsoft SQL Server 2019");
            }

            await _dockerClient.ShouldReportStoppedContainerNamedAsync(containerName);

            using (var testDatabaseThatRemoves = new SqlServerDockerDatabase(
                       new SqlServerDockerDatabaseOptions(dockerSqlServerHostPort: 1457,
                                                          dockerContainerName: containerName), _logger))
            {
                await _dockerClient.ShouldReportRunningContainerNamedAsync(containerName);

                await using var connection = new SqlConnection(testDatabaseThatRemoves.GetConnectionString());
                await connection.OpenAsync();

                await connection.ShouldReportVersionAsync("Microsoft SQL Server 2019");
            }

            await _dockerClient.ShouldReportNoContainerNamedAsync(containerName);
        }
コード例 #3
0
        public void OneTimeSetup()
        {
            var logger = new TestLogger(m => TestContext.Out.WriteLine(m));

            _loggerFactory = new TestLoggerFactory(logger);

            var dockerSqlServerDatabaseLogger = _loggerFactory.CreateLogger <SqlServerDockerDatabase>();
            var testDatabase     = new SqlServerDockerDatabase(new SqlServerDockerDatabaseOptions(dockerSqlServerHostPort: 1456, stopDockerInstanceOnDispose: false), dockerSqlServerDatabaseLogger);
            var connectionString = testDatabase.GetConnectionString("Blogging");

            _dbContextOptionsWithSqlServer = new DbContextOptionsBuilder <BloggingContext>()
                                             .UseSqlServer(connectionString)
                                             .UseLoggerFactory(_loggerFactory)
                                             .Options;

            var dbContext = new BloggingContext(_dbContextOptionsWithSqlServer);

            PrePopulateDatabase(dbContext);
        }