From 1912563cc32bd889bfd3a7325437aa18d3276663 Mon Sep 17 00:00:00 2001 From: ceee Date: Thu, 19 Sep 2013 18:08:41 +0200 Subject: [PATCH] RemoveTag method; fix tag conversion; add modify tests --- PocketSharp.Tests/ModifyTagsTests.cs | 91 ++++++++++++++++++- PocketSharp/Components/ModifyTags.cs | 26 +++++- .../Models/Parameters/ActionParameter.cs | 47 +++++----- PocketSharp/PocketClient.cs | 2 +- 4 files changed, 140 insertions(+), 26 deletions(-) diff --git a/PocketSharp.Tests/ModifyTagsTests.cs b/PocketSharp.Tests/ModifyTagsTests.cs index fa237d9..e432544 100644 --- a/PocketSharp.Tests/ModifyTagsTests.cs +++ b/PocketSharp.Tests/ModifyTagsTests.cs @@ -3,11 +3,100 @@ using System.Threading.Tasks; using System.Collections.Generic; using Xunit; using PocketSharp.Models; +using System.Linq; namespace PocketSharp.Tests { - class ModifyTagsTests : TestsBase + public class ModifyTagsTests : TestsBase { public ModifyTagsTests() : base() { } + + + [Fact] + public async Task AreTagsAddedAndDeletedToAnItem() + { + PocketItem item = await Setup(); + + Assert.True(await client.AddTags(item, new string[] { "test_tag", "test_tag2" })); + + item = await GetItemById(item.ID); + + Assert.True(item.Tags.Count >= 2); + + Assert.NotNull(item.Tags.Single(tag => tag.Name == "test_tag")); + Assert.NotNull(item.Tags.Single(tag => tag.Name == "test_tag2")); + + Assert.True(await client.RemoveTags(item, new string[] { "test_tag", "test_tag2" })); + + item = await GetItemById(item.ID); + + Assert.Null(item.Tags.SingleOrDefault(tag => tag.Name == "test_tag")); + Assert.Null(item.Tags.SingleOrDefault(tag => tag.Name == "test_tag2")); + } + + + [Fact] + public async Task AreAllTagsRemovedFromItem() + { + PocketItem item = await Setup(); + + Assert.True(await client.AddTags(item, new string[] { "test_tag", "test_tag2" })); + + item = await GetItemById(item.ID); + + Assert.True(item.Tags.Count >= 2); + + Assert.True(await client.RemoveTags(item)); + + item = await GetItemById(item.ID); + + Assert.Null(item.Tags); + } + + + [Fact] + public async Task AreTagsReplaced() + { + PocketItem item = await Setup(); + + Assert.True(await client.ReplaceTags(item.ID, new string[] { "test_tag", "test_tag2" })); + + item = await GetItemById(item.ID); + + Assert.Equal(item.Tags.Count, 2); + + Assert.NotNull(item.Tags.SingleOrDefault(tag => tag.Name == "test_tag")); + Assert.NotNull(item.Tags.SingleOrDefault(tag => tag.Name == "test_tag2")); + } + + + private async Task Setup() + { + PocketItem item = await client.Add( + uri: new Uri("https://github.com"), + tags: new string[] { "github", "code", "social" } + ); + + itemsToDelete.Add(item.ID); + + return await GetItemById(item.ID); + } + + + private async Task GetItemById(int id, bool archive = false) + { + List items = await client.Retrieve(state: archive ? State.archive : State.unread); + PocketItem itemDesired = null; + + items.ForEach(itm => + { + if (itm.ID == id) + { + itemDesired = itm; + } + }); + + return itemDesired; + } } } diff --git a/PocketSharp/Components/ModifyTags.cs b/PocketSharp/Components/ModifyTags.cs index 29bf181..5eff5f4 100644 --- a/PocketSharp/Components/ModifyTags.cs +++ b/PocketSharp/Components/ModifyTags.cs @@ -56,6 +56,30 @@ namespace PocketSharp } + /// + /// Removes a tag. + /// + /// The item ID. + /// The tags. + /// + public async Task RemoveTag(int itemID, string tag) + { + return await SendTags(itemID, "tags_remove", new string[] { tag }); + } + + + /// + /// Removes a tag. + /// + /// The item. + /// The tags. + /// + public async Task RemoveTag(PocketItem item, string tag) + { + return await RemoveTag(item.ID, tag); + } + + /// /// Clears all tags. /// @@ -79,7 +103,7 @@ namespace PocketSharp /// - /// Replaces all existing tags with new ones. + /// Replaces all existing tags with the given tags. /// /// The item ID. /// The tags. diff --git a/PocketSharp/Models/Parameters/ActionParameter.cs b/PocketSharp/Models/Parameters/ActionParameter.cs index 52189dd..b1f9bd4 100644 --- a/PocketSharp/Models/Parameters/ActionParameter.cs +++ b/PocketSharp/Models/Parameters/ActionParameter.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Runtime.Serialization; @@ -8,7 +9,7 @@ namespace PocketSharp.Models /// All parameters which can be passed for a modify action /// [DataContract] - internal class ActionParameter : Parameters + internal class ActionParameter { /// /// Gets or sets the action. @@ -67,28 +68,28 @@ namespace PocketSharp.Models public string NewTag { get; set; } - ///// - ///// Converts this instance to a parameter list. - ///// - ///// - //public Dictionary Convert() - //{ - // Dictionary parameters = new Dictionary - // { - // { "item_id", ID }, - // { "action", Action } - // }; + /// + /// Converts this instance to a parameter list. + /// + /// + public Dictionary Convert() + { + Dictionary parameters = new Dictionary + { + { "item_id", ID.ToString() }, + { "action", Action } + }; - // if (Time != null) - // parameters.Add("time", Utilities.GetUnixTimestamp(Time)); - // if (Tags != null) - // parameters.Add("tags", Tags); - // if (OldTag != null) - // parameters.Add("old_tag", OldTag); - // if (NewTag != null) - // parameters.Add("new_tag", NewTag); + if (Time != null) + parameters.Add("time", Time != null ? Utilities.GetUnixTimestamp(Time).ToString() : null); + if (Tags != null) + parameters.Add("tags", Tags); + if (OldTag != null) + parameters.Add("old_tag", OldTag); + if (NewTag != null) + parameters.Add("new_tag", NewTag); - // return parameters; - //} + return parameters; + } } } diff --git a/PocketSharp/PocketClient.cs b/PocketSharp/PocketClient.cs index 8f1f51a..86a9c5a 100644 --- a/PocketSharp/PocketClient.cs +++ b/PocketSharp/PocketClient.cs @@ -165,7 +165,7 @@ namespace PocketSharp /// internal async Task Send(List actionParameters) { - List> actionParamList = new List>(); + List> actionParamList = new List>(); foreach (var action in actionParameters) {