Files

139 lines
4.2 KiB
C#
Raw Permalink Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
2017-12-22 12:29:56 +01:00
using System.Data;
2020-09-17 13:36:26 +02:00
using Microsoft.Extensions.Logging;
2018-11-15 19:34:32 +01:00
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Events;
2022-01-13 17:44:11 +00:00
using Umbraco.Cms.Core.Persistence.Querying;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Cms.Infrastructure.Persistence;
2022-01-13 17:44:11 +00:00
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
2017-12-22 12:29:56 +01:00
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Migrations;
[TestFixture]
public class MigrationTests
2017-12-22 12:29:56 +01:00
{
public class TestScopeProvider : IScopeProvider, IScopeAccessor
2017-12-22 12:29:56 +01:00
{
public TestScopeProvider(IScope scope) => AmbientScope = scope;
2017-12-22 12:29:56 +01:00
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;
2017-12-22 12:29:56 +01:00
public IScope CreateDetachedScope(
IsolationLevel isolationLevel = IsolationLevel.Unspecified,
RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
IEventDispatcher eventDispatcher = null,
IScopedNotificationPublisher notificationPublisher = null,
bool? scopeFileSystems = null) => throw new NotImplementedException();
2017-12-22 12:29:56 +01:00
public void AttachScope(IScope scope, bool callContext = false) => throw new NotImplementedException();
2017-12-22 12:29:56 +01:00
public IScope DetachScope() => throw new NotImplementedException();
2017-12-22 12:29:56 +01:00
public IScopeContext Context { get; set; }
2020-09-17 13:36:26 +02:00
public IQuery<T> CreateQuery<T>() => SqlContext.Query<T>();
public ISqlContext SqlContext { get; set; }
2019-12-20 15:12:54 +11:00
public IScope AmbientScope { get; }
2019-12-20 15:12:54 +11:00
#if DEBUG_SCOPES
public IEnumerable<ScopeInfo> ScopeInfos => throw new NotImplementedException();
public ScopeInfo GetScopeInfo(IScope scope) => throw new NotImplementedException();
2019-12-20 15:12:54 +11:00
#endif
}
2018-11-15 19:34:32 +01:00
private class TestPlan : MigrationPlan
{
public TestPlan()
: base("Test")
{
}
}
private MigrationContext GetMigrationContext() =>
new(
new TestPlan(),
Mock.Of<IUmbracoDatabase>(),
Mock.Of<ILogger<MigrationContext>>());
2018-11-15 19:34:32 +01:00
[Test]
public async Task RunGoodMigration()
{
var migrationContext = GetMigrationContext();
MigrationBase migration = new GoodMigration(migrationContext);
await migration.RunAsync();
}
[Test]
public void DetectBadMigration1()
{
var migrationContext = GetMigrationContext();
MigrationBase migration = new BadMigration1(migrationContext);
Assert.ThrowsAsync<IncompleteMigrationExpressionException>(migration.RunAsync);
}
[Test]
public void DetectBadMigration2()
{
var migrationContext = GetMigrationContext();
MigrationBase migration = new BadMigration2(migrationContext);
Assert.ThrowsAsync<IncompleteMigrationExpressionException>(migration.RunAsync);
}
2018-11-15 19:34:32 +01:00
public class GoodMigration : MigrationBase
{
public GoodMigration(IMigrationContext context)
: base(context)
2018-11-15 19:34:32 +01:00
{
}
protected override void Migrate() => Execute.Sql(string.Empty).Do();
}
public class BadMigration1 : MigrationBase
{
public BadMigration1(IMigrationContext context)
: base(context)
{
2018-11-15 19:34:32 +01:00
}
protected override void Migrate() => Alter.Table("foo"); // stop here, don't Do it
}
public class BadMigration2 : MigrationBase
{
public BadMigration2(IMigrationContext context)
: base(context)
{
2018-11-15 19:34:32 +01:00
}
protected override void Migrate()
2018-11-15 19:34:32 +01:00
{
Alter.Table("foo"); // stop here, don't Do it
2018-11-15 19:34:32 +01:00
// and try to start another one
Alter.Table("bar");
2018-11-15 19:34:32 +01:00
}
2017-12-22 12:29:56 +01:00
}
}