1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
using System;
using System.Linq;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Server.Implementations.Item;
using MediaBrowser.Controller;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Item;
/// <summary>
/// DeleteItem has to hand SQLite one statement that already contains everything the foreign keys
/// on BaseItems require, because FK_BaseItems_BaseItems_OwnerId is NO ACTION: anything left behind
/// pointing at a deleted row fails the whole delete with SQLite error 19.
/// </summary>
public sealed class ItemPersistenceDeleteItemTests : SqliteDbTestFixture
{
private static readonly Guid _owner = Guid.Parse("aaaaaaaa-0000-0000-0000-000000000001");
private static readonly Guid _extra = Guid.Parse("eeeeeeee-0000-0000-0000-000000000001");
private static readonly Guid _extraOfExtra = Guid.Parse("eeeeeeee-0000-0000-0000-000000000002");
private static readonly Guid _child = Guid.Parse("cccccccc-0000-0000-0000-000000000001");
private static readonly Guid _extraOfChild = Guid.Parse("eeeeeeee-0000-0000-0000-000000000003");
private readonly ItemPersistenceService _service;
public ItemPersistenceDeleteItemTests()
{
_service = new ItemPersistenceService(
CreateDbContextFactory(),
new Mock<IServerApplicationHost>().Object,
NullLogger<ItemPersistenceService>.Instance);
}
[Fact]
public void DeleteItem_OwnerIdChain_DeletesWholeChain()
{
// An extra that owns an extra of its own. Real libraries carry these in bulk, and a single
// expansion pass over OwnerId leaves the second level behind.
Seed(
(_owner, null, null),
(_extra, _owner, null),
(_extraOfExtra, _extra, null));
_service.DeleteItem([_owner]);
using var context = CreateDbContext();
Assert.Empty(context.BaseItems.Where(e => e.Id.Equals(_owner) || e.Id.Equals(_extra) || e.Id.Equals(_extraOfExtra)));
}
[Fact]
public void DeleteItem_ExtraOwnedByCascadedChild_DeletesExtraToo()
{
// The child goes away through FK_BaseItems_BaseItems_ParentId's ON DELETE CASCADE whether or
// not it is listed, so an extra owned by that child has to be listed with it.
Seed(
(_owner, null, null),
(_child, null, _owner),
(_extraOfChild, _child, null));
_service.DeleteItem([_owner]);
using var context = CreateDbContext();
Assert.Empty(context.BaseItems.Where(e => e.Id.Equals(_owner) || e.Id.Equals(_child) || e.Id.Equals(_extraOfChild)));
}
[Fact]
public void DeleteItem_OwnershipCycle_Terminates()
{
// A malformed pair that owns each other must not spin the closure loop forever.
Seed((_owner, null, null), (_extra, _owner, null));
using (var context = CreateDbContext())
{
context.BaseItems.Single(e => e.Id.Equals(_owner)).OwnerId = _extra;
context.SaveChanges();
}
_service.DeleteItem([_owner]);
using var assertContext = CreateDbContext();
Assert.Empty(assertContext.BaseItems.Where(e => e.Id.Equals(_owner) || e.Id.Equals(_extra)));
}
private void Seed(params (Guid Id, Guid? OwnerId, Guid? ParentId)[] items)
{
using var context = CreateDbContext();
// Owners before the rows referencing them: the seed itself is foreign key checked.
foreach (var (id, ownerId, parentId) in items)
{
context.BaseItems.Add(new BaseItemEntity
{
Id = id,
Type = "MediaBrowser.Controller.Entities.Video",
OwnerId = ownerId,
ParentId = parentId
});
context.SaveChanges();
}
}
}
|