Ejemplo n.º 1
0
        // It's internal because `Umbraco.Core.Persistence.LocalDb` is internal
        internal LocalDbTestDatabase(TestDatabaseSettings settings, ILoggerFactory loggerFactory, LocalDb localDb, string filesPath, IUmbracoDatabaseFactory dbFactory)
        {
            _loggerFactory   = loggerFactory;
            _databaseFactory = dbFactory;

            _settings   = settings;
            _localDb    = localDb;
            s_filesPath = filesPath;

            Instance = this; // For GlobalSetupTeardown.cs

            var counter = 0;

            var schema = Enumerable.Range(0, _settings.SchemaDatabaseCount)
                         .Select(x => TestDbMeta.CreateWithoutConnectionString($"{DatabaseName}-{++counter}", false));

            var empty = Enumerable.Range(0, _settings.EmptyDatabasesCount)
                        .Select(x => TestDbMeta.CreateWithoutConnectionString($"{DatabaseName}-{++counter}", true));

            _testDatabases = schema.Concat(empty).ToList();

            s_localDbInstance = _localDb.GetInstance(InstanceName);
            if (s_localDbInstance != null)
            {
                return;
            }

            if (_localDb.CreateInstance(InstanceName) == false)
            {
                throw new Exception("Failed to create a LocalDb instance.");
            }

            s_localDbInstance = _localDb.GetInstance(InstanceName);
        }
Ejemplo n.º 2
0
        public static ITestDatabase Create(TestDatabaseSettings settings, string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
        {
            string connectionString = Environment.GetEnvironmentVariable("UmbracoIntegrationTestConnectionString");

            return(string.IsNullOrEmpty(connectionString)
                ? CreateLocalDb(settings, filesPath, loggerFactory, dbFactory)
                : CreateSqlDeveloper(settings, loggerFactory, dbFactory, connectionString));
        }
Ejemplo n.º 3
0
        private static ITestDatabase CreateSqlDeveloper(TestDatabaseSettings settings, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory, string connectionString)
        {
            // NOTE: Example setup for Linux box.
            // $ export SA_PASSWORD=Foobar123!
            // $ export UmbracoIntegrationTestConnectionString="Server=localhost,1433;User Id=sa;Password=$SA_PASSWORD;"
            // $ docker run -e 'ACCEPT_EULA=Y' -e "SA_PASSWORD=$SA_PASSWORD" -e 'MSSQL_PID=Developer' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest-ubuntu
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new InvalidOperationException("ENV: UmbracoIntegrationTestConnectionString is not set");
            }

            return(new SqlDeveloperTestDatabase(settings, loggerFactory, dbFactory.Create(), connectionString));
        }
Ejemplo n.º 4
0
        private static ITestDatabase CreateLocalDb(TestDatabaseSettings settings, string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
        {
            if (!Directory.Exists(filesPath))
            {
                Directory.CreateDirectory(filesPath);
            }

            var localDb = new LocalDb();

            if (!localDb.IsAvailable)
            {
                throw new InvalidOperationException("LocalDB is not available.");
            }

            return(new LocalDbTestDatabase(settings, loggerFactory, localDb, filesPath, dbFactory.Create()));
        }
        public SqlServerTestDatabase(TestDatabaseSettings settings, ILoggerFactory loggerFactory,
                                     IUmbracoDatabaseFactory databaseFactory)
        {
            _loggerFactory   = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
            _databaseFactory = databaseFactory ?? throw new ArgumentNullException(nameof(databaseFactory));

            _settings = settings;

            var counter = 0;

            var schema = Enumerable.Range(0, _settings.SchemaDatabaseCount)
                         .Select(x => TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-{++counter}", false,
                                                                                  _settings.SQLServerMasterConnectionString));

            var empty = Enumerable.Range(0, _settings.EmptyDatabasesCount)
                        .Select(x => TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-{++counter}", true,
                                                                                 _settings.SQLServerMasterConnectionString));

            _testDatabases = schema.Concat(empty).ToList();
        }
Ejemplo n.º 6
0
        public SqlDeveloperTestDatabase(TestDatabaseSettings settings, ILoggerFactory loggerFactory, IUmbracoDatabaseFactory databaseFactory, string masterConnectionString)
        {
            _loggerFactory   = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
            _databaseFactory = databaseFactory ?? throw new ArgumentNullException(nameof(databaseFactory));

            _settings = settings;
            _masterConnectionString = masterConnectionString;

            var counter = 0;

            var schema = Enumerable.Range(0, _settings.SchemaDatabaseCount)
                         .Select(x => TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-{++counter}", false, masterConnectionString));

            var empty = Enumerable.Range(0, _settings.EmptyDatabasesCount)
                        .Select(x => TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-{++counter}", true, masterConnectionString));

            _testDatabases = schema.Concat(empty).ToList();

            Instance = this; // For GlobalSetupTeardown.cs
        }
        /// <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);
            }
        }
 /// <summary>
 /// Creates a TestDatabase instance
 /// </summary>
 /// <remarks>
 /// SQL Server setup requires configured master connection string &amp; privileges to create database.
 /// </remarks>
 /// <example>
 /// <code>
 /// # SQL Server Environment variable setup
 /// $ export Tests__Database__DatabaseType="SqlServer"
 /// $ export Tests__Database__SQLServerMasterConnectionString="Server=localhost,1433; User Id=sa; Password=MySuperSecretPassword123!;"
 /// </code>
 /// </example>
 /// <example>
 /// <code>
 /// # Docker cheat sheet
 /// $ docker run -e 'ACCEPT_EULA=Y' -e "SA_PASSWORD=MySuperSecretPassword123!" -e 'MSSQL_PID=Developer' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest-ubuntu
 /// </code>
 /// </example>
 public static ITestDatabase Create(TestDatabaseSettings settings, TestUmbracoDatabaseFactoryProvider dbFactory, ILoggerFactory loggerFactory) =>
     settings.DatabaseType switch
     {