public async Task CreateConnection_Creates_A_Sql_Connection() { // Arrange using (var api = new SqlLocalDbApi(_loggerFactory)) { using (TemporarySqlLocalDbInstance temporary = api.CreateTemporaryInstance(deleteFiles: true)) { ISqlLocalDbInstanceManager manager = temporary.Manage(); manager.ShouldNotBeNull(); manager.Name.ShouldBe(temporary.Name); // Act using (SqlConnection actual = manager.CreateConnection()) { // Assert actual.ShouldNotBeNull(); actual.ConnectionString.ShouldNotBeNull(); actual.State.ShouldBe(ConnectionState.Closed); await actual.OpenAsync(); actual.Close(); } } } }
public void TemporaryInstance_Ignores_Exception_If_Stop_Fails(int errorCode) { // Arrange var mock = new Mock <ISqlLocalDbApi>(); mock.Setup((p) => p.LatestVersion) .Returns("v99.9"); mock.Setup((p) => p.CreateInstance(It.IsAny <string>(), "v99.9")) .Verifiable(); mock.Setup((p) => p.StartInstance(It.IsAny <string>())) .Verifiable(); mock.Setup((p) => p.StopInstance(It.IsAny <string>(), null)) .Throws(new SqlLocalDbException("Error", errorCode)) .Verifiable(); mock.Setup((p) => p.DeleteInstance(It.IsAny <string>())) .Verifiable(); ISqlLocalDbApi api = mock.Object; // Act using (TemporarySqlLocalDbInstance target = api.CreateTemporaryInstance()) { target.GetInstanceInfo(); } // Assert mock.Verify(); }
public void TemporaryInstance_Deletes_Instance_If_Start_Fails() { // Arrange var mock = new Mock <ISqlLocalDbApi>(); mock.Setup((p) => p.LatestVersion) .Returns("v99.9"); mock.Setup((p) => p.CreateInstance(It.IsAny <string>(), "v99.9")) .Verifiable(); mock.Setup((p) => p.StartInstance(It.IsAny <string>())) .Throws <PlatformNotSupportedException>(); mock.Setup((p) => p.DeleteInstance(It.IsAny <string>())) .Verifiable(); ISqlLocalDbApi api = mock.Object; using (TemporarySqlLocalDbInstance target = api.CreateTemporaryInstance()) { // Act and Assert Assert.Throws <PlatformNotSupportedException>(() => target.GetInstanceInfo()); } mock.Verify(); }
public void TemporaryInstance_Disposes_Cleanly_If_Not_Used() { // Arrange var api = Mock.Of <ISqlLocalDbApi>(); // Act and Assert using TemporarySqlLocalDbInstance instance = api.CreateTemporaryInstance(); instance.Dispose(); }
public void TemporaryInstance_Is_ISqlLocalDbApiAdapter() { // Arrange var api = Mock.Of <ISqlLocalDbApi>(); using TemporarySqlLocalDbInstance instance = api.CreateTemporaryInstance(); // Act ISqlLocalDbApiAdapter adapter = instance; // Assert adapter.LocalDb.ShouldBeSameAs(api); }
public void Restart_Stops_And_Starts_Instance() { // Arrange using var api = new SqlLocalDbApi(_loggerFactory); using TemporarySqlLocalDbInstance temporary = api.CreateTemporaryInstance(deleteFiles: true); ISqlLocalDbInstanceManager manager = temporary.Manage(); // Act manager.Restart(); // Assert temporary.GetInstanceInfo().IsRunning.ShouldBeTrue(); }
public void TemporaryInstance_Throws_If_Used_After_Disposal() { // Arrange var api = Mock.Of <ISqlLocalDbApi>(); TemporarySqlLocalDbInstance instance = api.CreateTemporaryInstance(); // Act instance.Dispose(); // Assert Assert.Throws <ObjectDisposedException>(() => instance.Name); Assert.Throws <ObjectDisposedException>(() => instance.GetInstanceInfo()); }
public void ShareInstance_Shares_Instance_For_Current_User() { // Arrange using var api = new SqlLocalDbApi(_loggerFactory); using TemporarySqlLocalDbInstance target = api.CreateTemporaryInstance(deleteFiles: true); target.GetInstanceInfo().IsShared.ShouldBeFalse(); // Act api.ShareInstance(target.Name, Guid.NewGuid().ToString()); // Assert target.GetInstanceInfo().IsShared.ShouldBeTrue(); }
public void Create_A_Temporary_Sql_LocalDB_Instance() { using (var localDB = new SqlLocalDbApi(OutputHelper.ToLoggerFactory())) { using (TemporarySqlLocalDbInstance instance = localDB.CreateTemporaryInstance(deleteFiles: true)) { using (var connection = new SqlConnection(instance.ConnectionString)) { connection.Open(); // Use the SQL connection... } } } }
public void CreateTemporaryInstance_Creates_Starts_And_Deletes_An_Instance(bool deleteFiles) { // Arrange if (!SqlLocalDbApi.IsWindows) { // HACK Theories dont seem to work correctly with subclasses now // so cannot make a derived class for a "Windows-only" theory. return; } using (var api = new SqlLocalDbApi(_loggerFactory)) { ISqlLocalDbInstanceInfo info; string name; // Act using (TemporarySqlLocalDbInstance target = api.CreateTemporaryInstance(deleteFiles)) { // Assert target.ShouldNotBeNull(); target.Name.ShouldNotBeNull(); target.Name.ShouldNotBeEmpty(); Guid.TryParse(target.Name, out Guid nameAsGuid).ShouldBeTrue(); nameAsGuid.ShouldNotBe(Guid.Empty); // Act info = target.GetInstanceInfo(); // Assert info.ShouldNotBeNull(); info.Exists.ShouldBeTrue(); info.IsRunning.ShouldBeTrue(); name = target.Name; } // Act info = api.GetInstanceInfo(name); // Assert info.ShouldNotBeNull(); info.Exists.ShouldBeFalse(); } }
public static void TemporaryInstance_Ignores_Exception_If_Delete_Fails(int errorCode) { // Arrange if (!SqlLocalDbApi.IsWindows) { // HACK Theories dont seem to work correctly with subclasses now // so cannot make a derived class for a "Windows-only" theory. return; } // Arrange var mock = new Mock <ISqlLocalDbApi>(); mock.Setup((p) => p.LatestVersion) .Returns("v99.9"); mock.Setup((p) => p.CreateInstance(It.IsAny <string>(), "v99.9")) .Verifiable(); mock.Setup((p) => p.StartInstance(It.IsAny <string>())) .Verifiable(); mock.Setup((p) => p.StopInstance(It.IsAny <string>(), null)) .Verifiable(); mock.Setup((p) => p.DeleteInstance(It.IsAny <string>())) .Throws(new SqlLocalDbException("Error", errorCode)) .Verifiable(); ISqlLocalDbApi api = mock.Object; // Act using (TemporarySqlLocalDbInstance target = api.CreateTemporaryInstance()) { target.GetInstanceInfo(); } // Assert mock.Verify(); }
private void CreateTemporaryInstance_Creates_Starts_And_Deletes_An_Instance(bool deleteFiles) { // Arrange using (var api = new SqlLocalDbApi(_loggerFactory)) { ISqlLocalDbInstanceInfo info; string name; // Act using (TemporarySqlLocalDbInstance target = api.CreateTemporaryInstance(deleteFiles)) { // Assert target.ShouldNotBeNull(); target.Name.ShouldNotBeNull(); target.Name.ShouldNotBeEmpty(); Guid.TryParse(target.Name, out Guid nameAsGuid).ShouldBeTrue(); nameAsGuid.ShouldNotBe(Guid.Empty); // Act info = target.GetInstanceInfo(); // Assert info.ShouldNotBeNull(); info.Exists.ShouldBeTrue(); info.IsRunning.ShouldBeTrue(); name = target.Name; } // Act info = api.GetInstanceInfo(name); // Assert info.ShouldNotBeNull(); info.Exists.ShouldBeFalse(); } }