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

Commit

Permalink
tests: improve test coverage (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored Dec 12, 2023
1 parent e94a46a commit b4c683e
Show file tree
Hide file tree
Showing 10 changed files with 656 additions and 138 deletions.
115 changes: 115 additions & 0 deletions Jellyfin.Plugin.Themerr.Tests/BootstrapJellyfinServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using MetadataProvider = MediaBrowser.Model.Entities.MetadataProvider;
using Movie = MediaBrowser.Controller.Entities.Movies.Movie;

namespace Jellyfin.Plugin.Themerr.Tests;

[CollectionDefinition("Bootstrapped Collection")]
public class BootstrappedCollection : ICollectionFixture<BootstrapJellyfinServer>
{
// This class doesn't need to have any code, or even be long-lived.
// All it needs is to just exist, and be annotated with CollectionDefinition.
}

/// <summary>
/// This class is used to bootstrap a Jellyfin server with mock movies
/// </summary>
public class BootstrapJellyfinServer
{
/// <summary>
/// Mock movies to use for testing
/// </summary>
/// <returns></returns>
public static List<Movie> MockMovies()
{
return new List<Movie>
{
new()
{
Name = "Elephants Dream",
ProductionYear = 2006,
ProviderIds = new Dictionary<string, string>
{
{ MetadataProvider.Imdb.ToString(), "tt0807840"},
{ MetadataProvider.Tmdb.ToString(), "9761"},
}
},
new()
{
Name = "Sita Sings the Blues",
ProductionYear = 2008,
ProviderIds = new Dictionary<string, string>
{
{ MetadataProvider.Imdb.ToString(), "tt1172203"},
{ MetadataProvider.Tmdb.ToString(), "20529"},
}
},
new()
{
Name = "Big Buck Bunny",
ProductionYear = 2008,
ProviderIds = new Dictionary<string, string>
{
{ MetadataProvider.Imdb.ToString(), "tt1254207"},
{ MetadataProvider.Tmdb.ToString(), "10378"},
}
},
new()
{
Name = "Sintel",
ProductionYear = 2010,
ProviderIds = new Dictionary<string, string>
{
{ MetadataProvider.Imdb.ToString(), "tt1727587"},
{ MetadataProvider.Tmdb.ToString(), "45745"},
}
},
};
}


/// <summary>
/// Create mock movies from stub video
/// </summary>
[Fact]
[Trait("Category", "Init")]
private void CreateMockMovies()
{
var mockMovies = MockMovies();

// get the stub video path based on the directory of this file
var stubVideoPath = Path.Combine(
Directory.GetCurrentDirectory(),
"data",
"video_stub.mp4"
);

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

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

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

// copy the video_stub.mp4 to the movie folder, renaming it based on movie name
var movieVideoPath = Path.Combine(
movieFolder,
$"{movie.Name} ({movie.ProductionYear}).mp4"
);

// if file does not exist
if (!File.Exists(movieVideoPath))
{
// copy the stub video to the movie folder
File.Copy(stubVideoPath, movieVideoPath);
}

Assert.True(File.Exists(movieVideoPath), $"Could not find {movieVideoPath}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -29,4 +30,10 @@
<ProjectReference Include="..\Jellyfin.Plugin.Themerr\Jellyfin.Plugin.Themerr.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="data\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
71 changes: 71 additions & 0 deletions Jellyfin.Plugin.Themerr.Tests/TestLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace Jellyfin.Plugin.Themerr.Tests
{
/// <summary>
/// A simple logger for tests
/// </summary>
public static class TestLogger
{
// log a message to console
private static ITestOutputHelper? _output;

public static void Initialize(ITestOutputHelper output)
{
_output = output ?? throw new ArgumentNullException(nameof(output));
}

/// <summary>
/// Logs a message to the test output
/// </summary>
/// <param name="message"></param>
/// <param name="type"></param>
public static void Log(string message, string type = "INFO")
{
_output?.WriteLine($"[{type}] {message}");
}

/// <summary>
/// Logs a critical message to the test output
/// </summary>
/// <param name="message"></param>
public static void Critical(string message)
{
Log(message, "CRITICAL");
}

/// <summary>
/// Logs a debug message to the test output
/// </summary>
/// <param name="message"></param>
public static void Debug(string message)
{
Log(message, "DEBUG");
}

/// <summary>
/// Logs an error message to the test output
/// </summary>
/// <param name="message"></param>
public static void Error(string message)
{
Log(message, "ERROR");
}

/// <summary>
/// Logs an info message to the test output
/// </summary>
/// <param name="message"></param>
public static void Info(string message)
{
Log(message, "INFO");
}

/// <summary>
/// Logs a warning message to the test output
/// </summary>
/// <param name="message"></param>
public static void Warn(string message)
{
Log(message, "WARN");
}
}
}
Loading

0 comments on commit b4c683e

Please sign in to comment.