예제 #1
0
        public void TestGetAndListManagedDatabase()
        {
            string testPrefix = "sqlcrudtest-";

            using (SqlManagementTestContext context = new SqlManagementTestContext(this))
            {
                SqlManagementClient sqlClient = context.GetClient <SqlManagementClient>();

                // Update with values from a current MI on the region
                //
                string resourceGroup       = "testclrg";
                string managedInstanceName = "tdstage-haimb-dont-delete-3";

                // Get MI
                var managedInstance = sqlClient.ManagedInstances.Get(resourceGroup, managedInstanceName);

                // Create some small databases to run the get/List tests on.
                ManagedDatabase[] mngdDatabases = SqlManagementTestUtilities.CreateManagedDatabasesAsync(
                    sqlClient, resourceGroup, managedInstance, testPrefix, 4).Result;

                // Organize into a dictionary for better lookup later
                IDictionary <string, ManagedDatabase> inputs = mngdDatabases.ToDictionary(
                    keySelector: d => d.Name,
                    elementSelector: d => d);

                // Get each database and compare to the results of create database
                //
                foreach (var db in inputs)
                {
                    var response = sqlClient.ManagedDatabases.Get(resourceGroup, managedInstance.Name, db.Key);
                    SqlManagementTestUtilities.ValidateManagedDatabaseEx(db.Value, response);
                }

                // List all databases
                //
                var listResponse = sqlClient.ManagedDatabases.ListByInstance(resourceGroup, managedInstance.Name);

                // Check that all created Managed Databases are created
                foreach (var db in inputs.Keys)
                {
                    var actualDbList = listResponse.Where(d => d.Name.Equals(db));
                    Assert.True(actualDbList.Count() == 1);
                    ManagedDatabase actualDb = actualDbList.FirstOrDefault();
                    SqlManagementTestUtilities.ValidateManagedDatabase(inputs[db], actualDb, db);
                }

                foreach (var db in inputs.Keys)
                {
                    sqlClient.ManagedDatabases.Delete(resourceGroup, managedInstance.Name, db);
                }
            }
        }