Adds unit tests and fixes others

This commit is contained in:
Shannon
2017-01-19 18:25:45 +11:00
parent a275567dba
commit b51e571803
8 changed files with 183 additions and 14 deletions

View File

@@ -6,19 +6,6 @@ using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
{
public abstract class ScopeRepositoryService : RepositoryService
{
protected ScopeRepositoryService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, ILogger logger, IEventMessagesFactory eventMessagesFactory)
: base(provider, repositoryFactory, logger, eventMessagesFactory)
{
var scopeUow = provider as IScopeUnitOfWorkProvider;
if (scopeUow == null) throw new NotSupportedException("The provider type passed in: " + provider.GetType() + " is not of type " + typeof(IScopeUnitOfWorkProvider));
UowProvider = scopeUow;
}
internal new IScopeUnitOfWorkProvider UowProvider { get; private set; }
}
/// <summary>
/// Base service class
/// </summary>

View File

@@ -0,0 +1,21 @@
using System;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
{
public abstract class ScopeRepositoryService : RepositoryService
{
protected ScopeRepositoryService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, ILogger logger, IEventMessagesFactory eventMessagesFactory)
: base(provider, repositoryFactory, logger, eventMessagesFactory)
{
var scopeUow = provider as IScopeUnitOfWorkProvider;
if (scopeUow == null) throw new NotSupportedException("The provider type passed in: " + provider.GetType() + " is not of type " + typeof(IScopeUnitOfWorkProvider));
UowProvider = scopeUow;
}
internal new IScopeUnitOfWorkProvider UowProvider { get; private set; }
}
}

View File

@@ -574,6 +574,7 @@
<Compile Include="Services\RepositoryService.cs" />
<Compile Include="Services\OperationStatus.cs" />
<Compile Include="Services\OperationStatusType.cs" />
<Compile Include="Services\ScopeRepositoryService.cs" />
<Compile Include="Services\TaskService.cs" />
<Compile Include="Strings\Css\StylesheetHelper.cs" />
<Compile Include="Models\IPartialView.cs" />

View File

@@ -0,0 +1,67 @@
using System;
using System.Linq;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Events;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Scoping;
namespace Umbraco.Tests.Scoping
{
[TestFixture]
public class NoScopedEventManagerTests
{
[Test]
public void Does_Support_Event_Cancellation()
{
var scopeProvider = new ScopeProvider(Mock.Of<IDatabaseFactory2>());
Assert.IsTrue(scopeProvider.AmbientOrNoScope.EventManager.SupportsEventCancellation);
}
[Test]
public void Does_Immediately_Raise_Events()
{
var counter = 0;
this.DoThing1 += (sender, args) =>
{
counter++;
};
this.DoThing2 += (sender, args) =>
{
counter++;
};
this.DoThing3 += (sender, args) =>
{
counter++;
};
var scopeProvider = new ScopeProvider(Mock.Of<IDatabaseFactory2>());
scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing1, this, new EventArgs());
scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing2, this, new EventArgs());
scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing3, this, new EventArgs());
Assert.AreEqual(3, counter);
}
[Test]
public void Can_Not_Raise_Events_Later()
{
var scopeProvider = new ScopeProvider(Mock.Of<IDatabaseFactory2>());
scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing1, this, new EventArgs());
scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing2, this, new EventArgs());
scopeProvider.AmbientOrNoScope.EventManager.TrackEvent(DoThing3, this, new EventArgs());
Assert.AreEqual(0, scopeProvider.AmbientOrNoScope.EventManager.GetEvents().Count());
}
public event EventHandler DoThing1;
public event EventHandler<EventArgs> DoThing2;
public event TypedEventHandler<NoScopedEventManagerTests, EventArgs> DoThing3;
}
}

View File

@@ -4,6 +4,7 @@ using Umbraco.Core;
using Umbraco.Core.Persistence;
using Umbraco.Core.Scoping;
using Umbraco.Tests.TestHelpers;
using Umbraco.Core.Events;
namespace Umbraco.Tests.Scoping
{

View File

@@ -0,0 +1,90 @@
using System;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Events;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Scoping;
namespace Umbraco.Tests.Scoping
{
[TestFixture]
public class ScopedEventManagerTests
{
[Test]
public void Does_Not_Support_Event_Cancellation()
{
var provider = new PetaPocoUnitOfWorkProvider(new ScopeProvider(Mock.Of<IDatabaseFactory2>()));
using (var uow = provider.GetUnitOfWork())
{
Assert.IsFalse(uow.EventManager.SupportsEventCancellation);
}
}
[Test]
public void Does_Not_Immediately_Raise_Events()
{
this.DoThing1 += OnDoThingFail;
this.DoThing2 += OnDoThingFail;
this.DoThing3 += OnDoThingFail;
var provider = new PetaPocoUnitOfWorkProvider(new ScopeProvider(Mock.Of<IDatabaseFactory2>()));
using (var uow = provider.GetUnitOfWork())
{
uow.EventManager.TrackEvent(DoThing1, this, new EventArgs());
uow.EventManager.TrackEvent(DoThing2, this, new EventArgs());
uow.EventManager.TrackEvent(DoThing3, this, new EventArgs());
Assert.Pass();
}
}
[Test]
public void Can_Raise_Events_Later()
{
var counter = 0;
this.DoThing1 += (sender, args) =>
{
counter++;
};
this.DoThing2 += (sender, args) =>
{
counter++;
};
this.DoThing3 += (sender, args) =>
{
counter++;
};
var provider = new PetaPocoUnitOfWorkProvider(new ScopeProvider(Mock.Of<IDatabaseFactory2>()));
using (var uow = provider.GetUnitOfWork())
{
uow.EventManager.TrackEvent(DoThing1, this, new EventArgs());
uow.EventManager.TrackEvent(DoThing2, this, new EventArgs());
uow.EventManager.TrackEvent(DoThing3, this, new EventArgs());
Assert.AreEqual(0, counter);
foreach (var e in uow.EventManager.GetEvents())
{
e.RaiseEvent();
}
Assert.AreEqual(3, counter);
}
}
private void OnDoThingFail(object sender, EventArgs eventArgs)
{
Assert.Fail();
}
public event EventHandler DoThing1;
public event EventHandler<EventArgs> DoThing2;
public event TypedEventHandler<ScopedEventManagerTests, EventArgs> DoThing3;
}
}

View File

@@ -170,6 +170,8 @@
<Compile Include="Scheduling\DeployTest.cs" />
<Compile Include="Routing\NiceUrlRoutesTests.cs" />
<Compile Include="Scoping\LeakTests.cs" />
<Compile Include="Scoping\NoScopedEventManagerTests.cs" />
<Compile Include="Scoping\ScopedEventManagerTests.cs" />
<Compile Include="Scoping\ScopeTests.cs" />
<Compile Include="TestHelpers\Entities\MockedPropertyTypes.cs" />
<Compile Include="TryConvertToTests.cs" />

View File

@@ -396,7 +396,7 @@ namespace Umbraco.Tests.Web.Mvc
new Mock<IScopeUnitOfWorkProvider>().Object),
new Mock<IEntityService>().Object,
new RelationService(
new Mock<IDatabaseUnitOfWorkProvider>().Object,
new Mock<IScopeUnitOfWorkProvider>().Object,
new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), logger, Mock.Of<ISqlSyntaxProvider>(), umbracoSettings),
logger,
new TransientMessagesFactory(),