Commit 509f621

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-12-05 19:07:36
test ActiveExportColumnConfiguration
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 6be0d67
lib/model/export_import/export_configuration.dart
@@ -1,6 +1,7 @@
 import 'dart:convert';
 
 import 'package:blood_pressure_app/model/export_import/column.dart';
+import 'package:blood_pressure_app/model/storage/convert_util.dart';
 import 'package:blood_pressure_app/model/storage/export_columns_store.dart';
 import 'package:collection/collection.dart';
 import 'package:flutter/widgets.dart';
@@ -17,16 +18,16 @@ class ActiveExportColumnConfiguration extends ChangeNotifier {
       _userSelectedColumns = userSelectedColumnIds ?? [];
 
   factory ActiveExportColumnConfiguration.fromJson(String jsonString) {
-    final json = jsonDecode(jsonString);
-    final columns = (){
-      final columns = json['columns'];
-      if (columns is! List<String>) return <String>[];
-      return columns;
-    }();
-    return ActiveExportColumnConfiguration(
-      activePreset: ExportImportPreset.decode(json['preset']) ?? ExportImportPreset.bloodPressureApp,
-      userSelectedColumnIds: columns
-    );
+    try {
+      final json = jsonDecode(jsonString);
+      return ActiveExportColumnConfiguration(
+          activePreset: ExportImportPreset.decode(json['preset']) ?? ExportImportPreset.bloodPressureApp,
+          userSelectedColumnIds: ConvertUtil.parseList<String>(json['columns'])
+      );
+    } on FormatException {
+      return ActiveExportColumnConfiguration();
+    }
+
   }
 
   String toJson() => jsonEncode({
@@ -97,7 +98,7 @@ enum ExportImportPreset {
       1 => ExportImportPreset.bloodPressureApp,
       2 => ExportImportPreset.myHeart,
       _ => (){
-        assert(false);
+        assert(e is! int, 'non ints can happen through bad user values, other ints can happen as well, but should developers should be notified.');
         return null;
       }(),
     };
lib/model/storage/convert_util.dart
@@ -76,6 +76,7 @@ class ConvertUtil {
     );
   }
 
+  /// Example usage: `ConvertUtil.parseList<String>(json['columns'])`
   static List<T>? parseList<T>(dynamic value) {
     if (value is List<T>) return value;
     if (value is List<dynamic>) {
lib/model/storage/export_csv_settings_store.dart
@@ -32,7 +32,9 @@ class CsvExportSettings extends ChangeNotifier implements CustomFieldsSettings {
   factory CsvExportSettings.fromJson(String json) {
     try {
       return CsvExportSettings.fromMap(jsonDecode(json));
-    } catch (exception) {
+    } on FormatException {
+      return CsvExportSettings();
+    } on TypeError {
       return CsvExportSettings();
     }
   }
test/model/json_serialization_test.dart
@@ -174,6 +174,35 @@ void main() {
       expect(v3.exportAfterEveryEntry, ExportSettings().exportAfterEveryEntry);
     });
   });
+  group('ActiveExportColumnConfiguration', () {
+    test('should be able to recreate all values from json', () {
+      final initial = ActiveExportColumnConfiguration(
+        activePreset: ExportImportPreset.myHeart,
+        userSelectedColumnIds: ['a', 'b', 'c'],
+      );
+      final fromJson = ActiveExportColumnConfiguration.fromJson(initial.toJson());
+
+      expect(initial.activePreset, fromJson.activePreset);
+      expect(initial.toJson(), fromJson.toJson());
+    });
+    test('should not crash when parsing incorrect json', () {
+      ActiveExportColumnConfiguration.fromJson('banana');
+      ActiveExportColumnConfiguration.fromJson('{"preset" = false}');
+      ActiveExportColumnConfiguration.fromJson('{"preset": false');
+      ActiveExportColumnConfiguration.fromJson('{"preset": null');
+      ActiveExportColumnConfiguration.fromJson('{"columns": null');
+      ActiveExportColumnConfiguration.fromJson('{"columns": [123]');
+      ActiveExportColumnConfiguration.fromJson('{preset: false}');
+      ActiveExportColumnConfiguration.fromJson('green{preset: false}');
+    });
+    test('should not crash when parsing invalid values and ignore them', () {
+      final v1 = ActiveExportColumnConfiguration.fromJson('{"preset": ["test"]}');
+      final v2 = ActiveExportColumnConfiguration.fromJson('{"columns": [123, 456]}');
+
+      expect(v1.activePreset, ActiveExportColumnConfiguration().activePreset);
+      expect(v2.toJson(), ActiveExportColumnConfiguration().toJson());
+    });
+  });
 
   group('CsvExportSettings', (){
     test('should be able to recreate all values from json', () {
pubspec.lock
@@ -325,22 +325,6 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "1.3.0"
-  leak_tracker:
-    dependency: transitive
-    description:
-      name: leak_tracker
-      sha256: "04be76c4a4bb50f14904e64749237e541e7c7bcf7ec0b196907322ab5d2fc739"
-      url: "https://pub.dev"
-    source: hosted
-    version: "9.0.16"
-  leak_tracker_testing:
-    dependency: transitive
-    description:
-      name: leak_tracker_testing
-      sha256: b06739349ec2477e943055aea30172c5c7000225f79dad4702e2ec0eda79a6ff
-      url: "https://pub.dev"
-    source: hosted
-    version: "1.0.5"
   lints:
     dependency: transitive
     description:
@@ -377,18 +361,18 @@ packages:
     dependency: transitive
     description:
       name: material_color_utilities
-      sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
+      sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
       url: "https://pub.dev"
     source: hosted
-    version: "0.8.0"
+    version: "0.5.0"
   meta:
     dependency: transitive
     description:
       name: meta
-      sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
+      sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
       url: "https://pub.dev"
     source: hosted
-    version: "1.11.0"
+    version: "1.10.0"
   mockito:
     dependency: "direct dev"
     description:
@@ -778,14 +762,6 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.1.4"
-  vm_service:
-    dependency: transitive
-    description:
-      name: vm_service
-      sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
-      url: "https://pub.dev"
-    source: hosted
-    version: "13.0.0"
   watcher:
     dependency: transitive
     description:
@@ -798,10 +774,10 @@ packages:
     dependency: transitive
     description:
       name: web
-      sha256: edc8a9573dd8c5a83a183dae1af2b6fd4131377404706ca4e5420474784906fa
+      sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
       url: "https://pub.dev"
     source: hosted
-    version: "0.4.0"
+    version: "0.3.0"
   win32:
     dependency: transitive
     description: