nc-photos/test/test_util.dart

286 lines
7.6 KiB
Dart
Raw Normal View History

2021-11-11 18:04:17 +01:00
import 'package:flutter/foundation.dart';
2021-12-02 07:04:11 +01:00
import 'package:idb_shim/idb.dart';
2021-11-11 18:04:17 +01:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-12-02 07:04:11 +01:00
import 'package:nc_photos/app_db.dart';
2021-11-12 22:13:02 +01:00
import 'package:nc_photos/ci_string.dart';
2021-11-23 08:39:35 +01:00
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/cover_provider.dart';
import 'package:nc_photos/entity/album/item.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/album/sort_provider.dart';
2021-11-11 18:04:17 +01:00
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/share.dart';
import 'package:nc_photos/entity/sharee.dart';
2021-11-23 08:39:35 +01:00
import 'package:nc_photos/iterable_extension.dart';
2021-12-02 07:04:11 +01:00
import 'mock_type.dart';
2021-11-23 08:39:35 +01:00
class FilesBuilder {
FilesBuilder({
int initialFileId = 0,
}) : fileId = initialFileId;
List<File> build() {
return files.map((f) => f.copyWith()).toList();
}
void addJpeg(
String relativePath, {
int contentLength = 1024,
DateTime? lastModified,
bool hasPreview = true,
String ownerId = "admin",
}) {
files.add(buildJpegFile(
path: "remote.php/dav/files/$relativePath",
contentLength: contentLength,
lastModified:
lastModified ?? DateTime.utc(2020, 1, 2, 3, 4, 5 + files.length),
hasPreview: hasPreview,
fileId: fileId++,
ownerId: ownerId,
));
}
final files = <File>[];
int fileId;
}
/// Create an album for testing
class AlbumBuilder {
AlbumBuilder({
DateTime? lastUpdated,
2021-11-27 10:22:51 +01:00
String? name,
this.albumFilename = "test0.nc_album.json",
2021-11-23 08:39:35 +01:00
this.fileId = 0,
2021-11-27 10:22:51 +01:00
String? ownerId,
}) : lastUpdated = lastUpdated ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
name = name ?? "test",
ownerId = ownerId ?? "admin";
factory AlbumBuilder.ofId({
required int albumId,
DateTime? lastUpdated,
String? name,
String? ownerId,
}) =>
AlbumBuilder(
lastUpdated: lastUpdated,
name: name,
albumFilename: "test$albumId.nc_album.json",
fileId: albumId,
ownerId: ownerId,
);
2021-11-23 08:39:35 +01:00
Album build() {
final latestFileItem = items
.whereType<AlbumFileItem>()
.stableSorted(
(a, b) => a.file.lastModified!.compareTo(b.file.lastModified!))
.reversed
.firstOrNull;
return Album(
lastUpdated: lastUpdated,
name: name,
provider: AlbumStaticProvider(
items: items,
latestItemTime: latestFileItem?.file.lastModified,
),
coverProvider: cover == null
? AlbumAutoCoverProvider(coverFile: latestFileItem?.file)
: AlbumManualCoverProvider(coverFile: cover!),
sortProvider: const AlbumNullSortProvider(),
shares: shares.isEmpty ? null : shares,
albumFile: buildAlbumFile(
path: buildAlbumFilePath(albumFilename, user: ownerId),
fileId: fileId,
ownerId: ownerId,
),
);
}
/// Add a file item
///
/// By default, the item will be added by admin and added at the same time as
/// the file's lastModified.
///
/// If [isCover] is true, the coverProvider of the album will become
/// [AlbumManualCoverProvider]
void addFileItem(
File file, {
String addedBy = "admin",
DateTime? addedAt,
bool isCover = false,
}) {
final fileItem = AlbumFileItem(
file: file,
addedBy: addedBy.toCi(),
addedAt: addedAt ?? file.lastModified!,
);
items.add(fileItem);
if (isCover) {
cover = file;
}
}
/// Add an album share
2021-11-24 09:36:16 +01:00
///
/// By default, the album will be shared at 2020-01-02 03:04:05
void addShare(
String userId, {
DateTime? sharedAt,
}) {
shares.add(buildAlbumShare(
userId: userId,
sharedAt: sharedAt,
));
2021-11-23 08:39:35 +01:00
}
2021-12-02 06:00:18 +01:00
static List<AlbumFileItem> fileItemsOf(Album album) =>
2021-11-23 08:39:35 +01:00
AlbumStaticProvider.of(album).items.whereType<AlbumFileItem>().toList();
final DateTime lastUpdated;
final String name;
final String albumFilename;
final int fileId;
final String ownerId;
final items = <AlbumItem>[];
File? cover;
final shares = <AlbumShare>[];
}
2021-11-11 18:04:17 +01:00
void initLog() {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) {
2021-12-02 06:00:18 +01:00
String msg =
"[${record.loggerName}] ${record.level.name}: ${record.message}";
if (record.error != null) {
msg += " (throw: ${record.error.runtimeType} { ${record.error} })";
}
if (record.stackTrace != null) {
msg += "\nStack Trace:\n${record.stackTrace}";
}
debugPrint(msg);
2021-11-11 18:04:17 +01:00
});
}
Account buildAccount({
2021-12-05 13:02:22 +01:00
String id = "123456-000000",
2021-11-11 18:04:17 +01:00
String scheme = "http",
String address = "example.com",
String username = "admin",
String password = "pass",
List<String> roots = const [""],
}) =>
2021-12-05 13:02:22 +01:00
Account(id, scheme, address, username.toCi(), password, roots);
2021-11-11 18:04:17 +01:00
/// Build a mock [File] pointing to a album JSON file
///
/// Warning: not all fields are filled, but the most essential ones are
File buildAlbumFile({
required String path,
int contentLength = 1024,
DateTime? lastModified,
required int fileId,
String ownerId = "admin",
}) =>
File(
path: path,
contentLength: contentLength,
contentType: "application/json",
lastModified: lastModified ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
isCollection: false,
hasPreview: false,
fileId: fileId,
2021-11-12 22:13:02 +01:00
ownerId: ownerId.toCi(),
2021-11-11 18:04:17 +01:00
);
String buildAlbumFilePath(
String filename, {
String user = "admin",
}) =>
"remote.php/dav/files/$user/.com.nkming.nc_photos/albums/$filename";
2021-11-23 08:39:35 +01:00
AlbumShare buildAlbumShare({
required String userId,
String? displayName,
2021-11-24 09:36:16 +01:00
DateTime? sharedAt,
2021-11-23 08:39:35 +01:00
}) =>
AlbumShare(
userId: userId.toCi(),
displayName: displayName ?? userId,
2021-11-24 09:36:16 +01:00
sharedAt: sharedAt ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
2021-11-23 08:39:35 +01:00
);
2021-11-11 18:04:17 +01:00
/// Build a mock [File] pointing to a JPEG image file
///
/// Warning: not all fields are filled, but the most essential ones are
File buildJpegFile({
required String path,
int contentLength = 1024,
DateTime? lastModified,
bool hasPreview = true,
required int fileId,
String ownerId = "admin",
}) =>
File(
path: path,
contentLength: contentLength,
contentType: "image/jpeg",
lastModified: lastModified ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
isCollection: false,
hasPreview: hasPreview,
fileId: fileId,
2021-11-12 22:13:02 +01:00
ownerId: ownerId.toCi(),
2021-11-11 18:04:17 +01:00
);
Share buildShare({
required String id,
DateTime? stime,
String uidOwner = "admin",
String? displaynameOwner,
required File file,
required String shareWith,
}) =>
Share(
id: id,
shareType: ShareType.user,
stime: stime ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
2021-11-12 22:13:02 +01:00
uidOwner: uidOwner.toCi(),
2021-11-11 18:04:17 +01:00
displaynameOwner: displaynameOwner ?? uidOwner,
path: file.strippedPath,
itemType: ShareItemType.file,
mimeType: file.contentType ?? "",
itemSource: file.fileId!,
2021-11-12 22:13:02 +01:00
shareWith: shareWith.toCi(),
2021-11-11 18:04:17 +01:00
shareWithDisplayName: shareWith,
);
Sharee buildSharee({
ShareeType type = ShareeType.user,
2021-11-23 08:39:35 +01:00
String? label,
2021-11-11 18:04:17 +01:00
int shareType = 0,
2021-11-12 22:13:02 +01:00
required CiString shareWith,
2021-11-11 18:04:17 +01:00
String? shareWithDisplayNameUnique,
}) =>
Sharee(
type: type,
2021-11-23 08:39:35 +01:00
label: label ?? shareWith.toString(),
2021-11-11 18:04:17 +01:00
shareType: shareType,
shareWith: shareWith,
);
2021-12-02 07:04:11 +01:00
Future<void> fillAppDb(
MockAppDb appDb, Account account, Iterable<File> files) async {
await appDb.use((db) async {
final transaction = db.transaction(AppDb.fileDbStoreName, idbModeReadWrite);
final store = transaction.objectStore(AppDb.fileDbStoreName);
for (final f in files) {
await store.put(AppDbFileDbEntry.fromFile(account, f).toJson(),
AppDbFileDbEntry.toPrimaryKey(account, f));
}
});
}