aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Item/BaseItemRepositoryChildrenTests.cs
blob: 5e045e9f83425e5576b35b287ef40c57703ada82 (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
using System;
using System.Linq;
using Emby.Server.Implementations.Data;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Server.Implementations.Item;
using MediaBrowser.Controller.Entities;
using Xunit;
using BaseItemKind = Jellyfin.Data.Enums.BaseItemKind;

namespace Jellyfin.Server.Implementations.Tests.Item;

/// <summary>
/// Covers the children query the library scan runs against a folder: a version merged by hand is
/// hidden from ordinary queries, but the scan has to see it or it takes the row for a new item and
/// recreates it, splitting the version group apart again.
/// </summary>
public sealed class BaseItemRepositoryChildrenTests : SqliteDbTestFixture
{
    private static readonly Guid _folderId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");
    private static readonly Guid _primaryId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb");
    private static readonly Guid _mergedVersionId = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc");
    private static readonly Guid _ownedVersionId = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd");

    private readonly BaseItemRepository _repository;

    public BaseItemRepositoryChildrenTests()
    {
        var itemTypeLookup = new ItemTypeLookup();
        _repository = CreateBaseItemRepository(itemTypeLookup);

        var movieTypeName = itemTypeLookup.BaseItemKindNames[BaseItemKind.Movie];
        using var ctx = CreateDbContext();
        ctx.BaseItems.Add(new BaseItemEntity
        {
            Id = _folderId,
            Type = itemTypeLookup.BaseItemKindNames[BaseItemKind.Folder]!,
            Name = "Movies",
            Path = "/movies",
            IsFolder = true
        });
        ctx.BaseItems.Add(CreateMovie(_primaryId, movieTypeName!, "Big Buck Bunny", "/media1/Big Buck Bunny/bbb-1080p.mp4", null, null));
        ctx.BaseItems.Add(CreateMovie(_mergedVersionId, movieTypeName!, "Big Buck Bunny", "/media2/Big Buck Bunny/bbb-2160p.mp4", _primaryId, null));
        ctx.BaseItems.Add(CreateMovie(_ownedVersionId, movieTypeName!, "Big Buck Bunny - 720p", "/media1/Big Buck Bunny/bbb-720p.mp4", _primaryId, _primaryId));
        ctx.SaveChanges();
    }

    [Fact]
    public void GetItemList_ChildrenOfFolder_ExcludesAlternateVersionsByDefault()
    {
        var result = _repository.GetItemList(new InternalItemsQuery { ParentId = _folderId });

        var item = Assert.Single(result);
        Assert.Equal(_primaryId, item.Id);
    }

    [Fact]
    public void GetItemList_ChildrenOfFolderIncludingAlternateVersions_KeepsMergedVersion()
    {
        var result = _repository.GetItemList(new InternalItemsQuery
        {
            ParentId = _folderId,
            IncludeAlternateVersions = true
        });

        Assert.Equal(2, result.Count);
        Assert.Contains(result, i => i.Id.Equals(_primaryId));
        Assert.Contains(result, i => i.Id.Equals(_mergedVersionId));
    }

    [Fact]
    public void GetItemList_ChildrenOfFolderIncludingAlternateVersions_StillExcludesOwnedVersion()
    {
        // A version stored next to the file it belongs to is owned by its primary and is never
        // resolved on its own, so the scan must not see it as a child of the folder either.
        var result = _repository.GetItemList(new InternalItemsQuery
        {
            ParentId = _folderId,
            IncludeAlternateVersions = true
        });

        Assert.DoesNotContain(result, i => i.Id.Equals(_ownedVersionId));
    }

    private static BaseItemEntity CreateMovie(Guid id, string typeName, string name, string path, Guid? primaryVersionId, Guid? ownerId)
    {
        return new BaseItemEntity
        {
            Id = id,
            Type = typeName,
            Name = name,
            Path = path,
            ParentId = _folderId,
            TopParentId = _folderId,
            PresentationUniqueKey = (primaryVersionId ?? id).ToString("N"),
            PrimaryVersionId = primaryVersionId,
            OwnerId = ownerId,
            MediaType = "Video",
            IsMovie = true,
            IsFolder = false,
            IsVirtualItem = false
        };
    }
}