4 Commits

8 changed files with 252 additions and 29 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;
}
}
}
+82 -1
View File
@@ -6,8 +6,89 @@ using PocketSharp.Models;
namespace PocketSharp.Tests
{
class ModifyTests : TestsBase
public class ModifyTests : TestsBase
{
public ModifyTests() : base() { }
[Fact]
public async Task IsAnItemArchivedAndUnarchived()
{
PocketItem item = await Setup();
Assert.True(await client.Archive(item));
item = await GetItemById(item.ID, true);
Assert.True(item.IsArchive);
Assert.True(await client.Unarchive(item));
item = await GetItemById(item.ID);
Assert.False(item.IsArchive);
}
[Fact]
public async Task IsAnItemFavoritedAndUnfavorited()
{
PocketItem item = await Setup();
Assert.True(await client.Favorite(item));
item = await GetItemById(item.ID);
Assert.True(item.IsFavorite);
Assert.True(await client.Unfavorite(item));
item = await GetItemById(item.ID);
Assert.False(item.IsFavorite);
}
[Fact]
public async Task IsAnItemDeleted()
{
PocketItem item = await Setup();
Assert.True(await client.Delete(item));
item = await GetItemById(item.ID);
Assert.Null(item);
}
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;
}
}
}
+12
View File
@@ -20,6 +20,18 @@ namespace PocketSharp.Tests
}
[Fact]
public async Task IsItemRetrievedById()
{
List<PocketItem> items = await client.Retrieve();
PocketItem item = items[0];
PocketItem itemDuplicate = await client.Retrieve(item.ID);
Assert.True(item.ID == itemDuplicate.ID);
Assert.True(item.Uri == itemDuplicate.Uri);
}
[Fact]
public async Task AreFilteredItemsRetrieved()
{
+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>
+16
View File
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
namespace PocketSharp
{
@@ -49,6 +50,21 @@ namespace PocketSharp
}
/// <summary>
/// Retrieves an item by a given ID
/// </summary>
/// <param name="itemID">The item ID.</param>
/// <returns></returns>
public async Task<PocketItem> Retrieve(int itemID)
{
List<PocketItem> items = await Retrieve(
state: State.all
);
return items.Single<PocketItem>(item => item.ID == itemID);
}
/// <summary>
/// Retrieves all items with a filter from pocket
/// </summary>
@@ -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;
}
}
}
+2 -2
View File
@@ -15,7 +15,7 @@ namespace PocketSharp.Models
/// <value>
/// The action results.
/// </value>
[JsonProperty("action_results")]
public bool[] ActionResults { get; set; }
//[JsonProperty("action_results")]
//public bool[] ActionResults { get; set; }
}
}
+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)
{