-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathShuffleBagAssetPoolTests.cs
More file actions
85 lines (68 loc) · 2.88 KB
/
Copy pathShuffleBagAssetPoolTests.cs
File metadata and controls
85 lines (68 loc) · 2.88 KB
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
using NUnit.Framework;
using Moq;
using ImmichFrame.Core.Api;
using ImmichFrame.Core.Logic.Pool;
namespace ImmichFrame.Core.Tests.Logic.Pool;
[TestFixture]
public class ShuffleBagAssetPoolTests
{
private static List<AssetResponseDto> Sample(int n) =>
Enumerable.Range(0, n)
.Select(i => new AssetResponseDto { Id = $"asset{i}", Type = AssetTypeEnum.IMAGE })
.ToList();
private static IAssetPool InnerReturning(IReadOnlyList<AssetResponseDto> assets)
{
var mock = new Mock<IAssetPool>();
mock.Setup(p => p.GetAssetCount(It.IsAny<CancellationToken>())).ReturnsAsync(assets.Count);
mock.Setup(p => p.GetAssets(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(() => assets);
return mock.Object;
}
[Test]
public async Task ShowsEachAssetOnceBeforeRepeating()
{
var assets = Sample(10);
var pool = new ShuffleBagAssetPool(InnerReturning(assets));
var cycle = (await pool.GetAssets(10)).ToList();
Assert.That(cycle, Has.Count.EqualTo(10));
Assert.That(cycle.Select(a => a.Id).Distinct().Count(), Is.EqualTo(10), "no repeats within a cycle");
Assert.That(cycle.Select(a => a.Id), Is.EquivalentTo(assets.Select(a => a.Id)), "every asset shown once");
}
[Test]
public async Task ReshufflesAndCoversFullSetEachCycle()
{
var assets = Sample(10);
var pool = new ShuffleBagAssetPool(InnerReturning(assets));
var first = (await pool.GetAssets(10)).Select(a => a.Id).ToList();
var second = (await pool.GetAssets(10)).Select(a => a.Id).ToList();
Assert.That(first, Is.EquivalentTo(assets.Select(a => a.Id)));
Assert.That(second, Is.EquivalentTo(assets.Select(a => a.Id)), "no starvation: full set served again next cycle");
}
[Test]
public async Task DedupesAssetsFromInnerPool()
{
var withDupes = new List<AssetResponseDto>
{
new() { Id = "a", Type = AssetTypeEnum.IMAGE },
new() { Id = "a", Type = AssetTypeEnum.IMAGE },
new() { Id = "b", Type = AssetTypeEnum.IMAGE },
};
var pool = new ShuffleBagAssetPool(InnerReturning(withDupes));
var cycle = (await pool.GetAssets(2)).Select(a => a.Id).ToList();
Assert.That(cycle, Has.Count.EqualTo(2));
Assert.That(cycle.Distinct().Count(), Is.EqualTo(cycle.Count), "no duplicate ids served within a cycle");
}
[Test]
public async Task EmptyPoolYieldsNothing()
{
var pool = new ShuffleBagAssetPool(InnerReturning(new List<AssetResponseDto>()));
var result = await pool.GetAssets(5);
Assert.That(result, Is.Empty);
}
[Test]
public async Task GetAssetCountDelegatesToInner()
{
var pool = new ShuffleBagAssetPool(InnerReturning(Sample(7)));
Assert.That(await pool.GetAssetCount(), Is.EqualTo(7));
}
}