From 57ee598fc524bee9d49a40b831167c0f738033a1 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 18 May 2016 09:38:58 +0200 Subject: [PATCH] Make sure to dispose UnitOfWork in tests --- .../NotificationsRepositoryTest.cs | 184 +++++----- .../PartialViewRepositoryTests.cs | 108 +++--- .../Repositories/ScriptRepositoryTest.cs | 323 +++++++++-------- .../Repositories/StylesheetRepositoryTest.cs | 339 +++++++++--------- .../Services/ContentServiceTests.cs | 12 +- .../Services/MediaServiceTests.cs | 8 +- 6 files changed, 512 insertions(+), 462 deletions(-) diff --git a/src/Umbraco.Tests/Persistence/Repositories/NotificationsRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/NotificationsRepositoryTest.cs index 9c68797952..982df857d1 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/NotificationsRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/NotificationsRepositoryTest.cs @@ -21,126 +21,140 @@ namespace Umbraco.Tests.Persistence.Repositories public void CreateNotification() { var provider = new NPocoUnitOfWorkProvider(Logger); - var unitOfWork = provider.CreateUnitOfWork(); - var repo = new NotificationsRepository(unitOfWork); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repo = new NotificationsRepository(unitOfWork); - var node = new NodeDto {CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,123", SortOrder = 1, Text = "hello", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0}; - var result = unitOfWork.Database.Insert(node); - var entity = Mock.Of(e => e.Id == node.NodeId); - var user = Mock.Of(e => e.Id == node.UserId); + var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,123", SortOrder = 1, Text = "hello", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; + var result = unitOfWork.Database.Insert(node); + var entity = Mock.Of(e => e.Id == node.NodeId); + var user = Mock.Of(e => e.Id == node.UserId); - var notification = repo.CreateNotification(user, entity, "A"); + var notification = repo.CreateNotification(user, entity, "A"); - Assert.AreEqual("A", notification.Action); - Assert.AreEqual(node.NodeId, notification.EntityId); - Assert.AreEqual(node.NodeObjectType, notification.EntityType); - Assert.AreEqual(node.UserId, notification.UserId); + Assert.AreEqual("A", notification.Action); + Assert.AreEqual(node.NodeId, notification.EntityId); + Assert.AreEqual(node.NodeObjectType, notification.EntityType); + Assert.AreEqual(node.UserId, notification.UserId); + } } [Test] public void GetUserNotifications() { var provider = new NPocoUnitOfWorkProvider(Logger); - var unitOfWork = provider.CreateUnitOfWork(); - var repo = new NotificationsRepository(unitOfWork); - - var userDto = new UserDto { ContentStartId = -1, Email = "test" , Login = "test" , MediaStartId = -1, Password = "test" , Type = 1, UserName = "test" , UserLanguage = "en" }; - unitOfWork.Database.Insert(userDto); - - var userNew = Mock.Of(e => e.Id == userDto.Id); - var userAdmin = Mock.Of(e => e.Id == 0); - - for (var i = 0; i < 10; i++) + using (var unitOfWork = provider.CreateUnitOfWork()) { - var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; - var result = unitOfWork.Database.Insert(node); - var entity = Mock.Of(e => e.Id == node.NodeId); - var notification = repo.CreateNotification((i % 2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture)); + + var repo = new NotificationsRepository(unitOfWork); + + var userDto = new UserDto { ContentStartId = -1, Email = "test", Login = "test", MediaStartId = -1, Password = "test", Type = 1, UserName = "test", UserLanguage = "en" }; + unitOfWork.Database.Insert(userDto); + + var userNew = Mock.Of(e => e.Id == userDto.Id); + var userAdmin = Mock.Of(e => e.Id == 0); + + for (var i = 0; i < 10; i++) + { + var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; + var result = unitOfWork.Database.Insert(node); + var entity = Mock.Of(e => e.Id == node.NodeId); + var notification = repo.CreateNotification((i%2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture)); + } + + var notifications = repo.GetUserNotifications(userAdmin); + + Assert.AreEqual(5, notifications.Count()); } - - var notifications = repo.GetUserNotifications(userAdmin); - - Assert.AreEqual(5, notifications.Count()); } [Test] public void GetEntityNotifications() { var provider = new NPocoUnitOfWorkProvider(Logger); - var unitOfWork = provider.CreateUnitOfWork(); - var repo = new NotificationsRepository(unitOfWork); - - var node1 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,1", SortOrder = 1, Text = "hello1", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; - unitOfWork.Database.Insert(node1); - var entity1 = Mock.Of(e => e.Id == node1.NodeId); - var node2 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,2", SortOrder = 1, Text = "hello2", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; - unitOfWork.Database.Insert(node2); - var entity2 = Mock.Of(e => e.Id == node2.NodeId); - - for (var i = 0; i < 10; i++) + using (var unitOfWork = provider.CreateUnitOfWork()) { - var userDto = new UserDto { ContentStartId = -1, Email = "test" + i, Login = "test" + i, MediaStartId = -1, Password = "test", Type = 1, UserName = "test" + i, UserLanguage = "en" }; - unitOfWork.Database.Insert(userDto); - var userNew = Mock.Of(e => e.Id == userDto.Id); - var notification = repo.CreateNotification(userNew, (i % 2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture)); + + var repo = new NotificationsRepository(unitOfWork); + + var node1 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,1", SortOrder = 1, Text = "hello1", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; + unitOfWork.Database.Insert(node1); + var entity1 = Mock.Of(e => e.Id == node1.NodeId); + var node2 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,2", SortOrder = 1, Text = "hello2", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; + unitOfWork.Database.Insert(node2); + var entity2 = Mock.Of(e => e.Id == node2.NodeId); + + for (var i = 0; i < 10; i++) + { + var userDto = new UserDto { ContentStartId = -1, Email = "test" + i, Login = "test" + i, MediaStartId = -1, Password = "test", Type = 1, UserName = "test" + i, UserLanguage = "en" }; + unitOfWork.Database.Insert(userDto); + var userNew = Mock.Of(e => e.Id == userDto.Id); + var notification = repo.CreateNotification(userNew, (i%2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture)); + } + + var notifications = repo.GetEntityNotifications(entity1); + + Assert.AreEqual(5, notifications.Count()); } - - var notifications = repo.GetEntityNotifications(entity1); - - Assert.AreEqual(5, notifications.Count()); } [Test] public void Delete_By_Entity() { var provider = new NPocoUnitOfWorkProvider(Logger); - var unitOfWork = provider.CreateUnitOfWork(); - var repo = new NotificationsRepository(unitOfWork); - - var node1 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,1", SortOrder = 1, Text = "hello1", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; - unitOfWork.Database.Insert(node1); - var entity1 = Mock.Of(e => e.Id == node1.NodeId); - var node2 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,2", SortOrder = 1, Text = "hello2", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; - unitOfWork.Database.Insert(node2); - var entity2 = Mock.Of(e => e.Id == node2.NodeId); - - for (var i = 0; i < 10; i++) + using (var unitOfWork = provider.CreateUnitOfWork()) { - var userDto = new UserDto { ContentStartId = -1, Email = "test" + i, Login = "test" + i, MediaStartId = -1, Password = "test", Type = 1, UserName = "test" + i, UserLanguage = "en" }; - unitOfWork.Database.Insert(userDto); - var userNew = Mock.Of(e => e.Id == userDto.Id); - var notification = repo.CreateNotification(userNew, (i % 2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture)); + + var repo = new NotificationsRepository(unitOfWork); + + var node1 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,1", SortOrder = 1, Text = "hello1", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; + unitOfWork.Database.Insert(node1); + var entity1 = Mock.Of(e => e.Id == node1.NodeId); + var node2 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1,2", SortOrder = 1, Text = "hello2", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; + unitOfWork.Database.Insert(node2); + var entity2 = Mock.Of(e => e.Id == node2.NodeId); + + for (var i = 0; i < 10; i++) + { + var userDto = new UserDto { ContentStartId = -1, Email = "test" + i, Login = "test" + i, MediaStartId = -1, Password = "test", Type = 1, UserName = "test" + i, UserLanguage = "en" }; + unitOfWork.Database.Insert(userDto); + var userNew = Mock.Of(e => e.Id == userDto.Id); + var notification = repo.CreateNotification(userNew, (i%2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture)); + } + + var delCount = repo.DeleteNotifications(entity1); + + Assert.AreEqual(5, delCount); } - - var delCount = repo.DeleteNotifications(entity1); - - Assert.AreEqual(5, delCount); } [Test] public void Delete_By_User() { var provider = new NPocoUnitOfWorkProvider(Logger); - var unitOfWork = provider.CreateUnitOfWork(); - var repo = new NotificationsRepository(unitOfWork); - - var userDto = new UserDto { ContentStartId = -1, Email = "test", Login = "test", MediaStartId = -1, Password = "test", Type = 1, UserName = "test", UserLanguage = "en" }; - unitOfWork.Database.Insert(userDto); - - var userNew = Mock.Of(e => e.Id == userDto.Id); - var userAdmin = Mock.Of(e => e.Id == 0); - - for (var i = 0; i < 10; i++) + using (var unitOfWork = provider.CreateUnitOfWork()) { - var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; - var result = unitOfWork.Database.Insert(node); - var entity = Mock.Of(e => e.Id == node.NodeId); - var notification = repo.CreateNotification((i % 2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture)); + + var repo = new NotificationsRepository(unitOfWork); + + var userDto = new UserDto { ContentStartId = -1, Email = "test", Login = "test", MediaStartId = -1, Password = "test", Type = 1, UserName = "test", UserLanguage = "en" }; + unitOfWork.Database.Insert(userDto); + + var userNew = Mock.Of(e => e.Id == userDto.Id); + var userAdmin = Mock.Of(e => e.Id == 0); + + for (var i = 0; i < 10; i++) + { + var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Guid.Parse(Constants.ObjectTypes.ContentItem), ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 }; + var result = unitOfWork.Database.Insert(node); + var entity = Mock.Of(e => e.Id == node.NodeId); + var notification = repo.CreateNotification((i%2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture)); + } + + var delCount = repo.DeleteNotifications(userAdmin); + + Assert.AreEqual(5, delCount); } - - var delCount = repo.DeleteNotifications(userAdmin); - - Assert.AreEqual(5, delCount); } } } \ No newline at end of file diff --git a/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs b/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs index 9a732db778..643cc66991 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/PartialViewRepositoryTests.cs @@ -32,63 +32,65 @@ namespace Umbraco.Tests.Persistence.Repositories // unless noted otherwise, no changes / 7.2.8 var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new PartialViewRepository(unitOfWork, _fileSystem); - - var partialView = new PartialView("test-path-1.cshtml") { Content = "// partialView" }; - repository.AddOrUpdate(partialView); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("test-path-1.cshtml")); - Assert.AreEqual("test-path-1.cshtml", partialView.Path); - Assert.AreEqual("/Views/Partials/test-path-1.cshtml", partialView.VirtualPath); - - partialView = new PartialView("path-2/test-path-2.cshtml") { Content = "// partialView" }; - repository.AddOrUpdate(partialView); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.cshtml")); - Assert.AreEqual("path-2\\test-path-2.cshtml", partialView.Path); // fixed in 7.3 - 7.2.8 does not update the path - Assert.AreEqual("/Views/Partials/path-2/test-path-2.cshtml", partialView.VirtualPath); - - partialView = (PartialView) repository.Get("path-2/test-path-2.cshtml"); - Assert.IsNotNull(partialView); - Assert.AreEqual("path-2\\test-path-2.cshtml", partialView.Path); - Assert.AreEqual("/Views/Partials/path-2/test-path-2.cshtml", partialView.VirtualPath); - - partialView = new PartialView("path-2\\test-path-3.cshtml") { Content = "// partialView" }; - repository.AddOrUpdate(partialView); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.cshtml")); - Assert.AreEqual("path-2\\test-path-3.cshtml", partialView.Path); - Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath); - - partialView = (PartialView) repository.Get("path-2/test-path-3.cshtml"); - Assert.IsNotNull(partialView); - Assert.AreEqual("path-2\\test-path-3.cshtml", partialView.Path); - Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath); - - partialView = (PartialView) repository.Get("path-2\\test-path-3.cshtml"); - Assert.IsNotNull(partialView); - Assert.AreEqual("path-2\\test-path-3.cshtml", partialView.Path); - Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath); - - partialView = new PartialView("\\test-path-4.cshtml") { Content = "// partialView" }; - Assert.Throws(() => // fixed in 7.3 - 7.2.8 used to strip the \ + using (var unitOfWork = provider.CreateUnitOfWork()) { + var repository = new PartialViewRepository(unitOfWork, _fileSystem); + + var partialView = new PartialView("test-path-1.cshtml") { Content = "// partialView" }; repository.AddOrUpdate(partialView); - }); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("test-path-1.cshtml")); + Assert.AreEqual("test-path-1.cshtml", partialView.Path); + Assert.AreEqual("/Views/Partials/test-path-1.cshtml", partialView.VirtualPath); - partialView = (PartialView) repository.Get("missing.cshtml"); - Assert.IsNull(partialView); + partialView = new PartialView("path-2/test-path-2.cshtml") { Content = "// partialView" }; + repository.AddOrUpdate(partialView); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.cshtml")); + Assert.AreEqual("path-2\\test-path-2.cshtml", partialView.Path); // fixed in 7.3 - 7.2.8 does not update the path + Assert.AreEqual("/Views/Partials/path-2/test-path-2.cshtml", partialView.VirtualPath); - // fixed in 7.3 - 7.2.8 used to... - Assert.Throws(() => - { - partialView = (PartialView) repository.Get("\\test-path-4.cshtml"); // outside the filesystem, does not exist - }); - Assert.Throws(() => - { - partialView = (PartialView) repository.Get("../../packages.config"); // outside the filesystem, exists - }); + partialView = (PartialView) repository.Get("path-2/test-path-2.cshtml"); + Assert.IsNotNull(partialView); + Assert.AreEqual("path-2\\test-path-2.cshtml", partialView.Path); + Assert.AreEqual("/Views/Partials/path-2/test-path-2.cshtml", partialView.VirtualPath); + + partialView = new PartialView("path-2\\test-path-3.cshtml") { Content = "// partialView" }; + repository.AddOrUpdate(partialView); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.cshtml")); + Assert.AreEqual("path-2\\test-path-3.cshtml", partialView.Path); + Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath); + + partialView = (PartialView) repository.Get("path-2/test-path-3.cshtml"); + Assert.IsNotNull(partialView); + Assert.AreEqual("path-2\\test-path-3.cshtml", partialView.Path); + Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath); + + partialView = (PartialView) repository.Get("path-2\\test-path-3.cshtml"); + Assert.IsNotNull(partialView); + Assert.AreEqual("path-2\\test-path-3.cshtml", partialView.Path); + Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath); + + partialView = new PartialView("\\test-path-4.cshtml") { Content = "// partialView" }; + Assert.Throws(() => // fixed in 7.3 - 7.2.8 used to strip the \ + { + repository.AddOrUpdate(partialView); + }); + + partialView = (PartialView) repository.Get("missing.cshtml"); + Assert.IsNull(partialView); + + // fixed in 7.3 - 7.2.8 used to... + Assert.Throws(() => + { + partialView = (PartialView) repository.Get("\\test-path-4.cshtml"); // outside the filesystem, does not exist + }); + Assert.Throws(() => + { + partialView = (PartialView) repository.Get("../../packages.config"); // outside the filesystem, exists + }); + } } [TearDown] diff --git a/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs index f73aa673b4..51bae37c4d 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs @@ -35,13 +35,15 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); + using (var unitOfWork = provider.CreateUnitOfWork()) + { - // Act - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + // Act + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - // Assert - Assert.That(repository, Is.Not.Null); + // Assert + Assert.That(repository, Is.Not.Null); + } } [Test] @@ -49,16 +51,18 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - // Act - var script = new Script("test-add-script.js") {Content = "/// "}; - repository.AddOrUpdate(script); - unitOfWork.Flush(); + // Act + var script = new Script("test-add-script.js") { Content = "/// " }; + repository.AddOrUpdate(script); + unitOfWork.Flush(); - //Assert - Assert.That(_fileSystem.FileExists("test-add-script.js"), Is.True); + //Assert + Assert.That(_fileSystem.FileExists("test-add-script.js"), Is.True); + } } [Test] @@ -66,23 +70,25 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - // Act - var script = new Script("test-updated-script.js") { Content = "/// " }; - repository.AddOrUpdate(script); - unitOfWork.Flush(); + // Act + var script = new Script("test-updated-script.js") { Content = "/// " }; + repository.AddOrUpdate(script); + unitOfWork.Flush(); - script.Content = "/// "; - repository.AddOrUpdate(script); - unitOfWork.Flush(); + script.Content = "/// "; + repository.AddOrUpdate(script); + unitOfWork.Flush(); - var scriptUpdated = repository.Get("test-updated-script.js"); + var scriptUpdated = repository.Get("test-updated-script.js"); - // Assert - Assert.That(_fileSystem.FileExists("test-updated-script.js"), Is.True); - Assert.That(scriptUpdated.Content, Is.EqualTo("/// ")); + // Assert + Assert.That(_fileSystem.FileExists("test-updated-script.js"), Is.True); + Assert.That(scriptUpdated.Content, Is.EqualTo("/// ")); + } } [Test] @@ -90,18 +96,19 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - // Act - var script = repository.Get("test-script.js"); - repository.Delete(script); - unitOfWork.Flush(); + // Act + var script = repository.Get("test-script.js"); + repository.Delete(script); + unitOfWork.Flush(); - // Assert - - Assert.IsFalse(repository.Exists("test-script.js")); + // Assert + Assert.IsFalse(repository.Exists("test-script.js")); + } } [Test] @@ -109,16 +116,18 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - // Act - var exists = repository.Get("test-script.js"); + // Act + var exists = repository.Get("test-script.js"); - // Assert - Assert.That(exists, Is.Not.Null); - Assert.That(exists.Alias, Is.EqualTo("test-script")); - Assert.That(exists.Name, Is.EqualTo("test-script.js")); + // Assert + Assert.That(exists, Is.Not.Null); + Assert.That(exists.Alias, Is.EqualTo("test-script")); + Assert.That(exists.Name, Is.EqualTo("test-script.js")); + } } [Test] @@ -126,25 +135,27 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - var script = new Script("test-script1.js") { Content = "/// " }; - repository.AddOrUpdate(script); - var script2 = new Script("test-script2.js") { Content = "/// " }; - repository.AddOrUpdate(script2); - var script3 = new Script("test-script3.js") { Content = "/// " }; - repository.AddOrUpdate(script3); - unitOfWork.Flush(); + var script = new Script("test-script1.js") { Content = "/// " }; + repository.AddOrUpdate(script); + var script2 = new Script("test-script2.js") { Content = "/// " }; + repository.AddOrUpdate(script2); + var script3 = new Script("test-script3.js") { Content = "/// " }; + repository.AddOrUpdate(script3); + unitOfWork.Flush(); - // Act - var scripts = repository.GetAll(); + // Act + var scripts = repository.GetAll(); - // Assert - Assert.That(scripts, Is.Not.Null); - Assert.That(scripts.Any(), Is.True); - Assert.That(scripts.Any(x => x == null), Is.False); - Assert.That(scripts.Count(), Is.EqualTo(4)); + // Assert + Assert.That(scripts, Is.Not.Null); + Assert.That(scripts.Any(), Is.True); + Assert.That(scripts.Any(x => x == null), Is.False); + Assert.That(scripts.Count(), Is.EqualTo(4)); + } } [Test] @@ -152,25 +163,27 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - var script = new Script("test-script1.js") { Content = "/// " }; - repository.AddOrUpdate(script); - var script2 = new Script("test-script2.js") { Content = "/// " }; - repository.AddOrUpdate(script2); - var script3 = new Script("test-script3.js") { Content = "/// " }; - repository.AddOrUpdate(script3); - unitOfWork.Flush(); + var script = new Script("test-script1.js") { Content = "/// " }; + repository.AddOrUpdate(script); + var script2 = new Script("test-script2.js") { Content = "/// " }; + repository.AddOrUpdate(script2); + var script3 = new Script("test-script3.js") { Content = "/// " }; + repository.AddOrUpdate(script3); + unitOfWork.Flush(); - // Act - var scripts = repository.GetAll("test-script1.js", "test-script2.js"); + // Act + var scripts = repository.GetAll("test-script1.js", "test-script2.js"); - // Assert - Assert.That(scripts, Is.Not.Null); - Assert.That(scripts.Any(), Is.True); - Assert.That(scripts.Any(x => x == null), Is.False); - Assert.That(scripts.Count(), Is.EqualTo(2)); + // Assert + Assert.That(scripts, Is.Not.Null); + Assert.That(scripts.Any(), Is.True); + Assert.That(scripts.Any(x => x == null), Is.False); + Assert.That(scripts.Count(), Is.EqualTo(2)); + } } [Test] @@ -178,14 +191,16 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - // Act - var exists = repository.Exists("test-script.js"); + // Act + var exists = repository.Exists("test-script.js"); - // Assert - Assert.That(exists, Is.True); + // Assert + Assert.That(exists, Is.True); + } } [Test] @@ -195,29 +210,31 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - var script = new Script("test-move-script.js") { Content = content }; - repository.AddOrUpdate(script); - unitOfWork.Flush(); + var script = new Script("test-move-script.js") { Content = content }; + repository.AddOrUpdate(script); + unitOfWork.Flush(); - // Act - script = repository.Get("test-move-script.js"); - script.Path = "moved/test-move-script.js"; - repository.AddOrUpdate(script); - unitOfWork.Flush(); + // Act + script = repository.Get("test-move-script.js"); + script.Path = "moved/test-move-script.js"; + repository.AddOrUpdate(script); + unitOfWork.Flush(); - var existsOld = repository.Exists("test-move-script.js"); - var existsNew = repository.Exists("moved/test-move-script.js"); + var existsOld = repository.Exists("test-move-script.js"); + var existsNew = repository.Exists("moved/test-move-script.js"); - script = repository.Get("moved/test-move-script.js"); + script = repository.Get("moved/test-move-script.js"); - // Assert - Assert.IsNotNull(script); - Assert.IsFalse(existsOld); - Assert.IsTrue(existsNew); - Assert.AreEqual(content, script.Content); + // Assert + Assert.IsNotNull(script); + Assert.IsFalse(existsOld); + Assert.IsTrue(existsNew); + Assert.AreEqual(content, script.Content); + } } [Test] @@ -226,63 +243,65 @@ namespace Umbraco.Tests.Persistence.Repositories // unless noted otherwise, no changes / 7.2.8 var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); - - var script = new Script("test-path-1.js") { Content = "// script" }; - repository.AddOrUpdate(script); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("test-path-1.js")); - Assert.AreEqual("test-path-1.js", script.Path); - Assert.AreEqual("/scripts/test-path-1.js", script.VirtualPath); - - script = new Script("path-2/test-path-2.js") { Content = "// script" }; - repository.AddOrUpdate(script); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.js")); - Assert.AreEqual("path-2\\test-path-2.js", script.Path); // fixed in 7.3 - 7.2.8 does not update the path - Assert.AreEqual("/scripts/path-2/test-path-2.js", script.VirtualPath); - - script = repository.Get("path-2/test-path-2.js"); - Assert.IsNotNull(script); - Assert.AreEqual("path-2\\test-path-2.js", script.Path); - Assert.AreEqual("/scripts/path-2/test-path-2.js", script.VirtualPath); - - script = new Script("path-2\\test-path-3.js") { Content = "// script" }; - repository.AddOrUpdate(script); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.js")); - Assert.AreEqual("path-2\\test-path-3.js", script.Path); - Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath); - - script = repository.Get("path-2/test-path-3.js"); - Assert.IsNotNull(script); - Assert.AreEqual("path-2\\test-path-3.js", script.Path); - Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath); - - script = repository.Get("path-2\\test-path-3.js"); - Assert.IsNotNull(script); - Assert.AreEqual("path-2\\test-path-3.js", script.Path); - Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath); - - script = new Script("\\test-path-4.js") { Content = "// script" }; - Assert.Throws(() => // fixed in 7.3 - 7.2.8 used to strip the \ + using (var unitOfWork = provider.CreateUnitOfWork()) { + var repository = new ScriptRepository(unitOfWork, _fileSystem, Mock.Of()); + + var script = new Script("test-path-1.js") { Content = "// script" }; repository.AddOrUpdate(script); - }); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("test-path-1.js")); + Assert.AreEqual("test-path-1.js", script.Path); + Assert.AreEqual("/scripts/test-path-1.js", script.VirtualPath); - script = repository.Get("missing.js"); - Assert.IsNull(script); + script = new Script("path-2/test-path-2.js") { Content = "// script" }; + repository.AddOrUpdate(script); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.js")); + Assert.AreEqual("path-2\\test-path-2.js", script.Path); // fixed in 7.3 - 7.2.8 does not update the path + Assert.AreEqual("/scripts/path-2/test-path-2.js", script.VirtualPath); - // fixed in 7.3 - 7.2.8 used to... - Assert.Throws(() => - { - script = repository.Get("\\test-path-4.js"); // outside the filesystem, does not exist - }); - Assert.Throws(() => - { - script = repository.Get("../packages.config"); // outside the filesystem, exists - }); + script = repository.Get("path-2/test-path-2.js"); + Assert.IsNotNull(script); + Assert.AreEqual("path-2\\test-path-2.js", script.Path); + Assert.AreEqual("/scripts/path-2/test-path-2.js", script.VirtualPath); + + script = new Script("path-2\\test-path-3.js") { Content = "// script" }; + repository.AddOrUpdate(script); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.js")); + Assert.AreEqual("path-2\\test-path-3.js", script.Path); + Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath); + + script = repository.Get("path-2/test-path-3.js"); + Assert.IsNotNull(script); + Assert.AreEqual("path-2\\test-path-3.js", script.Path); + Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath); + + script = repository.Get("path-2\\test-path-3.js"); + Assert.IsNotNull(script); + Assert.AreEqual("path-2\\test-path-3.js", script.Path); + Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath); + + script = new Script("\\test-path-4.js") { Content = "// script" }; + Assert.Throws(() => // fixed in 7.3 - 7.2.8 used to strip the \ + { + repository.AddOrUpdate(script); + }); + + script = repository.Get("missing.js"); + Assert.IsNull(script); + + // fixed in 7.3 - 7.2.8 used to... + Assert.Throws(() => + { + script = repository.Get("\\test-path-4.js"); // outside the filesystem, does not exist + }); + Assert.Throws(() => + { + script = repository.Get("../packages.config"); // outside the filesystem, exists + }); + } } [TearDown] diff --git a/src/Umbraco.Tests/Persistence/Repositories/StylesheetRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/StylesheetRepositoryTest.cs index 62956e920e..fff6e8a483 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/StylesheetRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/StylesheetRepositoryTest.cs @@ -35,14 +35,15 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - - // Act - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + // Act + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - // Assert - Assert.That(repository, Is.Not.Null); + // Assert + Assert.That(repository, Is.Not.Null); + } } [Test] @@ -50,17 +51,18 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + // Act + var stylesheet = new Stylesheet("test-add.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); - // Act - var stylesheet = new Stylesheet("test-add.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); - - //Assert - Assert.That(_fileSystem.FileExists("test-add.css"), Is.True); + //Assert + Assert.That(_fileSystem.FileExists("test-add.css"), Is.True); + } } [Test] @@ -68,26 +70,27 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - // Act - var stylesheet = new Stylesheet("test-update.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); + // Act + var stylesheet = new Stylesheet("test-update.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); - var stylesheetUpdate = repository.Get("test-update.css"); - stylesheetUpdate.Content = "body { color:#000; }"; - repository.AddOrUpdate(stylesheetUpdate); - unitOfWork.Flush(); + var stylesheetUpdate = repository.Get("test-update.css"); + stylesheetUpdate.Content = "body { color:#000; }"; + repository.AddOrUpdate(stylesheetUpdate); + unitOfWork.Flush(); - var stylesheetUpdated = repository.Get("test-update.css"); + var stylesheetUpdated = repository.Get("test-update.css"); - //Assert - Assert.That(stylesheetUpdated, Is.Not.Null); - Assert.That(stylesheetUpdated.HasIdentity, Is.True); - Assert.That(stylesheetUpdated.Content, Is.EqualTo("body { color:#000; }")); + //Assert + Assert.That(stylesheetUpdated, Is.Not.Null); + Assert.That(stylesheetUpdated.HasIdentity, Is.True); + Assert.That(stylesheetUpdated.Content, Is.EqualTo("body { color:#000; }")); + } } [Test] @@ -95,29 +98,30 @@ namespace Umbraco.Tests.Persistence.Repositories { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - // Act - var stylesheet = new Stylesheet("test-update.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); + // Act + var stylesheet = new Stylesheet("test-update.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); - stylesheet.AddProperty(new StylesheetProperty("Test", "p", "font-size:2em;")); + stylesheet.AddProperty(new StylesheetProperty("Test", "p", "font-size:2em;")); - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); - //re-get - stylesheet = repository.Get(stylesheet.Name); + //re-get + stylesheet = repository.Get(stylesheet.Name); - //Assert - Assert.That(stylesheet.Content, Is.EqualTo(@"body { color:#000; } .bold {font-weight:bold;} + //Assert + Assert.That(stylesheet.Content, Is.EqualTo(@"body { color:#000; } .bold {font-weight:bold;} /**umb_name:Test*/ p{font-size:2em;}")); - Assert.AreEqual(1, stylesheet.Properties.Count()); + Assert.AreEqual(1, stylesheet.Properties.Count()); + } } [Test] @@ -125,19 +129,19 @@ p{font-size:2em;}")); { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + // Act + var stylesheet = new Stylesheet("test-update.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); - // Act - var stylesheet = new Stylesheet("test-update.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); - - stylesheet.AddProperty(new StylesheetProperty("Test", "p", "font-size:2em;")); - - Assert.Throws(() => stylesheet.AddProperty(new StylesheetProperty("test", "p", "font-size:2em;"))); + stylesheet.AddProperty(new StylesheetProperty("Test", "p", "font-size:2em;")); + Assert.Throws(() => stylesheet.AddProperty(new StylesheetProperty("test", "p", "font-size:2em;"))); + } } [Test] @@ -145,20 +149,21 @@ p{font-size:2em;}")); { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - // Act - var stylesheet = new Stylesheet("test-delete.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); + // Act + var stylesheet = new Stylesheet("test-delete.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); - repository.Delete(stylesheet); - unitOfWork.Flush(); + repository.Delete(stylesheet); + unitOfWork.Flush(); - //Assert - Assert.That(_fileSystem.FileExists("test-delete.css"), Is.False); + //Assert + Assert.That(_fileSystem.FileExists("test-delete.css"), Is.False); + } } [Test] @@ -166,18 +171,19 @@ p{font-size:2em;}")); { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - // Act - var stylesheet = repository.Get("styles.css"); + // Act + var stylesheet = repository.Get("styles.css"); - // Assert - Assert.That(stylesheet, Is.Not.Null); - Assert.That(stylesheet.HasIdentity, Is.True); - Assert.That(stylesheet.Content, Is.EqualTo("body {background:#EE7600; color:#FFF;}")); - Assert.That(repository.ValidateStylesheet(stylesheet), Is.True); + // Assert + Assert.That(stylesheet, Is.Not.Null); + Assert.That(stylesheet.HasIdentity, Is.True); + Assert.That(stylesheet.Content, Is.EqualTo("body {background:#EE7600; color:#FFF;}")); + Assert.That(repository.ValidateStylesheet(stylesheet), Is.True); + } } [Test] @@ -185,22 +191,23 @@ p{font-size:2em;}")); { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - var stylesheet = new Stylesheet("styles-v2.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); + var stylesheet = new Stylesheet("styles-v2.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); - // Act - var stylesheets = repository.GetAll(); + // Act + var stylesheets = repository.GetAll(); - // Assert - Assert.That(stylesheets, Is.Not.Null); - Assert.That(stylesheets.Any(), Is.True); - Assert.That(stylesheets.Any(x => x == null), Is.False); - Assert.That(stylesheets.Count(), Is.EqualTo(2)); + // Assert + Assert.That(stylesheets, Is.Not.Null); + Assert.That(stylesheets.Any(), Is.True); + Assert.That(stylesheets.Any(x => x == null), Is.False); + Assert.That(stylesheets.Count(), Is.EqualTo(2)); + } } [Test] @@ -208,22 +215,23 @@ p{font-size:2em;}")); { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - var stylesheet = new Stylesheet("styles-v2.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); + var stylesheet = new Stylesheet("styles-v2.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); - // Act - var stylesheets = repository.GetAll("styles-v2.css", "styles.css"); + // Act + var stylesheets = repository.GetAll("styles-v2.css", "styles.css"); - // Assert - Assert.That(stylesheets, Is.Not.Null); - Assert.That(stylesheets.Any(), Is.True); - Assert.That(stylesheets.Any(x => x == null), Is.False); - Assert.That(stylesheets.Count(), Is.EqualTo(2)); + // Assert + Assert.That(stylesheets, Is.Not.Null); + Assert.That(stylesheets.Any(), Is.True); + Assert.That(stylesheets.Any(x => x == null), Is.False); + Assert.That(stylesheets.Count(), Is.EqualTo(2)); + } } [Test] @@ -231,15 +239,16 @@ p{font-size:2em;}")); { // Arrange var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - - var repository = new StylesheetRepository(unitOfWork, _fileSystem); + using (var unitOfWork = provider.CreateUnitOfWork()) + { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); - // Act - var exists = repository.Exists("styles.css"); + // Act + var exists = repository.Exists("styles.css"); - // Assert - Assert.That(exists, Is.True); + // Assert + Assert.That(exists, Is.True); + } } [Test] @@ -248,64 +257,66 @@ p{font-size:2em;}")); // unless noted otherwise, no changes / 7.2.8 var provider = new FileUnitOfWorkProvider(); - var unitOfWork = provider.CreateUnitOfWork(); - var repository = new StylesheetRepository(unitOfWork, _fileSystem); - - var stylesheet = new Stylesheet("test-path-1.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("test-path-1.css")); - Assert.AreEqual("test-path-1.css", stylesheet.Path); - Assert.AreEqual("/css/test-path-1.css", stylesheet.VirtualPath); - - stylesheet = new Stylesheet("path-2/test-path-2.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.css")); - Assert.AreEqual("path-2\\test-path-2.css", stylesheet.Path); // fixed in 7.3 - 7.2.8 does not update the path - Assert.AreEqual("/css/path-2/test-path-2.css", stylesheet.VirtualPath); - - stylesheet = repository.Get("path-2/test-path-2.css"); - Assert.IsNotNull(stylesheet); - Assert.AreEqual("path-2\\test-path-2.css", stylesheet.Path); - Assert.AreEqual("/css/path-2/test-path-2.css", stylesheet.VirtualPath); - - stylesheet = new Stylesheet("path-2\\test-path-3.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - repository.AddOrUpdate(stylesheet); - unitOfWork.Flush(); - Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.css")); - Assert.AreEqual("path-2\\test-path-3.css", stylesheet.Path); - Assert.AreEqual("/css/path-2/test-path-3.css", stylesheet.VirtualPath); - - stylesheet = repository.Get("path-2/test-path-3.css"); - Assert.IsNotNull(stylesheet); - Assert.AreEqual("path-2\\test-path-3.css", stylesheet.Path); - Assert.AreEqual("/css/path-2/test-path-3.css", stylesheet.VirtualPath); - - stylesheet = repository.Get("path-2\\test-path-3.css"); - Assert.IsNotNull(stylesheet); - Assert.AreEqual("path-2\\test-path-3.css", stylesheet.Path); - Assert.AreEqual("/css/path-2/test-path-3.css", stylesheet.VirtualPath); - - stylesheet = new Stylesheet("\\test-path-4.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; - Assert.Throws(() => // fixed in 7.3 - 7.2.8 used to strip the \ + using (var unitOfWork = provider.CreateUnitOfWork()) { + var repository = new StylesheetRepository(unitOfWork, _fileSystem); + + var stylesheet = new Stylesheet("test-path-1.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; repository.AddOrUpdate(stylesheet); - }); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("test-path-1.css")); + Assert.AreEqual("test-path-1.css", stylesheet.Path); + Assert.AreEqual("/css/test-path-1.css", stylesheet.VirtualPath); - // fixed in 7.3 - 7.2.8 used to throw - stylesheet = repository.Get("missing.css"); - Assert.IsNull(stylesheet); + stylesheet = new Stylesheet("path-2/test-path-2.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.css")); + Assert.AreEqual("path-2\\test-path-2.css", stylesheet.Path); // fixed in 7.3 - 7.2.8 does not update the path + Assert.AreEqual("/css/path-2/test-path-2.css", stylesheet.VirtualPath); - // fixed in 7.3 - 7.2.8 used to... - Assert.Throws(() => - { - stylesheet = repository.Get("\\test-path-4.css"); // outside the filesystem, does not exist - }); - Assert.Throws(() => - { - stylesheet = repository.Get("../packages.config"); // outside the filesystem, exists - }); + stylesheet = repository.Get("path-2/test-path-2.css"); + Assert.IsNotNull(stylesheet); + Assert.AreEqual("path-2\\test-path-2.css", stylesheet.Path); + Assert.AreEqual("/css/path-2/test-path-2.css", stylesheet.VirtualPath); + + stylesheet = new Stylesheet("path-2\\test-path-3.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + repository.AddOrUpdate(stylesheet); + unitOfWork.Flush(); + Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.css")); + Assert.AreEqual("path-2\\test-path-3.css", stylesheet.Path); + Assert.AreEqual("/css/path-2/test-path-3.css", stylesheet.VirtualPath); + + stylesheet = repository.Get("path-2/test-path-3.css"); + Assert.IsNotNull(stylesheet); + Assert.AreEqual("path-2\\test-path-3.css", stylesheet.Path); + Assert.AreEqual("/css/path-2/test-path-3.css", stylesheet.VirtualPath); + + stylesheet = repository.Get("path-2\\test-path-3.css"); + Assert.IsNotNull(stylesheet); + Assert.AreEqual("path-2\\test-path-3.css", stylesheet.Path); + Assert.AreEqual("/css/path-2/test-path-3.css", stylesheet.VirtualPath); + + stylesheet = new Stylesheet("\\test-path-4.css") { Content = "body { color:#000; } .bold {font-weight:bold;}" }; + Assert.Throws(() => // fixed in 7.3 - 7.2.8 used to strip the \ + { + repository.AddOrUpdate(stylesheet); + }); + + // fixed in 7.3 - 7.2.8 used to throw + stylesheet = repository.Get("missing.css"); + Assert.IsNull(stylesheet); + + // fixed in 7.3 - 7.2.8 used to... + Assert.Throws(() => + { + stylesheet = repository.Get("\\test-path-4.css"); // outside the filesystem, does not exist + }); + Assert.Throws(() => + { + stylesheet = repository.Get("../packages.config"); // outside the filesystem, exists + }); + } } [TearDown] diff --git a/src/Umbraco.Tests/Services/ContentServiceTests.cs b/src/Umbraco.Tests/Services/ContentServiceTests.cs index 552b3f7e82..012fe72b81 100644 --- a/src/Umbraco.Tests/Services/ContentServiceTests.cs +++ b/src/Umbraco.Tests/Services/ContentServiceTests.cs @@ -864,8 +864,10 @@ namespace Umbraco.Tests.Services bool published = contentService.Publish(content, 0); var provider = new NPocoUnitOfWorkProvider(Logger); - var uow = provider.CreateUnitOfWork(); - Assert.IsTrue(uow.Database.Exists(content.Id)); + using (var uow = provider.CreateUnitOfWork()) + { + Assert.IsTrue(uow.Database.Exists(content.Id)); + } // Act bool unpublished = contentService.UnPublish(content, 0); @@ -875,8 +877,10 @@ namespace Umbraco.Tests.Services Assert.That(unpublished, Is.True); Assert.That(content.Published, Is.False); - uow = provider.CreateUnitOfWork(); - Assert.IsFalse(uow.Database.Exists(content.Id)); + using (var uow = provider.CreateUnitOfWork()) + { + Assert.IsFalse(uow.Database.Exists(content.Id)); + } } /// diff --git a/src/Umbraco.Tests/Services/MediaServiceTests.cs b/src/Umbraco.Tests/Services/MediaServiceTests.cs index 055a359940..8cd2daa0a6 100644 --- a/src/Umbraco.Tests/Services/MediaServiceTests.cs +++ b/src/Umbraco.Tests/Services/MediaServiceTests.cs @@ -91,10 +91,10 @@ namespace Umbraco.Tests.Services mediaService.Save(media); var provider = new NPocoUnitOfWorkProvider(Logger); - var uow = provider.CreateUnitOfWork(); - - Assert.IsTrue(uow.Database.Exists(media.Id)); - + using (var uow = provider.CreateUnitOfWork()) + { + Assert.IsTrue(uow.Database.Exists(media.Id)); + } } private Tuple CreateTrashedTestMedia()