RemoveTag method; fix tag conversion; add modify tests

This commit is contained in:
2013-09-19 18:08:41 +02:00
parent 64c74b11d7
commit 7eee6ed433
4 changed files with 140 additions and 26 deletions
+90 -1
View File
@@ -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<PocketTag>(tag => tag.Name == "test_tag"));
Assert.NotNull(item.Tags.Single<PocketTag>(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<PocketTag>(tag => tag.Name == "test_tag"));
Assert.Null(item.Tags.SingleOrDefault<PocketTag>(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<PocketTag>(tag => tag.Name == "test_tag"));
Assert.NotNull(item.Tags.SingleOrDefault<PocketTag>(tag => tag.Name == "test_tag2"));
}
private async Task<PocketItem> 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<PocketItem> GetItemById(int id, bool archive = false)
{
List<PocketItem> items = await client.Retrieve(state: archive ? State.archive : State.unread);
PocketItem itemDesired = null;
items.ForEach(itm =>
{
if (itm.ID == id)
{
itemDesired = itm;
}
});
return itemDesired;
}
}
}
+25 -1
View File
@@ -56,6 +56,30 @@ namespace PocketSharp
}
/// <summary>
/// Removes a tag.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
public async Task<bool> RemoveTag(int itemID, string tag)
{
return await SendTags(itemID, "tags_remove", new string[] { tag });
}
/// <summary>
/// Removes a tag.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="tags">The tags.</param>
/// <returns></returns>
public async Task<bool> RemoveTag(PocketItem item, string tag)
{
return await RemoveTag(item.ID, tag);
}
/// <summary>
/// Clears all tags.
/// </summary>
@@ -79,7 +103,7 @@ namespace PocketSharp
/// <summary>
/// Replaces all existing tags with new ones.
/// Replaces all existing tags with the given tags.
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <param name="tags">The tags.</param>
@@ -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
/// </summary>
[DataContract]
internal class ActionParameter : Parameters
internal class ActionParameter
{
/// <summary>
/// Gets or sets the action.
@@ -67,28 +68,28 @@ namespace PocketSharp.Models
public string NewTag { get; set; }
///// <summary>
///// Converts this instance to a parameter list.
///// </summary>
///// <returns></returns>
//public Dictionary<string, object> Convert()
//{
// Dictionary<string, object> parameters = new Dictionary<string, object>
// {
// { "item_id", ID },
// { "action", Action }
// };
/// <summary>
/// Converts this instance to a parameter list.
/// </summary>
/// <returns></returns>
public Dictionary<string, object> Convert()
{
Dictionary<string, object> parameters = new Dictionary<string, object>
{
{ "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;
}
}
}
+1 -1
View File
@@ -165,7 +165,7 @@ namespace PocketSharp
/// <returns></returns>
internal async Task<bool> Send(List<ActionParameter> actionParameters)
{
List<Dictionary<string, string>> actionParamList = new List<Dictionary<string, string>>();
List<Dictionary<string, object>> actionParamList = new List<Dictionary<string, object>>();
foreach (var action in actionParameters)
{