test(mobile): album_sort_options_provider unit tests

This commit is contained in:
shalong-tanwen 2023-12-06 14:44:53 +05:30
parent 1867020746
commit e797998a85
6 changed files with 299 additions and 4 deletions

View file

@ -102,9 +102,10 @@ class AlbumSortFunction extends _$AlbumSortFunction {
final sortOpt = ref
.watch(appSettingsServiceProvider)
.getSetting(AppSettingsEnum.selectedAlbumSortOrder);
return sortOpt < AlbumSortMode.values.length && sortOpt >= 0
? AlbumSortMode.values.firstWhere((e) => e.index == sortOpt)
: AlbumSortMode.title;
return AlbumSortMode.values.firstWhere(
(e) => e.index == sortOpt,
orElse: () => AlbumSortMode.title,
);
}
void changeSortMode(AlbumSortMode sortOption) {

View file

@ -6,7 +6,7 @@ part of 'album_sort_options.provider.dart';
// RiverpodGenerator
// **************************************************************************
String _$albumSortFunctionHash() => r'19a199c30181c19bb8f834f3105b27e6b0fb92f4';
String _$albumSortFunctionHash() => r'2758b7d6274a1adc51e554e99cd6725bd875140f';
/// See also [AlbumSortFunction].
@ProviderFor(AlbumSortFunction)

View file

@ -0,0 +1,54 @@
import 'package:immich_mobile/shared/models/album.dart';
import 'asset.stub.dart';
import 'user.stub.dart';
final class AlbumStub {
const AlbumStub._();
static final emptyAlbum = Album(
name: "empty-album",
localId: "empty-album-local",
remoteId: "empty-album-remote",
createdAt: DateTime(2000),
modifiedAt: DateTime(2023),
shared: false,
activityEnabled: false,
startDate: DateTime(2020),
);
static final sharedWithUser = Album(
name: "empty-album-shared-with-user",
localId: "empty-album-shared-with-user-local",
remoteId: "empty-album-shared-with-user-remote",
createdAt: DateTime(2023),
modifiedAt: DateTime(2023),
shared: true,
activityEnabled: false,
endDate: DateTime(2020),
)..sharedUsers.addAll([UserStub.admin]);
static final oneAsset = Album(
name: "album-with-single-asset",
localId: "album-with-single-asset-local",
remoteId: "album-with-single-asset-remote",
createdAt: DateTime(2022),
modifiedAt: DateTime(2023),
shared: false,
activityEnabled: false,
startDate: DateTime(2020),
endDate: DateTime(2023),
)..assets.addAll([AssetStub.image1]);
static final twoAsset = Album(
name: "album-with-two-assets",
localId: "album-with-two-assets-local",
remoteId: "album-with-two-assets-remote",
createdAt: DateTime(2001),
modifiedAt: DateTime(2010),
shared: false,
activityEnabled: false,
startDate: DateTime(2019),
endDate: DateTime(2020),
)..assets.addAll([AssetStub.image1, AssetStub.image2]);
}

View file

@ -0,0 +1,182 @@
import 'package:collection/collection.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:immich_mobile/modules/album/providers/album_sort_options.provider.dart';
import 'package:immich_mobile/shared/models/album.dart';
import 'package:immich_mobile/shared/models/asset.dart';
import 'package:immich_mobile/shared/models/user.dart';
import 'package:isar/isar.dart';
import 'album.stub.dart';
import 'asset.stub.dart';
void main() {
late final Isar db;
setUpAll(() async {
await Isar.initializeIsarCore(download: true);
db = await Isar.open(
[
AssetSchema,
AlbumSchema,
UserSchema,
],
maxSizeMiB: 256,
directory: ".",
);
});
final albums = [
AlbumStub.emptyAlbum,
AlbumStub.sharedWithUser,
AlbumStub.oneAsset,
AlbumStub.twoAsset,
];
setUp(() {
db.writeTxnSync(() {
db.clearSync();
// Save all assets
db.assets.putAllSync([AssetStub.image1, AssetStub.image2]);
db.albums.putAllSync(albums);
for (final album in albums) {
album.sharedUsers.saveSync();
album.assets.saveSync();
}
});
expect(db.albums.countSync(), 4);
expect(db.assets.countSync(), 2);
});
group("Album sort - Created Time", () {
const created = AlbumSortMode.created;
test("Created time - ASC", () {
final sorted = created.sortFn(albums, false);
expect(sorted.isSortedBy((a) => a.createdAt), true);
});
test("Created time - DESC", () {
final sorted = created.sortFn(albums, true);
expect(
sorted.isSorted((b, a) => a.createdAt.compareTo(b.createdAt)),
true,
);
});
});
group("Album sort - Asset count", () {
const assetCount = AlbumSortMode.assetCount;
test("Asset Count - ASC", () {
final sorted = assetCount.sortFn(albums, false);
expect(
sorted.isSorted((a, b) => a.assetCount.compareTo(b.assetCount)),
true,
);
});
test("Asset Count - DESC", () {
final sorted = assetCount.sortFn(albums, true);
expect(
sorted.isSorted((b, a) => a.assetCount.compareTo(b.assetCount)),
true,
);
});
});
group("Album sort - Last modified", () {
const lastModified = AlbumSortMode.lastModified;
test("Last modified - ASC", () {
final sorted = lastModified.sortFn(albums, false);
expect(
sorted.isSorted((a, b) => a.modifiedAt.compareTo(b.modifiedAt)),
true,
);
});
test("Last modified - DESC", () {
final sorted = lastModified.sortFn(albums, true);
expect(
sorted.isSorted((b, a) => a.modifiedAt.compareTo(b.modifiedAt)),
true,
);
});
});
group("Album sort - Created", () {
const created = AlbumSortMode.created;
test("Created - ASC", () {
final sorted = created.sortFn(albums, false);
expect(
sorted.isSorted((a, b) => a.createdAt.compareTo(b.createdAt)),
true,
);
});
test("Created - DESC", () {
final sorted = created.sortFn(albums, true);
expect(
sorted.isSorted((b, a) => a.createdAt.compareTo(b.createdAt)),
true,
);
});
});
group("Album sort - Most Recent", () {
const mostRecent = AlbumSortMode.mostRecent;
test("Most Recent - ASC", () {
final sorted = mostRecent.sortFn(albums, false);
expect(
sorted,
[
AlbumStub.sharedWithUser,
AlbumStub.twoAsset,
AlbumStub.oneAsset,
AlbumStub.emptyAlbum,
],
);
});
test("Most Recent - DESC", () {
final sorted = mostRecent.sortFn(albums, true);
expect(
sorted,
[
AlbumStub.emptyAlbum,
AlbumStub.oneAsset,
AlbumStub.twoAsset,
AlbumStub.sharedWithUser,
],
);
});
});
group("Album sort - Most Oldest", () {
const mostOldest = AlbumSortMode.mostOldest;
test("Most Oldest - ASC", () {
final sorted = mostOldest.sortFn(albums, false);
expect(
sorted,
[
AlbumStub.twoAsset,
AlbumStub.emptyAlbum,
AlbumStub.oneAsset,
AlbumStub.sharedWithUser,
],
);
});
test("Most Oldest - DESC", () {
final sorted = mostOldest.sortFn(albums, true);
expect(
sorted,
[
AlbumStub.sharedWithUser,
AlbumStub.oneAsset,
AlbumStub.emptyAlbum,
AlbumStub.twoAsset,
],
);
});
});
}

View file

@ -0,0 +1,37 @@
import 'package:immich_mobile/shared/models/asset.dart';
final class AssetStub {
const AssetStub._();
static final image1 = Asset(
checksum: "image1-checksum",
localId: "image1",
ownerId: 1,
fileCreatedAt: DateTime.now(),
fileModifiedAt: DateTime.now(),
updatedAt: DateTime.now(),
durationInSeconds: 0,
type: AssetType.image,
fileName: "image1.jpg",
isFavorite: true,
isArchived: false,
isTrashed: false,
stackCount: 0,
);
static final image2 = Asset(
checksum: "image2-checksum",
localId: "image2",
ownerId: 1,
fileCreatedAt: DateTime(2000),
fileModifiedAt: DateTime(2010),
updatedAt: DateTime.now(),
durationInSeconds: 60,
type: AssetType.video,
fileName: "image2.jpg",
isFavorite: false,
isArchived: false,
isTrashed: false,
stackCount: 0,
);
}

View file

@ -0,0 +1,21 @@
import 'package:immich_mobile/shared/models/user.dart';
final class UserStub {
const UserStub._();
static final admin = User(
id: "admin",
updatedAt: DateTime(2021),
email: "admin@test.com",
name: "admin",
isAdmin: true,
);
static final user1 = User(
id: "user1",
updatedAt: DateTime(2022),
email: "user1@test.com",
name: "user1",
isAdmin: false,
);
}