Reorganising test resources and GetTrackShoutsCommandTests

This commit is contained in:
Rikki Tooley 2014-10-12 16:30:17 +01:00
parent 70a557097b
commit 1e6aa46f49
14 changed files with 318 additions and 58 deletions

View File

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using IF.Lastfm.Core.Api;
using Moq;
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
{
@ -21,6 +18,9 @@ protected CommandTestsBase()
public abstract void Constructor();
public abstract Task HandleSuccessResponse();
public abstract Task HandleResponseSingle();
public abstract Task HandleEmptyResponse();
public abstract Task HandleErrorResponse();
protected HttpResponseMessage CreateResponseMessage(string message)
{

View File

@ -1,8 +1,9 @@
using System.Linq;
using IF.Lastfm.Core.Api.Commands.TrackApi;
using IF.Lastfm.Core.Tests.Resources;
using Microsoft.VisualStudio.TestTools.UnitTesting;
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
{
@ -32,26 +33,50 @@ public override void Constructor()
[TestMethod]
public async override Task HandleSuccessResponse()
{
var response = CreateResponseMessage(Encoding.UTF8.GetString(TestData.TrackGetShouts));
var response = CreateResponseMessage(Encoding.UTF8.GetString(TrackApiResponses.TrackGetShouts));
var parsed = await _command.HandleResponse(response);
Assert.IsTrue(parsed.Success);
Assert.IsNotNull(parsed.Content);
Assert.IsTrue(parsed.Page == 5);
Assert.IsTrue(parsed.Content.Count() == 7);
}
/// <summary>
/// The shouts API uses a different schema when there's only one shout in the page
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task HandleResponseSingle()
public async override Task HandleResponseSingle()
{
var response = CreateResponseMessage(Encoding.UTF8.GetString(TestData.TrackGetShoutsSingle));
var response = CreateResponseMessage(Encoding.UTF8.GetString(TrackApiResponses.TrackGetShoutsSingle));
var parsed = await _command.HandleResponse(response);
Assert.IsTrue(parsed.Success);
Assert.IsNotNull(parsed.Content);
Assert.IsTrue(parsed.Content.Count() == 1);
}
[TestMethod]
public async override Task HandleEmptyResponse()
{
var response = CreateResponseMessage(Encoding.UTF8.GetString(TrackApiResponses.TrackGetShoutsEmpty));
var parsed = await _command.HandleResponse(response);
Assert.IsTrue(parsed.Success);
Assert.IsNotNull(parsed.Content);
Assert.IsTrue(!parsed.Content.Any());
}
[TestMethod]
public async override Task HandleErrorResponse()
{
var response = CreateResponseMessage(Encoding.UTF8.GetString(TrackApiResponses.TrackGetShoutsError));
var parsed = await _command.HandleResponse(response);
Assert.IsFalse(parsed.Success);
Assert.IsNotNull(parsed.Content);
Assert.IsTrue(!parsed.Content.Any());
}
}
}

View File

@ -76,10 +76,15 @@
<Compile Include="LastFmTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceManager.cs" />
<Compile Include="TestData.Designer.cs">
<Compile Include="Resources\AlbumApiResponses.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>TestData.resx</DependentUpon>
<DependentUpon>AlbumApiResponses.resx</DependentUpon>
</Compile>
<Compile Include="Resources\TrackApiResponses.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>TrackApiResponses.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
@ -87,17 +92,22 @@
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
<None Include="Resources\AlbumGetInfo.json" />
<None Include="Resources\TrackGetShoutsSingle.json" />
<None Include="Resources\AlbumApi\AlbumGetInfo.json" />
<None Include="Resources\TrackApi\TrackGetShouts.json" />
<None Include="Resources\TrackApi\TrackGetShoutsEmpty.json" />
<None Include="Resources\TrackApi\TrackGetShoutsError.json" />
<None Include="Resources\TrackApi\TrackGetShoutsSingle.json" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="TestData.resx">
<EmbeddedResource Include="Resources\AlbumApiResponses.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TestData.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
<LastGenOutput>AlbumApiResponses.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Resources\TrackApiResponses.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TrackApiResponses.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="Resources\TrackGetShouts.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IF.Lastfm.Core\IF.Lastfm.Core.csproj">
@ -105,6 +115,9 @@
<Name>IF.Lastfm.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Api\Commands\AlbumApi\" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>

View File

@ -1,12 +1,9 @@
using System;
using IF.Lastfm.Core.Objects;
using IF.Lastfm.Core.Tests.Resources;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
using IF.Lastfm.Core.Api;
using IF.Lastfm.Core.Objects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Tests.Objects
{
@ -15,7 +12,7 @@ public class AlbumTests
{
public void AlbumParsesValidJson()
{
var jo = ResourceManager.LoadResource(Encoding.UTF8.GetString(TestData.AlbumGetInfo));
var jo = ResourceManager.LoadResource(Encoding.UTF8.GetString(AlbumApiResponses.AlbumGetInfo));
var parsed = LastAlbum.ParseJToken(jo.SelectToken("album"));

View File

@ -0,0 +1,73 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34014
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace IF.Lastfm.Core.Tests.Resources {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class AlbumApiResponses {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal AlbumApiResponses() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("IF.Lastfm.Core.Tests.Resources.AlbumApiResponses", typeof(AlbumApiResponses).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] AlbumGetInfo {
get {
object obj = ResourceManager.GetObject("AlbumGetInfo", resourceCulture);
return ((byte[])(obj));
}
}
}
}

View File

@ -119,12 +119,6 @@
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="AlbumGetInfo" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>resources\albumgetinfo.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="TrackGetShouts" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>resources\trackgetshouts.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="TrackGetShoutsSingle" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>resources\trackgetshoutssingle.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>AlbumApi\AlbumGetInfo.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>

View File

@ -0,0 +1,11 @@
{
"shouts": {
"#text": "\n",
"artist": "Cristobal Tapia de Veer",
"track": "The Experiment",
"page": "0",
"perPage": "50",
"totalPages": "0",
"total": "0"
}
}

View File

@ -0,0 +1,5 @@
{
"error": 6,
"message": "Artist not found",
"links": []
}

View File

@ -1,14 +1,14 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.32559
// Runtime Version:4.0.30319.34014
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace IF.Lastfm.Core.Tests {
namespace IF.Lastfm.Core.Tests.Resources {
using System;
@ -22,14 +22,14 @@ namespace IF.Lastfm.Core.Tests {
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class TestData {
internal class TrackApiResponses {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal TestData() {
internal TrackApiResponses() {
}
/// <summary>
@ -39,7 +39,7 @@ internal TestData() {
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("IF.Lastfm.Core.Tests.TestData", typeof(TestData).Assembly);
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("IF.Lastfm.Core.Tests.Resources.TrackApiResponses", typeof(TrackApiResponses).Assembly);
resourceMan = temp;
}
return resourceMan;
@ -63,9 +63,9 @@ internal TestData() {
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] AlbumGetInfo {
internal static byte[] TrackGetShouts {
get {
object obj = ResourceManager.GetObject("AlbumGetInfo", resourceCulture);
object obj = ResourceManager.GetObject("TrackGetShouts", resourceCulture);
return ((byte[])(obj));
}
}
@ -73,9 +73,19 @@ internal static byte[] AlbumGetInfo {
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] TrackGetShouts {
internal static byte[] TrackGetShoutsEmpty {
get {
object obj = ResourceManager.GetObject("TrackGetShouts", resourceCulture);
object obj = ResourceManager.GetObject("TrackGetShoutsEmpty", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] TrackGetShoutsError {
get {
object obj = ResourceManager.GetObject("TrackGetShoutsError", resourceCulture);
return ((byte[])(obj));
}
}

View File

@ -0,0 +1,133 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="TrackGetShouts" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TrackApi\TrackGetShouts.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="TrackGetShoutsEmpty" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TrackApi\TrackGetShoutsEmpty.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="TrackGetShoutsError" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TrackApi\TrackGetShoutsError.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="TrackGetShoutsSingle" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TrackApi\TrackGetShoutsSingle.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>

View File

@ -17,11 +17,12 @@ public PageResponse()
{
Page = 1;
TotalPages = 1;
Content = new List<T>();
}
#region Properties
public IEnumerable<T> Content { get; internal set; }
public List<T> Content { get; internal set; }
public int Page { get; internal set; }
@ -87,31 +88,31 @@ public static PageResponse<T> CreateSuccessResponse(IEnumerable<T> content)
{
var r = new PageResponse<T>
{
Content = content,
Success = true,
Error = LastFmApiError.None
};
r.Content.AddRange(content);
return r;
}
public static PageResponse<T> CreateSuccessResponse(JToken itemsToken, JToken pageInfoToken, Func<JToken, T> parseToken, bool isOpenQueryToken = false)
{
var pageresponse = CreateSuccessResponse();
var content = new List<T>();
if (itemsToken.Children().Any())
if (itemsToken != null && itemsToken.Children().Any())
{
// array notation isn't used on the api when only one object is available
if (itemsToken.Type != JTokenType.Array)
{
var item = parseToken(itemsToken);
content.Add(item);
pageresponse.Content.Add(item);
}
else
{
var items = itemsToken.Children().Select(parseToken);
content.AddRange(items);
pageresponse.Content.AddRange(items);
}
}
@ -128,11 +129,9 @@ public static PageResponse<T> CreateSuccessResponse(JToken itemsToken, JToken pa
}
else
{
pageresponse.AddDefaultPageInfo(content.Count);
pageresponse.AddDefaultPageInfo(pageresponse.Content.Count);
}
pageresponse.Content = content;
return pageresponse;
}