Commit c5e48d0
Changed files (2)
health_data_store
lib
src
repositories
test
src
repositories
health_data_store/lib/src/repositories/blood_pressure_repository.dart
@@ -79,8 +79,41 @@ class BloodPressureRepository extends Repository<BloodPressureRecord> {
@override
Future<void> remove(BloodPressureRecord value) async {
- // TODO: implement remove
- throw UnimplementedError();
+ await _db.transaction((txn) async {
+ String query = 'SELECT t.entryID FROM Timestamps AS t ';
+ if (value.sys != null)
+ query += 'LEFT JOIN Systolic AS s ON t.entryID = s.entryID ';
+ if (value.dia != null)
+ query += 'LEFT JOIN Diastolic AS d ON t.entryID = d.entryID ';
+ if (value.pul != null)
+ query += 'LEFT JOIN Pulse AS p ON t.entryID = p.entryID ';
+ query += 'WHERE timestampUnixS = ? ';
+ if (value.sys != null)
+ query += 'AND sys = ? ';
+ if (value.dia != null)
+ query += 'AND dia = ? ';
+ if (value.pul != null)
+ query += 'AND pul = ? ';
+
+ final entryResult = await txn.rawQuery(query, [
+ value.time.millisecondsSinceEpoch ~/ 1000,
+ if (value.sys != null)
+ value.sys,
+ if (value.dia != null)
+ value.dia,
+ if (value.pul != null)
+ value.pul,
+ ]);
+ if (entryResult.isEmpty) return;
+ final entryID = entryResult.first['entryID'];
+ if (value.sys != null)
+ await txn.delete('Systolic', where: 'entryID = ?', whereArgs:[entryID]);
+ if (value.dia != null)
+ await txn.delete('Diastolic', where:'entryID = ?', whereArgs:[entryID]);
+ if (value.pul != null)
+ await txn.delete('Pulse', where: 'entryID = ?', whereArgs: [entryID]);
+ });
+ // TODO: implement central cleanup of unused timestamp entries (by no table)
}
}
health_data_store/test/src/repositories/blood_pressure_repository_test.dart
@@ -40,4 +40,98 @@ void main() {
expect(values, hasLength(3));
expect(values, containsAll([r1,r2,r3]));
});
+ test('should remove records', () async {
+ final db = await mockDBManager();
+ addTearDown(db.close);
+ final repo = BloodPressureRepository(db.db);
+ final r1 = mockRecord(time: 10000, sys: 456, dia: 457, pul: 458);
+ await repo.add(r1);
+
+ final values1 = await repo.get(DateRange(
+ start: DateTime.fromMillisecondsSinceEpoch(0),
+ end: DateTime.fromMillisecondsSinceEpoch(80000),
+ ));
+ expect(values1, hasLength(1));
+ expect(values1, contains(r1));
+
+ await repo.remove(r1);
+ final values2 = await repo.get(DateRange(
+ start: DateTime.fromMillisecondsSinceEpoch(0),
+ end: DateTime.fromMillisecondsSinceEpoch(80000),
+ ));
+ expect(values2, isEmpty);
+ });
+ test('should remove partial records', () async {
+ final db = await mockDBManager();
+ addTearDown(db.close);
+ final repo = BloodPressureRepository(db.db);
+ final r1 = mockRecord(time: 10000, sys: 456, dia: 457, pul: 458);
+ final r2 = mockRecord(time: 20000, sys: 123);
+ final r3 = mockRecord(time: 30000, sys: 788, pul: 789);
+ await repo.add(r1);
+ await repo.add(r2);
+ await repo.add(r3);
+
+ final values0 = await repo.get(DateRange(
+ start: DateTime.fromMillisecondsSinceEpoch(0),
+ end: DateTime.fromMillisecondsSinceEpoch(80000),
+ ));
+ expect(values0, hasLength(3));
+ expect(values0, containsAll([r1,r2,r3]));
+
+ await repo.remove(r1);
+ final values1 = await repo.get(DateRange(
+ start: DateTime.fromMillisecondsSinceEpoch(0),
+ end: DateTime.fromMillisecondsSinceEpoch(80000),
+ ));
+ print(values1);
+ expect(values1, hasLength(2));
+ expect(values1, containsAll([r2,r3]));
+
+ await repo.remove(r2);
+ final values2 = await repo.get(DateRange(
+ start: DateTime.fromMillisecondsSinceEpoch(0),
+ end: DateTime.fromMillisecondsSinceEpoch(80000),
+ ));
+ print(values2);
+ expect(values2, hasLength(1));
+ expect(values2, containsAll([r3]));
+
+ await repo.remove(r3);
+ final values3 = await repo.get(DateRange(
+ start: DateTime.fromMillisecondsSinceEpoch(0),
+ end: DateTime.fromMillisecondsSinceEpoch(80000),
+ ));
+ expect(values3, isEmpty);
+ });
+ test('should remove correct record when multiple are at same time', () async {
+ final db = await mockDBManager();
+ addTearDown(db.close);
+ final repo = BloodPressureRepository(db.db);
+ final r1 = mockRecord(time: 10000, sys: 456, dia: 457, pul: 458);
+ final r2 = mockRecord(time: 10000, sys: 678, dia: 457, pul: 458);
+ await repo.add(r1);
+ await repo.add(r2);
+
+ await repo.remove(r1);
+ final values2 = await repo.get(DateRange(
+ start: DateTime.fromMillisecondsSinceEpoch(0),
+ end: DateTime.fromMillisecondsSinceEpoch(80000),
+ ));
+ expect(values2, hasLength(1));
+ expect(values2, contains(r2));
+ });
+ test('should not throw when removing non existent record', () async {
+ final db = await mockDBManager();
+ addTearDown(db.close);
+ final repo = BloodPressureRepository(db.db);
+ final r1 = mockRecord(time: 10000, sys: 456, dia: 457, pul: 458);
+
+ await repo.remove(r1);
+ final values = await repo.get(DateRange(
+ start: DateTime.fromMillisecondsSinceEpoch(0),
+ end: DateTime.fromMillisecondsSinceEpoch(80000),
+ ));
+ expect(values, isEmpty);
+ });
}