mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-05 04:57:37 +01:00
Log.exerciseObj was declared late and is only ever assigned when hydrating the logs of a routine's sessions. Logs read from the database are built through drift's row class and only carry exerciseId, so copyWith threw a LateInitializationError when it unconditionally read the field. Copying a past log in gym mode does exactly that. Make the field nullable behind a getter that throws a descriptive error, add exerciseObjOrNull for callers that legitimately handle logs straight from the database, and only carry the exercise over when there is one. Hydrating the exercise in watchLogsByExerciseDrift instead would mean joining its translations, images, muscles and equipment for every log, which most callers don't need. The log page tests mocked the query with the test routine's logs, which are hydrated, which is why this was never caught. They now return logs without an exercise, like the real query does. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XzxNFoPMxsFPxHD3P2Ma3w
301 lines
8.4 KiB
Dart
301 lines
8.4 KiB
Dart
/*
|
|
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
|
* Copyright (c) 2020, 2025 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:wger/core/consts.dart';
|
|
import 'package:wger/features/exercises/models/category.dart';
|
|
import 'package:wger/features/exercises/models/exercise.dart';
|
|
import 'package:wger/features/routines/models/log.dart';
|
|
import 'package:wger/features/routines/models/set_config_data.dart';
|
|
|
|
import '../../../../test_data/exercises.dart';
|
|
|
|
void main() {
|
|
group('Log.volume', () {
|
|
test('returns weight * repetitions for metric (kg) and repetition unit', () {
|
|
final log = Log(
|
|
exerciseId: 1,
|
|
routineId: 1,
|
|
rir: null,
|
|
date: DateTime.now(),
|
|
weight: 100,
|
|
weightUnitId: WEIGHT_UNIT_KG,
|
|
repetitions: 5,
|
|
repetitionsUnitId: REP_UNIT_REPETITIONS_ID,
|
|
);
|
|
|
|
expect(log.volume(metric: true), equals(500));
|
|
});
|
|
|
|
test('returns 0 when weight unit does not match metric flag', () {
|
|
final log = Log(
|
|
exerciseId: 2,
|
|
routineId: 1,
|
|
rir: null,
|
|
date: DateTime.now(),
|
|
weight: 100,
|
|
weightUnitId: WEIGHT_UNIT_LB, // pounds
|
|
repetitions: 5,
|
|
repetitionsUnitId: REP_UNIT_REPETITIONS_ID,
|
|
);
|
|
|
|
// metric = true expects KG -> mismatch -> 0
|
|
expect(log.volume(metric: true), equals(0));
|
|
});
|
|
|
|
test('returns weight * repetitions for imperial (lb) when metric=false', () {
|
|
final log = Log(
|
|
exerciseId: 3,
|
|
routineId: 1,
|
|
rir: null,
|
|
date: DateTime.now(),
|
|
weight: 220, // lb
|
|
weightUnitId: WEIGHT_UNIT_LB,
|
|
repetitions: 3,
|
|
repetitionsUnitId: REP_UNIT_REPETITIONS_ID,
|
|
);
|
|
|
|
expect(log.volume(metric: false), equals(660));
|
|
});
|
|
|
|
test('returns 0 when repetitions unit is not repetitions', () {
|
|
final log = Log(
|
|
exerciseId: 4,
|
|
routineId: 1,
|
|
rir: null,
|
|
date: DateTime.now(),
|
|
weight: 50,
|
|
weightUnitId: WEIGHT_UNIT_KG,
|
|
repetitions: 10,
|
|
repetitionsUnitId: 999, // some other unit id
|
|
);
|
|
|
|
expect(log.volume(metric: true), equals(0));
|
|
});
|
|
|
|
test('returns 0 when weight or repetitions are null', () {
|
|
final a = Log(
|
|
exerciseId: 5,
|
|
routineId: 1,
|
|
rir: null,
|
|
date: DateTime.now(),
|
|
weight: null,
|
|
weightUnitId: WEIGHT_UNIT_KG,
|
|
repetitions: 5,
|
|
repetitionsUnitId: REP_UNIT_REPETITIONS_ID,
|
|
);
|
|
|
|
final b = Log(
|
|
exerciseId: 6,
|
|
routineId: 1,
|
|
rir: null,
|
|
date: DateTime.now(),
|
|
weight: 50,
|
|
weightUnitId: WEIGHT_UNIT_KG,
|
|
repetitions: null,
|
|
repetitionsUnitId: REP_UNIT_REPETITIONS_ID,
|
|
);
|
|
|
|
expect(a.volume(metric: true), equals(0));
|
|
expect(b.volume(metric: true), equals(0));
|
|
});
|
|
|
|
test('works with non-integer (num) weight and repetitions', () {
|
|
final log = Log(
|
|
exerciseId: 7,
|
|
routineId: 1,
|
|
rir: null,
|
|
date: DateTime.now(),
|
|
weight: 10.5,
|
|
weightUnitId: WEIGHT_UNIT_KG,
|
|
repetitions: 3,
|
|
repetitionsUnitId: REP_UNIT_REPETITIONS_ID,
|
|
);
|
|
|
|
expect(log.volume(metric: true), closeTo(31.5, 0.0001));
|
|
});
|
|
});
|
|
|
|
group('Log.fromSetConfigData', () {
|
|
late Exercise exercise;
|
|
|
|
setUp(() {
|
|
exercise = const Exercise(
|
|
id: 1,
|
|
uuid: 'test-uuid',
|
|
category: ExerciseCategory(id: 1, name: 'Test'),
|
|
);
|
|
});
|
|
|
|
test('uses default unit IDs when unit objects are not loaded', () {
|
|
// Reproduces: "Repetitions unit must be present if repetitions have a value"
|
|
//
|
|
// When the user taps "+" in gym mode to add an extra exercise,
|
|
// addExerciseAfterPage() creates a SetConfigData with only exerciseId and
|
|
// slotEntryId, no unit objects. The default constructor sets
|
|
// weightUnitId=WEIGHT_UNIT_KG and repetitionsUnitId=REP_UNIT_REPETITIONS_ID,
|
|
// but Log.fromSetConfigData was using the object setters which overwrite
|
|
// those IDs with null when the objects are null, causing the server to
|
|
// reject the request with a 400 ValidationError.
|
|
final setConfig = SetConfigData(
|
|
exerciseId: 1,
|
|
slotEntryId: 1,
|
|
exercise: exercise,
|
|
// no weightUnit or repetitionsUnit objects, mirrors addExerciseAfterPage()
|
|
);
|
|
|
|
final log = Log.fromSetConfigData(setConfig);
|
|
|
|
expect(log.weightUnitId, WEIGHT_UNIT_KG);
|
|
expect(log.repetitionsUnitId, REP_UNIT_REPETITIONS_ID);
|
|
});
|
|
|
|
test('uses custom unit IDs from SetConfigData when set', () {
|
|
final setConfig = SetConfigData(
|
|
exerciseId: 1,
|
|
slotEntryId: 1,
|
|
exercise: exercise,
|
|
weightUnitId: WEIGHT_UNIT_LB,
|
|
repetitionsUnitId: REP_UNIT_TILL_FAILURE_ID,
|
|
);
|
|
|
|
final log = Log.fromSetConfigData(setConfig);
|
|
|
|
expect(log.weightUnitId, WEIGHT_UNIT_LB);
|
|
expect(log.repetitionsUnitId, REP_UNIT_TILL_FAILURE_ID);
|
|
});
|
|
|
|
test('copies weight and repetitions values', () {
|
|
final setConfig = SetConfigData(
|
|
exerciseId: 1,
|
|
slotEntryId: 1,
|
|
exercise: exercise,
|
|
weight: 80,
|
|
repetitions: 12,
|
|
rir: 2,
|
|
);
|
|
|
|
final log = Log.fromSetConfigData(setConfig);
|
|
|
|
expect(log.weight, 80);
|
|
expect(log.weightTarget, 80);
|
|
expect(log.repetitions, 12);
|
|
expect(log.repetitionsTarget, 12);
|
|
expect(log.rir, 2);
|
|
expect(log.rirTarget, 2);
|
|
});
|
|
});
|
|
|
|
group('Log exercise hydration', () {
|
|
/// A log as it comes out of the database, with only the exercise ID set
|
|
Log makeDriftLog() => Log(
|
|
id: 'log-1',
|
|
exerciseId: 1,
|
|
routineId: 100,
|
|
weight: 20,
|
|
repetitions: 10,
|
|
date: DateTime(2026, 1, 1),
|
|
);
|
|
|
|
test('copies a log whose exercise was never hydrated', () {
|
|
final copy = makeDriftLog().copyWith(date: DateTime(2026, 2, 2));
|
|
|
|
expect(copy.exerciseObjOrNull, isNull);
|
|
});
|
|
|
|
test('reading an unhydrated exercise throws a descriptive error', () {
|
|
expect(
|
|
() => makeDriftLog().exerciseObj,
|
|
throwsA(isA<StateError>().having((e) => e.message, 'message', contains('log-1'))),
|
|
);
|
|
});
|
|
|
|
test('propagates the exercise when it is hydrated', () {
|
|
final log = makeDriftLog()..exercise = testBenchPress;
|
|
|
|
final copy = log.copyWith(weight: 30);
|
|
|
|
expect(copy.exerciseObj.id, testBenchPress.id);
|
|
expect(copy.exerciseId, testBenchPress.id);
|
|
});
|
|
});
|
|
|
|
group('Test the workout log model', () {
|
|
late Log log1;
|
|
late Log log2;
|
|
|
|
setUp(() {
|
|
log1 = Log(
|
|
id: '123',
|
|
iteration: 1,
|
|
slotEntryId: 100,
|
|
routineId: 100,
|
|
exerciseId: 1,
|
|
repetitions: 10,
|
|
rir: 1.5,
|
|
repetitionsUnitId: 1,
|
|
weight: 20,
|
|
weightUnitId: 1,
|
|
date: DateTime(2010, 10, 1),
|
|
);
|
|
log2 = Log(
|
|
id: '9',
|
|
iteration: 2,
|
|
slotEntryId: 42,
|
|
routineId: 42,
|
|
exerciseId: 1,
|
|
repetitions: 10,
|
|
rir: 1.5,
|
|
repetitionsUnitId: 1,
|
|
weight: 20,
|
|
weightUnitId: 1,
|
|
date: DateTime(2063, 4, 5),
|
|
);
|
|
});
|
|
|
|
test('Test equal values (besides Id, workoutPlan and date)', () {
|
|
expect(log1, log2);
|
|
});
|
|
|
|
test('Test different rir values', () {
|
|
log1.rir = null;
|
|
expect(log1, isNot(log2));
|
|
});
|
|
|
|
test('Test different weight values', () {
|
|
log1.weight = 100;
|
|
expect(log1, isNot(log2));
|
|
});
|
|
|
|
test('Test different weight units', () {
|
|
log1.weightUnitId = 2;
|
|
expect(log1, isNot(log2));
|
|
});
|
|
|
|
test('Test different reps', () {
|
|
log1.repetitions = 99;
|
|
expect(log1, isNot(log2));
|
|
});
|
|
|
|
test('Test different rep units', () {
|
|
log1.repetitionsUnitId = 44;
|
|
expect(log1, isNot(log2));
|
|
});
|
|
});
|
|
}
|