Skip to content

Commit

Permalink
Refactor tests, employ async, use better Assert() calls
Browse files Browse the repository at this point in the history
Use async Xunit tests
Apply xunit analyzer suggestions, such as "Assert.False()" instead of "Assert.Equal(false, ..)
Introduce errors for missing await calls
Remove all .Result properties
Remvoe all .Sync() calls
  • Loading branch information
LordMike committed Mar 27, 2021
1 parent 8e622cb commit f5cdd0c
Show file tree
Hide file tree
Showing 40 changed files with 880 additions and 883 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
@@ -0,0 +1,4 @@
[*.cs]

# CS4014: Because this call is not awaited, execution of the current method continues before the call is completed
dotnet_diagnostic.CS4014.severity = error
1 change: 1 addition & 0 deletions TMDbLib.sln
Expand Up @@ -11,6 +11,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{26BF5A0A-354A-46F5-A20C-0BEB5D45783A}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
.editorconfig = .editorconfig
Readme.md = Readme.md
EndProjectSection
EndProject
Expand Down
4 changes: 2 additions & 2 deletions TMDbLib/Client/TMDbClient.cs
Expand Up @@ -243,7 +243,7 @@ public void SetConfig(TMDbConfig config)
/// - Use the 'AuthenticationGetUserSessionAsync' and 'AuthenticationCreateGuestSessionAsync' methods to optain the respective session ids.
/// - User sessions have access to far for methods than guest sessions, these can currently only be used to rate media.
/// </remarks>
public void SetSessionInformation(string sessionId, SessionType sessionType)
public async Task SetSessionInformationAsync(string sessionId, SessionType sessionType)
{
ActiveAccount = null;
SessionId = sessionId;
Expand All @@ -259,7 +259,7 @@ public void SetSessionInformation(string sessionId, SessionType sessionType)
{
try
{
ActiveAccount = AccountGetDetailsAsync().Result;
ActiveAccount = await AccountGetDetailsAsync().ConfigureAwait(false);
}
catch (Exception)
{
Expand Down
1 change: 1 addition & 0 deletions TMDbLib/Rest/RestResponse.cs
Expand Up @@ -51,6 +51,7 @@ public async Task<T> GetDataObject()
return _client.Serializer.Deserialize<T>(tr);
}

[Obsolete("Remove this, uses .Result")]
public static implicit operator T(RestResponse<T> response)
{
try
Expand Down
172 changes: 86 additions & 86 deletions TMDbLibTests/ClientAccountTests.cs

Large diffs are not rendered by default.

36 changes: 17 additions & 19 deletions TMDbLibTests/ClientAuthenticationTests.cs
@@ -1,8 +1,8 @@
using System;
using System.Threading.Tasks;
using TMDbLibTests.Exceptions;
using Xunit;
using TMDbLib.Objects.Authentication;
using TMDbLibTests.Helpers;
using TMDbLibTests.JsonHelpers;

namespace TMDbLibTests
Expand All @@ -19,14 +19,13 @@ public ClientAuthenticationTests()
}

[Fact]
public void TestAuthenticationRequestNewToken()
public async Task TestAuthenticationRequestNewToken()
{
Token token = Config.Client.AuthenticationRequestAutenticationTokenAsync().Sync();
Token token = await Config.Client.AuthenticationRequestAutenticationTokenAsync();

Assert.NotNull(token);
Assert.True(token.Success);
Assert.NotNull(token.AuthenticationCallback);
Assert.NotNull(token.ExpiresAt);
Assert.NotNull(token.RequestToken);
}

Expand All @@ -49,61 +48,60 @@ public void TestAuthenticationRequestNewToken()
//}

[Fact]
public void TestAuthenticationUserAuthenticatedSessionInvalidToken()
public async Task TestAuthenticationUserAuthenticatedSessionInvalidTokenAsync()
{
const string requestToken = "bla";

Assert.Throws<UnauthorizedAccessException>(() => Config.Client.AuthenticationGetUserSessionAsync(requestToken).Sync());
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => Config.Client.AuthenticationGetUserSessionAsync(requestToken));
}

/// <remarks>
/// Requires a valid test user to be assigned
/// </remarks>
[Fact]
public void TestAuthenticationGetUserSessionApiUserValidationSuccess()
public async Task TestAuthenticationGetUserSessionApiUserValidationSuccessAsync()
{
Token token = Config.Client.AuthenticationRequestAutenticationTokenAsync().Sync();
Token token = await Config.Client.AuthenticationRequestAutenticationTokenAsync();

Config.Client.AuthenticationValidateUserTokenAsync(token.RequestToken, Config.Username, Config.Password).Sync();
await Config.Client.AuthenticationValidateUserTokenAsync(token.RequestToken, Config.Username, Config.Password);
}

[Fact]
public void TestAuthenticationGetUserSessionApiUserValidationInvalidLogin()
public async Task TestAuthenticationGetUserSessionApiUserValidationInvalidLoginAsync()
{
Token token = Config.Client.AuthenticationRequestAutenticationTokenAsync().Sync();
Token token = await Config.Client.AuthenticationRequestAutenticationTokenAsync();

Assert.Throws<UnauthorizedAccessException>(() => Config.Client.AuthenticationValidateUserTokenAsync(token.RequestToken, "bla", "bla").Sync());
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => Config.Client.AuthenticationValidateUserTokenAsync(token.RequestToken, "bla", "bla"));
}

/// <remarks>
/// Requires a valid test user to be assigned
/// </remarks>
[Fact]
public void AuthenticationGetUserSessionWithLoginSuccess()
public async Task AuthenticationGetUserSessionWithLoginSuccess()
{
UserSession session = Config.Client.AuthenticationGetUserSessionAsync(Config.Username, Config.Password).Result;
UserSession session = await Config.Client.AuthenticationGetUserSessionAsync(Config.Username, Config.Password);

Assert.NotNull(session);
Assert.True(session.Success);
Assert.NotNull(session.SessionId);
}

[Fact]
public void TestAuthenticationUserAuthenticatedSessionOldToken()
public async Task TestAuthenticationUserAuthenticatedSessionOldTokenAsync()
{
const string requestToken = "5f3a62c0d7977319e3d14adf1a2064c0c0938bcf";

Assert.Throws<UnauthorizedAccessException>(() => Config.Client.AuthenticationGetUserSessionAsync(requestToken).Sync());
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => Config.Client.AuthenticationGetUserSessionAsync(requestToken));
}

[Fact]
public void TestAuthenticationCreateGuestSession()
public async Task TestAuthenticationCreateGuestSessionAsync()
{
GuestSession guestSession = Config.Client.AuthenticationCreateGuestSessionAsync().Sync();
GuestSession guestSession = await Config.Client.AuthenticationCreateGuestSessionAsync();

Assert.NotNull(guestSession);
Assert.True(guestSession.Success);
Assert.NotNull(guestSession.ExpiresAt);
Assert.NotNull(guestSession.GuestSessionId);
}
}
Expand Down
10 changes: 5 additions & 5 deletions TMDbLibTests/ClientCertificationsTests.cs
@@ -1,18 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using TMDbLib.Objects.Certifications;
using TMDbLibTests.Helpers;
using TMDbLibTests.JsonHelpers;

namespace TMDbLibTests
{
public class ClientCertificationsTests : TestBase
{
[Fact]
public void TestCertificationsListMovie()
public async Task TestCertificationsListMovieAsync()
{
CertificationsContainer result = Config.Client.GetMovieCertificationsAsync().Sync();
CertificationsContainer result = await Config.Client.GetMovieCertificationsAsync();
Assert.NotNull(result);
Assert.NotNull(result.Certifications);
Assert.True(result.Certifications.Count > 1);
Expand All @@ -30,9 +30,9 @@ public void TestCertificationsListMovie()
}

[Fact]
public void TestCertificationsListTv()
public async Task TestCertificationsListTvAsync()
{
CertificationsContainer result = Config.Client.GetTvCertificationsAsync().Sync();
CertificationsContainer result = await Config.Client.GetTvCertificationsAsync();
Assert.NotNull(result);
Assert.NotNull(result.Certifications);
Assert.True(result.Certifications.Count > 1);
Expand Down
32 changes: 16 additions & 16 deletions TMDbLibTests/ClientChangesTests.cs
@@ -1,77 +1,77 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using TMDbLib.Objects.Changes;
using TMDbLib.Objects.General;
using TMDbLibTests.Helpers;
using TMDbLibTests.JsonHelpers;

namespace TMDbLibTests
{
public class ClientChangesTests : TestBase
{
[Fact]
public void TestChangesMovies()
public async Task TestChangesMoviesAsync()
{
// Basic check
SearchContainer<ChangesListItem> changesPage1 = Config.Client.GetChangesMoviesAsync().Sync();
SearchContainer<ChangesListItem> changesPage1 = await Config.Client.GetChangesMoviesAsync();

Assert.NotNull(changesPage1);
Assert.True(changesPage1.Results.Count > 0);
Assert.True(changesPage1.TotalResults > changesPage1.Results.Count);
Assert.Equal(1, changesPage1.Page);

// Page 2
SearchContainer<ChangesListItem> changesPage2 = Config.Client.GetChangesMoviesAsync(2).Result;
SearchContainer<ChangesListItem> changesPage2 = await Config.Client.GetChangesMoviesAsync(2);

Assert.NotNull(changesPage2);
Assert.Equal(2, changesPage2.Page);

// Check date range (max)
DateTime higher = DateTime.UtcNow.AddDays(-7);
SearchContainer<ChangesListItem> changesMaxDate = Config.Client.GetChangesMoviesAsync(endDate: higher).Result;
SearchContainer<ChangesListItem> changesMaxDate = await Config.Client.GetChangesMoviesAsync(endDate: higher);

Assert.NotNull(changesMaxDate);
Assert.Equal(1, changesMaxDate.Page);
Assert.NotEqual(changesPage1.TotalResults, changesMaxDate.TotalResults);

// Check date range (lower)
DateTime lower = DateTime.UtcNow.AddDays(-6); // Use 6 days to avoid clashes with the 'higher'
SearchContainer<ChangesListItem> changesLowDate = Config.Client.GetChangesMoviesAsync(startDate: lower).Result;
SearchContainer<ChangesListItem> changesLowDate = await Config.Client.GetChangesMoviesAsync(startDate: lower);

Assert.NotNull(changesLowDate);
Assert.Equal(1, changesLowDate.Page);
Assert.NotEqual(changesPage1.TotalResults, changesLowDate.TotalResults);
}

[Fact]
public void TestChangesPeople()
public async Task TestChangesPeopleAsync()
{
// Basic check
SearchContainer<ChangesListItem> changesPage1 = Config.Client.GetChangesPeopleAsync().Sync();
SearchContainer<ChangesListItem> changesPage1 = await Config.Client.GetChangesPeopleAsync();

Assert.NotNull(changesPage1);
Assert.True(changesPage1.Results.Count > 0);
Assert.True(changesPage1.TotalResults > changesPage1.Results.Count);
Assert.Equal(1, changesPage1.Page);

// Page 2
SearchContainer<ChangesListItem> changesPage2 = Config.Client.GetChangesPeopleAsync(2).Result;
SearchContainer<ChangesListItem> changesPage2 = await Config.Client.GetChangesPeopleAsync(2);

Assert.NotNull(changesPage2);
Assert.Equal(2, changesPage2.Page);

// Check date range (max)
DateTime higher = DateTime.UtcNow.AddDays(-7);
SearchContainer<ChangesListItem> changesMaxDate = Config.Client.GetChangesPeopleAsync(endDate: higher).Result;
SearchContainer<ChangesListItem> changesMaxDate = await Config.Client.GetChangesPeopleAsync(endDate: higher);

Assert.NotNull(changesMaxDate);
Assert.Equal(1, changesMaxDate.Page);
Assert.NotEqual(changesPage1.TotalResults, changesMaxDate.TotalResults);

// Check date range (lower)
DateTime lower = DateTime.UtcNow.AddDays(-6); // Use 6 days to avoid clashes with the 'higher'
SearchContainer<ChangesListItem> changesLowDate = Config.Client.GetChangesPeopleAsync(startDate: lower).Result;
SearchContainer<ChangesListItem> changesLowDate = await Config.Client.GetChangesPeopleAsync(startDate: lower);

Assert.NotNull(changesLowDate);
Assert.Equal(1, changesLowDate.Page);
Expand All @@ -84,10 +84,10 @@ public void TestChangesPeople()


[Fact]
public void TestChangesTvShows()
public async Task TestChangesTvShowsAsync()
{
// Basic check
SearchContainer<ChangesListItem> changesPage1 = Config.Client.GetChangesTvAsync().Sync();
SearchContainer<ChangesListItem> changesPage1 = await Config.Client.GetChangesTvAsync();

Assert.NotNull(changesPage1);
Assert.NotNull(changesPage1.Results);
Expand All @@ -99,23 +99,23 @@ public void TestChangesTvShows()
{
Assert.True(changesPage1.TotalResults > changesPage1.Results.Count);
// Page 2
SearchContainer<ChangesListItem> changesPage2 = Config.Client.GetChangesTvAsync(2).Result;
SearchContainer<ChangesListItem> changesPage2 = await Config.Client.GetChangesTvAsync(2);

Assert.NotNull(changesPage2);
Assert.Equal(2, changesPage2.Page);
}

// Check date range (max)
DateTime higher = DateTime.UtcNow.AddDays(-8);
SearchContainer<ChangesListItem> changesMaxDate = Config.Client.GetChangesTvAsync(endDate: higher).Result;
SearchContainer<ChangesListItem> changesMaxDate = await Config.Client.GetChangesTvAsync(endDate: higher);

Assert.NotNull(changesMaxDate);
Assert.Equal(1, changesMaxDate.Page);
Assert.NotEqual(changesPage1.TotalResults, changesMaxDate.TotalResults);

// Check date range (lower)
DateTime lower = DateTime.UtcNow.AddDays(-6); // Use 6 days to avoid clashes with the 'higher'
SearchContainer<ChangesListItem> changesLowDate = Config.Client.GetChangesTvAsync(startDate: lower).Result;
SearchContainer<ChangesListItem> changesLowDate = await Config.Client.GetChangesTvAsync(startDate: lower);

Assert.NotNull(changesLowDate);
Assert.Equal(1, changesLowDate.Page);
Expand Down
33 changes: 15 additions & 18 deletions TMDbLibTests/ClientCollectionTests.cs
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using TMDbLib.Objects.Collections;
using TMDbLib.Objects.General;
Expand All @@ -22,9 +22,9 @@ public ClientCollectionTests()
}

[Fact]
public void TestCollectionsExtrasNone()
public async Task TestCollectionsExtrasNone()
{
Collection collection = Config.Client.GetCollectionAsync(IdHelper.JamesBondCollection).Result;
Collection collection = await Config.Client.GetCollectionAsync(IdHelper.JamesBondCollection);

Assert.NotNull(collection);
Assert.Equal("James Bond Collection", collection.Name);
Expand All @@ -39,17 +39,17 @@ public void TestCollectionsExtrasNone()
}

[Fact]
public void TestCollectionMissing()
public async Task TestCollectionMissing()
{
Collection collection = Config.Client.GetCollectionAsync(IdHelper.MissingID).Result;
Collection collection = await Config.Client.GetCollectionAsync(IdHelper.MissingID);

Assert.Null(collection);
}

[Fact]
public void TestCollectionsParts()
public async Task TestCollectionsParts()
{
Collection collection = Config.Client.GetCollectionAsync(IdHelper.JamesBondCollection).Result;
Collection collection = await Config.Client.GetCollectionAsync(IdHelper.JamesBondCollection);

Assert.NotNull(collection);
Assert.Equal("James Bond Collection", collection.Name);
Expand All @@ -62,31 +62,28 @@ public void TestCollectionsParts()
}

[Fact]
public void TestCollectionsExtrasExclusive()
public async Task TestCollectionsExtrasExclusive()
{
TestMethodsHelper.TestGetExclusive(_methods, (id, extras) => Config.Client.GetCollectionAsync(id, extras).Result, IdHelper.JamesBondCollection);
await TestMethodsHelper.TestGetExclusive(_methods, extras => Config.Client.GetCollectionAsync(IdHelper.JamesBondCollection, extras));
}

[Fact]
public void TestCollectionsExtrasAll()
public async Task TestCollectionsExtrasAll()
{
CollectionMethods combinedEnum = _methods.Keys.Aggregate((methods, movieMethods) => methods | movieMethods);
Collection item = Config.Client.GetCollectionAsync(IdHelper.JamesBondCollection, combinedEnum).Result;

TestMethodsHelper.TestAllNotNull(_methods, item);
await TestMethodsHelper.TestGetAll(_methods, combined => Config.Client.GetCollectionAsync(IdHelper.JamesBondCollection, combined));
}

[Fact]
public void TestCollectionsImages()
public async Task TestCollectionsImagesAsync()
{
// Get config
Config.Client.GetConfigAsync().Sync();
await Config.Client.GetConfigAsync();

// Test image url generator
ImagesWithId images = Config.Client.GetCollectionImagesAsync(IdHelper.JamesBondCollection).Result;
ImagesWithId images = await Config.Client.GetCollectionImagesAsync(IdHelper.JamesBondCollection);

Assert.Equal(IdHelper.JamesBondCollection, images.Id);
TestImagesHelpers.TestImages(Config, images);
await TestImagesHelpers.TestImagesAsync(Config, images);
}
}
}

0 comments on commit f5cdd0c

Please sign in to comment.