mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-23 01:06:21 +01:00
152 lines
4.6 KiB
Dart
152 lines
4.6 KiB
Dart
import 'package:nc_photos/db/entity_converter.dart';
|
|
import 'package:nc_photos/di_container.dart';
|
|
import 'package:nc_photos/entity/file.dart';
|
|
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
|
import 'package:nc_photos/use_case/scan_dir_offline.dart';
|
|
import 'package:np_db_sqlite/np_db_sqlite_compat.dart' as compat;
|
|
import 'package:test/test.dart';
|
|
|
|
import '../test_util.dart' as util;
|
|
|
|
void main() {
|
|
group("ScanDirOffline", () {
|
|
group("single account", () {
|
|
test("root", _root);
|
|
test("subdir", _subDir);
|
|
test("unsupported file", _unsupportedFile);
|
|
});
|
|
group("multiple account", () {
|
|
test("root", _multiAccountRoot);
|
|
});
|
|
});
|
|
}
|
|
|
|
/// Scan root dir
|
|
///
|
|
/// Files: admin/test1.jpg, admin/test/test2.jpg
|
|
/// Scan: admin
|
|
/// Expect: all files
|
|
Future<void> _root() async {
|
|
final account = util.buildAccount();
|
|
final files = (util.FilesBuilder()
|
|
..addJpeg("admin/test1.jpg")
|
|
..addJpeg("admin/test/test2.jpg"))
|
|
.build();
|
|
final c = DiContainer(
|
|
npDb: util.buildTestDb(),
|
|
);
|
|
addTearDown(() => c.sqliteDb.close());
|
|
await c.sqliteDb.transaction(() async {
|
|
await c.sqliteDb.insertAccounts([account.toDb()]);
|
|
await util.insertFiles(c.sqliteDb, account, files);
|
|
});
|
|
|
|
// convert to set because ScanDirOffline does not guarantee order
|
|
expect(
|
|
(await ScanDirOffline(c)(
|
|
account, File(path: file_util.unstripPath(account, "."))))
|
|
.toSet(),
|
|
files.map(util.fileToFileDescriptor).toSet(),
|
|
);
|
|
}
|
|
|
|
/// Scan sub dir (admin/test)
|
|
///
|
|
/// Files: admin/test1.jpg, admin/test/test2.jpg
|
|
/// Scan: admin/test
|
|
/// Expect: admin/test/test2.jpg
|
|
Future<void> _subDir() async {
|
|
final account = util.buildAccount();
|
|
final files = (util.FilesBuilder()
|
|
..addJpeg("admin/test1.jpg")
|
|
..addJpeg("admin/test/test2.jpg"))
|
|
.build();
|
|
final c = DiContainer(
|
|
npDb: util.buildTestDb(),
|
|
);
|
|
addTearDown(() => c.sqliteDb.close());
|
|
await c.sqliteDb.transaction(() async {
|
|
await c.sqliteDb.insertAccounts([account.toDb()]);
|
|
await util.insertFiles(c.sqliteDb, account, files);
|
|
});
|
|
|
|
expect(
|
|
(await ScanDirOffline(c)(
|
|
account, File(path: file_util.unstripPath(account, "test"))))
|
|
.toSet(),
|
|
[files[1]].map(util.fileToFileDescriptor).toSet(),
|
|
);
|
|
}
|
|
|
|
/// Scan dir with unsupported file
|
|
///
|
|
/// Files: admin/test1.jpg, admin/test2.pdf
|
|
/// Scan: admin
|
|
/// Expect: admin/test1.jpg
|
|
Future<void> _unsupportedFile() async {
|
|
final account = util.buildAccount();
|
|
final files = (util.FilesBuilder()
|
|
..addJpeg("admin/test1.jpg")
|
|
..addGenericFile("admin/test2.pdf", "application/pdf"))
|
|
.build();
|
|
final c = DiContainer(
|
|
npDb: util.buildTestDb(),
|
|
);
|
|
addTearDown(() => c.sqliteDb.close());
|
|
await c.sqliteDb.transaction(() async {
|
|
await c.sqliteDb.insertAccounts([account.toDb()]);
|
|
await util.insertFiles(c.sqliteDb, account, files);
|
|
});
|
|
|
|
// convert to set because ScanDirOffline does not guarantee order
|
|
expect(
|
|
(await ScanDirOffline(c)(
|
|
account, File(path: file_util.unstripPath(account, "."))))
|
|
.toSet(),
|
|
[files[0]].map(util.fileToFileDescriptor).toSet(),
|
|
);
|
|
}
|
|
|
|
/// Scan root dir with multiple accounts
|
|
///
|
|
/// Files: admin/test1.jpg, admin/test/test2.jpg, user1/test1.jpg,
|
|
/// user1/test/test2.jpg
|
|
/// Scan: admin
|
|
/// Expect: admin/test1.jpg, admin/test/test2.jpg
|
|
/// Scan: user1
|
|
/// Expect: user1/test1.jpg, user1/test/test2.jpg
|
|
Future<void> _multiAccountRoot() async {
|
|
final account = util.buildAccount();
|
|
final user1Account = util.buildAccount(userId: "user1");
|
|
final files = (util.FilesBuilder()
|
|
..addJpeg("admin/test1.jpg")
|
|
..addJpeg("admin/test/test2.jpg"))
|
|
.build();
|
|
final user1Files = (util.FilesBuilder(initialFileId: files.length)
|
|
..addJpeg("user1/test1.jpg", ownerId: "user1")
|
|
..addJpeg("user1/test/test2.jpg", ownerId: "user1"))
|
|
.build();
|
|
final c = DiContainer(
|
|
npDb: util.buildTestDb(),
|
|
);
|
|
addTearDown(() => c.sqliteDb.close());
|
|
await c.sqliteDb.transaction(() async {
|
|
await c.sqliteDb.insertAccounts([account.toDb()]);
|
|
await util.insertFiles(c.sqliteDb, account, files);
|
|
await c.sqliteDb.insertAccounts([user1Account.toDb()]);
|
|
await util.insertFiles(c.sqliteDb, user1Account, user1Files);
|
|
});
|
|
|
|
expect(
|
|
(await ScanDirOffline(c)(
|
|
account, File(path: file_util.unstripPath(account, "."))))
|
|
.toSet(),
|
|
files.map(util.fileToFileDescriptor).toSet(),
|
|
);
|
|
expect(
|
|
(await ScanDirOffline(c)(
|
|
user1Account, File(path: file_util.unstripPath(user1Account, "."))))
|
|
.toSet(),
|
|
user1Files.map(util.fileToFileDescriptor).toSet(),
|
|
);
|
|
}
|