user get top albums command

This commit is contained in:
Rikki Tooley 2015-04-02 16:58:00 +01:00 committed by Rikki Tooley
parent 4c7ae32cd0
commit cd157882eb
5 changed files with 126 additions and 8 deletions

View File

@ -1,5 +1,7 @@
using IF.Lastfm.Core.Api.Commands.UserApi;
using IF.Lastfm.Core.Api;
using IF.Lastfm.Core.Api.Commands.UserApi;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;
using IF.Lastfm.Core.Tests.Resources;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@ -7,6 +9,8 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Moq;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Tests.Api.Commands
{
@ -82,4 +86,67 @@ public async Task HandleErrorResponse()
Assert.IsTrue(parsed.Error == LastFmApiError.MissingParameters);
}
}
[TestClass]
public class UserGetTopAlbumsCommandTests : CommandTestsBase
{
private const string USER = "test";
private const LastStatsTimeSpan SPAN = LastStatsTimeSpan.Month;
private GetTopAlbumsCommand _command;
private Mock<ILastAuth> _mockAuth;
[TestInitialize]
public void TestInitialise()
{
_mockAuth = new Mock<ILastAuth>();
_command = new GetTopAlbumsCommand(_mockAuth.Object, USER, SPAN)
{
Page = 5,
Count = 20
};
_command.SetParameters();
}
[TestMethod]
public void CorrectParameters()
{
var expected = new Dictionary<string, string>
{
{"user", USER},
{"period", SPAN.GetApiName()},
{"limit", "20"},
{"page", "5"},
{"disablecachetoken", ""}
};
_command.Parameters["disablecachetoken"] = "";
TestHelper.AssertEqual(expected, _command.Parameters);
}
public void HandleErrorResponse()
{
}
[TestMethod]
public void HandleResponseEmpty()
{
}
[TestMethod]
public void HandleResponseSingle()
{
}
[TestMethod]
public void HandleResponseMultiple()
{
}
}
}

View File

@ -95,6 +95,7 @@
<Compile Include="Api\MockAlbumApi.cs" />
<Compile Include="MockLastFm.cs" />
<Compile Include="LastFmTests.cs" />
<Compile Include="OrderedContractResolver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources\AlbumApiResponses.Designer.cs">
<AutoGen>True</AutoGen>

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace IF.Lastfm.Core.Tests
{
/// <summary>
/// http://stackoverflow.com/a/11309106/268555
/// </summary>
public class OrderedContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization).OrderBy(p => p.PropertyName).ToList();
}
}
}

View File

@ -1,29 +1,60 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using IF.Lastfm.Core.Api.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Tests
{
public static class TestHelper
{
private static readonly JsonSerializerSettings _testSerialiserSettings;
static TestHelper()
private static JsonSerializer GetTestSerialiser()
{
_testSerialiserSettings = new JsonSerializerSettings
return new JsonSerializer
{
DateFormatString = "yyyy-MM-dd HH:mm:ss.fff",
NullValueHandling = NullValueHandling.Ignore
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new OrderedContractResolver()
};
}
private static JObject WithSortedProperties(this JObject jo)
{
var result = new JObject();
foreach (var prop in jo.Properties().OrderBy(p => p.Name))
{
var nestedJo = prop.Value as JObject;
if (nestedJo != null)
{
result.Add(nestedJo.WithSortedProperties());
}
else
{
result.Add(prop);
}
}
return result;
}
public static string TestSerialise<T>(this T poco)
{
return JsonConvert.SerializeObject(poco, Formatting.Indented, _testSerialiserSettings);
var serialiser = GetTestSerialiser();
var jo = JObject.FromObject(poco, serialiser);
var ordered = jo.WithSortedProperties();
return ordered.ToString();
}
public static void AssertEqual<T>(T one, T two)
{
var ones = one.TestSerialise();
var twos = two.TestSerialise();
Assert.AreEqual(ones, twos, ones.DifferencesTo(twos));
}
public static string DifferencesTo<T>(this IEnumerable<T> expected, IEnumerable<T> actual)

View File

@ -22,7 +22,7 @@ public GetTopAlbumsCommand(ILastAuth auth, string username, LastStatsTimeSpan sp
public override void SetParameters()
{
Parameters.Add("username", Username);
Parameters.Add("user", Username);
Parameters.Add("period", TimeSpan.GetApiName());
AddPagingParameters();