89 lines
3.0 KiB
C#
89 lines
3.0 KiB
C#
using System;
|
|
using System.Threading;
|
|
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;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public abstract class SqliteDbTestFixture : IDisposable
|
|
{
|
|
private readonly SqliteConnection _connection;
|
|
private readonly DbContextOptions<JellyfinDbContext> _dbOptions;
|
|
|
|
protected SqliteDbTestFixture()
|
|
{
|
|
ApplicationPaths = new Mock<IApplicationPaths>().Object;
|
|
|
|
_connection = new SqliteConnection("Data Source=:memory:");
|
|
_connection.Open();
|
|
|
|
_dbOptions = new DbContextOptionsBuilder<JellyfinDbContext>()
|
|
.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<JellyfinDbContext>.Instance,
|
|
new SqliteDatabaseProvider(ApplicationPaths, NullLogger<SqliteDatabaseProvider>.Instance),
|
|
new NoLockBehavior(NullLogger<NoLockBehavior>.Instance));
|
|
|
|
protected IDbContextFactory<JellyfinDbContext> CreateDbContextFactory()
|
|
{
|
|
var factory = new Mock<IDbContextFactory<JellyfinDbContext>>();
|
|
factory.Setup(f => f.CreateDbContext()).Returns(CreateDbContext);
|
|
factory.Setup(f => f.CreateDbContextAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(CreateDbContext);
|
|
|
|
return factory.Object;
|
|
}
|
|
|
|
protected BaseItemRepository CreateBaseItemRepository(ItemTypeLookup itemTypeLookup)
|
|
{
|
|
var serverConfigurationManager = new Mock<IServerConfigurationManager>();
|
|
serverConfigurationManager.Setup(c => c.Configuration).Returns(new ServerConfiguration());
|
|
|
|
return new BaseItemRepository(
|
|
CreateDbContextFactory(),
|
|
new Mock<IServerApplicationHost>().Object,
|
|
itemTypeLookup,
|
|
serverConfigurationManager.Object,
|
|
NullLogger<BaseItemRepository>.Instance);
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
_connection.Dispose();
|
|
}
|
|
}
|
|
}
|