Exemplo n.º 1
0
        public async Task BaseOrchestrator_Provision_Check_Interceptors()
        {
            var dbName = HelperDatabase.GetRandomName("tcp_lo_");
            await HelperDatabase.CreateDatabaseAsync(ProviderType.Sql, dbName, true);

            var cs          = HelperDatabase.GetConnectionString(ProviderType.Sql, dbName);
            var sqlProvider = new SqlSyncProvider(cs);
            var ctx         = new AdventureWorksContext((dbName, ProviderType.Sql, sqlProvider), true, false);
            await ctx.Database.EnsureCreatedAsync();

            var scopeName = "scope";

            var options = new SyncOptions();

            // Options set here and we must check if TableBuilder has correct version
            options.UseBulkOperations = true;

            var setup = new SyncSetup(new string[] { "SalesLT.Product" });

            var onTableProvisioned     = false;
            var onTableProvisioning    = false;
            var onDatabaseProvisioned  = false;
            var onDatabaseProvisioning = false;

            var provision = SyncProvision.Table | SyncProvision.TrackingTable | SyncProvision.StoredProcedures | SyncProvision.Triggers;

            var localOrchestrator = new LocalOrchestrator(sqlProvider, options, setup, scopeName);

            // Assuming GetSchemaAsync() is correct
            // make the GetSchema call, before registrating interceptors
            var schema = await localOrchestrator.GetSchemaAsync();


            localOrchestrator.OnDatabaseProvisioning(args =>
            {
                Assert.IsType <DatabaseProvisioningArgs>(args);
                Assert.Equal(SyncStage.Provisioning, args.Context.SyncStage);
                Assert.Equal(scopeName, args.Context.ScopeName);
                Assert.NotNull(args.Connection);
                Assert.NotNull(args.Transaction);
                Assert.Equal(ConnectionState.Open, args.Connection.State);
                Assert.Equal(provision, args.Provision);
                Assert.Equal(schema, args.Schema);

                onDatabaseProvisioning = true;
            });

            localOrchestrator.OnDatabaseProvisioned(args =>
            {
                Assert.IsType <DatabaseProvisionedArgs>(args);
                Assert.Equal(SyncStage.Provisioned, args.Context.SyncStage);
                Assert.Equal(scopeName, args.Context.ScopeName);
                Assert.NotNull(args.Connection);
                Assert.Null(args.Transaction);
                Assert.Equal(ConnectionState.Closed, args.Connection.State);
                Assert.Equal(provision, args.Provision);
                Assert.Equal(schema, args.Schema);

                onDatabaseProvisioned = true;
            });

            localOrchestrator.OnTableProvisioning(args =>
            {
                Assert.IsType <TableProvisioningArgs>(args);
                Assert.Equal(SyncStage.Provisioning, args.Context.SyncStage);
                Assert.Equal(scopeName, args.Context.ScopeName);
                Assert.NotNull(args.Connection);
                Assert.NotNull(args.Transaction);
                Assert.Equal(ConnectionState.Open, args.Connection.State);
                Assert.True(args.TableBuilder.UseBulkProcedures);
                Assert.False(args.TableBuilder.UseChangeTracking);
                Assert.NotNull(args.TableBuilder.TableDescription);
                Assert.Equal("SalesLT.Product", args.TableBuilder.TableDescription.GetFullName());

                onTableProvisioning = true;
            });
            localOrchestrator.OnTableProvisioned(args =>
            {
                Assert.IsType <TableProvisionedArgs>(args);

                // We are still provisioning the tables
                Assert.Equal(SyncStage.Provisioning, args.Context.SyncStage);
                Assert.Equal(scopeName, args.Context.ScopeName);
                Assert.NotNull(args.Connection);
                Assert.NotNull(args.Transaction);
                Assert.Equal(ConnectionState.Open, args.Connection.State);
                Assert.NotNull(args.SchemaTable);
                Assert.Equal("SalesLT.Product", args.SchemaTable.GetFullName());

                onTableProvisioned = true;
            });

            AssertConnectionAndTransaction(localOrchestrator, SyncStage.Provisioning, SyncStage.Provisioned);

            await localOrchestrator.ProvisionAsync(schema, provision);

            Assert.Equal(SyncStage.Provisioned, localOrchestrator.GetContext().SyncStage);

            Assert.True(onTableProvisioned);
            Assert.True(onTableProvisioning);
            Assert.True(onDatabaseProvisioned);
            Assert.True(onDatabaseProvisioning);

            HelperDatabase.DropDatabase(ProviderType.Sql, dbName);
        }