Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Migrations/PostMigrationTests.cs
Nikolaj Geisle 7aeb400fce V10: fix build warnings in test projects (#12509)
* Run code cleanup

* Dotnet format benchmarks project

* Fix up Test.Common

* Run dotnet format + manual cleanup

* Run code cleanup for unit tests

* Run dotnet format

* Fix up errors

* Manual cleanup of Unit test project

* Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Fix according to review

* Fix after merge

* Fix errors

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
Co-authored-by: Zeegaan <nge@umbraco.dk>
2022-06-21 08:09:38 +02:00

165 lines
5.4 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using NPoco;
using NUnit.Framework;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Migrations;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Cms.Infrastructure.Migrations.Upgrade;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Scoping;
using Umbraco.Cms.Persistence.SqlServer.Services;
using Umbraco.Cms.Tests.Common.TestHelpers;
using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Migrations;
[TestFixture]
public class PostMigrationTests
{
private static readonly ILoggerFactory s_loggerFactory = NullLoggerFactory.Instance;
private IMigrationPlanExecutor GetMigrationPlanExecutor(
ICoreScopeProvider scopeProvider,
IScopeAccessor scopeAccessor,
IMigrationBuilder builder)
=> new MigrationPlanExecutor(scopeProvider, scopeAccessor, s_loggerFactory, builder);
[Test]
public void ExecutesPlanPostMigration()
{
var builder = Mock.Of<IMigrationBuilder>();
Mock.Get(builder)
.Setup(x => x.Build(It.IsAny<Type>(), It.IsAny<IMigrationContext>()))
.Returns<Type, IMigrationContext>((t, c) =>
{
switch (t.Name)
{
case nameof(NoopMigration):
return new NoopMigration(c);
case nameof(TestPostMigration):
return new TestPostMigration(c);
default:
throw new NotSupportedException();
}
});
var database = new TestDatabase();
var scope = Mock.Of<IScope>(x => x.Notifications == Mock.Of<IScopedNotificationPublisher>());
Mock.Get(scope)
.Setup(x => x.Database)
.Returns(database);
var sqlContext = new SqlContext(
new SqlServerSyntaxProvider(Options.Create(new GlobalSettings())),
DatabaseType.SQLCe,
Mock.Of<IPocoDataFactory>());
var scopeProvider = new MigrationTests.TestScopeProvider(scope) { SqlContext = sqlContext };
var plan = new MigrationPlan("Test")
.From(string.Empty).To("done");
plan.AddPostMigration<TestPostMigration>();
TestPostMigration.MigrateCount = 0;
var upgrader = new Upgrader(plan);
var executor = GetMigrationPlanExecutor(scopeProvider, scopeProvider, builder);
upgrader.Execute(
executor,
scopeProvider,
Mock.Of<IKeyValueService>());
Assert.AreEqual(1, TestPostMigration.MigrateCount);
}
[Test]
public void MigrationCanAddPostMigration()
{
var builder = Mock.Of<IMigrationBuilder>();
Mock.Get(builder)
.Setup(x => x.Build(It.IsAny<Type>(), It.IsAny<IMigrationContext>()))
.Returns<Type, IMigrationContext>((t, c) =>
{
switch (t.Name)
{
case nameof(NoopMigration):
return new NoopMigration(c);
case nameof(TestMigration):
return new TestMigration(c);
case nameof(TestPostMigration):
return new TestPostMigration(c);
default:
throw new NotSupportedException();
}
});
var database = new TestDatabase();
var scope = Mock.Of<IScope>(x => x.Notifications == Mock.Of<IScopedNotificationPublisher>());
Mock.Get(scope)
.Setup(x => x.Database)
.Returns(database);
var sqlContext = new SqlContext(
new SqlServerSyntaxProvider(Options.Create(new GlobalSettings())),
DatabaseType.SQLCe,
Mock.Of<IPocoDataFactory>());
var scopeProvider = new MigrationTests.TestScopeProvider(scope) { SqlContext = sqlContext };
var plan = new MigrationPlan("Test")
.From(string.Empty).To<TestMigration>("done");
TestMigration.MigrateCount = 0;
TestPostMigration.MigrateCount = 0;
new MigrationContext(plan, database, s_loggerFactory.CreateLogger<MigrationContext>());
var upgrader = new Upgrader(plan);
var executor = GetMigrationPlanExecutor(scopeProvider, scopeProvider, builder);
upgrader.Execute(
executor,
scopeProvider,
Mock.Of<IKeyValueService>());
Assert.AreEqual(1, TestMigration.MigrateCount);
Assert.AreEqual(1, TestPostMigration.MigrateCount);
}
public class TestMigration : MigrationBase
{
public TestMigration(IMigrationContext context)
: base(context)
{
}
public static int MigrateCount { get; set; }
protected override void Migrate()
{
MigrateCount++;
Context.AddPostMigration<TestPostMigration>();
}
}
public class TestPostMigration : MigrationBase
{
public TestPostMigration(IMigrationContext context)
: base(context)
{
}
public static int MigrateCount { get; set; }
protected override void Migrate() => MigrateCount++;
}
}