Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
tests: parametrize tests using theories (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored Mar 24, 2024
1 parent b5b9c2b commit a85b2ad
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 416 deletions.
113 changes: 62 additions & 51 deletions Jellyfin.Plugin.Themerr.Tests/FixtureJellyfinServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,25 @@ namespace Jellyfin.Plugin.Themerr.Tests;
public class FixtureJellyfinServer
{
/// <summary>
/// Mock media items to use for testing
/// Gets a list of mock items.
/// </summary>
public static IEnumerable<object[]> MockItemsData => MockItems().Select(item => new object[] { item });

/// <summary>
/// Gets a list of mock items.
/// </summary>
public static IEnumerable<object[]> MockItems2Data => MockItems2().Select(item => new object[] { item });

/// <summary>
/// Gets a list of unsupported mock items.
/// </summary>
public static IEnumerable<object[]> UnsupportedMockItemsData => UnsupportedMockItems().Select(item => new object[] { item });

/// <summary>
/// Mock media items to use for testing.
/// </summary>
/// <returns>List containing mock <see cref="BaseItem"/> objects.</returns>
public static List<BaseItem> MockItems()
private static List<BaseItem> MockItems()
{
return new List<BaseItem>
{
Expand Down Expand Up @@ -83,10 +98,10 @@ public static List<BaseItem> MockItems()
}

/// <summary>
/// Mock items without an associated theme in ThemerrDB to use for testing
/// Mock items without an associated theme in ThemerrDB to use for testing.
/// </summary>
/// <returns>List containing mock <see cref="BaseItem"/> objects.</returns>
public static List<BaseItem> MockItems2()
private static List<BaseItem> MockItems2()
{
return new List<BaseItem>
{
Expand Down Expand Up @@ -114,10 +129,10 @@ public static List<BaseItem> MockItems2()
}

/// <summary>
/// Mock items which are not supported by Themerr
/// Mock items which are not supported by Themerr.
/// </summary>
/// <returns>List containing mock <see cref="BaseItem"/> objects.</returns>
public static List<BaseItem> UnsupportedMockItems()
private static List<BaseItem> UnsupportedMockItems()
{
return new List<BaseItem>
{
Expand All @@ -133,14 +148,13 @@ public static List<BaseItem> UnsupportedMockItems()
}

/// <summary>
/// Create mock items from stub video
/// Create mock items from stub video.
/// </summary>
[Fact]
[Theory]
[Trait("Category", "Init")]
private void CreateMockItems()
[MemberData(nameof(MockItemsData))]
private void CreateMockItems(BaseItem item)
{
var mockItems = MockItems();

// get the stub video path based on the directory of this file
var stubVideoPath = Path.Combine(
Directory.GetCurrentDirectory(),
Expand All @@ -149,50 +163,47 @@ private void CreateMockItems()

Assert.True(File.Exists(stubVideoPath), "Could not find ./data/video_stub.mp4");

foreach (var item in mockItems)
{
// copy the ./data/video_stub.mp4 to the movie folder "movie.Name (movie.ProductionYear)"
var itemFolder = Path.Combine(
"themerr_jellyfin_tests",
$"{item.Name} ({item.ProductionYear})");
// copy the ./data/video_stub.mp4 to the movie folder "movie.Name (movie.ProductionYear)"
var itemFolder = Path.Combine(
"themerr_jellyfin_tests",
$"{item.Name} ({item.ProductionYear})");

// create the movie folder
Directory.CreateDirectory(itemFolder);

string? itemVideoPath = null;
if (item is Movie)
{
// copy the video_stub.mp4 to the movie folder, renaming it based on movie name
itemVideoPath = Path.Combine(
itemFolder,
$"{item.Name} ({item.ProductionYear}).mp4");
}
else if (item is Series)
{
// season folder
var seasonFolder = Path.Combine(
itemFolder,
"Season 01");
Directory.CreateDirectory(seasonFolder);
// create the movie folder
Directory.CreateDirectory(itemFolder);

// copy the video_stub.mp4 to the season folder, renaming it based on series name
itemVideoPath = Path.Combine(
seasonFolder,
$"{item.Name} ({item.ProductionYear}) - S01E01 - Episode Name.mp4");
}
else
{
Assert.Fail($"Unknown item type: {item.GetType()}");
}
string? itemVideoPath = null;
if (item is Movie)
{
// copy the video_stub.mp4 to the movie folder, renaming it based on movie name
itemVideoPath = Path.Combine(
itemFolder,
$"{item.Name} ({item.ProductionYear}).mp4");
}
else if (item is Series)
{
// season folder
var seasonFolder = Path.Combine(
itemFolder,
"Season 01");
Directory.CreateDirectory(seasonFolder);

// if file does not exist
if (!File.Exists(itemVideoPath))
{
// copy the stub video to the item folder
File.Copy(stubVideoPath, itemVideoPath);
}
// copy the video_stub.mp4 to the season folder, renaming it based on series name
itemVideoPath = Path.Combine(
seasonFolder,
$"{item.Name} ({item.ProductionYear}) - S01E01 - Episode Name.mp4");
}
else
{
Assert.Fail($"Unknown item type: {item.GetType()}");
}

Assert.True(File.Exists(itemVideoPath), $"Could not find {itemVideoPath}");
// if file does not exist
if (!File.Exists(itemVideoPath))
{
// copy the stub video to the item folder
File.Copy(stubVideoPath, itemVideoPath);
}

Assert.True(File.Exists(itemVideoPath), $"Could not find {itemVideoPath}");
}
}
57 changes: 25 additions & 32 deletions Jellyfin.Plugin.Themerr.Tests/TestThemerrController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,42 +97,35 @@ public void TestGetTranslations()
/// <summary>
/// Test GetCultureResource function.
/// </summary>
[Fact]
/// <param name="culture">The culture to test.</param>
[Theory]
[Trait("Category", "Unit")]
public void TestGetCultureResource()
[InlineData("de")]
[InlineData("en")]
[InlineData("en-GB")]
[InlineData("en-US")]
[InlineData("es")]
[InlineData("fr")]
[InlineData("it")]
[InlineData("ru")]
[InlineData("sv")]
[InlineData("zh")]
public void TestGetCultureResource(string culture)
{
// list of english cultures
var enCultures = new List<string>
{
"de",
"en",
"en-GB",
"en-US",
"es",
"fr",
"it",
"ru",
"sv",
"zh"
};

foreach (var t in enCultures)
{
var result = _controller.GetCultureResource(t);
Assert.IsType<List<string>>(result);
var result = _controller.GetCultureResource(culture);
Assert.IsType<List<string>>(result);

// replace - with _ in the culture
var t2 = t.Replace("-", "_");
// replace - with _ in the culture
var culture2 = culture.Replace("-", "_");

// en is not included in the list
if (t != "en")
{
// assert that `en_<>.json` is in the list
Assert.Contains(t2 + ".json", result);
}

// assert that `en` is NOT in the list
Assert.DoesNotContain("en.json", result);
// en is not included in the list
if (culture != "en")
{
// assert that `en_<>.json` is in the list
Assert.Contains(culture2 + ".json", result);
}

// assert that `en` is NOT in the list
Assert.DoesNotContain("en.json", result);
}
}
Loading

0 comments on commit a85b2ad

Please sign in to comment.