mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 02:24:30 +01:00
Compare the chart bounds against the format the sync writes
This commit is contained in:
@@ -300,10 +300,24 @@ class MeasurementRepository {
|
||||
/// query reads the same rows as the typed ones.
|
||||
List<Variable> _bucketVariables(String id, DateTime? since, DateTime? until) => [
|
||||
Variable.withString(id),
|
||||
if (since != null) Variable.withString(since.toUtc().toIso8601String()),
|
||||
if (until != null) Variable.withString(until.toUtc().toIso8601String()),
|
||||
if (since != null) Variable.withString(_dateBound(since)),
|
||||
if (until != null) Variable.withString(_dateBound(until)),
|
||||
];
|
||||
|
||||
/// A date bound for the string comparisons the queries here do.
|
||||
///
|
||||
/// Padded to the six fractional digits the sync writes, while Dart prints
|
||||
/// three for a round instant. Since `'0' < 'Z'`, an unpadded bound compares
|
||||
/// as later than a synced row on the very same instant, which is where the
|
||||
/// daily aggregates of a health metric sit: on the day the window starts.
|
||||
String _dateBound(DateTime value) {
|
||||
final utc = value.toUtc().toIso8601String();
|
||||
final dot = utc.indexOf('.');
|
||||
final fraction = utc.substring(dot + 1, utc.length - 1);
|
||||
|
||||
return '${utc.substring(0, dot + 1)}${fraction.padRight(6, '0')}Z';
|
||||
}
|
||||
|
||||
/// The date bounds as SQL, against the variables [_bucketVariables] binds
|
||||
/// after the id. The upper one is exclusive, so a caller passes the start of
|
||||
/// the day after the last one it wants.
|
||||
@@ -330,7 +344,7 @@ class MeasurementRepository {
|
||||
|
||||
final variables = [
|
||||
Variable.withString(categoryId),
|
||||
if (since != null) Variable.withString(since.toUtc().toIso8601String()),
|
||||
if (since != null) Variable.withString(_dateBound(since)),
|
||||
];
|
||||
final where = 'WHERE category_id = ?1${since == null ? '' : ' AND date >= ?2'}';
|
||||
final day = _bucketExpression(MeasurementBucketUnit.day);
|
||||
|
||||
@@ -527,6 +527,38 @@ void main() {
|
||||
expect(buckets.map((b) => b.sum), [80]);
|
||||
});
|
||||
|
||||
/// An entry as the sync writes it back: the service serialises timestamps
|
||||
/// with six fractional digits, drift with three.
|
||||
Future<void> seedSyncedEntry(String id, DateTime date, num value) {
|
||||
final iso = date.toUtc().toIso8601String();
|
||||
final utc = '${iso.substring(0, iso.length - 1)}000Z';
|
||||
|
||||
return db.customStatement(
|
||||
'INSERT INTO measurements_measurement (id, category_id, date, value, notes) '
|
||||
"VALUES ('$id', '1', '$utc', $value, '')",
|
||||
);
|
||||
}
|
||||
|
||||
test('since keeps a synced entry sitting exactly on it', () async {
|
||||
// Where the daily aggregates of a health metric are: on the start of the
|
||||
// day, which is what a window starts on too
|
||||
await seedEntries([]);
|
||||
await seedSyncedEntry('synced', DateTime(2026, 3, 1), 60);
|
||||
|
||||
final buckets = await repo.watchEntryBuckets('1', since: DateTime(2026, 3, 1)).first;
|
||||
|
||||
expect(buckets.map((b) => b.sum), [60]);
|
||||
});
|
||||
|
||||
test('until drops a synced entry sitting exactly on it', () async {
|
||||
await seedEntries([]);
|
||||
await seedSyncedEntry('synced', DateTime(2026, 5, 4), 80);
|
||||
|
||||
final buckets = await repo.watchEntryBuckets('1', until: DateTime(2026, 5, 4)).first;
|
||||
|
||||
expect(buckets, isEmpty);
|
||||
});
|
||||
|
||||
test('until bounds the entries read, excluding its own day', () async {
|
||||
// What a chart of a finished plan period reads: everything after the
|
||||
// last day belongs to no plan
|
||||
@@ -739,6 +771,19 @@ void main() {
|
||||
expect(counts.single.count, 2);
|
||||
});
|
||||
|
||||
test('since keeps a synced entry sitting exactly on it', () async {
|
||||
await seedValues([(DateTime(2026, 5, 4, 8), 60)]);
|
||||
final iso = DateTime(2026, 3, 1).toUtc().toIso8601String();
|
||||
await db.customStatement(
|
||||
'INSERT INTO measurements_measurement (id, category_id, date, value, notes) '
|
||||
"VALUES ('synced', '1', '${iso.substring(0, iso.length - 1)}000Z', 50, '')",
|
||||
);
|
||||
|
||||
final counts = await repo.watchValueCounts('1', since: DateTime(2026, 3, 1)).first;
|
||||
|
||||
expect(counts.map((c) => c.value), [50, 60]);
|
||||
});
|
||||
|
||||
test('mixed units are kept apart', () async {
|
||||
await db
|
||||
.into(db.measurementCategoryTable)
|
||||
|
||||
Reference in New Issue
Block a user