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
|
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using Emby.Server.Implementations.Data;
using Jellyfin.Data.Enums;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Server.Implementations.Item;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Moq;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Item;
public sealed class PeopleUpdateQueryTests : SqliteDbTestFixture
{
private readonly CommandRecorder _recorder;
private readonly Guid _itemId = Guid.NewGuid();
private readonly PeopleRepository _people;
public PeopleUpdateQueryTests()
: this(new CommandRecorder())
{
}
private PeopleUpdateQueryTests(CommandRecorder recorder)
: base(recorder)
{
_recorder = recorder;
using var context = CreateDbContext();
context.BaseItems.Add(new BaseItemEntity
{
Id = _itemId,
Name = "Movie",
Type = new ItemTypeLookup().BaseItemKindNames[BaseItemKind.Movie]
});
context.SaveChanges();
_people = new PeopleRepository(CreateDbContextFactory(), new ItemTypeLookup(), Mock.Of<IItemQueryHelpers>());
}
[Theory]
[InlineData("Hero")]
[InlineData("HERO")]
public void UnchangedCredits_DoNotWriteOrLookUpAllPeople(string role)
{
_people.UpdatePeople(_itemId, [new PersonInfo { Name = "Actor", Type = PersonKind.Actor, Role = "Hero" }]);
_recorder.Commands.Clear();
_people.UpdatePeople(_itemId, [new PersonInfo { Name = "actor", Type = PersonKind.Actor, Role = role }]);
Assert.Single(_recorder.Commands);
Assert.StartsWith("SELECT", _recorder.Commands[0].Sql, StringComparison.Ordinal);
using var context = CreateDbContext();
Assert.Equal("Hero", Assert.Single(context.PeopleBaseItemMap).Role);
}
[Fact]
public void SortOrderChange_IsPersisted()
{
_people.UpdatePeople(_itemId, [new PersonInfo { Name = "Actor", Type = PersonKind.Actor, SortOrder = 1 }]);
_people.UpdatePeople(_itemId, [new PersonInfo { Name = "Actor", Type = PersonKind.Actor, SortOrder = 2 }]);
using var context = CreateDbContext();
Assert.Equal(2, Assert.Single(context.PeopleBaseItemMap).SortOrder);
}
[Fact]
public void UpdatePeople_GeneratedSqlUsesPeopleNameIndex()
{
ApplyMigration(new Jellyfin.Server.Implementations.Migrations.AddPeopleNameLowerIndex());
_recorder.Commands.Clear();
_people.UpdatePeople(_itemId, [
new PersonInfo { Name = "Actor A", Type = PersonKind.Actor },
new PersonInfo { Name = "Actor B", Type = PersonKind.Actor }
]);
var query = Assert.Single(_recorder.Commands, c => c.Sql.Contains("lower(\"p\".\"Name\")", StringComparison.Ordinal));
Assert.Contains(Explain(query), line => line.Contains("SEARCH p USING INDEX IX_Peoples_NameLower", StringComparison.Ordinal));
}
private void ApplyMigration(Migration migration)
{
using var context = CreateDbContext();
foreach (var operation in migration.UpOperations.Cast<SqlOperation>())
{
context.Database.ExecuteSqlRaw(operation.Sql);
}
}
private string[] Explain(RecordedCommand query)
{
using var context = CreateDbContext();
using var command = context.Database.GetDbConnection().CreateCommand();
#pragma warning disable CA2100 // query.Sql is generated by EF Core; query values remain bound parameters.
command.CommandText = "EXPLAIN QUERY PLAN " + query.Sql;
#pragma warning restore CA2100
foreach (var value in query.Parameters)
{
var parameter = command.CreateParameter();
parameter.ParameterName = value.Name;
parameter.Value = value.Value;
command.Parameters.Add(parameter);
}
using var reader = command.ExecuteReader();
var plan = new List<string>();
while (reader.Read())
{
plan.Add(reader.GetString(3));
}
return plan.ToArray();
}
private sealed record RecordedCommand(string Sql, (string Name, object? Value)[] Parameters);
private sealed class CommandRecorder : DbCommandInterceptor
{
public List<RecordedCommand> Commands { get; } = [];
public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
{
Record(command);
return result;
}
public override InterceptionResult<int> NonQueryExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<int> result)
{
Record(command);
return result;
}
private void Record(DbCommand command) => Commands.Add(new RecordedCommand(
command.CommandText,
command.Parameters.Cast<DbParameter>().Select(p => (p.ParameterName, p.Value)).ToArray()));
}
}
|