Commit 8d86806

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-10-04 11:10:14
add tests for ConfigDB
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 12479f4
Changed files (2)
lib
model
storage
test
lib/model/storage/db/config_db.dart
@@ -6,7 +6,7 @@ import 'package:sqflite/sqflite.dart';
 /// The config database has the following tables:
 ///
 /// settings
-/// profile_id: INTEGER PRIMARY KEY, settings_json: STRING
+/// profile_id INTEGER PRIMARY KEY, settings_json STRING
 ///
 /// exportStrings
 /// internalColumnName STRING PRIMARY KEY, columnTitle STRING, formatPattern STRING
@@ -21,11 +21,11 @@ class ConfigDB {
   /// be only one entry in the table.
   ///
   /// Format:
-  /// `CREATE TABLE settings(profile_id: INTEGER PRIMARY KEY, settings_json: STRING)`
+  /// `CREATE TABLE settings(profile_id INTEGER PRIMARY KEY, settings_json STRING)`
   static const String settingsTable = 'settings';
   // instead of just changing this string when changing the format, _onDBUpgrade should be used.
   static const String _settingsTableCreationString =
-      'CREATE TABLE settings(profile_id: INTEGER PRIMARY KEY, settings_json: STRING)';
+      'CREATE TABLE settings(profile_id INTEGER PRIMARY KEY, settings_json STRING)';
 
   /// Name of the export settings table.
   ///
@@ -33,10 +33,10 @@ class ConfigDB {
   /// [settingsTable]
   ///
   /// Format:
-  /// `CREATE TABLE exportSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)`
+  /// `CREATE TABLE exportSettings(profile_id INTEGER PRIMARY KEY, json STRING)`
   static const String exportSettingsTable = 'exportSettings';
   static const String _exportSettingsTableCreationString =
-      'CREATE TABLE exportSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)';
+      'CREATE TABLE exportSettings(profile_id INTEGER PRIMARY KEY, json STRING)';
 
   /// Name of the table for csv export settings.
   ///
@@ -44,10 +44,10 @@ class ConfigDB {
   /// [settingsTable]
   ///
   /// Format:
-  /// `CREATE TABLE exportCsvSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)`
+  /// `CREATE TABLE exportCsvSettings(profile_id INTEGER PRIMARY KEY, json STRING)`
   static const String exportCsvSettingsTable = 'exportCsvSettings';
   static const String _exportCsvSettingsTableCreationString =
-      'CREATE TABLE exportCsvSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)';
+      'CREATE TABLE exportCsvSettings(profile_id INTEGER PRIMARY KEY, json STRING)';
 
   /// Name of the table for pdf export settings.
   ///
@@ -55,10 +55,10 @@ class ConfigDB {
   /// [settingsTable]
   ///
   /// Format:
-  /// `CREATE TABLE exportPdfSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)`
+  /// `CREATE TABLE exportPdfSettings(profile_id INTEGER PRIMARY KEY, json STRING)`
   static const String exportPdfSettingsTable = 'exportPdfSettings';
   static const String _exportPdfSettingsTableCreationString =
-      'CREATE TABLE exportPdfSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)';
+      'CREATE TABLE exportPdfSettings(profile_id INTEGER PRIMARY KEY, json STRING)';
 
   /// Name of the table for storing time intervals to display.
   ///
@@ -69,11 +69,11 @@ class ConfigDB {
   /// Note that this table has 2 keys: profile_key to support multiple profiles and storage_id to support saving
   /// multiple entries per profile (e.g. export screen and main page).
   ///
-  /// Format: `CREATE TABLE selectedIntervallStorage(profile_id: INTEGER, storage_id: INTEGER, stepSize: INTEGER,`
-  /// ` start: INTEGER, end: INTEGER, PRIMARY KEY(profile_id, storage_id))`
+  /// Format: `CREATE TABLE selectedIntervallStorage(profile_id INTEGER, storage_id INTEGER, stepSize INTEGER,`
+  /// ` start INTEGER, end INTEGER, PRIMARY KEY(profile_id, storage_id))`
   static const String selectedIntervallStorageTable = 'selectedIntervallStorage';
-  static const String _selectedIntervallStorageCreationString = 'CREATE TABLE selectedIntervallStorage(profile_id: '
-      'INTEGER, storage_id: INTEGER, stepSize: INTEGER, start: INTEGER, end: INTEGER, '
+  static const String _selectedIntervallStorageCreationString = 'CREATE TABLE selectedIntervallStorage(profile_id '
+      'INTEGER, storage_id INTEGER, stepSize INTEGER, start INTEGER, end INTEGER, '
       'PRIMARY KEY(profile_id, storage_id))';
 
   /// Name of the exportStrings table. It is used to store formats used in the [ExportConfigurationModel].
@@ -102,25 +102,25 @@ class ConfigDB {
     );
   }
 
-  FutureOr<void> _onDBCreate(Database db, int version) {
-    db.execute(_exportStringsTableCreationString);
-    db.execute(_settingsTableCreationString);
-    db.execute(_exportSettingsTableCreationString);
-    db.execute(_exportCsvSettingsTableCreationString);
-    db.execute(_exportPdfSettingsTableCreationString);
-    db.execute(_selectedIntervallStorageCreationString);
+  FutureOr<void> _onDBCreate(Database db, int version) async {
+    await db.execute(_exportStringsTableCreationString);
+    await db.execute(_settingsTableCreationString);
+    await db.execute(_exportSettingsTableCreationString);
+    await db.execute(_exportCsvSettingsTableCreationString);
+    await db.execute(_exportPdfSettingsTableCreationString);
+    await db.execute(_selectedIntervallStorageCreationString);
   }
 
   FutureOr<void> _onDBUpgrade(Database db, int oldVersion, int newVersion) async {
     // When adding more versions the upgrade procedure proposed in https://stackoverflow.com/a/75153875/21489239
     // might be useful, to avoid duplicated code. Currently this would only lead to complexity, without benefits.
     if (oldVersion == 1 && newVersion == 2) {
-      db.execute(_settingsTableCreationString);
-      db.execute(_exportSettingsTableCreationString);
-      db.execute(_exportCsvSettingsTableCreationString);
-      db.execute(_exportPdfSettingsTableCreationString);
-      db.execute(_selectedIntervallStorageCreationString);
-      db.database.setVersion(2);
+      await db.execute(_settingsTableCreationString);
+      await db.execute(_exportSettingsTableCreationString);
+      await db.execute(_exportCsvSettingsTableCreationString);
+      await db.execute(_exportPdfSettingsTableCreationString);
+      await db.execute(_selectedIntervallStorageCreationString);
+      await db.database.setVersion(2);
     } else {
       assert(false, 'Unexpected version upgrade from $oldVersion to $newVersion.');
     }
test/model/config_db_test.dart
@@ -0,0 +1,37 @@
+import 'package:blood_pressure_app/model/storage/db/config_db.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:sqflite_common_ffi/sqflite_ffi.dart';
+
+void main() {
+  group('ConfigDB', () {
+    setUpAll(() {
+      sqfliteFfiInit();
+      databaseFactory = databaseFactoryFfi;
+    });
+
+    test('should initialize database without error', () async {
+      final db = await ConfigDB.open(dbPath: inMemoryDatabasePath, isFullPath: true);
+      expect(db.database.isOpen, true);
+    });
+    test('tables should exist', () async {
+      final db = await ConfigDB.open(dbPath: inMemoryDatabasePath, isFullPath: true);
+      final existingTables = await db.database.query('sqlite_master');
+      final tableNames = existingTables.map((e) => e['name'] as String);
+
+      expect(tableNames.contains(ConfigDB.settingsTable), true);
+      expect(tableNames.contains(ConfigDB.exportSettingsTable), true);
+      expect(tableNames.contains(ConfigDB.exportCsvSettingsTable), true);
+      expect(tableNames.contains(ConfigDB.exportPdfSettingsTable), true);
+      expect(tableNames.contains(ConfigDB.selectedIntervallStorageTable), true);
+      expect(tableNames.contains(ConfigDB.exportStringsTable), true);
+    });
+    test('should save and load table entries', () async {
+      final db = await ConfigDB.open(dbPath: inMemoryDatabasePath, isFullPath: true);
+      await db.database.insert(ConfigDB.settingsTable, {'profile_id': 0, 'settings_json': '{"test": 123}'});
+      final queriedData = await db.database.query(ConfigDB.settingsTable);
+      expect(queriedData.length, 1);
+      expect(queriedData[0]['profile_id'], 0);
+      expect(queriedData[0]['settings_json'], '{"test": 123}');
+    });
+  });
+}
\ No newline at end of file