Round imported health values to match backend and lowercase external ids

This commit is contained in:
Roland Geider
2026-07-11 01:31:22 +02:00
parent 5a65b84943
commit f0a088eb0e
4 changed files with 85 additions and 4 deletions

View File

@@ -53,7 +53,10 @@ class HealthReading {
type: point.type,
value: value.numericValue.toDouble(),
date: point.dateFrom,
externalId: point.uuid.isEmpty ? null : point.uuid,
// HealthKit reports UUIDs in uppercase, but the server's UUIDField
// normalizes to lowercase. Lowercase here so the dedup key stays stable
// once the entry round-trips through the server.
externalId: point.uuid.isEmpty ? null : point.uuid.toLowerCase(),
);
}
}

View File

@@ -153,11 +153,16 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
continue;
}
// The server stores values as Decimal with 2 places and rejects
// anything more precise, so round away unit-conversion float noise
// (1.803 m * 100 = 180.29999999999998).
final value = (metric.toCategoryValue(reading.value) * 100).roundToDouble() / 100;
await _measurements.addLocalDrift(
MeasurementEntry(
categoryId: category.id!,
date: reading.date,
value: metric.toCategoryValue(reading.value),
value: value,
notes: '',
source: source,
externalId: uuid,

View File

@@ -0,0 +1,70 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter_test/flutter_test.dart';
import 'package:health_bridge/health.dart';
import 'package:wger/features/health/models/health_reading.dart';
HealthDataPoint dataPoint({String uuid = 'abc-123', HealthValue? value}) {
return HealthDataPoint(
uuid: uuid,
value: value ?? NumericHealthValue(numericValue: 1.8),
type: HealthDataType.HEIGHT,
unit: HealthDataUnit.METER,
dateFrom: DateTime(2026, 1, 1, 8, 30),
dateTo: DateTime(2026, 1, 1, 8, 30),
sourcePlatform: HealthPlatformType.appleHealth,
sourceDeviceId: 'device',
sourceId: 'source',
sourceName: 'test',
);
}
void main() {
group('fromDataPoint', () {
test('extracts the numeric value, type and start date', () {
final reading = HealthReading.fromDataPoint(dataPoint())!;
expect(reading.type, HealthDataType.HEIGHT);
expect(reading.value, 1.8);
expect(reading.date, DateTime(2026, 1, 1, 8, 30));
expect(reading.externalId, 'abc-123');
});
test('returns null for a non-numeric value', () {
final point = dataPoint(value: NutritionHealthValue());
expect(HealthReading.fromDataPoint(point), isNull);
});
test('lowercases the platform UUID to match the server normalization', () {
final point = dataPoint(uuid: '1A2B3C4D-C36C-495A-93FC-0C247A3E6E5F');
expect(
HealthReading.fromDataPoint(point)!.externalId,
'1a2b3c4d-c36c-495a-93fc-0c247a3e6e5f',
);
});
test('maps an empty UUID to null', () {
final point = dataPoint(uuid: '');
expect(HealthReading.fromDataPoint(point)!.externalId, isNull);
});
});
}

View File

@@ -100,7 +100,10 @@ void main() {
),
HealthReading(
type: HealthDataType.HEIGHT,
value: 1.8,
// 1.803 * 100 is 180.29999999999998 in double arithmetic; the
// server's Decimal(2) validation requires the stored value to be
// rounded to exactly 180.3
value: 1.803,
date: DateTime(2026, 1, 2),
externalId: 'h-1',
),
@@ -126,7 +129,7 @@ void main() {
expect(bodyFat.source, 'apple');
final height = entries.firstWhere((e) => e.externalId == 'h-1');
expect(height.value, closeTo(180, 0.001)); // meters -> cm
expect(height.value, 180.3); // meters -> cm, rounded to two decimals
});
test('skips readings already imported (dedup by externalId)', () async {