Ejemplo n.º 1
0
    public void CanExecute()
    {
        var loggerFactory = NullLoggerFactory.Instance;

        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 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);

        var plan = new MigrationPlan("default")
                   .From(string.Empty)
                   .To <DeleteRedirectUrlTable>("{4A9A1A8F-0DA1-4BCF-AD06-C19D79152E35}")
                   .To <NoopMigration>("VERSION.33");

        var 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 (var 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);
    }