Skip to content

Commit

Permalink
Refactor step retrieval logic and update device_info_plus dependency;…
Browse files Browse the repository at this point in the history
… add unit tests for health data retrieval
  • Loading branch information
iarata committed Dec 18, 2024
1 parent 3464029 commit 6e98ad3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
7 changes: 4 additions & 3 deletions packages/health/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,10 @@ class _HealthAppState extends State<HealthApp> {

if (stepsPermission) {
try {
steps = await Health().getTotalStepsInInterval(midnight, now,
includeManualEntry:
!recordingMethodsToFilter.contains(RecordingMethod.manual));
steps = await Health().getTotalStepsInInterval(midnight, now, includeManualEntry: false);
// steps = await Health().getTotalStepsInInterval(midnight, now,
// includeManualEntry:
// !recordingMethodsToFilter.contains(RecordingMethod.manual));
} catch (error) {
debugPrint("Exception in getTotalStepsInInterval: $error");
}
Expand Down
3 changes: 2 additions & 1 deletion packages/health/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ dependencies:
flutter:
sdk: flutter
intl: '>=0.18.0 <0.20.0'
device_info_plus: '>=9.0.0 <11.0.0'
# device_info_plus: '>=9.0.0 <11.0.0'
device_info_plus: '^11.1.1'
json_annotation: ^4.8.0
carp_serializable: ^2.0.0 # polymorphic json serialization

Expand Down
51 changes: 51 additions & 0 deletions packages/health/test/health_test.dart
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:health/health.dart';
import 'package:flutter/widgets.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('Health Plugin Tests', () {
late Health health;

setUp(() {
health = Health();
});

test('getHealthDataFromTypes returns results within the correct date range', () async {
final startTime = DateTime(2023, 1, 1);
final endTime = DateTime(2024, 1, 1);
final types = [HealthDataType.WORKOUT];

final healthData = await health.getHealthDataFromTypes(
types: types,
startTime: startTime,
endTime: endTime,
);

for (var dataPoint in healthData) {

// print the values time
print('dateFrom: ${dataPoint.dateFrom} - required: $startTime');
print('dateTo: ${dataPoint.dateTo} - required: $endTime');
expect(dataPoint.dateFrom.isAfter(startTime) || dataPoint.dateFrom.isAtSameMomentAs(startTime), isTrue);
expect(dataPoint.dateTo.isBefore(endTime) || dataPoint.dateTo.isAtSameMomentAs(endTime), isTrue);
}
});

test('getHealthDataFromTypes returns no results outside the specified date range', () async {
final startTime = DateTime(2023, 1, 1);
final endTime = DateTime(2024, 1, 1);
final types = [HealthDataType.WORKOUT];

final healthData = await health.getHealthDataFromTypes(
types: types,
startTime: startTime,
endTime: endTime,
);

for (var dataPoint in healthData) {
expect(dataPoint.dateFrom.isBefore(startTime), isFalse);
expect(dataPoint.dateTo.isAfter(endTime), isFalse);
}
});
});
}

0 comments on commit 6e98ad3

Please sign in to comment.