Beispiel #1
0
 public void Throws_PlatformNotSupportedException()
 {
     // Arrange
     using (var actual = new SqlLocalDbApi(_loggerFactory))
     {
         // Act and Assert
         Assert.Throws <PlatformNotSupportedException>(() => actual.CreateInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.DeleteInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.DeleteUserInstances());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetDefaultInstance());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstanceInfo("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstanceNames());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstances());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetOrCreateInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetVersionInfo("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.InstanceExists("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.LatestVersion);
         Assert.Throws <PlatformNotSupportedException>(() => actual.ShareInstance("name", "sharedName"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.ShareInstance("sid", "name", "sharedName"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StartInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StartTracing());
         Assert.Throws <PlatformNotSupportedException>(() => actual.StopInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StopTracing());
         Assert.Throws <PlatformNotSupportedException>(() => actual.UnshareInstance("name"));
     }
 }
Beispiel #2
0
        public void Throws_InvalidOperationException_If_SQL_LocalDB_Not_Installed()
        {
            // Arrange
            var options  = new SqlLocalDbOptions();
            var registry = Mock.Of <Interop.IRegistry>();

            using (var actual = new SqlLocalDbApi(options, registry, _loggerFactory))
            {
                // Act and Assert
                Assert.Throws <InvalidOperationException>(() => actual.CreateInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.DeleteInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.DeleteUserInstances());
                Assert.Throws <InvalidOperationException>(() => actual.GetDefaultInstance());
                Assert.Throws <InvalidOperationException>(() => actual.GetInstanceInfo("name"));
                Assert.Throws <InvalidOperationException>(() => actual.GetInstanceNames());
                Assert.Throws <InvalidOperationException>(() => actual.GetInstances());
                Assert.Throws <InvalidOperationException>(() => actual.GetOrCreateInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.GetVersionInfo("name"));
                Assert.Throws <InvalidOperationException>(() => actual.InstanceExists("name"));
                Assert.Throws <InvalidOperationException>(() => actual.LatestVersion);
                Assert.Throws <InvalidOperationException>(() => actual.ShareInstance("name", "sharedName"));
                Assert.Throws <InvalidOperationException>(() => actual.StartInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.StartTracing());
                Assert.Throws <InvalidOperationException>(() => actual.StopInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.StopTracing());
                Assert.Throws <InvalidOperationException>(() => actual.UnshareInstance("name"));
            }
        }
Beispiel #3
0
 public SqlLocalDb()
 {
     _localDb  = new SqlLocalDbApi();
     _instance = _localDb.GetOrCreateInstance("SSS-LoadTests");
     _manager  = _instance.Manage();
     Initialize().Wait();
 }
        public DacpacDbFixture()
        {
            Console.WriteLine("Running fixture constructor...");

            _localDb = new SqlLocalDbApi();
            DateTime nowUtc = DateTime.UtcNow;

            LocalDbInstanceName = $"{nowUtc.ToString("yyyyMMddHHmmssFFFFFFF")}";    //something mostly unique
            _instance           = _localDb.GetOrCreateInstance(LocalDbInstanceName);
            _manager            = _instance.Manage();

            if (!_instance.IsRunning)
            {
                _manager.Start();
            }

            var packagePath = "SimpleDb.dacpac";

            var deployOptions = new DacDeployOptions
            {
                CreateNewDatabase     = true,
                GenerateSmartDefaults = true,
            };

            deployOptions.SqlCommandVariableValues["LoginName"] = DatabaseUserName;
            deployOptions.SqlCommandVariableValues["Password"]  = DatabasePassword;

            SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();

            csb.DataSource         = _manager.NamedPipe;
            csb.IntegratedSecurity = true;
            var databaseName          = "SimpleDbUnitTest";
            var debugConnectionString = csb.ConnectionString;
            var dacServices           = new DacServices(debugConnectionString);

            using (var package = DacPackage.Load(packagePath))
            {
                dacServices.Deploy(package, databaseName, true, deployOptions);
            }
            csb.InitialCatalog = databaseName;
            //csb.UserID = DatabaseUserName;
            //csb.Password = DatabasePassword;
            //csb.IntegratedSecurity = false;
            ConnectionString = csb.ConnectionString;

            EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
            string nameOfConnectionString      = "SimpleDbModel";         //NOTE: HACK: this must match the name of my Entity Framework model (the .edmx guy)
            string providerName = "System.Data.SqlClient";

            ecsb.Provider = providerName;
            ecsb.ProviderConnectionString = csb.ConnectionString;
            ecsb.Metadata      = $"res://*/{nameOfConnectionString}.csdl|res://*/{nameOfConnectionString}.ssdl|res://*/{nameOfConnectionString}.msl";
            EfConnectionString = ecsb.ConnectionString;

            NumberOfTimesDacpacWasApplied++;
            Debug.WriteLine($">> The DACPAC has been applied {NumberOfTimesDacpacWasApplied} times");
            Console.WriteLine($">> The DACPAC has been applied {NumberOfTimesDacpacWasApplied} times");
        }
 public DatabaseFixture()
 {
     this.localDB = new SqlLocalDbApi();
     instance     = localDB.GetOrCreateInstance(DatabaseFixture.LOCALDB_NAME);
     manager      = instance.Manage();
     if (!instance.IsRunning)
     {
         manager.Start();
     }
     RestoreBackup().GetAwaiter().GetResult();
 }
Beispiel #6
0
        private static string InitSqlLocalDB()
        {
            ISqlLocalDbApi          localDB  = new SqlLocalDbApi();
            ISqlLocalDbInstanceInfo instance = localDB.GetOrCreateInstance(@"localtestdb");

            ISqlLocalDbInstanceManager manager = instance.Manage();

            if (!instance.IsRunning)
            {
                manager.Start();
            }

            return(instance.GetConnectionString());
        }
        public void Create_A_Sql_LocalDB_Instance()
        {
            using (var localDB = new SqlLocalDbApi(OutputHelper.ToLoggerFactory()))
            {
                ISqlLocalDbInstanceInfo    instance = localDB.GetOrCreateInstance("MyInstance");
                ISqlLocalDbInstanceManager manager  = instance.Manage();

                if (!instance.IsRunning)
                {
                    manager.Start();
                }

                using (SqlConnection connection = instance.CreateConnection())
                {
                    connection.Open();

                    // Use the SQL connection...
                }

                manager.Stop();
            }
        }
Beispiel #8
0
        public void Test1()
        {
            // download sqlpackage https://docs.microsoft.com/fr-fr/sql/tools/sqlpackage-download?view=sql-server-2017
            var databaseName     = "Clients";
            var instanceName     = "Clients";
            var currentDirectory = Directory.GetCurrentDirectory();

            var localDB = new SqlLocalDbApi();

            try
            {
                var instance = localDB.GetOrCreateInstance(instanceName);
                var manager  = instance.Manage();

                if (!instance.IsRunning)
                {
                    manager.Start();
                }

                var connectionString = instance.GetConnectionString();
                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    ExecuteSql(connection, $@"
USE master;
IF (EXISTS (SELECT name 
    FROM master.dbo.sysdatabases 
    WHERE ('[' + name + ']' = '[{databaseName}]' 
    OR name = '{databaseName}')))
BEGIN
    ALTER DATABASE [{databaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    DROP DATABASE [{databaseName}];
END
");
                }

                var mdfFile = new FileInfo(
                    Path.Combine(currentDirectory, $"{databaseName}.mdf"));
                var ldfFile = new FileInfo(
                    Path.Combine(currentDirectory, $"{databaseName}.ldf"));

                if (mdfFile.Exists)
                {
                    mdfFile.Delete();
                }
                if (ldfFile.Exists)
                {
                    ldfFile.Delete();
                }

                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();

                    // create database
                    ExecuteSql(connection, $@"
CREATE DATABASE [{databaseName}]
ON
(
    NAME = {databaseName}_data,  
    FILENAME = '{mdfFile.FullName}'
)  
LOG ON
(
    NAME = {databaseName}_log,  
    FILENAME = '{ldfFile.FullName}'
);
");

                    // publish dacpac to newly created database
                    PrepareDatabase(connectionString, databaseName);

                    ExecuteSql(connection, $"USE [{databaseName}]");

                    // modify datas
                    var insertNumber = 10000;
                    for (var i = 1; i <= insertNumber; i++)
                    {
                        ExecuteSql(connection, $"INSERT INTO [Client] ([Name]) VALUES ('Client{i}')");
                    }

                    // check datas
                    var sqlCommand = new SqlCommand("SELECT count(*) FROM Client", connection);
                    var result     = sqlCommand.ExecuteScalar();

                    Assert.Equal(insertNumber, result);
                }

                manager.Stop();
            }
            finally
            {
                localDB.DeleteUserInstances(true);
                localDB.Dispose();
            }
        }