From 88be157f50dfdc33b2aef43e98565c0c6b91ee00 Mon Sep 17 00:00:00 2001 From: derdilla Date: Sun, 20 Aug 2023 10:11:57 +0200 Subject: [PATCH 1/4] fix settings test --- lib/model/settings_store.dart | 9 +++++---- test/model/settings_test.dart | 10 ++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/model/settings_store.dart b/lib/model/settings_store.dart index 74fe300e..399b42cc 100644 --- a/lib/model/settings_store.dart +++ b/lib/model/settings_store.dart @@ -8,14 +8,15 @@ import 'package:shared_preferences/shared_preferences.dart'; class Settings extends ChangeNotifier { late final SharedPreferences _prefs; + final PackageInfo _packageInfo; DateTime? _displayDataStart; DateTime? _displayDataEnd; - Settings._create(); + Settings._create(this._packageInfo); // factory method, to allow for async constructor - static Future create() async { - final component = Settings._create(); + static Future create([PackageInfo? packageInfo]) async { + final component = Settings._create(packageInfo ?? (await PackageInfo.fromPlatform())); component._prefs = await SharedPreferences.getInstance(); await component._update(); return component; @@ -61,7 +62,7 @@ class Settings extends ChangeNotifier { for (var e in toAwait) { await e; } - await _prefs.setInt('lastAppVersion', int.parse((await PackageInfo.fromPlatform()).buildNumber)); + await _prefs.setInt('lastAppVersion', int.parse(_packageInfo.buildNumber)); return; } diff --git a/test/model/settings_test.dart b/test/model/settings_test.dart index 4773d649..d45d9b4d 100644 --- a/test/model/settings_test.dart +++ b/test/model/settings_test.dart @@ -4,12 +4,14 @@ import 'package:blood_pressure_app/model/ram_only_implementations.dart'; import 'package:blood_pressure_app/model/settings_store.dart'; import 'package:file_saver/file_saver.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:package_info_plus/package_info_plus.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); SharedPreferences.setMockInitialValues({}); + final testPackageInfo = PackageInfo(appName: 'blood_pressure_app', packageName: 'com.derdilla.bloodPressureApp', version: 'UnitTests', buildNumber: '999999'); group('Settings model', () { // setup db path @@ -17,12 +19,12 @@ void main() { test('should initialize', () async { expect(() async { - await Settings.create(); + await Settings.create(testPackageInfo); }, returnsNormally); }); test('fields defaults should be set after initialization', () async { - var s = await Settings.create(); + var s = await Settings.create(testPackageInfo); expect(s.graphStepSize, TimeStep.day); expect(s.followSystemDarkMode, true); expect(s.darkMode, true); @@ -52,7 +54,7 @@ void main() { }); test('setting fields should save changes', () async { - var s = await Settings.create(); + var s = await Settings.create(testPackageInfo); int i = 0; s.addListener(() { @@ -123,7 +125,7 @@ void main() { }); test('setting fields should notify listeners and change values', () async { - var s = await Settings.create(); + var s = await Settings.create(testPackageInfo); int i = 0; s.addListener(() { From 93d3b4c77a076360ddfc7bae740ffd330ef24e8f Mon Sep 17 00:00:00 2001 From: derdilla Date: Mon, 21 Aug 2023 18:57:22 +0200 Subject: [PATCH 2/4] fix BloodPressureRecord --- lib/model/blood_pressure.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/model/blood_pressure.dart b/lib/model/blood_pressure.dart index d9f94a8b..4ce3baf8 100644 --- a/lib/model/blood_pressure.dart +++ b/lib/model/blood_pressure.dart @@ -115,8 +115,8 @@ class BloodPressureRecord { DateTime get creationTime => _creationTime; /// datetime needs to be after epoch set creationTime(DateTime value) { - if (creationTime.millisecondsSinceEpoch > 0) { - _creationTime = creationTime; + if (value.millisecondsSinceEpoch > 0) { + _creationTime = value; } else { assert(false, "Tried to create BloodPressureRecord at or before epoch"); _creationTime = DateTime.fromMillisecondsSinceEpoch(1); From 2c147dba961ea874acd37a09b0423d5cd815a271 Mon Sep 17 00:00:00 2001 From: derdilla Date: Mon, 21 Aug 2023 19:06:55 +0200 Subject: [PATCH 3/4] add BloodPressureRecord tests --- test/model/bood_pressure_test.dart | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/model/bood_pressure_test.dart b/test/model/bood_pressure_test.dart index a25f8717..46aabb66 100644 --- a/test/model/bood_pressure_test.dart +++ b/test/model/bood_pressure_test.dart @@ -6,16 +6,19 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart'; void main() { group('BloodPressureRecord', () { test('should initialize with all values supported by dart', () { - BloodPressureRecord record = BloodPressureRecord(DateTime.fromMicrosecondsSinceEpoch(0), 0, -50, 1000, + BloodPressureRecord record = BloodPressureRecord(DateTime.fromMicrosecondsSinceEpoch(1582991592), 0, -50, 1000, "((V⍳V)=⍳⍴V)/V←,V ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈๏ แผ่นดินฮั่นเABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ–—‘“”„†•…‰™œŠŸž€ ΑΒΓΔΩαβγδω АБВГДабвг, \n \t д∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა"); - expect(record.creationTime, DateTime.fromMicrosecondsSinceEpoch(0)); + expect(record.creationTime, DateTime.fromMicrosecondsSinceEpoch(1582991592)); expect(record.systolic, 0); expect(record.diastolic, -50); expect(record.pulse, 1000); expect(record.notes, "((V⍳V)=⍳⍴V)/V←,V ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈๏ แผ่นดินฮั่นเABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ–—‘“”„†•…‰™œŠŸž€ ΑΒΓΔΩαβγδω АБВГДабвг, \n \t д∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა"); }); + test('should not save times at or before epoch', () { + expect(() => BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(0), 0, 0, 0, ""), throwsAssertionError); + }); }); group('BloodPressureModel', () { From a9178e416b60903fbdbd4ac7b1a240e0adece851 Mon Sep 17 00:00:00 2001 From: derdilla Date: Tue, 22 Aug 2023 19:03:05 +0200 Subject: [PATCH 4/4] fix remaining test --- test/model/analyzer_test.dart | 6 ++---- test/ui/statistics_test.dart | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/test/model/analyzer_test.dart b/test/model/analyzer_test.dart index de2cefc1..8c596688 100644 --- a/test/model/analyzer_test.dart +++ b/test/model/analyzer_test.dart @@ -53,15 +53,13 @@ void main() { test('should determine special days', () async { var m = BloodPressureAnalyser([BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(100), 0, 0, 0, ''), - BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(-2200), 0, 0, 0, ''), + BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(20), 0, 0, 0, ''), BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(9000000), 0, 0, 0, ''), BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(3124159), 0, 0, 0, ''), ]); - expect((m.firstDay), DateTime.fromMillisecondsSinceEpoch(-2200)); + expect((m.firstDay), DateTime.fromMillisecondsSinceEpoch(20)); expect((m.lastDay), DateTime.fromMillisecondsSinceEpoch(9000000)); }); - - // TODO null tests, test with 1 element }); } \ No newline at end of file diff --git a/test/ui/statistics_test.dart b/test/ui/statistics_test.dart index 58158f09..73c86c46 100644 --- a/test/ui/statistics_test.dart +++ b/test/ui/statistics_test.dart @@ -16,7 +16,7 @@ void main() { testWidgets("should report measurement count", (widgetTester) async { await _initStatsPage(widgetTester, _allMeasurements(), [ for (int i = 1; i<51; i++) // can't safe entries at or before epoch - BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(i), 40+i, 60+i, 30+i, 'Test comment $i'), + BloodPressureRecord(DateTime.fromMillisecondsSinceEpoch(1582991592 + i), 40+i, 60+i, 30+i, 'Test comment $i'), ]); final measurementCountWidget = find.byKey(const Key('measurementCount')); expect(measurementCountWidget, findsOneWidget);