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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
using System;
using System.Linq;
using Emby.Server.Implementations.Data;
using Jellyfin.Data.Enums;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Server.Implementations.Item;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Persistence;
using Moq;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Item;
/// <summary>
/// Covers <see cref="InternalPeopleQuery.ParentId"/>, which limits /Persons to the people credited below a folder.
/// </summary>
public sealed class PeopleRepositoryParentFilterTests : SqliteDbTestFixture
{
private readonly QueryPlanRecorder _recorder;
private readonly PeopleRepository _people;
private readonly Guid _library = Guid.NewGuid();
private readonly Guid _series = Guid.NewGuid();
private readonly Guid _episode = Guid.NewGuid();
private readonly Guid _secondEpisode = Guid.NewGuid();
private readonly Guid _otherSeries = Guid.NewGuid();
private readonly Guid _otherEpisode = Guid.NewGuid();
private readonly Guid _emptyFolder = Guid.NewGuid();
public PeopleRepositoryParentFilterTests()
: this(new QueryPlanRecorder())
{
}
private PeopleRepositoryParentFilterTests(QueryPlanRecorder recorder)
: base(recorder)
{
_recorder = recorder;
var lookup = new ItemTypeLookup();
using (var context = CreateDbContext())
{
var folder = lookup.BaseItemKindNames[BaseItemKind.Folder];
var series = lookup.BaseItemKindNames[BaseItemKind.Series];
var episode = lookup.BaseItemKindNames[BaseItemKind.Episode];
context.BaseItems.Add(new BaseItemEntity { Id = _library, Type = folder, Name = "Shows", IsFolder = true });
context.BaseItems.Add(new BaseItemEntity { Id = _series, Type = series, Name = "Series", IsFolder = true });
context.BaseItems.Add(new BaseItemEntity { Id = _episode, Type = episode, Name = "Episode 1" });
context.BaseItems.Add(new BaseItemEntity { Id = _secondEpisode, Type = episode, Name = "Episode 2" });
context.BaseItems.Add(new BaseItemEntity { Id = _otherSeries, Type = series, Name = "Other series", IsFolder = true });
context.BaseItems.Add(new BaseItemEntity { Id = _otherEpisode, Type = episode, Name = "Other episode" });
context.BaseItems.Add(new BaseItemEntity { Id = _emptyFolder, Type = folder, Name = "Empty", IsFolder = true });
AddAncestors(context, _series, _library);
AddAncestors(context, _episode, _series, _library);
AddAncestors(context, _secondEpisode, _series, _library);
AddAncestors(context, _otherSeries, _library);
AddAncestors(context, _otherEpisode, _otherSeries, _library);
AddAncestors(context, _emptyFolder, _library);
context.SaveChanges();
}
_people = new PeopleRepository(CreateDbContextFactory(), lookup, Mock.Of<IItemQueryHelpers>());
_people.UpdatePeople(_episode, [Actor("Lead"), Actor("Guest")]);
_people.UpdatePeople(_secondEpisode, [Actor("Lead")]);
_people.UpdatePeople(_otherEpisode, [Actor("Stranger")]);
}
[Fact]
public void ParentId_ReturnsEachPersonCreditedBelowTheParentOnce()
{
var result = _people.GetPeople(new InternalPeopleQuery([], []) { ParentId = _series });
Assert.Equal(["Guest", "Lead"], result.Items.Select(p => p.Name).Distinct().Order());
Assert.Equal(2, result.TotalRecordCount);
}
[Fact]
public void ParentId_SpanningSeveralFolders_ReturnsTheirUnion()
{
var result = _people.GetPeople(new InternalPeopleQuery([], []) { ParentId = _library });
Assert.Equal(["Guest", "Lead", "Stranger"], result.Items.Select(p => p.Name).Distinct().Order());
Assert.Equal(3, result.TotalRecordCount);
}
[Fact]
public void ParentId_WithoutCreditsBelowIt_ReturnsNobody()
{
var result = _people.GetPeople(new InternalPeopleQuery([], []) { ParentId = _emptyFolder });
Assert.Empty(result.Items);
Assert.Equal(0, result.TotalRecordCount);
}
[Fact]
public void ParentId_SeeksCreditsByDescendant()
{
_recorder.Commands.Clear();
_people.GetPeople(new InternalPeopleQuery([], []) { ParentId = _series, Limit = 1 });
var query = Assert.Single(_recorder.Commands, c => c.Sql.Contains("\"AncestorIds\"", StringComparison.Ordinal) && c.Sql.Contains("ORDER BY", StringComparison.Ordinal));
var plan = Explain(query);
Assert.Contains(plan, line => line.Contains("COVERING INDEX IX_AncestorIds_ParentItemId_ItemId (ParentItemId=?)", StringComparison.Ordinal));
Assert.Contains(plan, line => line.StartsWith("SEARCH p USING INDEX sqlite_autoindex_Peoples_1 (Id=?)", StringComparison.Ordinal));
Assert.DoesNotContain(plan, line => line.StartsWith("SCAN p ", StringComparison.Ordinal) || line == "SCAN p");
}
private static PersonInfo Actor(string name) => new() { Name = name, Type = PersonKind.Actor };
private static void AddAncestors(JellyfinDbContext context, Guid itemId, params Guid[] ancestorIds)
{
foreach (var ancestorId in ancestorIds)
{
context.AncestorIds.Add(new AncestorId
{
ItemId = itemId,
ParentItemId = ancestorId,
Item = null!,
ParentItem = null!
});
}
}
private string[] Explain(RecordedCommand query)
{
using var context = CreateDbContext();
return QueryPlanRecorder.Explain(context, query);
}
}
|