Пример #1
0
        public void SqlLocalDbProvider_CreateInstance_Specifies_No_Version_If_Default_Instance_Name_Specified_2014_2016()
        {
            // Arrange
            string instanceName = "MSSQLLocalDB";
            string version      = "1.2.3.4";

            Mock <ISqlLocalDbInstanceInfo> mockInfo = new Mock <ISqlLocalDbInstanceInfo>();

            mockInfo
            .SetupSequence((p) => p.Exists)
            .Returns(false)
            .Returns(true);

            Mock <ISqlLocalDbApi> mock = new Mock <ISqlLocalDbApi>();

            mock.Setup((p) => p.CreateInstance(instanceName, string.Empty))
            .Verifiable();

            mock.Setup((p) => p.GetInstanceInfo(instanceName))
            .Returns(mockInfo.Object)
            .Verifiable();

            ISqlLocalDbApi localDB = mock.Object;

            SqlLocalDbProvider target = new SqlLocalDbProvider(localDB)
            {
                Version = version,
            };

            // Act
            target.CreateInstance(instanceName);

            // Assert
            mock.Verify();
        }
Пример #2
0
        public void SqlLocalDbProvider_CreateInstance_Creates_Instance_With_Specified_Name()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target       = new SqlLocalDbProvider();
            string             instanceName = Guid.NewGuid().ToString();

            // Act
            SqlLocalDbInstance result = target.CreateInstance(instanceName);

            // Assert
            Assert.IsNotNull(result, "CreateInstance() returned null.");
            Assert.AreEqual(instanceName, result.Name, "SqlLocalDbInstance.Name is incorrect.");

            try
            {
                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

                Assert.IsNotNull(info, "GetInstanceInfo() returned null.");
                Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
                Assert.IsFalse(info.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");

                Assert.AreEqual(
                    new Version(target.Version).Major,
                    info.LocalDbVersion.Major,
                    "ISqlLocalDbInstanceInfo.LocalDbVersion is incorrect.");
            }
            finally
            {
                SqlLocalDbInstance.Delete(result);
            }
        }
Пример #3
0
        public void SqlLocalDbProvider_As_ISqlLocalDbFactory_GetInstance_Returns_Specified_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            ISqlLocalDbProvider target = new SqlLocalDbProvider();
            string instanceName        = Guid.NewGuid().ToString();

            // Act
            SqlLocalDbApi.CreateInstance(instanceName);

            try
            {
                // Assert
                ISqlLocalDbInstance result = target.GetInstance(instanceName);

                Assert.IsNotNull(result, "CreateInstance() returned null.");
                Assert.AreEqual(instanceName, result.Name, "SqlLocalDbInstance.Name is incorrect.");

                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

                Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
                Assert.IsFalse(info.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");

                Assert.IsTrue(Guid.TryParse(result.Name, out Guid unused), "SqlLocalDbInstance.Name is not a valid GUID.");
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
Пример #4
0
        public void SqlLocalDbProvider_Version_Property_Can_Be_Set_Correctly()
        {
            // Arrange
            string value         = "3.4.5.6";
            string latestVersion = SqlLocalDbApi.LatestVersion;

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            string result = target.Version;

            // Assert
            Assert.AreEqual(latestVersion, result, "SqlLocalDbProvider.Version returned incorrect value.");

            // Act
            target.Version = value;
            result         = target.Version;

            // Assert
            Assert.AreEqual(value, result, "SqlLocalDbProvider.Version returned incorrect value.");

            // Act
            target.Version = null;
            result         = target.Version;

            // Assert
            Assert.AreEqual(latestVersion, result, "SqlLocalDbProvider.Version returned incorrect value.");
        }
Пример #5
0
        public void SqlLocalDbProvider_CreateInstance_Creates_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            SqlLocalDbInstance result = target.CreateInstance();

            // Assert
            Assert.IsNotNull(result, "CreateInstance() returned null.");
            Assert.IsNotNull(result.Name, "SqlLocalDbInstance.Name is null.");

            try
            {
                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

                Assert.IsNotNull(info, "GetInstanceInfo() returned null.");
                Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
                Assert.IsFalse(info.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");

                Assert.AreEqual(
                    new Version(target.Version).Major,
                    info.LocalDbVersion.Major,
                    "ISqlLocalDbInstanceInfo.LocalDbVersion is incorrect.");

                Assert.IsTrue(Guid.TryParse(result.Name, out Guid unused), "SqlLocalDbInstance.Name is not a valid GUID.");
            }
            finally
            {
                SqlLocalDbInstance.Delete(result);
            }
        }
        public void SqlLocalDbProvider_As_ISqlLocalDbFactory_GetInstance_Returns_Specified_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            ISqlLocalDbProvider target = new SqlLocalDbProvider();
            string instanceName = Guid.NewGuid().ToString();

            // Act
            SqlLocalDbApi.CreateInstance(instanceName);

            try
            {
                // Assert
                ISqlLocalDbInstance result = target.GetInstance(instanceName);

                Assert.IsNotNull(result, "CreateInstance() returned null.");
                Assert.AreEqual(instanceName, result.Name, "SqlLocalDbInstance.Name is incorrect.");

                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

                Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
                Assert.IsFalse(info.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");

                Guid guid;
                Assert.IsTrue(Guid.TryParse(result.Name, out guid), "SqlLocalDbInstance.Name is not a valid GUID.");
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
Пример #7
0
        public void TemporarySqlLocalDbInstance_Constructor_Creates_Temporary_Instance_With_Specified_Provider_And_Does_Not_Delete_Instance_Files()
        {
            // Arrange
            var mock = new Mock <SqlLocalDbProvider>()
            {
                CallBase = true,
            };

            string             instanceName = "MyTempInstance" + Guid.NewGuid().ToString();
            SqlLocalDbProvider provider     = mock.Object;
            bool deleteFiles = false;

            // Act
            using (TemporarySqlLocalDbInstance target = new TemporarySqlLocalDbInstance(instanceName, provider, deleteFiles))
            {
                // Assert
                Mock.Get(provider).Verify((p) => p.CreateInstance(instanceName), Times.Once());

                Assert.IsNotNull(target.Instance, "TemporarySqlLocalDbInstance.Instance is null.");

                // Check the instance was created
                AssertExistence(instanceName, exists: true);

                // The instance is not running if there is no pipe open
                Assert.IsFalse(string.IsNullOrEmpty(target.Instance.NamedPipe), "The temporary SQL LocalDB instance has not been started.");

                Assert.AreEqual(deleteFiles, target.DeleteFiles, "TemporarySqlLocalDbInstance.DeleteFiles is incorrect.");
                Assert.AreEqual(target.Instance.Name, target.Name, "TemporarySqlLocalDbInstance.Name is incorrect.");
                Assert.AreEqual(target.Instance.NamedPipe, target.NamedPipe, "TemporarySqlLocalDbInstance.NamedPipe is incorrect.");
            }

            // The instance should have been deleted
            AssertExistence(instanceName, exists: false);
            AssertFileExistence(instanceName, shouldFilesExist: true);
        }
Пример #8
0
        /// <summary>
        /// Ensures that the specified SQL Server LocalDB version is installed on the current machine.
        /// </summary>
        /// <param name="majorVersion">The major version of SQL Server LocalDB required.</param>
        /// <remarks>
        /// Any unit test calling this method is marked as Inconclusive if the required version of
        /// SQL Server LocalDB is not installed on the local machine.
        /// </remarks>
        public static void EnsureLocalDBVersionInstalled(int majorVersion)
        {
            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            if (!provider.GetVersions().Any((p) => p.Version.Major == majorVersion))
            {
                Assert.Inconclusive($"SQL Server LocalDB v{majorVersion} is not installed on {Environment.MachineName}.");
            }
        }
Пример #9
0
        public void SqlLocalDbProvider_Default_Constructor_Uses_Correct_LocalDB_Instance()
        {
            // Act
            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Assert
            Assert.IsNotNull(target.LocalDB, "SqlLocalDbProvider.LocalDB is null.");
            Assert.IsInstanceOfType(target.LocalDB, typeof(SqlLocalDbApiWrapper), "SqlLocalDbProvider.LocalDB is incorrect.");
        }
Пример #10
0
        public void SqlLocalDbProvider_Constructor_Uses_Specified_LocalDB_Instance()
        {
            // Arrange
            ISqlLocalDbApi localDB = Mock.Of <ISqlLocalDbApi>();

            // Act
            SqlLocalDbProvider target = new SqlLocalDbProvider(localDB);

            // Assert
            Assert.AreSame(localDB, target.LocalDB, "SqlLocalDbProvider.LocalDB is incorrect.");
        }
        public MsSqlEventStoreFixture()
        {
            var localDbProvider = new SqlLocalDbProvider
            {
                Version = "11.0"
            };
            _localDbInstance = localDbProvider.GetOrCreateInstance("CedarEventStoreTests");
            _localDbInstance.Start();

            var uniqueName = Guid.NewGuid().ToString().Replace("-", string.Empty);
            _databaseName = $"CedarEventStoreTests_{uniqueName}";
        }
Пример #12
0
        public void SqlLocalDbProvider_CreateInstance_Throws_If_No_Instance_Info_From_Api()
        {
            // Arrange
            ISqlLocalDbApi localDB = Mock.Of <ISqlLocalDbApi>();

            SqlLocalDbProvider target       = new SqlLocalDbProvider(localDB);
            string             instanceName = Guid.NewGuid().ToString();

            // Act and Assert
            throw ErrorAssert.Throws <InvalidOperationException>(
                      () => target.CreateInstance(instanceName));
        }
Пример #13
0
        /// <summary>
        /// Creates a new open connection to the configured SQLServer instance.
        /// </summary>
        /// <returns>A newly opened SqlConnection.</returns>
        public SqlConnection CreateConnection()
        {
            ProviderConfiguration config =
                (ProviderConfiguration)ConfigurationManager.GetSection("dbTestMonkey/" + ConfigurationSectionName);

            if (string.IsNullOrWhiteSpace(config.ConnectionString) && !config.IsLocalDbInstance)
            {
                string errorMessage =
                    "Configured connection string was empty or whitespace and database has not been configured as localdb. " +
                    "Connection string is required in this instance.";
                throw new InvalidOperationException(errorMessage);
            }

            SqlConnection connection = null;

            if (config.IsLocalDbInstance)
            {
                if (!string.IsNullOrWhiteSpace(config.ConnectionString))
                {
                    connection = new SqlConnection(config.ConnectionString);
                }
                else if (!string.IsNullOrWhiteSpace(config.LocalDbInstanceName))
                {
                    var localDbProvider = new System.Data.SqlLocalDb.SqlLocalDbProvider();

                    connection = localDbProvider.GetInstance(config.LocalDbInstanceName).CreateConnection();
                }
                else
                {
                    throw new InvalidOperationException(
                              "IsLocalDbInstance was true in configuration but no instance name or connection string was configured.");
                }

                connection.Open();
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(config.ConnectionString))
                {
                    connection = new SqlConnection(config.ConnectionString);
                    connection.Open();
                }
                else
                {
                    throw new InvalidOperationException(
                              "IsLocalDbInstance was false in configuration but no connection string was configured.");
                }
            }

            return(connection);
        }
Пример #14
0
        public void SqlLocalDbProvider_GetVersions_Returns_Installed_Versions()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            IList <ISqlLocalDbVersionInfo> result = target.GetVersions();

            // Assert
            Assert.IsNotNull(result, "GetVersions() returned null.");
            Assert.IsTrue(result.Count > 0, "No versions were returned by GetVersions().");
            CollectionAssert.AllItemsAreNotNull(result.ToList(), "GetVersions() returned a null instance.");
        }
        public void System_Data_SqlLocalDb_Assembly_Can_Be_Used_End_To_End_To_Create_And_Manage_Instances()
        {
            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                Assert.Fail("SQL LocalDB is not installed.");
            }

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            TestVersions(provider);

            TestInstances(provider);

            TestInstanceLifecycle(localDB, provider);
        }
Пример #16
0
        public void System_Data_SqlLocalDb_Assembly_Can_Be_Used_End_To_End_To_Create_And_Manage_Instances()
        {
            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                Assert.Fail("SQL LocalDB is not installed.");
            }

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            TestVersions(provider);

            TestInstances(provider);

            TestInstanceLifecycle(localDB, provider);
        }
Пример #17
0
        public void SqlLocalDbProvider_GetInstances_Returns_Installed_Instances()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            IList <ISqlLocalDbInstanceInfo> result = target.GetInstances();

            int initialCount = result.Count;

            // Assert
            Assert.IsNotNull(result, "GetInstances() returned null.");
            Assert.IsTrue(result.Count > 0, "No instances were returned by GetInstances().");
            CollectionAssert.AllItemsAreNotNull(result.ToList(), "GetInstances() returned a null instance.");

            string instanceName = Guid.NewGuid().ToString();

            target.CreateInstance(instanceName);

            try
            {
                result = target.GetInstances();

                // Assert
                Assert.IsNotNull(result, "GetInstances() returned null.");
                Assert.AreEqual(initialCount + 1, result.Count, "An incorrect number instances were returned by GetInstances().");
                CollectionAssert.AllItemsAreNotNull(result.ToList(), "GetInstances() returned a null instance.");
                Assert.IsTrue(result.Where((p) => p.Name == instanceName).Any(), "The new instance was not returned in the created set of instances.");
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }

            result = target.GetInstances();

            // Assert
            Assert.IsNotNull(result, "GetInstances() returned null.");
            Assert.AreEqual(initialCount, result.Count, "An incorrect number instances were returned by GetInstances().");
            CollectionAssert.AllItemsAreNotNull(result.ToList(), "GetInstances() returned a null instance.");
        }
        public PubSubStoreTests(ITestOutputHelper output) : base()
        {
            this.output = output;

            var rnd = new Random();

            dbNames = new Dictionary<string, string>()
            {
                { "PubSubStore", rnd.Next().ToString() },
                { "basic", rnd.Next().ToString() },
            };

            dbInstanceName = rnd.Next().ToString();

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();
            instance = provider.GetOrCreateInstance(dbInstanceName);

            instance.Start();

            Initialize();
        }
Пример #19
0
        public void SqlLocalDbProvider_CreateInstance_Throws_If_Instance_Already_Exists()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target       = new SqlLocalDbProvider();
            string             instanceName = Guid.NewGuid().ToString();

            SqlLocalDbApi.CreateInstance(instanceName);

            try
            {
                // Act and Assert
                throw ErrorAssert.Throws <InvalidOperationException>(
                          () => target.CreateInstance(instanceName));
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
Пример #20
0
        public void SqlLocalDbProvider_CreateInstance_Uses_Specified_Version_If_Overridden()
        {
            // Arrange
            string instanceName  = Guid.NewGuid().ToString();
            string latestVersion = "2.3.4.5";
            string version       = "1.2.3.4";

            Mock <ISqlLocalDbInstanceInfo> mockInfo = new Mock <ISqlLocalDbInstanceInfo>();

            mockInfo
            .SetupSequence((p) => p.Exists)
            .Returns(false)
            .Returns(true);

            Mock <ISqlLocalDbApi> mock = new Mock <ISqlLocalDbApi>();

            mock.Setup((p) => p.LatestVersion)
            .Returns(latestVersion);

            mock.Setup((p) => p.CreateInstance(instanceName, version))
            .Verifiable();

            mock.Setup((p) => p.GetInstanceInfo(instanceName))
            .Returns(mockInfo.Object)
            .Verifiable();

            ISqlLocalDbApi localDB = mock.Object;

            SqlLocalDbProvider target = new SqlLocalDbProvider(localDB)
            {
                Version = version,
            };

            // Act
            target.CreateInstance(instanceName);

            // Assert
            mock.Verify();
        }
        public GrainStorageTests(ITestOutputHelper output)
        {
            timingFactor = CalibrateTimings();

            this.output = output;

            var rnd = new Random();

            dbNames = new Dictionary<string, SqlConnectionStringBuilder>();

            dbInstanceName = rnd.Next().ToString();

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();
            instance = provider.GetOrCreateInstance(dbInstanceName);

            instance.Start();

            //do the database setups
            dbNames.Add("basic", CreateADatabase(rnd));
            dbNames.Add("SimpleSQLStore", CreateADatabase(rnd));

            //this is the call to start up the test cluster 
            base.Initialize();
        }
        public void SqlLocalDbProvider_CreateInstance_Throws_If_No_Instance_Info_From_Api()
        {
            // Arrange
            ISqlLocalDbApi localDB = Mock.Of<ISqlLocalDbApi>();

            SqlLocalDbProvider target = new SqlLocalDbProvider(localDB);
            string instanceName = Guid.NewGuid().ToString();

            // Act and Assert
            throw ErrorAssert.Throws<InvalidOperationException>(
                () => target.CreateInstance(instanceName));
        }
Пример #23
0
        public void Extensions_GetOrCreateInstance_If_InstanceName_Exists()
        {
            // Arrange
            ISqlLocalDbProvider value = new SqlLocalDbProvider();
            string instanceName = Guid.NewGuid().ToString();

            // Act
            ISqlLocalDbInstance result = value.GetOrCreateInstance(instanceName);

            try
            {
                // Assert
                Assert.IsNotNull(result, "GetOrCreateInstance() returned null.");
                Assert.AreEqual(instanceName, result.Name, "ISqlLocalDbInstance.Name is incorrect.");
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
        public void SqlLocalDbProvider_GetInstances_Returns_Installed_Instances()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            IList<ISqlLocalDbInstanceInfo> result = target.GetInstances();

            int initialCount = result.Count;

            // Assert
            Assert.IsNotNull(result, "GetInstances() returned null.");
            Assert.IsTrue(result.Count > 0, "No instances were returned by GetInstances().");
            CollectionAssert.AllItemsAreNotNull(result.ToList(), "GetInstances() returned a null instance.");

            string instanceName = Guid.NewGuid().ToString();
            target.CreateInstance(instanceName);

            try
            {
                result = target.GetInstances();

                // Assert
                Assert.IsNotNull(result, "GetInstances() returned null.");
                Assert.AreEqual(initialCount + 1, result.Count, "An incorrect number instances were returned by GetInstances().");
                CollectionAssert.AllItemsAreNotNull(result.ToList(), "GetInstances() returned a null instance.");
                Assert.IsTrue(result.Where((p) => p.Name == instanceName).Any(), "The new instance was not returned in the created set of instances.");
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }

            result = target.GetInstances();

            // Assert
            Assert.IsNotNull(result, "GetInstances() returned null.");
            Assert.AreEqual(initialCount, result.Count, "An incorrect number instances were returned by GetInstances().");
            CollectionAssert.AllItemsAreNotNull(result.ToList(), "GetInstances() returned a null instance.");
        }
Пример #25
0
        internal static void Main(string[] args)
        {
            PrintBanner();

            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                Console.WriteLine(SR.SqlLocalDbApi_NotInstalledFormat, Environment.MachineName);
                return;
            }

            if (args != null &&
                args.Length == 1 &&
                string.Equals(args[0], "/deleteuserinstances", StringComparison.OrdinalIgnoreCase))
            {
                SqlLocalDbApi.DeleteUserInstances(deleteFiles: true);
            }

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            IList<ISqlLocalDbVersionInfo> versions = provider.GetVersions();

            Console.WriteLine(Strings.Program_VersionsListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbVersionInfo version in versions)
            {
                Console.WriteLine(version.Name);
            }

            Console.WriteLine();

            IList<ISqlLocalDbInstanceInfo> instances = provider.GetInstances();

            Console.WriteLine(Strings.Program_InstancesListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbInstanceInfo instanceInfo in instances)
            {
                Console.WriteLine(instanceInfo.Name);
            }

            Console.WriteLine();

            string instanceName = Guid.NewGuid().ToString();

            ISqlLocalDbInstance instance = provider.CreateInstance(instanceName);

            instance.Start();

            try
            {
                if (IsCurrentUserAdmin())
                {
                    instance.Share(Guid.NewGuid().ToString());
                }

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

                        try
                        {
                            using (SqlCommand command = new SqlCommand("create database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }

                            using (SqlCommand command = new SqlCommand("drop database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
                }
                finally
                {
                    if (IsCurrentUserAdmin())
                    {
                        instance.Unshare();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                instance.Stop();
                localDB.DeleteInstance(instance.Name);
            }

            Console.WriteLine();
            Console.Write(Strings.Program_ExitPrompt);
            Console.ReadKey();
        }
Пример #26
0
        public void Extensions_GetDefaultInstance_Returns_Default_Instance()
        {
            // Arrange
            ISqlLocalDbProvider value = new SqlLocalDbProvider();

            // Act
            ISqlLocalDbInstance result = value.GetDefaultInstance();

            // Assert
            Assert.IsNotNull(result, "GetDefaultInstance() returned null.");
            Assert.AreEqual(SqlLocalDbApi.DefaultInstanceName, result.Name, "ISqlLocalDbInstance.Name is incorrect.");
        }
        public void SqlLocalDbProvider_CreateInstance_Specifies_No_Version_If_Default_Instance_Name_Specified_2014()
        {
            // Arrange
            string instanceName = "MSSQLLocalDB";
            string version = "1.2.3.4";

            Mock<ISqlLocalDbInstanceInfo> mockInfo = new Mock<ISqlLocalDbInstanceInfo>();

            mockInfo
                .SetupSequence((p) => p.Exists)
                .Returns(false)
                .Returns(true);

            Mock<ISqlLocalDbApi> mock = new Mock<ISqlLocalDbApi>();

            mock.Setup((p) => p.CreateInstance(instanceName, string.Empty))
                .Verifiable();

            mock.Setup((p) => p.GetInstanceInfo(instanceName))
                .Returns(mockInfo.Object)
                .Verifiable();

            ISqlLocalDbApi localDB = mock.Object;

            SqlLocalDbProvider target = new SqlLocalDbProvider(localDB);
            target.Version = version;

            // Act
            target.CreateInstance(instanceName);

            // Assert
            mock.Verify();
        }
Пример #28
0
        internal static void Main(string[] args)
        {
            PrintBanner();

            ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper();

            if (!localDB.IsLocalDBInstalled())
            {
                Console.WriteLine(SR.SqlLocalDbApi_NotInstalledFormat, Environment.MachineName);
                return;
            }

            if (args?.Length == 1 && string.Equals(args[0], "/deleteuserinstances", StringComparison.OrdinalIgnoreCase))
            {
                SqlLocalDbApi.DeleteUserInstances(deleteFiles: true);
            }

            ISqlLocalDbProvider provider = new SqlLocalDbProvider();

            IList <ISqlLocalDbVersionInfo> versions = provider.GetVersions();

            Console.WriteLine(Strings.Program_VersionsListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbVersionInfo version in versions)
            {
                Console.WriteLine(version.Name);
            }

            Console.WriteLine();

            IList <ISqlLocalDbInstanceInfo> instances = provider.GetInstances();

            Console.WriteLine(Strings.Program_InstancesListHeader);
            Console.WriteLine();

            foreach (ISqlLocalDbInstanceInfo instanceInfo in instances)
            {
                Console.WriteLine(instanceInfo.Name);
            }

            Console.WriteLine();

            string instanceName = Guid.NewGuid().ToString();

            ISqlLocalDbInstance instance = provider.CreateInstance(instanceName);

            instance.Start();

            try
            {
                if (IsCurrentUserAdmin())
                {
                    instance.Share(Guid.NewGuid().ToString());
                }

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

                        try
                        {
                            using (SqlCommand command = new SqlCommand("create database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }

                            using (SqlCommand command = new SqlCommand("drop database [MyDatabase]", connection))
                            {
                                command.ExecuteNonQuery();
                            }
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }
                }
                finally
                {
                    if (IsCurrentUserAdmin())
                    {
                        instance.Unshare();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                instance.Stop();
                localDB.DeleteInstance(instance.Name);
            }

            Console.WriteLine();
            Console.Write(Strings.Program_ExitPrompt);
            Console.ReadKey();
        }
        public void SqlLocalDbProvider_Version_Property_Can_Be_Set_Correctly()
        {
            // Arrange
            string value = "3.4.5.6";
            string latestVersion = SqlLocalDbApi.LatestVersion;

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            string result = target.Version;

            // Assert
            Assert.AreEqual(latestVersion, result, "SqlLocalDbProvider.Version returned incorrect value.");

            // Act
            target.Version = value;
            result = target.Version;

            // Assert
            Assert.AreEqual(value, result, "SqlLocalDbProvider.Version returned incorrect value.");

            // Act
            target.Version = null;
            result = target.Version;

            // Assert
            Assert.AreEqual(latestVersion, result, "SqlLocalDbProvider.Version returned incorrect value.");
        }
        public void SqlLocalDbProvider_GetVersions_Returns_Installed_Versions()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            IList<ISqlLocalDbVersionInfo> result = target.GetVersions();

            // Assert
            Assert.IsNotNull(result, "GetVersions() returned null.");
            Assert.IsTrue(result.Count > 0, "No versions were returned by GetVersions().");
            CollectionAssert.AllItemsAreNotNull(result.ToList(), "GetVersions() returned a null instance.");
        }
        public void SqlLocalDbProvider_CreateInstance_Throws_If_Instance_Already_Exists()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();
            string instanceName = Guid.NewGuid().ToString();

            SqlLocalDbApi.CreateInstance(instanceName);

            try
            {
                // Act and Assert
                throw ErrorAssert.Throws<InvalidOperationException>(
                    () => target.CreateInstance(instanceName));
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
Пример #32
0
 private void SetupLocalDbDatabase()
 {
     var localDbProvider = new SqlLocalDbProvider();
     var localInstance = localDbProvider.CreateInstance(DatabaseName); // instance name = db name, the api will always use the latest version
     localInstance.Start();
     this.SetupMsSqlDatabase(localInstance.CreateConnectionStringBuilder().ConnectionString);
 }
Пример #33
0
        public void Extensions_GetOrCreateInstance_For_2014_Default_Instance_Name()
        {
            // Arrange
            ISqlLocalDbProvider value = new SqlLocalDbProvider();
            string instanceName = "MSSQLLocalDB";

            // Act
            ISqlLocalDbInstance result = value.GetOrCreateInstance(instanceName);

            // Assert
            Assert.IsNotNull(result, "GetOrCreateInstance() returned null.");
            Assert.AreEqual(instanceName, result.Name, "ISqlLocalDbInstance.Name is incorrect.");
        }
        public void SqlLocalDbProvider_CreateInstance_Creates_Instance()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Act
            SqlLocalDbInstance result = target.CreateInstance();

            // Assert
            Assert.IsNotNull(result, "CreateInstance() returned null.");
            Assert.IsNotNull(result.Name, "SqlLocalDbInstance.Name is null.");

            try
            {
                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

                Assert.IsNotNull(info, "GetInstanceInfo() returned null.");
                Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
                Assert.IsFalse(info.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");

                Assert.AreEqual(
                    new Version(target.Version),
                    new Version(info.LocalDbVersion.Major, info.LocalDbVersion.Minor),
                    "ISqlLocalDbInstanceInfo.LocalDbVersion is incorrect.");

                Guid guid;
                Assert.IsTrue(Guid.TryParse(result.Name, out guid), "SqlLocalDbInstance.Name is not a valid GUID.");
            }
            finally
            {
                SqlLocalDbInstance.Delete(result);
            }
        }
        public void SqlLocalDbProvider_CreateInstance_Creates_Instance_With_Specified_Name()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            SqlLocalDbProvider target = new SqlLocalDbProvider();
            string instanceName = Guid.NewGuid().ToString();

            // Act
            SqlLocalDbInstance result = target.CreateInstance(instanceName);

            // Assert
            Assert.IsNotNull(result, "CreateInstance() returned null.");
            Assert.AreEqual(instanceName, result.Name, "SqlLocalDbInstance.Name is incorrect.");

            try
            {
                ISqlLocalDbInstanceInfo info = result.GetInstanceInfo();

                Assert.IsNotNull(info, "GetInstanceInfo() returned null.");
                Assert.IsTrue(info.Exists, "ISqlLocalDbInstanceInfo.Exists is incorrect.");
                Assert.IsFalse(info.IsRunning, "ISqlLocalDbInstanceInfo.IsRunning is incorrect.");

                Assert.AreEqual(
                    new Version(target.Version),
                    new Version(info.LocalDbVersion.Major, info.LocalDbVersion.Minor),
                    "ISqlLocalDbInstanceInfo.LocalDbVersion is incorrect.");
            }
            finally
            {
                SqlLocalDbInstance.Delete(result);
            }
        }
        public void SqlLocalDbProvider_CreateInstance_Uses_Specified_Version_If_Overridden()
        {
            // Arrange
            string instanceName = Guid.NewGuid().ToString();
            string latestVersion = "2.3.4.5";
            string version = "1.2.3.4";

            Mock<ISqlLocalDbInstanceInfo> mockInfo = new Mock<ISqlLocalDbInstanceInfo>();

            mockInfo
                .SetupSequence((p) => p.Exists)
                .Returns(false)
                .Returns(true);

            Mock<ISqlLocalDbApi> mock = new Mock<ISqlLocalDbApi>();

            mock.Setup((p) => p.LatestVersion)
                .Returns(latestVersion);

            mock.Setup((p) => p.CreateInstance(instanceName, version))
                .Verifiable();

            mock.Setup((p) => p.GetInstanceInfo(instanceName))
                .Returns(mockInfo.Object)
                .Verifiable();

            ISqlLocalDbApi localDB = mock.Object;

            SqlLocalDbProvider target = new SqlLocalDbProvider(localDB);
            target.Version = version;

            // Act
            target.CreateInstance(instanceName);

            // Assert
            mock.Verify();
        }
Пример #37
0
 private static ISqlLocalDbInstance GetSqlLocalDbInstance(string instanceName)
 {
     var sqlLocalDbProvider = new SqlLocalDbProvider();
     return sqlLocalDbProvider.GetOrCreateInstance(instanceName);
 }
        public void SqlLocalDbProvider_Default_Constructor_Uses_Correct_LocalDB_Instance()
        {
            // Act
            SqlLocalDbProvider target = new SqlLocalDbProvider();

            // Assert
            Assert.IsNotNull(target.LocalDB, "SqlLocalDbProvider.LocalDB is null.");
            Assert.IsInstanceOfType(target.LocalDB, typeof(SqlLocalDbApiWrapper), "SqlLocalDbProvider.LocalDB is incorrect.");
        }
Пример #39
0
        private void CleanupLocalDbDatabase()
        {
            SqlLocalDbApi.AutomaticallyDeleteInstanceFiles = true;
            SqlLocalDbApi.StopOptions=StopInstanceOptions.KillProcess;

            var localDbProvider = new SqlLocalDbProvider();
            var localDbInstanceInfo = localDbProvider.GetInstances().FirstOrDefault(instance => instance.Name==DatabaseName);
            if (localDbInstanceInfo != null)
            {
                var localDbInstance = localDbProvider.GetInstance(DatabaseName);
                if (!localDbInstance.IsRunning)
                {
                    localDbInstance.Start();
                }
                this.CleanupMsSqlDatabase(localDbInstance.CreateConnectionStringBuilder().ConnectionString);
                SqlLocalDbApi.StopInstance(DatabaseName,TimeSpan.FromSeconds(20.0));
                SqlLocalDbApi.DeleteInstance(DatabaseName);
            }
        }
        public void SqlLocalDbProvider_Constructor_Uses_Specified_LocalDB_Instance()
        {
            // Arrange
            ISqlLocalDbApi localDB = Mock.Of<ISqlLocalDbApi>();

            // Act
            SqlLocalDbProvider target = new SqlLocalDbProvider(localDB);

            // Assert
            Assert.AreSame(localDB, target.LocalDB, "SqlLocalDbProvider.LocalDB is incorrect.");
        }