public void ImportNew ()
        {
            RunAsync (async delegate {
                var workspaceData = await DataStore.PutAsync (new WorkspaceData () {
                    RemoteId = 1,
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var userData = await DataStore.PutAsync (new UserData () {
                    RemoteId = 2,
                    ModifiedAt = new DateTime (2014, 1, 3),
                });
                var workspaceUserJson = new WorkspaceUserJson () {
                    Id = 2,
                    WorkspaceId = 1,
                    UserId = 2,
                    ModifiedAt = new DateTime (2014, 1, 3),
                };

                var workspaceUserData = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, workspaceUserJson));
                Assert.AreNotEqual (Guid.Empty, workspaceUserData.Id);
                Assert.AreEqual (2, workspaceUserData.RemoteId);
                Assert.AreEqual (new DateTime (2014, 1, 3), workspaceUserData.ModifiedAt);
                Assert.AreEqual (workspaceData.Id, workspaceUserData.WorkspaceId);
                Assert.AreEqual (userData.Id, workspaceUserData.UserId);
                Assert.IsFalse (workspaceUserData.IsDirty);
                Assert.IsFalse (workspaceUserData.RemoteRejected);
                Assert.IsNull (workspaceUserData.DeletedAt);
            });
        }
        public void ImportPastDeleted ()
        {
            RunAsync (async delegate {
                var workspaceData = await DataStore.PutAsync (new WorkspaceData () {
                    RemoteId = 1,
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var userData = await DataStore.PutAsync (new UserData () {
                    RemoteId = 2,
                    ModifiedAt = new DateTime (2014, 1, 3),
                });
                var workspaceUserData = await DataStore.PutAsync (new WorkspaceUserData () {
                    RemoteId = 2,
                    WorkspaceId = workspaceData.Id,
                    UserId = userData.Id,
                    ModifiedAt = new DateTime (2014, 1, 3),
                });

                var workspaceUserJson = new WorkspaceUserJson () {
                    Id = 2,
                    DeletedAt = new DateTime (2014, 1, 2),
                };

                var ret = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, workspaceUserJson));
                Assert.IsNull (ret);

                var rows = await DataStore.Table<WorkspaceUserData> ().Where (m => m.Id == workspaceUserData.Id).ToListAsync ();
                Assert.That (rows, Has.Exactly (0).Count);
            });
        }
        public void ImportUpdatedKeepDirtyLocal ()
        {
            RunAsync (async delegate {
                var workspaceData = await DataStore.PutAsync (new WorkspaceData () {
                    RemoteId = 1,
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var userData = await DataStore.PutAsync (new UserData () {
                    RemoteId = 2,
                    ModifiedAt = new DateTime (2014, 1, 3),
                });
                var workspaceUserData = await DataStore.PutAsync (new WorkspaceUserData () {
                    RemoteId = 2,
                    WorkspaceId = Guid.Empty,
                    UserId = Guid.Empty,
                    ModifiedAt = new DateTime (2014, 1, 2, 10, 0, 0, DateTimeKind.Utc),
                    IsDirty = true,
                });
                var workspaceUserJson = new WorkspaceUserJson () {
                    Id = 2,
                    WorkspaceId = 1,
                    UserId = 2,
                    ModifiedAt = new DateTime (2014, 1, 2, 10, 0, 0, DateTimeKind.Utc).ToLocalTime (),
                };

                workspaceUserData = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, workspaceUserJson));
                Assert.AreEqual (Guid.Empty, workspaceUserData.WorkspaceId);
                Assert.AreEqual (new DateTime (2014, 1, 2, 10, 0, 0, DateTimeKind.Utc), workspaceUserData.ModifiedAt);
            });
        }
        public void ImportMissingWorkspaceAndUser ()
        {
            RunAsync (async delegate {
                var workspaceUserJson = new WorkspaceUserJson () {
                    Id = 2,
                    WorkspaceId = 1,
                    UserId = 2,
                    ModifiedAt = new DateTime (2014, 1, 3),
                };

                var workspaceUserData = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, workspaceUserJson));
                Assert.AreNotEqual (Guid.Empty, workspaceUserData.WorkspaceId);
                Assert.AreNotEqual (Guid.Empty, workspaceUserData.UserId);

                var projectRows = await DataStore.Table<WorkspaceData> ().Where (m => m.Id == workspaceUserData.WorkspaceId).ToListAsync ();
                var workspaceData = projectRows.FirstOrDefault ();
                Assert.IsNotNull (workspaceData);
                Assert.IsNotNull (workspaceData.RemoteId);
                Assert.AreEqual (DateTime.MinValue, workspaceData.ModifiedAt);

                var userRows = await DataStore.Table<UserData> ().Where (m => m.Id == workspaceUserData.UserId).ToListAsync ();
                var userData = userRows.FirstOrDefault ();
                Assert.IsNotNull (userData);
                Assert.IsNotNull (userData.RemoteId);
                Assert.AreEqual (DateTime.MinValue, userData.ModifiedAt);
            });
        }
        public void ImportUpdated ()
        {
            RunAsync (async delegate {
                var workspaceData = await DataStore.PutAsync (new WorkspaceData () {
                    RemoteId = 1,
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var userData = await DataStore.PutAsync (new UserData () {
                    RemoteId = 2,
                    ModifiedAt = new DateTime (2014, 1, 3),
                });
                var workspaceUserData = await DataStore.PutAsync (new WorkspaceUserData () {
                    RemoteId = 2,
                    WorkspaceId = Guid.Empty,
                    UserId = Guid.Empty,
                    ModifiedAt = new DateTime (2014, 1, 2, 10, 0, 0, DateTimeKind.Utc),
                });
                var workspaceUserJson = new WorkspaceUserJson () {
                    Id = 2,
                    WorkspaceId = 1,
                    UserId = 2,
                    ModifiedAt = new DateTime (2014, 1, 2, 10, 1, 0, DateTimeKind.Utc).ToLocalTime (), // JSON deserialized to local
                };

                workspaceUserData = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, workspaceUserJson));
                Assert.AreNotEqual (Guid.Empty, workspaceUserData.Id);
                Assert.AreEqual (2, workspaceUserData.RemoteId);
                Assert.AreEqual (new DateTime (2014, 1, 2, 10, 1, 0, DateTimeKind.Utc), workspaceUserData.ModifiedAt);
                Assert.AreEqual (workspaceData.Id, workspaceUserData.WorkspaceId);
                Assert.AreEqual (userData.Id, workspaceUserData.UserId);
                Assert.IsFalse (workspaceUserData.IsDirty);
                Assert.IsFalse (workspaceUserData.RemoteRejected);
                Assert.IsNull (workspaceUserData.DeletedAt);
            });

            // Warn the user that the test result might be invalid
            if (TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.Now).TotalMinutes >= 0) {
                Assert.Inconclusive ("The test machine timezone should be set to GTM-1 or less to test datetime comparison.");
            }
        }