aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryDescendantFilterTests.cs
blob: 0ca11eb58de8ee8a7fde8619ace415032a1c24f5 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
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 Xunit;
using LinkedChildType = Jellyfin.Database.Implementations.Entities.LinkedChildType;

namespace Jellyfin.Server.Implementations.Tests.Item;

/// <summary>
/// Covers <see cref="InternalItemsQuery.DescendantOfId"/>, the filter a recursive query rooted at a
/// BoxSet or Playlist runs on. Those hold their contents as linked children, so the items below a
/// linked folder are only reachable by following the link and then the ancestor chain.
/// </summary>
public sealed class BaseItemRepositoryDescendantFilterTests : SqliteDbTestFixture
{
    private const string FolderType = "MediaBrowser.Controller.Entities.Folder";
    private const string BoxSetType = "MediaBrowser.Controller.Entities.Movies.BoxSet";
    private const string SeriesType = "MediaBrowser.Controller.Entities.TV.Series";
    private const string SeasonType = "MediaBrowser.Controller.Entities.TV.Season";
    private const string EpisodeType = "MediaBrowser.Controller.Entities.TV.Episode";
    private const string MovieType = "MediaBrowser.Controller.Entities.Movies.Movie";

    private readonly BaseItemRepository _repository;

    private readonly Guid _library = Guid.NewGuid();
    private readonly Guid _collection = Guid.NewGuid();
    private readonly Guid _series = Guid.NewGuid();
    private readonly Guid _season = Guid.NewGuid();
    private readonly Guid _episode = Guid.NewGuid();

    // A movie the collection links directly, so the direct-child case is covered alongside the nested one.
    private readonly Guid _collectionMovie = Guid.NewGuid();

    // In the same library but outside the collection, as the control the assertions are read against.
    private readonly Guid _otherSeries = Guid.NewGuid();
    private readonly Guid _otherEpisode = Guid.NewGuid();

    public BaseItemRepositoryDescendantFilterTests()
    {
        using (var ctx = CreateDbContext())
        {
            Seed(ctx);
        }

        _repository = CreateBaseItemRepository(new ItemTypeLookup());
    }

    [Fact]
    public void DescendantOfId_ReachesEpisodesOfALinkedSeries()
    {
        var ids = _repository.GetItemIdsList(new InternalItemsQuery
        {
            DescendantOfId = _collection,
            IncludeItemTypes = [BaseItemKind.Episode]
        });

        Assert.Equal([_episode], ids);
    }

    [Fact]
    public void DescendantOfId_ReturnsEveryLevelBelowTheCollection()
    {
        var ids = _repository.GetItemIdsList(new InternalItemsQuery { DescendantOfId = _collection }).ToHashSet();

        Assert.Equal(new[] { _series, _season, _episode, _collectionMovie }.Order(), ids.Order());
    }

    [Fact]
    public void DescendantOfId_KeepsDirectlyLinkedChildren()
    {
        var ids = _repository.GetItemIdsList(new InternalItemsQuery
        {
            DescendantOfId = _collection,
            IncludeItemTypes = [BaseItemKind.Movie]
        });

        Assert.Equal([_collectionMovie], ids);
    }

    [Fact]
    public void DescendantOfId_OnAnEmptyCollection_ReturnsNothing()
    {
        var ids = _repository.GetItemIdsList(new InternalItemsQuery { DescendantOfId = Guid.NewGuid() });

        Assert.Empty(ids);
    }

    private void Seed(JellyfinDbContext context)
    {
        context.BaseItems.Add(new BaseItemEntity { Id = _library, Type = FolderType, Name = "Shows", IsFolder = true });
        context.BaseItems.Add(new BaseItemEntity { Id = _collection, Type = BoxSetType, Name = "Collection", IsFolder = true });
        context.BaseItems.Add(new BaseItemEntity { Id = _series, Type = SeriesType, Name = "Series", IsFolder = true });
        context.BaseItems.Add(new BaseItemEntity { Id = _season, Type = SeasonType, Name = "Season 1", IsFolder = true });
        context.BaseItems.Add(new BaseItemEntity { Id = _episode, Type = EpisodeType, Name = "Episode 1" });
        context.BaseItems.Add(new BaseItemEntity { Id = _collectionMovie, Type = MovieType, Name = "Movie" });
        context.BaseItems.Add(new BaseItemEntity { Id = _otherSeries, Type = SeriesType, Name = "Other series", IsFolder = true });
        context.BaseItems.Add(new BaseItemEntity { Id = _otherEpisode, Type = EpisodeType, Name = "Other episode" });

        // AncestorIds is a closure: production writes one row per ancestor, not just the parent.
        AddAncestors(context, _series, _library);
        AddAncestors(context, _season, _series, _library);
        AddAncestors(context, _episode, _season, _series, _library);
        AddAncestors(context, _collectionMovie, _library);
        AddAncestors(context, _otherSeries, _library);
        AddAncestors(context, _otherEpisode, _otherSeries, _library);

        AddLink(context, _series, 0);
        AddLink(context, _collectionMovie, 1);

        context.SaveChanges();
    }

    private 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 void AddLink(JellyfinDbContext context, Guid childId, int sortOrder)
    {
        context.LinkedChildren.Add(new LinkedChildEntity
        {
            ParentId = _collection,
            ChildId = childId,
            ChildType = LinkedChildType.Manual,
            SortOrder = sortOrder
        });
    }
}