public void ImportNew()
        {
            RunAsync(async delegate {
                var workspaceData = await DataStore.PutAsync(new WorkspaceData()
                {
                    RemoteId   = 1,
                    Name       = "Test",
                    ModifiedAt = new DateTime(2014, 1, 2),
                });
                var tagJson = new TagJson()
                {
                    Id          = 2,
                    Name        = "Mobile",
                    WorkspaceId = 1,
                    ModifiedAt  = new DateTime(2014, 1, 3),
                };

                var tagData = await DataStore.ExecuteInTransactionAsync(ctx => converter.Import(ctx, tagJson));
                Assert.AreNotEqual(Guid.Empty, tagData.Id);
                Assert.AreEqual(2, tagData.RemoteId);
                Assert.AreEqual("Mobile", tagData.Name);
                Assert.AreEqual(new DateTime(2014, 1, 3), tagData.ModifiedAt);
                Assert.AreEqual(workspaceData.Id, tagData.WorkspaceId);
                Assert.IsFalse(tagData.IsDirty);
                Assert.IsFalse(tagData.RemoteRejected);
                Assert.IsNull(tagData.DeletedAt);
            });
        }
Esempio n. 2
0
        public TagData Import (IDataStoreContext ctx, TagJson json, Guid? localIdHint = null, TagData mergeBase = null)
        {
            var data = GetByRemoteId<TagData> (ctx, json.Id.Value, localIdHint);

            var merger = mergeBase != null ? new TagMerger (mergeBase) : null;
            if (merger != null && data != null)
                merger.Add (new TagData (data));

            if (json.DeletedAt.HasValue) {
                if (data != null) {
                    ctx.Delete (data);
                    data = null;
                }
            } else if (merger != null || ShouldOverwrite (data, json)) {
                data = ImportJson (ctx, data, json);

                if (merger != null) {
                    merger.Add (data);
                    data = merger.Result;
                }

                data = ctx.Put (data);
            }

            return data;
        }
Esempio n. 3
0
        public static TagData Import(this TagJson json, IDataStoreContext ctx,
                                     Guid?localIdHint = null, TagData mergeBase = null)
        {
            var converter = ServiceContainer.Resolve <TagJsonConverter> ();

            return(converter.Import(ctx, json, localIdHint, mergeBase));
        }
        public void ImportPastDeleted()
        {
            RunAsync(async delegate {
                var workspaceData = await DataStore.PutAsync(new WorkspaceData()
                {
                    RemoteId   = 1,
                    Name       = "Test",
                    ModifiedAt = new DateTime(2014, 1, 2),
                });
                var tagData = await DataStore.PutAsync(new TagData()
                {
                    RemoteId    = 2,
                    Name        = "Mobile",
                    WorkspaceId = workspaceData.Id,
                    ModifiedAt  = new DateTime(2014, 1, 3),
                });

                var tagJson = new TagJson()
                {
                    Id        = 2,
                    DeletedAt = new DateTime(2014, 1, 2),
                };

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

                var rows = await DataStore.Table <TagData> ().QueryAsync(m => m.Id == tagData.Id);
                Assert.That(rows, Has.Exactly(0).Count);
            });
        }
        public void ImportUpdatedKeepDirtyLocal()
        {
            RunAsync(async delegate {
                var workspaceData = await DataStore.PutAsync(new WorkspaceData()
                {
                    RemoteId   = 1,
                    Name       = "Test",
                    ModifiedAt = new DateTime(2014, 1, 2),
                });
                var tagData = await DataStore.PutAsync(new TagData()
                {
                    RemoteId    = 2,
                    Name        = "",
                    WorkspaceId = workspaceData.Id,
                    ModifiedAt  = new DateTime(2014, 1, 2, 10, 0, 0, DateTimeKind.Utc),
                    IsDirty     = true,
                });
                var tagJson = new TagJson()
                {
                    Id          = 2,
                    Name        = "Mobile",
                    WorkspaceId = 1,
                    ModifiedAt  = new DateTime(2014, 1, 2, 10, 0, 0, DateTimeKind.Utc).ToLocalTime(),
                };

                tagData = await DataStore.ExecuteInTransactionAsync(ctx => converter.Import(ctx, tagJson));
                Assert.AreEqual("", tagData.Name);
                Assert.AreEqual(new DateTime(2014, 1, 2, 10, 0, 0, DateTimeKind.Utc), tagData.ModifiedAt);
            });
        }
        public void TagSearchRoot_GetJson_SuccessWithoutExceptions()
        {
            // https://derpibooru.org/api/v1/json/search/tags?q=id:27724
            var uri = _baseUri.AppendPathSegment("/search/tags").SetQueryParam("q", "id:27724");
            TagSearchRootJson tagsRoot = null;
            TagJson           tag      = null;

            Func <Task> act = async() =>
            {
                var json = await uri.GetStringAsync();

                tagsRoot = JsonSerializer.Deserialize <TagSearchRootJson>(json);
                tag      = tagsRoot.Tags.First();
            };

            act.Should().NotThrow();
            tagsRoot.Should().NotBeNull();

            // Property validation
            tag.AliasedTag.Should().BeNull();
            tag.Aliases.Contains("fs");
            tag.Category.Should().Be("character");
            tag.Description.Should().Be("");
            tag.DoNotPostEntries.Should().BeEmpty();
            tag.Id.Should().Be(27724);
            tag.ImageCount.Should().BeGreaterOrEqualTo(191343);
            tag.ImpliedByTags.Should().NotBeEmpty();
            tag.ImpliedTags.Should().BeEmpty();
            tag.Name.Should().Be("fluttershy");
            tag.NameInNamespace.Should().Be("fluttershy");
            tag.Namespace.Should().BeNull();
            tag.ShortDescription.Should().Be("");
            tag.Slug.Should().Be("fluttershy");
            tag.SpoilerImageUri.Should().BeNull();
        }
        public TagData Import(IDataStoreContext ctx, TagJson json, Guid?localIdHint = null, TagData mergeBase = null)
        {
            var log = ServiceContainer.Resolve <Logger> ();

            var data = GetByRemoteId <TagData> (ctx, json.Id.Value, localIdHint);

            var merger = mergeBase != null ? new TagMerger(mergeBase) : null;

            if (merger != null && data != null)
            {
                merger.Add(new TagData(data));
            }

            if (json.DeletedAt.HasValue)
            {
                if (data != null)
                {
                    log.Info(Tag, "Deleting local data for {0}.", data.ToIdString());
                    ctx.Delete(data);
                    data = null;
                }
            }
            else if (merger != null || ShouldOverwrite(data, json))
            {
                data = ImportJson(ctx, data, json);

                if (merger != null)
                {
                    merger.Add(data);
                    data = merger.Result;
                }

                if (merger != null)
                {
                    log.Info(Tag, "Importing {0}, merging with local data.", data.ToIdString());
                }
                else
                {
                    log.Info(Tag, "Importing {0}, replacing local data.", data.ToIdString());
                }

                data = ctx.Put(data);
            }
            else
            {
                log.Info(Tag, "Skipping import of {0}.", json.ToIdString());
            }

            return(data);
        }
Esempio n. 8
0
        public TagData Import (IDataStoreContext ctx, TagJson json, Guid? localIdHint = null, bool forceUpdate = false)
        {
            var data = GetByRemoteId<TagData> (ctx, json.Id.Value, localIdHint);

            if (json.DeletedAt.HasValue) {
                if (data != null) {
                    ctx.Delete (data);
                    data = null;
                }
            } else if (data == null || forceUpdate || data.ModifiedAt < json.ModifiedAt) {
                data = Merge (ctx, data, json);
                data = ctx.Put (data);
            }

            return data;
        }
        public void ImportUpdated()
        {
            RunAsync(async delegate {
                var workspaceData = await DataStore.PutAsync(new WorkspaceData()
                {
                    RemoteId   = 1,
                    Name       = "Test",
                    ModifiedAt = new DateTime(2014, 1, 2),
                });
                var tagData = await DataStore.PutAsync(new TagData()
                {
                    RemoteId    = 2,
                    Name        = "",
                    WorkspaceId = workspaceData.Id,
                    ModifiedAt  = new DateTime(2014, 1, 2, 10, 0, 0, DateTimeKind.Utc),
                });
                var tagJson = new TagJson()
                {
                    Id          = 2,
                    Name        = "Mobile",
                    WorkspaceId = 1,
                    ModifiedAt  = new DateTime(2014, 1, 2, 10, 1, 0, DateTimeKind.Utc).ToLocalTime(),  // JSON deserialized to local
                };

                tagData = await DataStore.ExecuteInTransactionAsync(ctx => converter.Import(ctx, tagJson));
                Assert.AreNotEqual(Guid.Empty, tagData.Id);
                Assert.AreEqual(2, tagData.RemoteId);
                Assert.AreEqual("Mobile", tagData.Name);
                Assert.AreEqual(new DateTime(2014, 1, 2, 10, 1, 0, DateTimeKind.Utc), tagData.ModifiedAt);
                Assert.AreEqual(workspaceData.Id, tagData.WorkspaceId);
                Assert.IsFalse(tagData.IsDirty);
                Assert.IsFalse(tagData.RemoteRejected);
                Assert.IsNull(tagData.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.");
            }
        }
Esempio n. 10
0
        private static TagData ImportJson (IDataStoreContext ctx, TagData data, TagJson json)
        {
            var workspaceId = GetLocalId<WorkspaceData> (ctx, json.WorkspaceId);

            if (data == null) {
                // Fallback to name lookup for unsynced tags:
                var rows = ctx.Connection.Table<TagData> ().Take (1)
                    .Where (r => r.WorkspaceId == workspaceId && r.Name == json.Name && r.RemoteId == null);
                data = rows.FirstOrDefault ();
            }

            // As a last chance create new tag:
            data = data ?? new TagData ();

            data.Name = json.Name;
            data.WorkspaceId = workspaceId;

            ImportCommonJson (data, json);

            return data;
        }
        public void ImportMissingWorkspace()
        {
            RunAsync(async delegate {
                var tagJson = new TagJson()
                {
                    Id          = 2,
                    Name        = "Mobile",
                    WorkspaceId = 1,
                    ModifiedAt  = new DateTime(2014, 1, 3),
                };

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

                var rows          = await DataStore.Table <WorkspaceData> ().QueryAsync(m => m.Id == tagData.WorkspaceId);
                var workspaceData = rows.FirstOrDefault();
                Assert.IsNotNull(workspaceData);
                Assert.IsNotNull(workspaceData.RemoteId);
                Assert.AreEqual(DateTime.MinValue, workspaceData.ModifiedAt);
            });
        }
        private static TagData ImportJson(IDataStoreContext ctx, TagData data, TagJson json)
        {
            var workspaceId = GetLocalId <WorkspaceData> (ctx, json.WorkspaceId);

            if (data == null)
            {
                // Fallback to name lookup for unsynced tags:
                var rows = ctx.Connection.Table <TagData> ().Take(1)
                           .Where(r => r.WorkspaceId == workspaceId && r.Name == json.Name && r.RemoteId == null);
                data = rows.FirstOrDefault();
            }

            // As a last chance create new tag:
            data = data ?? new TagData();

            data.Name        = json.Name;
            data.WorkspaceId = workspaceId;

            ImportCommonJson(data, json);

            return(data);
        }
Esempio n. 13
0
        public TagData Import (IDataStoreContext ctx, TagJson json, Guid? localIdHint = null, TagData mergeBase = null)
        {
            var log = ServiceContainer.Resolve<ILogger> ();

            var data = GetByRemoteId<TagData> (ctx, json.Id.Value, localIdHint);

            var merger = mergeBase != null ? new TagMerger (mergeBase) : null;
            if (merger != null && data != null) {
                merger.Add (new TagData (data));
            }

            if (json.DeletedAt.HasValue) {
                if (data != null) {
                    log.Info (Tag, "Deleting local data for {0}.", data.ToIdString ());
                    ctx.Delete (data);
                    data = null;
                }
            } else if (merger != null || ShouldOverwrite (data, json)) {
                data = ImportJson (ctx, data, json);

                if (merger != null) {
                    merger.Add (data);
                    data = merger.Result;
                }

                if (merger != null) {
                    log.Info (Tag, "Importing {0}, merging with local data.", data.ToIdString ());
                } else {
                    log.Info (Tag, "Importing {0}, replacing local data.", data.ToIdString ());
                }

                data = ctx.Put (data);
            } else {
                log.Info (Tag, "Skipping import of {0}.", json.ToIdString ());
            }

            return data;
        }
Esempio n. 14
0
        public Task DeleteTag(TagJson jsonObject)
        {
            var url = new Uri(v8Url, String.Format("tags/{0}", jsonObject.Id.Value.ToString()));

            return(DeleteObject(url));
        }
Esempio n. 15
0
        public Task <TagJson> CreateTag(TagJson jsonObject)
        {
            var url = new Uri(v8Url, "tags");

            return(CreateObject(url, jsonObject));
        }