Skip to content

Commit 611772b

Browse files
committed
fix tests
1 parent f36df70 commit 611772b

5 files changed

Lines changed: 29 additions & 25 deletions

File tree

ImmichFrame.Core.Tests/Logic/Pool/AlbumAssetsPoolTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ private class TestableAlbumAssetsPool(IApiCache apiCache, ImmichApi immichApi, I
2323
: AlbumAssetsPool(apiCache, immichApi, accountSettings)
2424
{
2525
// Expose LoadAssets for testing
26-
public Task<IEnumerable<AssetResponseDto>> TestLoadAssets(CancellationToken ct = default) => base.LoadAssets(ct);
26+
public async Task<IEnumerable<AssetResponseDto>> TestLoadAssets(CancellationToken ct = default) => await base.GetAssets(25, ct);
2727
}
2828

2929
[SetUp]
3030
public void Setup()
3131
{
3232
_mockApiCache = new Mock<IApiCache>();
33+
34+
_mockApiCache
35+
.Setup(m => m.GetOrAddAsync(
36+
It.IsAny<string>(),
37+
It.IsAny<Func<Task<IEnumerable<AssetResponseDto>>>>()))
38+
.Returns<string, Func<Task<IEnumerable<AssetResponseDto>>>>((_, factory) => factory());
39+
3340
_mockImmichApi = new Mock<ImmichApi>("", null);
3441
_mockAccountSettings = new Mock<IAccountSettings>();
3542
_albumAssetsPool = new TestableAlbumAssetsPool(_mockApiCache.Object, _mockImmichApi.Object, _mockAccountSettings.Object);
@@ -50,7 +57,7 @@ public async Task LoadAssets_ReturnsAssetsPresentIIncludedNotExcludedAlbums()
5057
var assetA = CreateAsset("A"); // In album1
5158
var assetB = CreateAsset("B"); // In album1 and excludedAlbum
5259
var assetC = CreateAsset("C"); // In excludedAlbum only
53-
var assetD = CreateAsset("D"); // In album1 only (but not B)
60+
var assetD = CreateAsset("D"); // In album1 only
5461

5562
_mockAccountSettings.SetupGet(s => s.Albums).Returns(new List<Guid> { album1Id });
5663
_mockAccountSettings.SetupGet(s => s.ExcludedAlbums).Returns(new List<Guid> { excludedAlbumId });

ImmichFrame.Core.Tests/Logic/Pool/AllAssetsPoolTests.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ public void Setup()
4343
.Returns<string, Func<Task<AssetStatsResponseDto>>>(async (key, factory) => await factory());
4444
}
4545

46-
private List<AssetResponseDto> CreateSampleAssets(int count, string idPrefix, AssetTypeEnum type)
46+
private List<AssetResponseDto> CreateSampleAssets(int count, string idPrefix, AssetTypeEnum type, int? rating = null)
4747
{
4848
return Enumerable.Range(0, count)
49-
.Select(i => new AssetResponseDto { Id = $"{idPrefix}{i}", Type = type })
49+
.Select(i => new AssetResponseDto { Id = $"{idPrefix}{i}", Type = type, ExifInfo = new ExifResponseDto { Rating = rating } })
5050
.ToList();
5151
}
5252

53-
private List<AssetResponseDto> CreateSampleImageAssets(int count, string idPrefix = "asset")
53+
private List<AssetResponseDto> CreateSampleImageAssets(int count, string idPrefix = "asset", int? rating = null)
5454
{
55-
return CreateSampleAssets(count, idPrefix, AssetTypeEnum.IMAGE);
55+
return CreateSampleAssets(count, idPrefix, AssetTypeEnum.IMAGE, rating);
5656
}
5757

58-
private List<AssetResponseDto> CreateSampleVideoAssets(int count, string idPrefix = "asset")
58+
private List<AssetResponseDto> CreateSampleVideoAssets(int count, string idPrefix = "asset", int? rating = null)
5959
{
60-
return CreateSampleAssets(count, idPrefix, AssetTypeEnum.VIDEO);
60+
return CreateSampleAssets(count, idPrefix, AssetTypeEnum.VIDEO, rating);
6161
}
6262

6363
[Test]
@@ -100,10 +100,11 @@ public async Task GetAssets_CallsSearchRandomAsync_WithCorrectParameters_OnlyIma
100100
// Arrange
101101
var requestedImageCount = 5;
102102
var requestedVideoCount = 8;
103+
var rating = 3;
103104
_mockAccountSettings.SetupGet(s => s.ShowArchived).Returns(true);
104105
_mockAccountSettings.SetupGet(s => s.Rating).Returns(3);
105-
var returnedAssets = CreateSampleImageAssets(requestedImageCount);
106-
returnedAssets.AddRange(CreateSampleVideoAssets(requestedVideoCount));
106+
var returnedAssets = CreateSampleImageAssets(requestedImageCount, rating: rating);
107+
returnedAssets.AddRange(CreateSampleVideoAssets(requestedVideoCount, rating: rating));
107108
_mockImmichApi.Setup(api => api.SearchRandomAsync(It.IsAny<RandomSearchDto>(), It.IsAny<CancellationToken>()))
108109
.ReturnsAsync(returnedAssets.Where(a => a.Type == AssetTypeEnum.IMAGE).ToList());
109110

@@ -119,7 +120,7 @@ public async Task GetAssets_CallsSearchRandomAsync_WithCorrectParameters_OnlyIma
119120
dto.WithExif == true &&
120121
dto.WithPeople == true &&
121122
dto.Visibility == AssetVisibility.Archive && // ShowArchived = true
122-
dto.Rating == 3
123+
dto.Rating == rating
123124
), It.IsAny<CancellationToken>()), Times.Once);
124125
}
125126

@@ -129,11 +130,12 @@ public async Task GetAssets_CallsSearchRandomAsync_WithCorrectParameters_ImagesA
129130
// Arrange
130131
var requestedImageCount = 5;
131132
var requestedVideoCount = 8;
133+
var rating = 3;
132134
_mockAccountSettings.SetupGet(s => s.ShowArchived).Returns(true);
133135
_mockAccountSettings.SetupGet(s => s.ShowVideos).Returns(true);
134136
_mockAccountSettings.SetupGet(s => s.Rating).Returns(3);
135-
var returnedAssets = CreateSampleImageAssets(requestedImageCount);
136-
returnedAssets.AddRange(CreateSampleVideoAssets(requestedVideoCount));
137+
var returnedAssets = CreateSampleImageAssets(requestedImageCount, rating: rating);
138+
returnedAssets.AddRange(CreateSampleVideoAssets(requestedVideoCount, rating: rating));
137139
_mockImmichApi.Setup(api => api.SearchRandomAsync(It.IsAny<RandomSearchDto>(), It.IsAny<CancellationToken>()))
138140
.ReturnsAsync(returnedAssets.ToList());
139141

@@ -149,7 +151,7 @@ public async Task GetAssets_CallsSearchRandomAsync_WithCorrectParameters_ImagesA
149151
dto.WithExif == true &&
150152
dto.WithPeople == true &&
151153
dto.Visibility == AssetVisibility.Archive && // ShowArchived = true
152-
dto.Rating == 3
154+
dto.Rating == rating
153155
), It.IsAny<CancellationToken>()), Times.Once);
154156
}
155157

ImmichFrame.Core.Tests/Logic/Pool/CachingApiAssetsPoolTests.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
using ImmichFrame.Core.Api;
44
using ImmichFrame.Core.Interfaces;
55
using ImmichFrame.Core.Logic.Pool;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
9-
using System.Threading.Tasks;
10-
using System.Threading;
116

127
namespace ImmichFrame.Core.Tests.Logic.Pool;
138

@@ -166,19 +161,19 @@ public async Task AllAssets_UsesCache_LoadAssetsCalledOnce()
166161
};
167162

168163
// Setup cache to really cache after the first call
169-
IEnumerable<AssetResponseDto> cachedValue = null;
164+
Dictionary<string, IEnumerable<AssetResponseDto>> cacheStore = new();
170165
_mockApiCache.Setup(c => c.GetOrAddAsync(
171166
It.IsAny<string>(),
172167
It.IsAny<Func<Task<IEnumerable<AssetResponseDto>>>>()
173168
))
174169
.Returns<string, Func<Task<IEnumerable<AssetResponseDto>>>>(async (key, factory) =>
175170
{
176-
if (cachedValue == null)
171+
if (!cacheStore.ContainsKey(key))
177172
{
178-
cachedValue = await factory();
173+
cacheStore[key] = await factory();
179174
}
180175

181-
return cachedValue;
176+
return cacheStore[key];
182177
});
183178

184179
// Act

ImmichFrame.Core/Logic/Pool/AllAssetsPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private async Task<IEnumerable<AssetResponseDto>> GetExcludedAlbumAssets(Cancell
7070
{
7171
var excludedAlbumAssets = new List<AssetResponseDto>();
7272

73-
foreach (var albumId in accountSettings.ExcludedAlbums)
73+
foreach (var albumId in accountSettings?.ExcludedAlbums ?? new())
7474
{
7575
var albumInfo = await immichApi.GetAlbumInfoAsync(albumId, null, null, ct);
7676

ImmichFrame.Core/Logic/Pool/CachingApiAssetsPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private async Task<IEnumerable<AssetResponseDto>> GetExcludedAlbumAssets(Cancell
2929
{
3030
var excludedAlbumAssets = new List<AssetResponseDto>();
3131

32-
foreach (var albumId in accountSettings.ExcludedAlbums)
32+
foreach (var albumId in accountSettings?.ExcludedAlbums ?? new())
3333
{
3434
var albumInfo = await immichApi.GetAlbumInfoAsync(albumId, null, null, ct);
3535

0 commit comments

Comments
 (0)