public void ImportPastDeleted ()
        {
            RunAsync (async delegate {
                var workspaceData = await DataStore.PutAsync (new WorkspaceData () {
                    RemoteId = 1,
                    Name = "Test",
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var projectData = await DataStore.PutAsync (new ProjectData () {
                    RemoteId = 3,
                    Name = "Hosting",
                    WorkspaceId = Guid.NewGuid (),
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var taskData = await DataStore.PutAsync (new TaskData () {
                    RemoteId = 2,
                    Name = "Install Linux",
                    WorkspaceId = workspaceData.Id,
                    ProjectId = projectData.Id,
                    ModifiedAt = new DateTime (2014, 1, 3),
                });

                var taskJson = new TaskJson () {
                    Id = 2,
                    DeletedAt = new DateTime (2014, 1, 2),
                };

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

                var rows = await DataStore.Table<TaskData> ().QueryAsync (m => m.Id == taskData.Id);
                Assert.That (rows, Has.Exactly (0).Count);
            });
        }
        public void ImportMissingWorkspaceAndProject ()
        {
            RunAsync (async delegate {
                var taskJson = new TaskJson () {
                    Id = 2,
                    Name = "Install Linux",
                    WorkspaceId = 1,
                    ProjectId = 2,
                    ModifiedAt = new DateTime (2014, 1, 3),
                };

                var taskData = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, taskJson));
                Assert.AreNotEqual (Guid.Empty, taskData.WorkspaceId);

                var workspaceRows = await DataStore.Table<WorkspaceData> ().QueryAsync (m => m.Id == taskData.WorkspaceId);
                var workspaceData = workspaceRows.FirstOrDefault ();
                Assert.IsNotNull (workspaceData);
                Assert.IsNotNull (workspaceData.RemoteId);
                Assert.AreEqual (DateTime.MinValue, workspaceData.ModifiedAt);

                var projectRows = await DataStore.Table<ProjectData> ().QueryAsync (m => m.Id == taskData.ProjectId);
                var projectData = projectRows.FirstOrDefault ();
                Assert.IsNotNull (projectData);
                Assert.IsNotNull (projectData.RemoteId);
                Assert.AreEqual (DateTime.MinValue, projectData.ModifiedAt);
            });
        }
        public void ImportUpdated ()
        {
            RunAsync (async delegate {
                var workspaceData = await DataStore.PutAsync (new WorkspaceData () {
                    RemoteId = 1,
                    Name = "Test",
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var projectData = await DataStore.PutAsync (new ProjectData () {
                    RemoteId = 3,
                    Name = "Hosting",
                    WorkspaceId = workspaceData.Id,
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var taskData = await DataStore.PutAsync (new TaskData () {
                    RemoteId = 2,
                    Name = "",
                    WorkspaceId = workspaceData.Id,
                    ProjectId = projectData.Id,
                    ModifiedAt = new DateTime (2014, 1, 2, 10, 0, 0, DateTimeKind.Utc),
                });
                var taskJson = new TaskJson () {
                    Id = 2,
                    Name = "Install Linux",
                    WorkspaceId = 1,
                    ProjectId = 3,
                    ModifiedAt = new DateTime (2014, 1, 2, 10, 1, 0, DateTimeKind.Utc).ToLocalTime (), // JSON deserialized to local
                };

                taskData = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, taskJson));
                Assert.AreNotEqual (Guid.Empty, taskData.Id);
                Assert.AreEqual (2, taskData.RemoteId);
                Assert.AreEqual ("Install Linux", taskData.Name);
                Assert.AreEqual (new DateTime (2014, 1, 2, 10, 1, 0, DateTimeKind.Utc), taskData.ModifiedAt);
                Assert.AreEqual (workspaceData.Id, taskData.WorkspaceId);
                Assert.AreEqual (projectData.Id, taskData.ProjectId);
                Assert.IsFalse (taskData.IsDirty);
                Assert.IsFalse (taskData.RemoteRejected);
                Assert.IsNull (taskData.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.");
            }
        }
        public void ImportUpdatedKeepDirtyLocal ()
        {
            RunAsync (async delegate {
                var workspaceData = await DataStore.PutAsync (new WorkspaceData () {
                    RemoteId = 1,
                    Name = "Test",
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var projectData = await DataStore.PutAsync (new ProjectData () {
                    RemoteId = 3,
                    Name = "Hosting",
                    WorkspaceId = workspaceData.Id,
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var taskData = await DataStore.PutAsync (new TaskData () {
                    RemoteId = 2,
                    Name = "",
                    WorkspaceId = workspaceData.Id,
                    ProjectId = projectData.Id,
                    ModifiedAt = new DateTime (2014, 1, 2, 10, 0, 0, DateTimeKind.Utc),
                    IsDirty = true,
                });
                var taskJson = new TaskJson () {
                    Id = 2,
                    Name = "Install Linux",
                    WorkspaceId = 1,
                    ProjectId = 3,
                    ModifiedAt = new DateTime (2014, 1, 2, 10, 0, 0, DateTimeKind.Utc).ToLocalTime (),
                };

                taskData = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, taskJson));
                Assert.AreEqual ("", taskData.Name);
                Assert.AreEqual (new DateTime (2014, 1, 2, 10, 0, 0, DateTimeKind.Utc), taskData.ModifiedAt);
            });
        }
        public void ImportNew ()
        {
            RunAsync (async delegate {
                var workspaceData = await DataStore.PutAsync (new WorkspaceData () {
                    RemoteId = 1,
                    Name = "Test",
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var projectData = await DataStore.PutAsync (new ProjectData () {
                    RemoteId = 3,
                    Name = "Hosting",
                    WorkspaceId = workspaceData.Id,
                    ModifiedAt = new DateTime (2014, 1, 2),
                });
                var taskJson = new TaskJson () {
                    Id = 2,
                    Name = "Install Linux",
                    WorkspaceId = 1,
                    ProjectId = 3,
                    ModifiedAt = new DateTime (2014, 1, 3),
                };

                var taskData = await DataStore.ExecuteInTransactionAsync (ctx => converter.Import (ctx, taskJson));
                Assert.AreNotEqual (Guid.Empty, taskData.Id);
                Assert.AreEqual (2, taskData.RemoteId);
                Assert.AreEqual ("Install Linux", taskData.Name);
                Assert.AreEqual (new DateTime (2014, 1, 3), taskData.ModifiedAt);
                Assert.AreEqual (workspaceData.Id, taskData.WorkspaceId);
                Assert.AreEqual (projectData.Id, taskData.ProjectId);
                Assert.IsFalse (taskData.IsDirty);
                Assert.IsFalse (taskData.RemoteRejected);
                Assert.IsNull (taskData.DeletedAt);
            });
        }