2021-03-27 15:39:18 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2020-10-03 22:44:16 +00:00
|
|
|
using TMDbLib.Client;
|
2021-03-27 15:39:18 +00:00
|
|
|
using TMDbLib.Objects.General;
|
|
|
|
using TMDbLib.Objects.Search;
|
2021-04-15 20:00:26 +00:00
|
|
|
using TMDbLib.Utilities.Serializer;
|
2021-03-27 15:39:18 +00:00
|
|
|
using TMDbLibTests.Helpers;
|
|
|
|
using VerifyTests;
|
|
|
|
using VerifyXunit;
|
2016-05-22 12:10:30 +00:00
|
|
|
|
|
|
|
namespace TMDbLibTests.JsonHelpers
|
|
|
|
{
|
2021-03-27 15:39:18 +00:00
|
|
|
[UsesVerify]
|
2020-10-03 22:44:16 +00:00
|
|
|
public abstract class TestBase
|
2016-05-22 12:10:30 +00:00
|
|
|
{
|
2021-03-27 15:39:18 +00:00
|
|
|
private VerifySettings VerifySettings { get; }
|
|
|
|
|
2020-10-03 22:44:16 +00:00
|
|
|
protected readonly TestConfig TestConfig;
|
|
|
|
|
|
|
|
protected TMDbClient TMDbClient => TestConfig.Client;
|
2016-07-18 21:26:27 +00:00
|
|
|
|
2021-04-15 20:00:26 +00:00
|
|
|
protected ITMDbSerializer Serializer => TMDbJsonSerializer.Instance;
|
|
|
|
|
2020-10-03 21:30:18 +00:00
|
|
|
protected TestBase()
|
2016-05-22 12:10:30 +00:00
|
|
|
{
|
2021-03-27 15:39:18 +00:00
|
|
|
VerifySettings = new VerifySettings();
|
2021-04-05 22:05:44 +00:00
|
|
|
//VerifySettings.AutoVerify();
|
2021-03-27 15:39:18 +00:00
|
|
|
|
|
|
|
VerifySettings.UseDirectory("..\\Verification");
|
|
|
|
|
|
|
|
// Ignore and simplify many dynamic properties
|
|
|
|
VerifySettings.IgnoreProperty<SearchMovie>(x => x.VoteCount, x => x.Popularity, x => x.VoteAverage);
|
|
|
|
VerifySettings.SimplifyProperty<SearchMovie>(x => x.BackdropPath, x => x.PosterPath);
|
|
|
|
VerifySettings.SimplifyProperty<SearchPerson>(x => x.ProfilePath);
|
|
|
|
VerifySettings.SimplifyProperty<SearchTvEpisode>(x => x.StillPath);
|
|
|
|
VerifySettings.SimplifyProperty<ImageData>(x => x.FilePath);
|
|
|
|
VerifySettings.SimplifyProperty<SearchCompany>(x => x.LogoPath);
|
|
|
|
|
|
|
|
VerifySettings.AddExtraSettings(serializerSettings =>
|
|
|
|
{
|
|
|
|
serializerSettings.ContractResolver = new DataSortingContractResolver(serializerSettings.ContractResolver);
|
|
|
|
});
|
|
|
|
|
|
|
|
WebProxy proxy = null;
|
2021-10-19 20:28:27 +00:00
|
|
|
//proxy = new WebProxy("http://127.0.0.1:8888");
|
2021-03-27 15:39:18 +00:00
|
|
|
|
2021-04-15 20:00:26 +00:00
|
|
|
TestConfig = new TestConfig(serializer: null, proxy: proxy);
|
2021-03-27 15:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected Task Verify<T>(T obj, Action<VerifySettings> configure = null)
|
|
|
|
{
|
|
|
|
VerifySettings settings = VerifySettings;
|
|
|
|
|
|
|
|
if (configure != null)
|
|
|
|
{
|
|
|
|
settings = new VerifySettings(VerifySettings);
|
|
|
|
configure(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Verifier.Verify(obj, settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataSortingContractResolver : IContractResolver
|
|
|
|
{
|
|
|
|
private readonly IContractResolver _innerResolver;
|
|
|
|
|
|
|
|
public DataSortingContractResolver(IContractResolver innerResolver)
|
|
|
|
{
|
|
|
|
_innerResolver = innerResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JsonContract ResolveContract(Type type)
|
|
|
|
{
|
|
|
|
JsonContract contract = _innerResolver.ResolveContract(type);
|
|
|
|
|
|
|
|
// Add a callback that is invoked on each serialization of an object
|
|
|
|
// We do this to be able to sort lists
|
|
|
|
contract.OnSerializingCallbacks.Add(SerializingCallback);
|
|
|
|
|
|
|
|
return contract;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string[] _sortFieldsInOrder = { "CreditId", "Id", "Iso_3166_1", "EpisodeNumber", "SeasonNumber" };
|
|
|
|
|
|
|
|
private void SerializingCallback(object obj, StreamingContext context)
|
|
|
|
{
|
|
|
|
if (!(obj is IEnumerable) || obj is IDictionary)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Type objType = obj.GetType();
|
|
|
|
if (obj is IList objAsList)
|
|
|
|
{
|
|
|
|
Debug.Assert(objType.IsGenericType);
|
|
|
|
|
|
|
|
Type innerType = objType.GetGenericArguments().First();
|
|
|
|
|
|
|
|
// Determine which comparer to use
|
|
|
|
IComparer comparer = null;
|
|
|
|
if (innerType.IsValueType)
|
|
|
|
comparer = Comparer.Default;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (string fieldName in _sortFieldsInOrder)
|
|
|
|
{
|
|
|
|
PropertyInfo prop = innerType.GetProperty(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);
|
|
|
|
if (prop == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
comparer = new CompareObjectOnProperty(prop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comparer != null)
|
|
|
|
{
|
|
|
|
// Is sorted?
|
|
|
|
bool isSorted = IsSorted(objAsList, comparer);
|
|
|
|
|
|
|
|
if (!isSorted)
|
|
|
|
{
|
|
|
|
// Sort the list using our comparer
|
|
|
|
List<object> sortList = objAsList.Cast<object>().ToList();
|
|
|
|
sortList.Sort((x, y) => comparer.Compare(x, y));
|
|
|
|
|
|
|
|
// Transfer values
|
|
|
|
for (int i = 0; i < objAsList.Count; i++)
|
|
|
|
objAsList[i] = sortList[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsSorted(IList list, IComparer comparer)
|
|
|
|
{
|
2021-04-15 20:00:26 +00:00
|
|
|
for (int i = 1; i < list.Count; i++)
|
2021-03-27 15:39:18 +00:00
|
|
|
{
|
2021-04-15 20:00:26 +00:00
|
|
|
object a = list[i - 1];
|
|
|
|
object b = list[i];
|
2021-03-27 15:39:18 +00:00
|
|
|
|
|
|
|
if (comparer.Compare(a, b) > 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class CompareObjectOnProperty : IComparer
|
|
|
|
{
|
|
|
|
private readonly PropertyInfo _property;
|
|
|
|
|
|
|
|
public CompareObjectOnProperty(PropertyInfo property)
|
|
|
|
{
|
|
|
|
_property = property;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Compare(object x, object y)
|
|
|
|
{
|
2022-01-18 22:27:30 +00:00
|
|
|
object valX = _property.GetValue(x);
|
|
|
|
object valY = _property.GetValue(y);
|
2021-03-27 15:39:18 +00:00
|
|
|
|
|
|
|
return Comparer.Default.Compare(valX, valY);
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 12:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|