From fc42d1b34c6217fdd31d6dab6633b877c294aafb Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Mon, 9 Sep 2013 19:55:27 +0100 Subject: [PATCH] GetTrackShoutsCommandTests --- .../Api/Commands/CommandTestsBase.cs | 35 ++++++++++++ .../TrackApi/GetTrackShoutsCommandTests.cs | 57 +++++++++++++++++++ .../IF.Lastfm.Core.Tests.csproj | 6 ++ .../Resources/TrackGetShouts.json | 49 ++++++++++++++++ .../Resources/TrackGetShoutsSingle.json | 17 ++++++ IF.Lastfm.Core.Tests/TestData.Designer.cs | 22 ++++++- IF.Lastfm.Core.Tests/TestData.resx | 6 ++ 7 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 IF.Lastfm.Core.Tests/Api/Commands/CommandTestsBase.cs create mode 100644 IF.Lastfm.Core.Tests/Api/Commands/TrackApi/GetTrackShoutsCommandTests.cs create mode 100644 IF.Lastfm.Core.Tests/Resources/TrackGetShouts.json create mode 100644 IF.Lastfm.Core.Tests/Resources/TrackGetShoutsSingle.json diff --git a/IF.Lastfm.Core.Tests/Api/Commands/CommandTestsBase.cs b/IF.Lastfm.Core.Tests/Api/Commands/CommandTestsBase.cs new file mode 100644 index 0000000..514d52b --- /dev/null +++ b/IF.Lastfm.Core.Tests/Api/Commands/CommandTestsBase.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api; +using Moq; + +namespace IF.Lastfm.Core.Tests.Api.Commands +{ + public abstract class CommandTestsBase + { + public Mock MAuth { get; private set; } + + protected CommandTestsBase() + { + MAuth = new Mock(); + } + + public abstract void Constructor(); + public abstract Task HandleSuccessResponse(); + + protected HttpResponseMessage CreateResponseMessage(string message) + { + var response = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(message, Encoding.UTF8) + }; + + return response; + } + } +} diff --git a/IF.Lastfm.Core.Tests/Api/Commands/TrackApi/GetTrackShoutsCommandTests.cs b/IF.Lastfm.Core.Tests/Api/Commands/TrackApi/GetTrackShoutsCommandTests.cs new file mode 100644 index 0000000..d5c139c --- /dev/null +++ b/IF.Lastfm.Core.Tests/Api/Commands/TrackApi/GetTrackShoutsCommandTests.cs @@ -0,0 +1,57 @@ +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Commands.TrackApi; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IF.Lastfm.Core.Tests.Api.Commands.TrackApi +{ + [TestClass] + public class GetTrackShoutsCommandTests : CommandTestsBase + { + private GetTrackShoutsCommand _command; + + public GetTrackShoutsCommandTests() + { + _command = new GetTrackShoutsCommand(MAuth.Object, "Genesis", "Grimes") + { + Autocorrect = true, + Page = 5, + Count = 7 + }; + } + + [TestMethod] + public override void Constructor() + { + Assert.AreEqual(_command.TrackName, "Genesis"); + Assert.AreEqual(_command.ArtistName, "Grimes"); + Assert.AreEqual(_command.Method, "track.getShouts"); + } + + [TestMethod] + public async override Task HandleSuccessResponse() + { + var response = CreateResponseMessage(Encoding.UTF8.GetString(TestData.TrackGetShouts)); + + var parsed = await _command.HandleResponse(response); + + Assert.IsTrue(parsed.Page == 5); + Assert.IsTrue(parsed.Content.Count() == 7); + } + + /// + /// The shouts API uses a different schema when there's only one shout in the page + /// + /// + [TestMethod] + public async Task HandleResponseSingle() + { + var response = CreateResponseMessage(Encoding.UTF8.GetString(TestData.TrackGetShoutsSingle)); + + var parsed = await _command.HandleResponse(response); + + Assert.IsTrue(parsed.Content.Count() == 1); + } + } +} diff --git a/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj b/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj index a7f7825..7f60fed 100644 --- a/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj +++ b/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj @@ -92,6 +92,8 @@ + + @@ -108,6 +110,7 @@ + @@ -121,6 +124,9 @@ TestData.Designer.cs + + + diff --git a/IF.Lastfm.Core.Tests/Resources/TrackGetShouts.json b/IF.Lastfm.Core.Tests/Resources/TrackGetShouts.json new file mode 100644 index 0000000..35ee190 --- /dev/null +++ b/IF.Lastfm.Core.Tests/Resources/TrackGetShouts.json @@ -0,0 +1,49 @@ +{ + "shouts": { + "shout": [ + { + "body": "Timeless!", + "author": "lukasz_pulawski", + "date": "Thu, 29 Aug 2013 21:21:06" + }, + { + "body": "i have a weird feeling listening this song. kinda nostalgic, happy and sad at the same time [3]", + "author": "Einarr123", + "date": "Thu, 29 Aug 2013 02:00:33" + }, + { + "body": "LEGEND", + "author": "hlopez90", + "date": "Fri, 23 Aug 2013 23:15:37" + }, + { + "body": "This song gets stucked in you head for ages", + "author": "Go_diego", + "date": "Fri, 23 Aug 2013 19:02:17" + }, + { + "body": "Personal soundtrack", + "author": "guialoud", + "date": "Mon, 19 Aug 2013 00:24:32" + }, + { + "body": "Pop Anthem", + "author": "HazieI", + "date": "Sun, 28 Jul 2013 07:27:34" + }, + { + "body": "i have a weird feeling listening this song. kinda nostalgic, happy and sad at the same time [2]", + "author": "HazieI", + "date": "Sun, 28 Jul 2013 07:18:05" + } + ], + "@attr": { + "artist": "Grimes", + "track": "Genesis", + "page": "5", + "perPage": "7", + "totalPages": "15", + "total": "105" + } + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core.Tests/Resources/TrackGetShoutsSingle.json b/IF.Lastfm.Core.Tests/Resources/TrackGetShoutsSingle.json new file mode 100644 index 0000000..280179a --- /dev/null +++ b/IF.Lastfm.Core.Tests/Resources/TrackGetShoutsSingle.json @@ -0,0 +1,17 @@ +{ + "shouts": { + "shout": { + "body": "i have a weird feeling listening this song. kinda nostalgic, happy and sad at the same time [2]", + "author": "HazieI", + "date": "Sun, 28 Jul 2013 07:18:05" + }, + "@attr": { + "artist": "Grimes", + "track": "Genesis", + "page": "7", + "perPage": "10", + "totalPages": "7", + "total": "61" + } + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core.Tests/TestData.Designer.cs b/IF.Lastfm.Core.Tests/TestData.Designer.cs index c57c874..0e27c92 100644 --- a/IF.Lastfm.Core.Tests/TestData.Designer.cs +++ b/IF.Lastfm.Core.Tests/TestData.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.18033 +// Runtime Version:4.0.30319.32559 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -69,5 +69,25 @@ internal static byte[] AlbumGetInfo { return ((byte[])(obj)); } } + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] TrackGetShouts { + get { + object obj = ResourceManager.GetObject("TrackGetShouts", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] TrackGetShoutsSingle { + get { + object obj = ResourceManager.GetObject("TrackGetShoutsSingle", resourceCulture); + return ((byte[])(obj)); + } + } } } diff --git a/IF.Lastfm.Core.Tests/TestData.resx b/IF.Lastfm.Core.Tests/TestData.resx index 063439c..c583852 100644 --- a/IF.Lastfm.Core.Tests/TestData.resx +++ b/IF.Lastfm.Core.Tests/TestData.resx @@ -121,4 +121,10 @@ resources\albumgetinfo.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + resources\trackgetshouts.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + resources\trackgetshoutssingle.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + \ No newline at end of file