Commit f07512e

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-06-25 10:32:57
fix and test analyzer grouping
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 6fb8338
Changed files (2)
app/lib/model/blood_pressure_analyzer.dart
@@ -95,7 +95,7 @@ class BloodPressureAnalyser {
   /// and creates an [BloodPressureAnalyser] for each. The analyzers are
   /// returned ordered by the hour of the day and the index can be used as the
   /// hour.
-  List<BloodPressureAnalyser> groupAnalysers() { // TODO: test
+  List<BloodPressureAnalyser> groupAnalysers() {
     // Group records around the full hour so that there are 24 sublists from 0
     // to 23. ([0] -> 23:30-00:29.59; [1] -> ...).
     final Map<int, List<BloodPressureRecord>> grouped = _records.groupListsBy((BloodPressureRecord record) {
@@ -104,6 +104,9 @@ class BloodPressureAnalyser {
       hour %= 24; // midnight jumps
       return hour;
     });
+    for (int i = 0; i <= 23; i++) {
+      grouped[i] ??= [];
+    }
     final groupedAnalyzers = grouped.map((hour, subList) => MapEntry(
       hour,
       BloodPressureAnalyser(subList),
app/test/model/analyzer_test.dart
@@ -4,7 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
 import 'package:health_data_store/health_data_store.dart';
 
 void main() {
-  test('should return averages', () async {
+  test('should return averages', () {
     final m = BloodPressureAnalyser([
       mockRecordPos(DateTime.fromMillisecondsSinceEpoch(1), 122, 87, 65),
       mockRecordPos(DateTime.fromMillisecondsSinceEpoch(2), 100, 60, 62),
@@ -16,7 +16,7 @@ void main() {
     expect(m.avgPul, 66);
   });
 
-  test('should return max', () async {
+  test('should return max', () {
     final a = BloodPressureAnalyser([
       mockRecordPos(DateTime.fromMillisecondsSinceEpoch(1), 123, 87, 65),
       mockRecordPos(DateTime.fromMillisecondsSinceEpoch(2), 100, 60, 62),
@@ -29,7 +29,7 @@ void main() {
     expect(a.maxPul, 73);
   });
 
-  test('should return min', () async {
+  test('should return min', () {
     final a = BloodPressureAnalyser([
       mockRecordPos(DateTime.fromMillisecondsSinceEpoch(1), 123, 87, 65),
       mockRecordPos(DateTime.fromMillisecondsSinceEpoch(2), 100, 60, 62),
@@ -42,7 +42,7 @@ void main() {
     expect(a.minPul, 62);
   });
 
-  test('should know count', () async {
+  test('should know count', () {
     final m = BloodPressureAnalyser([
       for (int i = 1; i < 101; i++)
         mockRecordPos(DateTime.fromMillisecondsSinceEpoch(i), 0, 0, 0),
@@ -50,7 +50,7 @@ void main() {
     expect(m.count, 100);
   });
 
-  test('should determine special days', () async {
+  test('should determine special days', () {
     final m = BloodPressureAnalyser([mockRecordPos(DateTime.fromMillisecondsSinceEpoch(100), 0, 0, 0),
       mockRecordPos(DateTime.fromMillisecondsSinceEpoch(20), 0, 0, 0),
       mockRecordPos(DateTime.fromMillisecondsSinceEpoch(9000000), 0, 0, 0),
@@ -60,6 +60,38 @@ void main() {
     expect((m.firstDay), DateTime.fromMillisecondsSinceEpoch(20));
     expect((m.lastDay), DateTime.fromMillisecondsSinceEpoch(9000000));
   });
+  
+  test('groups analyzers for all hours', () {
+    final analyzer = BloodPressureAnalyser([]);
+    final groups = analyzer.groupAnalysers();
+    expect(groups, hasLength(24));
+    expect(groups, everyElement(isA<BloodPressureAnalyser>().having((a) => a.count, 'count', 0)));
+  });
+
+  test('creates groups correctly', () {
+    final analyzer = BloodPressureAnalyser([
+      mockRecordPos(DateTime(2000, 1, 1, 5), 123),
+      mockRecordPos(DateTime(2000, 1, 1, 5), 123),
+      mockRecordPos(DateTime(2000, 1, 1, 5), 123),
+      mockRecordPos(DateTime(2000, 1, 1, 8), 123),
+      mockRecordPos(DateTime(2000, 1, 1, 12), 123),
+      mockRecordPos(DateTime(2000, 1, 1, 12), 123),
+      mockRecordPos(DateTime(2000, 1, 1, 19), 123),
+      mockRecordPos(DateTime(2000, 1, 1, 19), 0, 122),
+      mockRecord(time: DateTime(2000, 1, 1, 19), pul: 12),
+      mockRecordPos(DateTime(2000, 1, 1, 23, 40), 123),
+    ]);
+    final groups = analyzer.groupAnalysers();
+    expect(groups[5].count, 3);
+    expect(groups[8].count, 1);
+    expect(groups[12].count, 2);
+    expect(groups[19].count, 3);
+    expect(groups[0].count, 1);
+
+    for (final i in [1,2,3,4,6,7,9,10,11,3,14,15,16,17,18,20,21,22,23]) {
+      expect(groups[i].count, 0);
+    }
+  });
 }
 
 BloodPressureRecord mockRecordPos([