From aa496245171f83bc5ee6205dc60c6473578d3147 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sat, 29 Apr 2023 12:07:46 +0800 Subject: [PATCH] Fix compatibility with very old album --- app/lib/entity/album/cover_provider.dart | 2 +- app/lib/entity/album/upgrader.dart | 28 +- app/test/entity/album_test.dart | 538 +-------------- .../entity/album_test/album_upgrader_v8.dart | 624 ++++++++++++++++++ 4 files changed, 649 insertions(+), 543 deletions(-) create mode 100644 app/test/entity/album_test/album_upgrader_v8.dart diff --git a/app/lib/entity/album/cover_provider.dart b/app/lib/entity/album/cover_provider.dart index 1703ac65..d280be3c 100644 --- a/app/lib/entity/album/cover_provider.dart +++ b/app/lib/entity/album/cover_provider.dart @@ -91,7 +91,7 @@ class AlbumAutoCoverProvider extends AlbumCoverProvider { @override FileDescriptor? getCover(Album album) { - if (coverFile == null) { + if (coverFile == null && album.provider is AlbumStaticProvider) { // use the latest file as cover return getCoverByItems(AlbumStaticProvider.of(album).items); } else { diff --git a/app/lib/entity/album/upgrader.dart b/app/lib/entity/album/upgrader.dart index da0d46ae..5b03aabe 100644 --- a/app/lib/entity/album/upgrader.dart +++ b/app/lib/entity/album/upgrader.dart @@ -281,16 +281,20 @@ class AlbumUpgraderV8 implements AlbumUpgrader { .cast(); final fd = _fileJsonToFileDescriptorJson(content); // some very old album file may contain files w/o id - if (fd["fdId"] == null) { + if (fd["fdId"] != null) { result["coverProvider"]["content"]["coverFile"] = fd; + } else { + result["coverProvider"]["content"] = {}; } } else if (result["coverProvider"]["type"] == "auto") { final content = (result["coverProvider"]["content"]["coverFile"] as Map?) ?.cast(); if (content != null) { final fd = _fileJsonToFileDescriptorJson(content); - if (fd["fdId"] == null) { + if (fd["fdId"] != null) { result["coverProvider"]["content"]["coverFile"] = fd; + } else { + result["coverProvider"]["content"] = {}; } } } @@ -305,18 +309,26 @@ class AlbumUpgraderV8 implements AlbumUpgrader { .cast(); final converted = _fileJsonToFileDescriptorJson( (content["coverFile"] as Map).cast()); - return dbObj.copyWith( - coverProviderContent: jsonEncode({"coverFile": converted}), - ); + if (converted["fdId"] != null) { + return dbObj.copyWith( + coverProviderContent: jsonEncode({"coverFile": converted}), + ); + } else { + return dbObj.copyWith(coverProviderContent: "{}"); + } } else if (dbObj.coverProviderType == "auto") { final content = (jsonDecode(dbObj.coverProviderContent) as Map) .cast(); if (content["coverFile"] != null) { final converted = _fileJsonToFileDescriptorJson( (content["coverFile"] as Map).cast()); - return dbObj.copyWith( - coverProviderContent: jsonEncode({"coverFile": converted}), - ); + if (converted["fdId"] != null) { + return dbObj.copyWith( + coverProviderContent: jsonEncode({"coverFile": converted}), + ); + } else { + return dbObj.copyWith(coverProviderContent: "{}"); + } } } return dbObj; diff --git a/app/test/entity/album_test.dart b/app/test/entity/album_test.dart index 50996184..1bf5b0e8 100644 --- a/app/test/entity/album_test.dart +++ b/app/test/entity/album_test.dart @@ -17,6 +17,8 @@ import 'package:test/test.dart'; import '../test_util.dart' as util; +part 'album_test/album_upgrader_v8.dart'; + void main() { group("Album", () { group("fromJson", () { @@ -1810,6 +1812,7 @@ void main() { group("auto cover", () { test("null", _upgradeV8JsonAutoNull); test("last modified", _upgradeV8JsonAutoLastModified); + test("no file id", _upgradeV8JsonAutoNoFileId); }); }); @@ -1824,6 +1827,7 @@ void main() { group("auto cover", () { test("null", _upgradeV8DbAutoNull); test("last modified", _upgradeV8DbAutoLastModified); + test("no file id", _upgradeV8DbAutoNoFileId); }); }); }); @@ -1952,540 +1956,6 @@ void _toAppDbJsonShares() { }); } -void _upgradeV8JsonNonManualCover() { - final json = { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "memory", - "content": { - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "2020-01-02T03:04:05.678901Z", - }, - }, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }; - expect(const AlbumUpgraderV8().doJson(json), { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "memory", - "content": { - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "2020-01-02T03:04:05.678901Z", - }, - }, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }); -} - -void _upgradeV8JsonManualNow() { - withClock(Clock.fixed(DateTime.utc(2020, 1, 2, 3, 4, 5)), () { - final json = { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "manual", - "content": { - "coverFile": { - "path": "remote.php/dav/files/admin/test1.jpg", - "fileId": 1, - }, - }, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }; - expect(const AlbumUpgraderV8().doJson(json), { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "manual", - "content": { - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "2020-01-02T03:04:05.000Z", - }, - }, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }); - }); -} - -void _upgradeV8JsonManualExifTime() { - final json = { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "manual", - "content": { - "coverFile": { - "path": "remote.php/dav/files/admin/test1.jpg", - "fileId": 1, - "metadata": { - "exif": { - "DateTimeOriginal": "2020:01:02 03:04:05", - }, - }, - }, - }, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }; - expect(const AlbumUpgraderV8().doJson(json), { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "manual", - "content": { - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - // dart does not provide a way to mock timezone - "fdDateTime": DateTime(2020, 1, 2, 3, 4, 5).toUtc().toIso8601String(), - }, - }, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }); -} - -void _upgradeV8JsonAutoNull() { - final json = { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "auto", - "content": {}, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }; - expect(const AlbumUpgraderV8().doJson(json), { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "auto", - "content": {}, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }); -} - -void _upgradeV8JsonAutoLastModified() { - final json = { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "auto", - "content": { - "coverFile": { - "path": "remote.php/dav/files/admin/test1.jpg", - "fileId": 1, - "lastModified": "2020-01-02T03:04:05.000Z", - }, - }, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }; - expect(const AlbumUpgraderV8().doJson(json), { - "version": 8, - "lastUpdated": "2020-01-02T03:04:05.678901Z", - "provider": { - "type": "static", - "content": { - "items": [], - }, - }, - "coverProvider": { - "type": "auto", - "content": { - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "2020-01-02T03:04:05.000Z", - }, - }, - }, - "sortProvider": { - "type": "null", - "content": {}, - }, - "albumFile": { - "path": "remote.php/dav/files/admin/test1.json", - }, - }); -} - -void _upgradeV8DbNonManualCover() { - final dbObj = sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "memory", - coverProviderContent: _stripJsonString("""{ - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "2020-01-02T03:04:05.678901Z" - } - }"""), - sortProviderType: "null", - sortProviderContent: "{}", - ); - expect( - const AlbumUpgraderV8().doDb(dbObj), - sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "memory", - coverProviderContent: _stripJsonString("""{ - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "2020-01-02T03:04:05.678901Z" - } - }"""), - sortProviderType: "null", - sortProviderContent: "{}", - ), - ); -} - -void _upgradeV8DbManualNow() { - withClock(Clock.fixed(DateTime.utc(2020, 1, 2, 3, 4, 5)), () { - final dbObj = sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "manual", - coverProviderContent: _stripJsonString("""{ - "coverFile": { - "path": "remote.php/dav/files/admin/test1.jpg", - "fileId": 1 - } - }"""), - sortProviderType: "null", - sortProviderContent: "{}", - ); - expect( - const AlbumUpgraderV8().doDb(dbObj), - sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "manual", - coverProviderContent: _stripJsonString("""{ - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "2020-01-02T03:04:05.000Z" - } - }"""), - sortProviderType: "null", - sortProviderContent: "{}", - ), - ); - }); -} - -void _upgradeV8DbManualExifTime() { - final dbObj = sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "manual", - coverProviderContent: _stripJsonString("""{ - "coverFile": { - "path": "remote.php/dav/files/admin/test1.jpg", - "fileId": 1, - "metadata": { - "exif": { - "DateTimeOriginal": "2020:01:02 03:04:05" - } - } - } - }"""), - sortProviderType: "null", - sortProviderContent: "{}", - ); - // dart does not provide a way to mock timezone - final dateTime = DateTime(2020, 1, 2, 3, 4, 5).toUtc().toIso8601String(); - expect( - const AlbumUpgraderV8().doDb(dbObj), - sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "manual", - coverProviderContent: _stripJsonString("""{ - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "$dateTime" - } - }"""), - sortProviderType: "null", - sortProviderContent: "{}", - ), - ); -} - -void _upgradeV8DbAutoNull() { - final dbObj = sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "auto", - coverProviderContent: "{}", - sortProviderType: "null", - sortProviderContent: "{}", - ); - expect( - const AlbumUpgraderV8().doDb(dbObj), - sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "auto", - coverProviderContent: "{}", - sortProviderType: "null", - sortProviderContent: "{}", - ), - ); -} - -void _upgradeV8DbAutoLastModified() { - final dbObj = sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "auto", - coverProviderContent: _stripJsonString("""{ - "coverFile": { - "path": "remote.php/dav/files/admin/test1.jpg", - "fileId": 1, - "lastModified": "2020-01-02T03:04:05.000Z" - } - }"""), - sortProviderType: "null", - sortProviderContent: "{}", - ); - expect( - const AlbumUpgraderV8().doDb(dbObj), - sql.Album( - rowId: 1, - file: 1, - fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", - version: 8, - lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), - name: "test1", - providerType: "static", - providerContent: """{"items": []}""", - coverProviderType: "auto", - coverProviderContent: _stripJsonString("""{ - "coverFile": { - "fdPath": "remote.php/dav/files/admin/test1.jpg", - "fdId": 1, - "fdMime": null, - "fdIsArchived": false, - "fdIsFavorite": false, - "fdDateTime": "2020-01-02T03:04:05.000Z" - } - }"""), - sortProviderType: "null", - sortProviderContent: "{}", - ), - ); -} - String _stripJsonString(String str) { return jsonEncode(jsonDecode(str)); } diff --git a/app/test/entity/album_test/album_upgrader_v8.dart b/app/test/entity/album_test/album_upgrader_v8.dart new file mode 100644 index 00000000..6318d5f4 --- /dev/null +++ b/app/test/entity/album_test/album_upgrader_v8.dart @@ -0,0 +1,624 @@ +part of '../album_test.dart'; + +void _upgradeV8JsonNonManualCover() { + final json = { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "memory", + "content": { + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "2020-01-02T03:04:05.678901Z", + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }; + expect(const AlbumUpgraderV8().doJson(json), { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "memory", + "content": { + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "2020-01-02T03:04:05.678901Z", + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }); +} + +void _upgradeV8JsonManualNow() { + withClock(Clock.fixed(DateTime.utc(2020, 1, 2, 3, 4, 5)), () { + final json = { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "manual", + "content": { + "coverFile": { + "path": "remote.php/dav/files/admin/test1.jpg", + "fileId": 1, + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }; + expect(const AlbumUpgraderV8().doJson(json), { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "manual", + "content": { + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "2020-01-02T03:04:05.000Z", + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }); + }); +} + +void _upgradeV8JsonManualExifTime() { + final json = { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "manual", + "content": { + "coverFile": { + "path": "remote.php/dav/files/admin/test1.jpg", + "fileId": 1, + "metadata": { + "exif": { + "DateTimeOriginal": "2020:01:02 03:04:05", + }, + }, + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }; + expect(const AlbumUpgraderV8().doJson(json), { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "manual", + "content": { + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + // dart does not provide a way to mock timezone + "fdDateTime": DateTime(2020, 1, 2, 3, 4, 5).toUtc().toIso8601String(), + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }); +} + +void _upgradeV8JsonAutoNull() { + final json = { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "auto", + "content": {}, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }; + expect(const AlbumUpgraderV8().doJson(json), { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "auto", + "content": {}, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }); +} + +void _upgradeV8JsonAutoLastModified() { + final json = { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "auto", + "content": { + "coverFile": { + "path": "remote.php/dav/files/admin/test1.jpg", + "fileId": 1, + "lastModified": "2020-01-02T03:04:05.000Z", + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }; + expect(const AlbumUpgraderV8().doJson(json), { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "auto", + "content": { + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "2020-01-02T03:04:05.000Z", + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }); +} + +void _upgradeV8JsonAutoNoFileId() { + final json = { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "auto", + "content": { + "coverFile": { + "path": "remote.php/dav/files/admin/test1.jpg", + "lastModified": "2020-01-02T03:04:05.000Z", + }, + }, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }; + expect(const AlbumUpgraderV8().doJson(json), { + "version": 8, + "lastUpdated": "2020-01-02T03:04:05.678901Z", + "provider": { + "type": "static", + "content": { + "items": [], + }, + }, + "coverProvider": { + "type": "auto", + "content": {}, + }, + "sortProvider": { + "type": "null", + "content": {}, + }, + "albumFile": { + "path": "remote.php/dav/files/admin/test1.json", + }, + }); +} + +void _upgradeV8DbNonManualCover() { + final dbObj = sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "memory", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "2020-01-02T03:04:05.678901Z" + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ); + expect( + const AlbumUpgraderV8().doDb(dbObj), + sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "memory", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "2020-01-02T03:04:05.678901Z" + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ), + ); +} + +void _upgradeV8DbManualNow() { + withClock(Clock.fixed(DateTime.utc(2020, 1, 2, 3, 4, 5)), () { + final dbObj = sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "manual", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "path": "remote.php/dav/files/admin/test1.jpg", + "fileId": 1 + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ); + expect( + const AlbumUpgraderV8().doDb(dbObj), + sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "manual", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "2020-01-02T03:04:05.000Z" + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ), + ); + }); +} + +void _upgradeV8DbManualExifTime() { + final dbObj = sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "manual", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "path": "remote.php/dav/files/admin/test1.jpg", + "fileId": 1, + "metadata": { + "exif": { + "DateTimeOriginal": "2020:01:02 03:04:05" + } + } + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ); + // dart does not provide a way to mock timezone + final dateTime = DateTime(2020, 1, 2, 3, 4, 5).toUtc().toIso8601String(); + expect( + const AlbumUpgraderV8().doDb(dbObj), + sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "manual", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "$dateTime" + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ), + ); +} + +void _upgradeV8DbAutoNull() { + final dbObj = sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "auto", + coverProviderContent: "{}", + sortProviderType: "null", + sortProviderContent: "{}", + ); + expect( + const AlbumUpgraderV8().doDb(dbObj), + sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "auto", + coverProviderContent: "{}", + sortProviderType: "null", + sortProviderContent: "{}", + ), + ); +} + +void _upgradeV8DbAutoLastModified() { + final dbObj = sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "auto", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "path": "remote.php/dav/files/admin/test1.jpg", + "fileId": 1, + "lastModified": "2020-01-02T03:04:05.000Z" + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ); + expect( + const AlbumUpgraderV8().doDb(dbObj), + sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "auto", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "fdPath": "remote.php/dav/files/admin/test1.jpg", + "fdId": 1, + "fdMime": null, + "fdIsArchived": false, + "fdIsFavorite": false, + "fdDateTime": "2020-01-02T03:04:05.000Z" + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ), + ); +} + +void _upgradeV8DbAutoNoFileId() { + final dbObj = sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "auto", + coverProviderContent: _stripJsonString("""{ + "coverFile": { + "path": "remote.php/dav/files/admin/test1.jpg", + "lastModified": "2020-01-02T03:04:05.000Z" + } + }"""), + sortProviderType: "null", + sortProviderContent: "{}", + ); + expect( + const AlbumUpgraderV8().doDb(dbObj), + sql.Album( + rowId: 1, + file: 1, + fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", + version: 8, + lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5), + name: "test1", + providerType: "static", + providerContent: """{"items": []}""", + coverProviderType: "auto", + coverProviderContent: "{}", + sortProviderType: "null", + sortProviderContent: "{}", + ), + ); +}