using System;
using Emby.Server.Implementations.Data;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Locking;
using Jellyfin.Database.Providers.Sqlite;
using Jellyfin.Server.Implementations.Item;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Configuration;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
namespace Jellyfin.Server.Implementations.Tests.Item;
///
/// Base fixture for the item tests that run against the SQLite provider: one in-memory database per
/// test class, plus the wiring the repositories under test need. The connection owns the database, so
/// it stays open for the lifetime of the fixture. Derived classes seed in their own constructor.
///
public abstract class SqliteDbTestFixture : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions _dbOptions;
protected SqliteDbTestFixture()
{
ApplicationPaths = new Mock().Object;
_connection = new SqliteConnection("Data Source=:memory:");
_connection.Open();
_dbOptions = new DbContextOptionsBuilder()
.UseSqlite(_connection)
.Options;
using var context = CreateDbContext();
context.Database.EnsureCreated();
}
protected IApplicationPaths ApplicationPaths { get; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected JellyfinDbContext CreateDbContext() => new(
_dbOptions,
NullLogger.Instance,
new SqliteDatabaseProvider(ApplicationPaths, NullLogger.Instance),
new NoLockBehavior(NullLogger.Instance));
protected IDbContextFactory CreateDbContextFactory()
{
var factory = new Mock>();
factory.Setup(f => f.CreateDbContext()).Returns(CreateDbContext);
return factory.Object;
}
protected BaseItemRepository CreateBaseItemRepository(ItemTypeLookup itemTypeLookup)
{
var serverConfigurationManager = new Mock();
serverConfigurationManager.Setup(c => c.Configuration).Returns(new ServerConfiguration());
return new BaseItemRepository(
CreateDbContextFactory(),
new Mock().Object,
itemTypeLookup,
serverConfigurationManager.Object,
NullLogger.Instance);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_connection.Dispose();
}
}
}