public void MigrationCanAddPostMigration() { IMigrationBuilder 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(); IScope 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 }; MigrationPlan 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); IMigrationPlanExecutor executor = GetMigrationPlanExecutor(scopeProvider, scopeProvider, builder); upgrader.Execute( executor, scopeProvider, Mock.Of <IKeyValueService>()); Assert.AreEqual(1, TestMigration.MigrateCount); Assert.AreEqual(1, TestPostMigration.MigrateCount); }
public void CanExecute() { NullLoggerFactory loggerFactory = NullLoggerFactory.Instance; var database = new TestDatabase(); IDatabaseScope scope = Mock.Of <IDatabaseScope>(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 }; IMigrationBuilder migrationBuilder = Mock.Of <IMigrationBuilder>(); Mock.Get(migrationBuilder) .Setup(x => x.Build(It.IsAny <Type>(), It.IsAny <IMigrationContext>())) .Returns <Type, IMigrationContext>((t, c) => { switch (t.Name) { case "DeleteRedirectUrlTable": return(new DeleteRedirectUrlTable(c)); case "NoopMigration": return(new NoopMigration(c)); default: throw new NotSupportedException(); } }); var executor = new MigrationPlanExecutor(scopeProvider, scopeProvider, loggerFactory, migrationBuilder); MigrationPlan plan = new MigrationPlan("default") .From(string.Empty) .To <DeleteRedirectUrlTable>("{4A9A1A8F-0DA1-4BCF-AD06-C19D79152E35}") .To <NoopMigration>("VERSION.33"); IKeyValueService kvs = Mock.Of <IKeyValueService>(); Mock.Get(kvs).Setup(x => x.GetValue(It.IsAny <string>())) .Returns <string>(k => k == "Umbraco.Tests.MigrationPlan" ? string.Empty : null); string state; using (IScope s = scopeProvider.CreateScope()) { // read current state var sourceState = kvs.GetValue("Umbraco.Tests.MigrationPlan") ?? string.Empty; // execute plan state = executor.Execute(plan, sourceState); // save new state kvs.SetValue("Umbraco.Tests.MigrationPlan", sourceState, state); s.Complete(); } Assert.AreEqual("VERSION.33", state); Assert.AreEqual(1, database.Operations.Count); Assert.AreEqual("DROP TABLE [umbracoRedirectUrl]", database.Operations[0].Sql); }