Skip to content

Commit

Permalink
Reworked tests. Reworked AddParameter method in UriExtensions. Rework…
Browse files Browse the repository at this point in the history
…ed MovieDb methods.
  • Loading branch information
nikolavn committed Jun 23, 2016
1 parent 96cc79d commit 98cb795
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 47 deletions.
53 changes: 32 additions & 21 deletions Web Services And Cloud/MovieInfoApp/MovieInfo.Tests/ActorTests.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MovieInfo.Tests
namespace MovieInfo.Tests
{
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using MovieInfoApp;
using MovieInfoApp.Models;

[TestClass]
public class ActorTests
{
private static ActorModel actorInfo;
private static CreditsModel credits;
private static string creditsId;
private static SeriesInfo seriesInfo;
private static List<EpisodeModel> episodes;
private static List<SeasonModel> seasons;

[ClassInitialize]
public static void InitializeData(TestContext context)
{
GetData();
}

//In Game of Thrones Nell Tiger Free plays Myrcella Baratheon, write a test to verify the following;

[TestMethod]
public void ActorIsNotInSeason6Episode1()
{
var actorInfo = MovieDb.GetActorInfoByName("Nell Tiger Free");
var credits = MovieDb.GetTVCreditsByActorId(actorInfo.Id);
var creditsId = credits.FirstOrDefault().CreditsId;
var seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId);

Assert.IsFalse(seriesInfo.Episodes.Any(e => e.Season == 6 && e.EpisodeNumber == 1));
Assert.IsFalse(episodes.Any(e => e.Season == 6 && e.EpisodeNumber == 1));
}

[TestMethod]
public void ActorIsInSeason5Episode1AsMainCastMemberButNotAsGuest()
{
var actorInfo = MovieDb.GetActorInfoByName("Nell Tiger Free");
var credits = MovieDb.GetTVCreditsByActorId(actorInfo.Id);
var creditsId = credits.FirstOrDefault().CreditsId;
var seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId);

Assert.IsTrue(seriesInfo.Seasons.Any(s => s.SeasonNumber == 5));
Assert.IsFalse(seriesInfo.Episodes.Any(e => e.Season == 5 && e.EpisodeNumber == 1));
Assert.IsTrue(seasons.Any(s => s.SeasonNumber == 5));
Assert.IsFalse(episodes.Any(e => e.Season == 5 && e.EpisodeNumber == 1));
}

[TestMethod]
public void ActorIsInSeason5Episode2AsMainCastMemberAndAsGuest()
{
var actorInfo = MovieDb.GetActorInfoByName("Nell Tiger Free");
var credits = MovieDb.GetTVCreditsByActorId(actorInfo.Id);
var creditsId = credits.FirstOrDefault().CreditsId;
var seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId);
Assert.IsTrue(seasons.Any(s => s.SeasonNumber == 5));
Assert.IsTrue(episodes.Any(e => e.Season == 5 && e.EpisodeNumber == 2));
}

Assert.IsTrue(seriesInfo.Seasons.Any(s => s.SeasonNumber == 5));
Assert.IsTrue(seriesInfo.Episodes.Any(e => e.Season == 5 && e.EpisodeNumber == 2));
private static void GetData()
{
actorInfo = MovieDb.GetActorInfoByName("Nell Tiger Free").Result;
credits = MovieDb.GetTVCreditsByActorId(actorInfo.Id).Result.FirstOrDefault();
creditsId = credits.CreditsId;
seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId).Result;
episodes = seriesInfo.Episodes;
seasons = seriesInfo.Seasons;
}
}
}
35 changes: 12 additions & 23 deletions Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,18 @@

using Newtonsoft.Json;
using Models;
using System.Threading.Tasks;

public class MovieDb
public static class MovieDb
{
private static readonly Uri BaseUrl = new Uri("https://api.themoviedb.org/");
private static readonly string PeopleEndpoint = "3/search/person";
private static readonly string TVCreditsEndpointFormat = "3/person/{0}/tv_credits";
private static readonly string CreditDetailsEndpointFormat = "3/credit/{0}";
private const string ApiKey = "xxxxxxxxxxxx";
private const string ApiKey = "8e44e41d1573ab3d390b11fc6d35d95e";

public static void Main()
{

var actor = GetActorInfoByName("Nell Tiger Free");
var credits = GetTVCreditsByActorId(actor.Id);
var creditsId = credits.FirstOrDefault().CreditsId;
var seriesInfo = GetTVSeriesDetailsByCreditsId(creditsId);

Console.WriteLine();
}

public static ActorModel GetActorInfoByName(string name)
public static async Task<ActorModel> GetActorInfoByName(string name)
{
string responseData;
string nameQuery = "query=" + Uri.EscapeUriString(name);
Expand All @@ -42,17 +33,15 @@ public static ActorModel GetActorInfoByName(string name)

client.BaseAddress = targetUrl.Uri;


var response = client.GetAsync("").Result;
responseData = response.Content.ReadAsStringAsync().Result;

Task<string> response = client.GetStringAsync("");
responseData = await response;

var actors = JsonConvert.DeserializeObject<ActorSearchResults>(responseData);

return actors.Results.FirstOrDefault();
}

public static List<CreditsModel> GetTVCreditsByActorId(int actorId)
public static async Task<List<CreditsModel>> GetTVCreditsByActorId(int actorId)
{
string responseData;
var endPointWithActorId = string.Format(TVCreditsEndpointFormat, actorId);
Expand All @@ -63,15 +52,15 @@ public static List<CreditsModel> GetTVCreditsByActorId(int actorId)
var targetUrl = CreateUrl(BaseUrl, endPointWithActorId);
client.BaseAddress = targetUrl.Uri;

var response = client.GetAsync("").Result;
responseData = response.Content.ReadAsStringAsync().Result;
Task<string> response = client.GetStringAsync("");
responseData = await response;

var credits = JsonConvert.DeserializeObject<CreditsSearchResults>(responseData);

return credits.Credits;
}

public static SeriesInfo GetTVSeriesDetailsByCreditsId(string creditsId)
public static async Task<SeriesInfo> GetTVSeriesDetailsByCreditsId(string creditsId)
{
string responseData;

Expand All @@ -83,8 +72,8 @@ public static SeriesInfo GetTVSeriesDetailsByCreditsId(string creditsId)
var targetUrl = CreateUrl(BaseUrl, endPointWithCreditsId);
client.BaseAddress = targetUrl.Uri;

var response = client.GetAsync("").Result;
responseData = response.Content.ReadAsStringAsync().Result;
var response = client.GetStringAsync("");
responseData = await response;

var result = JsonConvert.DeserializeObject<SeriesSearchResults>(responseData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Models\SeriesInfo.cs" />
<Compile Include="Models\SeriesSearchResults.cs" />
<Compile Include="MovieDb.cs" />
<Compile Include="Startup.cs" />
<Compile Include="UriExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
21 changes: 21 additions & 0 deletions Web Services And Cloud/MovieInfoApp/MovieInfoApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Linq;


namespace MovieInfoApp
{
class Startup
{
public static void Main()
{

var actor = MovieDb.GetActorInfoByName("Nell Tiger Free");
var credits = MovieDb.GetTVCreditsByActorId(actor.Id);
var creditsId = credits.Result.FirstOrDefault().CreditsId;
var seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId);

Console.WriteLine(creditsId);
Console.WriteLine(seriesInfo.Result.Episodes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ public static class UriExtensions
public static Uri AddParameter(this Uri url, string paramName, string paramValue)
{
var uriBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[paramName] = paramValue;
uriBuilder.Query = query.ToString();

if (uriBuilder.Query != null && uriBuilder.Query.Length > 1)
{
uriBuilder.Query = uriBuilder.Query.Substring(1) + "&" + paramName + "=" + paramValue;
}
else
{
uriBuilder.Query = paramName + "=" + paramValue;
}

return uriBuilder.Uri;
}
Expand Down

0 comments on commit 98cb795

Please sign in to comment.