From 19b89cb4e7119ecbfc53ccf35155354d11fd3534 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Mon, 17 Jan 2022 16:35:17 +0800 Subject: [PATCH] Update tests --- test/mock_type.dart | 2 +- test/use_case/db_compat/v5_test.dart | 89 ++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 test/use_case/db_compat/v5_test.dart diff --git a/test/mock_type.dart b/test/mock_type.dart index d8acedd7..87b7f179 100644 --- a/test/mock_type.dart +++ b/test/mock_type.dart @@ -100,7 +100,7 @@ class MockAppDb implements AppDb { } @override - Future use(FutureOr Function(Database) fn) async { + Future use(FutureOr Function(Database db) fn) async { final db = await _dbFactory.open( "test.db", version: 1, diff --git a/test/use_case/db_compat/v5_test.dart b/test/use_case/db_compat/v5_test.dart new file mode 100644 index 00000000..5dbb9b33 --- /dev/null +++ b/test/use_case/db_compat/v5_test.dart @@ -0,0 +1,89 @@ +import 'package:idb_shim/idb_client.dart'; +import 'package:nc_photos/account.dart'; +import 'package:nc_photos/app_db.dart'; +import 'package:nc_photos/entity/file.dart'; +import 'package:nc_photos/use_case/db_compat/v5.dart'; +import 'package:test/test.dart'; + +import '../../mock_type.dart'; +import '../../test_util.dart' as util; + +void main() { + group("DbCompatV5", () { + group("isNeedMigration", () { + test("w/ meta entry == false", () async { + final appDb = MockAppDb(); + await appDb.use((db) async { + final transaction = + db.transaction(AppDb.metaStoreName, idbModeReadWrite); + final metaStore = transaction.objectStore(AppDb.metaStoreName); + const entry = AppDbMetaEntryDbCompatV5(false); + await metaStore.put(entry.toEntry().toJson()); + }); + + expect(await DbCompatV5.isNeedMigration(appDb), true); + }); + + test("w/ meta entry == true", () async { + final appDb = MockAppDb(); + await appDb.use((db) async { + final transaction = + db.transaction(AppDb.metaStoreName, idbModeReadWrite); + final metaStore = transaction.objectStore(AppDb.metaStoreName); + const entry = AppDbMetaEntryDbCompatV5(true); + await metaStore.put(entry.toEntry().toJson()); + }); + + expect(await DbCompatV5.isNeedMigration(appDb), false); + }); + + test("w/o meta entry", () async { + final appDb = MockAppDb(); + await appDb.use((db) async { + final transaction = + db.transaction(AppDb.metaStoreName, idbModeReadWrite); + final metaStore = transaction.objectStore(AppDb.metaStoreName); + const entry = AppDbMetaEntryDbCompatV5(true); + await metaStore.put(entry.toEntry().toJson()); + }); + + expect(await DbCompatV5.isNeedMigration(appDb), false); + }); + }); + + test("migrate", () async { + final account = util.buildAccount(); + final files = (util.FilesBuilder() + ..addJpeg( + "admin/test1.jpg", + lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5), + )) + .build(); + final appDb = MockAppDb(); + await appDb.use((db) async { + final transaction = + db.transaction(AppDb.file2StoreName, idbModeReadWrite); + final fileStore = transaction.objectStore(AppDb.file2StoreName); + await fileStore.put({ + "server": account.url, + "userId": account.username.toCaseInsensitiveString(), + "strippedPath": files[0].strippedPathWithEmpty, + "file": files[0].toJson(), + }, "${account.url}/${account.username.toCaseInsensitiveString()}/${files[0].fileId}"); + }); + await DbCompatV5.migrate(appDb); + + final objs = + await util.listAppDb(appDb, AppDb.file2StoreName, (item) => item); + expect(objs, [ + { + "server": account.url, + "userId": account.username.toCaseInsensitiveString(), + "strippedPath": files[0].strippedPathWithEmpty, + "dateTimeEpochMs": 1577934245000, + "file": files[0].toJson(), + } + ]); + }); + }); +}