예제 #1
0
        public async Task BaseOrchestrator_Provision_SchemaCreated_If_SetupHasTables()
        {
            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();
            var setup   = new SyncSetup(new string[] { "SalesLT.Product" });

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

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

            var schema = await localOrchestrator.ProvisionAsync(provision);

            var context = localOrchestrator.GetContext();

            Assert.Equal(SyncStage.Provisioning, context.SyncStage);
            Assert.Single(schema.Tables);
            Assert.Equal("SalesLT.Product", schema.Tables[0].GetFullName());
            Assert.Equal(17, schema.Tables[0].Columns.Count);

            HelperDatabase.DropDatabase(ProviderType.Sql, dbName);
        }
예제 #2
0
        public async Task BaseOrchestrator_GetSchema_SetupColumnsDefined_ShouldReturn_SchemaWithSetupColumnsOnly()
        {
            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 options = new SyncOptions();
            // Create a bad setup with a non existing table

            var tables = new string[] { "Customer", "Address", "CustomerAddress" };
            var setup  = new SyncSetup(tables);

            setup.Tables["Customer"].Columns.AddRange(new string[] { "CustomerID", "FirstName", "LastName", "CompanyName" });

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

            var schema = await localOrchestrator.GetSchemaAsync();

            Assert.Equal(SyncStage.SchemaReading, localOrchestrator.GetContext().SyncStage);
            Assert.Equal(3, schema.Tables.Count);

            // Only 4 columns shoud be part of Customer table
            Assert.Equal(4, schema.Tables["Customer"].Columns.Count);

            Assert.Equal(9, schema.Tables["Address"].Columns.Count);
            Assert.Equal(5, schema.Tables["CustomerAddress"].Columns.Count);

            HelperDatabase.DropDatabase(ProviderType.Sql, dbName);
        }
        public async Task LocalOrchestrator_BeginSession_ShouldIncrement_SyncStage()
        {
            var options        = new SyncOptions();
            var setup          = new SyncSetup();
            var provider       = new SqlSyncProvider();
            var onSessionBegin = false;


            var localOrchestrator = new LocalOrchestrator(provider, options, setup);
            var ctx = localOrchestrator.GetContext();

            localOrchestrator.OnSessionBegin(args =>
            {
                Assert.Equal(SyncStage.BeginSession, args.Context.SyncStage);
                Assert.IsType <SessionBeginArgs>(args);
                Assert.Null(args.Connection);
                Assert.Null(args.Transaction);
                onSessionBegin = true;
            });

            await localOrchestrator.BeginSessionAsync();

            Assert.Equal(SyncStage.BeginSession, ctx.SyncStage);
            Assert.True(onSessionBegin);
        }
예제 #4
0
        public async Task BaseOrchestrator_GetSchema_ShouldReturnSchema()
        {
            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();
            var setup   = new SyncSetup(this.Tables);

            var onSchemaRead    = false;
            var onSchemaReading = false;

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

            localOrchestrator.OnSchemaLoading(args =>
            {
                onSchemaReading = true;
            });

            localOrchestrator.OnSchemaLoaded(args =>
            {
                Assert.IsType <SchemaLoadedArgs>(args);
                Assert.Equal(SyncStage.SchemaReading, args.Context.SyncStage);
                Assert.Equal(scopeName, args.Context.ScopeName);
                Assert.NotNull(args.Connection);
                Assert.Null(args.Transaction);
                Assert.Equal(ConnectionState.Open, args.Connection.State);
                Assert.Equal(17, args.Schema.Tables.Count);
                onSchemaRead = true;
            });

            AssertConnectionAndTransaction(localOrchestrator, SyncStage.SchemaReading);

            var schema = await localOrchestrator.GetSchemaAsync();

            Assert.NotNull(schema);
            Assert.Equal(SyncStage.SchemaReading, localOrchestrator.GetContext().SyncStage);
            Assert.Equal(17, schema.Tables.Count);
            Assert.True(onSchemaRead);
            Assert.True(onSchemaReading);

            HelperDatabase.DropDatabase(ProviderType.Sql, dbName);
        }
예제 #5
0
        public void BaseOrchestrator_GetContext_ShouldBeInitialized()
        {
            var options  = new SyncOptions();
            var setup    = new SyncSetup();
            var provider = new SqlSyncProvider();

            var localOrchestrator = new LocalOrchestrator(provider, options, setup, "scope1");

            var ctx = localOrchestrator.GetContext();

            Assert.Equal(SyncStage.None, ctx.SyncStage);
            Assert.Equal(localOrchestrator.ScopeName, ctx.ScopeName);
            Assert.Equal(SyncType.Normal, ctx.SyncType);
            Assert.Equal(SyncWay.None, ctx.SyncWay);
            Assert.Null(ctx.Parameters);
        }
예제 #6
0
        public async Task LocalOrchestrator_EnsureScope_NewScope()
        {
            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();
            var setup   = new SyncSetup(this.Tables);

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

            var localScopeInfo = await localOrchestrator.GetClientScopeAsync();

            Assert.NotNull(localScopeInfo);
            Assert.Equal(scopeName, localScopeInfo.Name);
            Assert.True(localScopeInfo.IsNewScope);
            Assert.NotEqual(Guid.Empty, localScopeInfo.Id);
            Assert.Null(localScopeInfo.LastServerSyncTimestamp);
            Assert.Null(localScopeInfo.LastSync);
            Assert.Equal(0, localScopeInfo.LastSyncDuration);
            Assert.Null(localScopeInfo.LastSyncTimestamp);
            Assert.Null(localScopeInfo.Schema);
            Assert.Equal(SyncVersion.Current, new Version(localScopeInfo.Version));

            // Check context
            SyncContext syncContext = localOrchestrator.GetContext();

            Assert.Equal(scopeName, syncContext.ScopeName);
            Assert.NotEqual(Guid.Empty, syncContext.SessionId);
            Assert.Null(syncContext.Parameters);
            Assert.Equal(SyncStage.ScopeLoading, syncContext.SyncStage);
            Assert.Equal(SyncType.Normal, syncContext.SyncType);
            Assert.Equal(SyncWay.None, syncContext.SyncWay);


            HelperDatabase.DropDatabase(ProviderType.Sql, dbName);
        }
예제 #7
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);
        }