Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Migrations/MigrationTests.cs
Bjarke Berg 935c3b8e42 Initialize important services before unattended installs (#17366)
* Added new notification to hook in after the premigrations and use this to init different services.

* Force MaxDegreeOfParallelism to 1, while investigating scopes

* Tried some more workarounds

* Updated scopes and changed parallel to non parallel to ensure migration works

* Missing scope

* Make it parallel again - The secret is, the SuppressFlow needs to be when you create the task, but not on the await!.

* Fixed issue when DEBUG_SCOPES is not added to tests.

* Remove test exception

* Try build on ubuntu again, even that we know it can be stuck. Just a test to see if all tests pass

* Updated comment

---------

Co-authored-by: kjac <kja@umbraco.dk>
2024-10-28 12:10:38 +01:00

139 lines
4.2 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Data;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Persistence.Querying;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Scoping;
using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope;
using IScopeProvider = Umbraco.Cms.Infrastructure.Scoping.IScopeProvider;
#if DEBUG_SCOPES
using System.Collections.Generic;
#endif
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Migrations;
[TestFixture]
public class MigrationTests
{
public class TestScopeProvider : IScopeProvider, IScopeAccessor
{
public TestScopeProvider(IScope scope) => AmbientScope = scope;
public IScope CreateScope(
IsolationLevel isolationLevel = IsolationLevel.Unspecified,
RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
IEventDispatcher eventDispatcher = null,
IScopedNotificationPublisher notificationPublisher = null,
bool? scopeFileSystems = null,
bool callContext = false,
bool autoComplete = false) => AmbientScope;
public IScope CreateDetachedScope(
IsolationLevel isolationLevel = IsolationLevel.Unspecified,
RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
IEventDispatcher eventDispatcher = null,
IScopedNotificationPublisher notificationPublisher = null,
bool? scopeFileSystems = null) => throw new NotImplementedException();
public void AttachScope(IScope scope, bool callContext = false) => throw new NotImplementedException();
public IScope DetachScope() => throw new NotImplementedException();
public IScopeContext Context { get; set; }
public IQuery<T> CreateQuery<T>() => SqlContext.Query<T>();
public ISqlContext SqlContext { get; set; }
public IScope AmbientScope { get; }
#if DEBUG_SCOPES
public IEnumerable<ScopeInfo> ScopeInfos => throw new NotImplementedException();
public ScopeInfo GetScopeInfo(IScope scope) => throw new NotImplementedException();
#endif
}
private class TestPlan : MigrationPlan
{
public TestPlan()
: base("Test")
{
}
}
private MigrationContext GetMigrationContext() =>
new(
new TestPlan(),
Mock.Of<IUmbracoDatabase>(),
Mock.Of<ILogger<MigrationContext>>());
[Test]
public void RunGoodMigration()
{
var migrationContext = GetMigrationContext();
MigrationBase migration = new GoodMigration(migrationContext);
migration.Run();
}
[Test]
public void DetectBadMigration1()
{
var migrationContext = GetMigrationContext();
MigrationBase migration = new BadMigration1(migrationContext);
Assert.Throws<IncompleteMigrationExpressionException>(() => migration.Run());
}
[Test]
public void DetectBadMigration2()
{
var migrationContext = GetMigrationContext();
MigrationBase migration = new BadMigration2(migrationContext);
Assert.Throws<IncompleteMigrationExpressionException>(() => migration.Run());
}
public class GoodMigration : MigrationBase
{
public GoodMigration(IMigrationContext context)
: base(context)
{
}
protected override void Migrate() => Execute.Sql(string.Empty).Do();
}
public class BadMigration1 : MigrationBase
{
public BadMigration1(IMigrationContext context)
: base(context)
{
}
protected override void Migrate() => Alter.Table("foo"); // stop here, don't Do it
}
public class BadMigration2 : MigrationBase
{
public BadMigration2(IMigrationContext context)
: base(context)
{
}
protected override void Migrate()
{
Alter.Table("foo"); // stop here, don't Do it
// and try to start another one
Alter.Table("bar");
}
}
}