Commit 61349a5

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-10-04 11:56:18
add tests for ConfigDao
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 8d86806
Changed files (2)
lib
model
storage
test
lib/model/storage/db/config_dao.dart
@@ -7,6 +7,10 @@ import 'package:blood_pressure_app/model/storage/intervall_store.dart';
 import 'package:blood_pressure_app/model/storage/settings_store.dart';
 import 'package:sqflite/sqflite.dart';
 
+/// Class for loading data from the database.
+///
+/// The user of this class needs to pay attention not to dispose all old instances of objects in order to ensure there
+/// are no concurrent writes to the database. Having multiple instances will cause data loss.
 class ConfigDao {
   ConfigDao(this._configDB);
   
test/model/config_db_test.dart
@@ -1,4 +1,11 @@
+import 'package:blood_pressure_app/model/storage/db/config_dao.dart';
 import 'package:blood_pressure_app/model/storage/db/config_db.dart';
+import 'package:blood_pressure_app/model/storage/export_csv_settings_store.dart';
+import 'package:blood_pressure_app/model/storage/export_pdf_settings_store.dart';
+import 'package:blood_pressure_app/model/storage/export_settings_store.dart';
+import 'package:blood_pressure_app/model/storage/intervall_store.dart';
+import 'package:blood_pressure_app/model/storage/settings_store.dart';
+import 'package:flutter/material.dart';
 import 'package:flutter_test/flutter_test.dart';
 import 'package:sqflite_common_ffi/sqflite_ffi.dart';
 
@@ -34,4 +41,42 @@ void main() {
       expect(queriedData[0]['settings_json'], '{"test": 123}');
     });
   });
+
+  group('ConfigDAO', () {
+    setUpAll(() {
+      sqfliteFfiInit();
+      databaseFactory = databaseFactoryFfi;
+    });
+
+    test('should initialize', () async {
+      final rawDB = await ConfigDB.open(dbPath: inMemoryDatabasePath, isFullPath: true);
+      final dao = ConfigDao(rawDB);
+    });
+    test('should create classes when no data is present', () async {
+      final rawDB = await ConfigDB.open(dbPath: inMemoryDatabasePath, isFullPath: true);
+      final dao = ConfigDao(rawDB);
+      
+      expect(await dao.loadExportColumns(), []);
+      expect((await dao.loadSettings(0)).toJson(), Settings().toJson());
+      expect((await dao.loadExportSettings(0)).toJson(), ExportSettings().toJson());
+      expect((await dao.loadCsvExportSettings(0)).toJson(), CsvExportSettings().toJson());
+      expect((await dao.loadPdfExportSettings(0)).toJson(), PdfExportSettings().toJson());
+      expect((await dao.loadIntervallStorage(0,0)).stepSize, IntervallStorage().stepSize);
+    });
+    test('should save changes', () async {
+      final rawDB = await ConfigDB.open(dbPath: inMemoryDatabasePath, isFullPath: true);
+      final dao = ConfigDao(rawDB);
+
+      final settings = await dao.loadSettings(0);
+      settings.dateFormatString = 'Test string';
+      settings.sysColor = Colors.deepOrange;
+      settings.animationSpeed = 69;
+
+      final initialSettingsJson = settings.toJson();
+      settings.dispose();
+
+      final newSettings = await dao.loadSettings(0);
+      expect(newSettings.toJson(), initialSettingsJson);
+    });
+  });
 }
\ No newline at end of file