mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-02 10:09:11 +01:00
Use the workout session and logs from the current routine in summary.dart
This data is now always current from powersync
This commit is contained in:
160
lib/providers/exercise_repository.dart
Normal file
160
lib/providers/exercise_repository.dart
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* 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:drift/drift.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:wger/database/powersync/database.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/providers/exercises.dart';
|
||||
|
||||
final exerciseRepositoryProvider = Provider<ExerciseRepository>((ref) {
|
||||
final db = ref.read(driftPowerSyncDatabase);
|
||||
return ExerciseRepository(db);
|
||||
});
|
||||
|
||||
/// Drift-backed read access for the exercise catalogue.
|
||||
///
|
||||
/// The catalogue is denormalised across many tables (translations,
|
||||
/// muscles, equipment, category, images, videos) — this repository owns
|
||||
/// the join + assembly so the [Exercises] notifier stays focused on
|
||||
/// state-shape and search, and so tests can swap a single mock instead of
|
||||
/// stubbing the whole `DriftPowersyncDatabase`.
|
||||
class ExerciseRepository {
|
||||
final _logger = Logger('ExerciseRepository');
|
||||
final DriftPowersyncDatabase _db;
|
||||
|
||||
ExerciseRepository(this._db);
|
||||
|
||||
/// Streams the full exercise catalogue, hydrated with translations,
|
||||
/// muscles, equipment, category, images and videos.
|
||||
Stream<ExerciseState> watchAllDrift() {
|
||||
_logger.finer('Watching all local exercises');
|
||||
|
||||
final primaryMuscleTable = _db.alias(_db.muscleTable, 'pm');
|
||||
final secondaryMuscleTable = _db.alias(_db.muscleTable, 'sm');
|
||||
|
||||
final joined = _db.select(_db.exerciseTable).join([
|
||||
// Translations
|
||||
leftOuterJoin(
|
||||
_db.exerciseTranslationTable,
|
||||
_db.exerciseTranslationTable.exerciseId.equalsExp(_db.exerciseTable.id),
|
||||
),
|
||||
|
||||
// Language
|
||||
leftOuterJoin(
|
||||
_db.languageTable,
|
||||
_db.languageTable.id.equalsExp(_db.exerciseTranslationTable.languageId),
|
||||
),
|
||||
|
||||
// Exercise <-> Muscle
|
||||
leftOuterJoin(
|
||||
_db.exerciseMuscleM2N,
|
||||
_db.exerciseMuscleM2N.exerciseId.equalsExp(_db.exerciseTable.id),
|
||||
),
|
||||
leftOuterJoin(
|
||||
primaryMuscleTable,
|
||||
primaryMuscleTable.id.equalsExp(_db.exerciseMuscleM2N.muscleId),
|
||||
),
|
||||
|
||||
// Exercise <-> Secondary Muscle
|
||||
leftOuterJoin(
|
||||
_db.exerciseSecondaryMuscleM2N,
|
||||
_db.exerciseSecondaryMuscleM2N.exerciseId.equalsExp(_db.exerciseTable.id),
|
||||
),
|
||||
leftOuterJoin(
|
||||
secondaryMuscleTable,
|
||||
secondaryMuscleTable.id.equalsExp(_db.exerciseSecondaryMuscleM2N.muscleId),
|
||||
),
|
||||
|
||||
// Exercise <-> Equipment
|
||||
leftOuterJoin(
|
||||
_db.exerciseEquipmentM2N,
|
||||
_db.exerciseEquipmentM2N.exerciseId.equalsExp(_db.exerciseTable.id),
|
||||
),
|
||||
leftOuterJoin(
|
||||
_db.equipmentTable,
|
||||
_db.equipmentTable.id.equalsExp(_db.exerciseEquipmentM2N.equipmentId),
|
||||
),
|
||||
|
||||
// Category
|
||||
leftOuterJoin(
|
||||
_db.exerciseCategoryTable,
|
||||
_db.exerciseCategoryTable.id.equalsExp(_db.exerciseTable.categoryId),
|
||||
),
|
||||
|
||||
// Images
|
||||
leftOuterJoin(
|
||||
_db.exerciseImageTable,
|
||||
_db.exerciseImageTable.exerciseId.equalsExp(_db.exerciseTable.id),
|
||||
),
|
||||
]);
|
||||
|
||||
return joined.watch().map((rows) {
|
||||
final Map<int, Exercise> map = {};
|
||||
|
||||
for (final row in rows) {
|
||||
final exercise = row.readTable(_db.exerciseTable);
|
||||
final primaryMuscle = row.readTableOrNull(primaryMuscleTable);
|
||||
final secondaryMuscle = row.readTableOrNull(secondaryMuscleTable);
|
||||
final equipment = row.readTableOrNull(_db.equipmentTable);
|
||||
final image = row.readTableOrNull(_db.exerciseImageTable);
|
||||
final video = row.readTableOrNull(_db.exerciseVideoTable);
|
||||
final translation = row.readTableOrNull(_db.exerciseTranslationTable);
|
||||
final category = row.readTableOrNull(_db.exerciseCategoryTable);
|
||||
|
||||
final entry = map.putIfAbsent(
|
||||
exercise.id,
|
||||
() => exercise,
|
||||
);
|
||||
|
||||
if (category != null) {
|
||||
entry.category = category;
|
||||
}
|
||||
|
||||
if (translation != null && !entry.translations.any((t) => t.id == translation.id)) {
|
||||
translation.language = row.readTable(_db.languageTable);
|
||||
entry.translations.add(translation);
|
||||
}
|
||||
|
||||
if (image != null && !entry.images.any((t) => t.id == image.id)) {
|
||||
entry.images.add(image);
|
||||
}
|
||||
|
||||
if (video != null && !entry.videos.any((t) => t.id == video.id)) {
|
||||
entry.videos.add(video);
|
||||
}
|
||||
|
||||
if (equipment != null && !entry.equipment.any((e) => e.id == equipment.id)) {
|
||||
entry.equipment.add(equipment);
|
||||
}
|
||||
|
||||
if (primaryMuscle != null && !entry.muscles.any((m) => m.id == primaryMuscle.id)) {
|
||||
entry.muscles.add(primaryMuscle);
|
||||
}
|
||||
|
||||
if (secondaryMuscle != null &&
|
||||
!entry.musclesSecondary.any((m) => m.id == secondaryMuscle.id)) {
|
||||
entry.musclesSecondary.add(secondaryMuscle);
|
||||
}
|
||||
}
|
||||
|
||||
return ExerciseState(map.values.toList());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:wger/database/powersync/database.dart';
|
||||
@@ -25,6 +24,7 @@ import 'package:wger/models/exercises/category.dart';
|
||||
import 'package:wger/models/exercises/equipment.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/exercises/muscle.dart';
|
||||
import 'package:wger/providers/exercise_repository.dart';
|
||||
import 'package:wger/providers/wger_base.dart';
|
||||
|
||||
part 'exercises.g.dart';
|
||||
@@ -68,119 +68,9 @@ class Exercises extends _$Exercises {
|
||||
@override
|
||||
Stream<ExerciseState> build() {
|
||||
ref.keepAlive();
|
||||
final db = ref.read(driftPowerSyncDatabase);
|
||||
_logger.finer('Building exercise stream');
|
||||
|
||||
final primaryMuscleTable = db.alias(db.muscleTable, 'pm');
|
||||
final secondaryMuscleTable = db.alias(db.muscleTable, 'sm');
|
||||
|
||||
final joined = db.select(db.exerciseTable).join([
|
||||
// Translations
|
||||
leftOuterJoin(
|
||||
db.exerciseTranslationTable,
|
||||
db.exerciseTranslationTable.exerciseId.equalsExp(db.exerciseTable.id),
|
||||
),
|
||||
|
||||
// Language
|
||||
leftOuterJoin(
|
||||
db.languageTable,
|
||||
db.languageTable.id.equalsExp(db.exerciseTranslationTable.languageId),
|
||||
),
|
||||
|
||||
// Exercise <-> Muscle
|
||||
leftOuterJoin(
|
||||
db.exerciseMuscleM2N,
|
||||
db.exerciseMuscleM2N.exerciseId.equalsExp(db.exerciseTable.id),
|
||||
),
|
||||
leftOuterJoin(
|
||||
primaryMuscleTable,
|
||||
primaryMuscleTable.id.equalsExp(db.exerciseMuscleM2N.muscleId),
|
||||
),
|
||||
|
||||
// Exercise <-> Secondary Muscle
|
||||
leftOuterJoin(
|
||||
db.exerciseSecondaryMuscleM2N,
|
||||
db.exerciseSecondaryMuscleM2N.exerciseId.equalsExp(db.exerciseTable.id),
|
||||
),
|
||||
leftOuterJoin(
|
||||
secondaryMuscleTable,
|
||||
secondaryMuscleTable.id.equalsExp(db.exerciseSecondaryMuscleM2N.muscleId),
|
||||
),
|
||||
|
||||
// Exercise <-> Equipment
|
||||
leftOuterJoin(
|
||||
db.exerciseEquipmentM2N,
|
||||
db.exerciseEquipmentM2N.exerciseId.equalsExp(db.exerciseTable.id),
|
||||
),
|
||||
leftOuterJoin(
|
||||
db.equipmentTable,
|
||||
db.equipmentTable.id.equalsExp(db.exerciseEquipmentM2N.equipmentId),
|
||||
),
|
||||
|
||||
// Category
|
||||
leftOuterJoin(
|
||||
db.exerciseCategoryTable,
|
||||
db.exerciseCategoryTable.id.equalsExp(db.exerciseTable.categoryId),
|
||||
),
|
||||
|
||||
// Images
|
||||
leftOuterJoin(
|
||||
db.exerciseImageTable,
|
||||
db.exerciseImageTable.exerciseId.equalsExp(db.exerciseTable.id),
|
||||
),
|
||||
]);
|
||||
|
||||
return joined.watch().map((rows) {
|
||||
final Map<int, Exercise> map = {};
|
||||
|
||||
for (final row in rows) {
|
||||
final exercise = row.readTable(db.exerciseTable);
|
||||
final primaryMuscle = row.readTableOrNull(primaryMuscleTable);
|
||||
final secondaryMuscle = row.readTableOrNull(secondaryMuscleTable);
|
||||
final equipment = row.readTableOrNull(db.equipmentTable);
|
||||
final image = row.readTableOrNull(db.exerciseImageTable);
|
||||
final video = row.readTableOrNull(db.exerciseVideoTable);
|
||||
final translation = row.readTableOrNull(db.exerciseTranslationTable);
|
||||
final category = row.readTableOrNull(db.exerciseCategoryTable);
|
||||
|
||||
final entry = map.putIfAbsent(
|
||||
exercise.id,
|
||||
() => exercise,
|
||||
);
|
||||
|
||||
if (category != null) {
|
||||
entry.category = category;
|
||||
}
|
||||
|
||||
if (translation != null && !entry.translations.any((t) => t.id == translation.id)) {
|
||||
translation.language = row.readTable(db.languageTable);
|
||||
entry.translations.add(translation);
|
||||
}
|
||||
|
||||
if (image != null && !entry.images.any((t) => t.id == image.id)) {
|
||||
entry.images.add(image);
|
||||
}
|
||||
|
||||
if (video != null && !entry.videos.any((t) => t.id == video.id)) {
|
||||
entry.videos.add(video);
|
||||
}
|
||||
|
||||
if (equipment != null && !entry.equipment.any((e) => e.id == equipment.id)) {
|
||||
entry.equipment.add(equipment);
|
||||
}
|
||||
|
||||
if (primaryMuscle != null && !entry.muscles.any((m) => m.id == primaryMuscle.id)) {
|
||||
entry.muscles.add(primaryMuscle);
|
||||
}
|
||||
|
||||
if (secondaryMuscle != null &&
|
||||
!entry.musclesSecondary.any((m) => m.id == secondaryMuscle.id)) {
|
||||
entry.musclesSecondary.add(secondaryMuscle);
|
||||
}
|
||||
}
|
||||
|
||||
return ExerciseState(map.values.toList());
|
||||
});
|
||||
final repo = ref.read(exerciseRepositoryProvider);
|
||||
return repo.watchAllDrift();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -32,7 +32,7 @@ final class ExercisesProvider extends $StreamNotifierProvider<Exercises, Exercis
|
||||
Exercises create() => Exercises();
|
||||
}
|
||||
|
||||
String _$exercisesHash() => r'45635541935c391b39a3ac8614b699f5a2ea1da3';
|
||||
String _$exercisesHash() => r'ab035c57f36ff107dc52b302b584e28c7d27e2ea';
|
||||
|
||||
abstract class _$Exercises extends $StreamNotifier<ExerciseState> {
|
||||
Stream<ExerciseState> build();
|
||||
|
||||
@@ -98,14 +98,19 @@ class RoutinesRiverpod extends _$RoutinesRiverpod {
|
||||
_logger.fine('Building Routines Riverpod notifier');
|
||||
ref.keepAlive();
|
||||
|
||||
// Top-level routine list comes from PowerSync (Drift stream over
|
||||
// the synced `manager_routine` table). The sub-hierarchy (days,
|
||||
// slots, …) still lives behind REST and is layered on top by
|
||||
// callers via [fetchAndSetRoutineFull]. To prevent those sub-data
|
||||
// from being wiped out every time the stream re-emits the routines
|
||||
// (e.g. after a remote edit), we re-attach them from the previous
|
||||
// state below.
|
||||
// Top-level routine list comes from PowerSync. Two kinds of sub-data are
|
||||
// layered on top:
|
||||
//
|
||||
// * Plan structure (days, slots, set configs) — fetched via REST by
|
||||
// [fetchAndSetRoutineFull]; we carry it forward from the previous
|
||||
// state on every stream emission so a remote edit doesn't wipe it.
|
||||
// * Sessions + their logs (with `log.exerciseObj` hydrated) — pulled
|
||||
// in from [workoutSessionProvider] / [exercisesProvider] below.
|
||||
final repo = ref.read(routinesRepositoryProvider);
|
||||
|
||||
ref.listen(workoutSessionProvider, (_, _) => _reattachSessions());
|
||||
ref.listen(exercisesProvider, (_, _) => _reattachSessions());
|
||||
|
||||
return repo.watchAllDrift().map((freshRoutines) {
|
||||
final existing = state.value?.routines ?? const <Routine>[];
|
||||
for (final fresh in freshRoutines) {
|
||||
@@ -114,13 +119,51 @@ class RoutinesRiverpod extends _$RoutinesRiverpod {
|
||||
fresh.days = old.days;
|
||||
fresh.dayData = old.dayData;
|
||||
fresh.dayDataGym = old.dayDataGym;
|
||||
fresh.sessions = old.sessions;
|
||||
}
|
||||
}
|
||||
_hydrateSessions(freshRoutines);
|
||||
return RoutinesState(routines: freshRoutines);
|
||||
});
|
||||
}
|
||||
|
||||
/// Attaches the latest sessions to each routine and hydrates
|
||||
/// `log.exerciseObj` from the exercise catalogue. No-op if the upstream
|
||||
/// providers haven't produced their first value yet — the listener will
|
||||
/// re-attach as soon as they do.
|
||||
void _hydrateSessions(List<Routine> routines) {
|
||||
final sessions = ref.read(workoutSessionProvider).value ?? const <WorkoutSession>[];
|
||||
final exerciseState = ref.read(exercisesProvider).value;
|
||||
for (final routine in routines) {
|
||||
routine.sessions = sessions.where((s) => s.routineId == routine.id).toList();
|
||||
if (exerciseState == null) {
|
||||
continue;
|
||||
}
|
||||
for (final session in routine.sessions) {
|
||||
for (final log in session.logs) {
|
||||
// Fall back gracefully if the referenced exercise hasn't been
|
||||
// synced yet (rare but possible on a cold start).
|
||||
final exercise = exerciseState.exercises.firstWhereOrNull(
|
||||
(e) => e.id == log.exerciseId,
|
||||
);
|
||||
if (exercise != null) {
|
||||
log.exerciseObj = exercise;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-runs hydration on the current state and re-emits it. Triggered by
|
||||
/// [ref.listen] on the upstream session/exercise providers.
|
||||
void _reattachSessions() {
|
||||
final current = state.value;
|
||||
if (current == null) {
|
||||
return;
|
||||
}
|
||||
_hydrateSessions(current.routines);
|
||||
state = AsyncData(RoutinesState(routines: List.of(current.routines)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Routines
|
||||
*/
|
||||
@@ -134,6 +177,7 @@ class RoutinesRiverpod extends _$RoutinesRiverpod {
|
||||
final repo = ref.read(routinesRepositoryProvider);
|
||||
final created = await repo.addRoutineServer(routine);
|
||||
final current = state.value ?? const RoutinesState();
|
||||
|
||||
// Re-sort to match Django's `Meta.ordering = ['-start', '-created']` so
|
||||
// a freshly-created routine with an older start date doesn't briefly
|
||||
// jump to the front before the next stream emission corrects it.
|
||||
|
||||
@@ -111,7 +111,7 @@ final class RoutinesRiverpodProvider
|
||||
RoutinesRiverpod create() => RoutinesRiverpod();
|
||||
}
|
||||
|
||||
String _$routinesRiverpodHash() => r'9574d6cbb8701111e8ec1c8c60081bed56aadc3c';
|
||||
String _$routinesRiverpodHash() => r'e2ccfaa499f24dcd774cf6b208853b24da041318';
|
||||
|
||||
abstract class _$RoutinesRiverpod extends $StreamNotifier<RoutinesState> {
|
||||
Stream<RoutinesState> build();
|
||||
|
||||
@@ -25,10 +25,9 @@ import 'package:wger/helpers/date.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/trophies/user_trophy.dart';
|
||||
import 'package:wger/models/workouts/session.dart';
|
||||
import 'package:wger/providers/exercises.dart';
|
||||
import 'package:wger/providers/gym_state.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/trophies.dart';
|
||||
import 'package:wger/providers/workout_session.dart';
|
||||
import 'package:wger/widgets/core/progress_indicator.dart';
|
||||
import 'package:wger/widgets/routines/gym_mode/navigation.dart';
|
||||
|
||||
@@ -64,8 +63,8 @@ class _WorkoutSummaryState extends ConsumerState<WorkoutSummary> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final routineId = ref.read(gymStateProvider).routine.id!;
|
||||
final sessions = ref.watch(workoutSessionProvider).value;
|
||||
final exerciseState = ref.watch(exercisesProvider).value;
|
||||
final routinesState = ref.watch(routinesRiverpodProvider).value;
|
||||
final routine = routinesState?.routines.firstWhereOrNull((r) => r.id == routineId);
|
||||
final trophyState = ref.watch(trophyStateProvider);
|
||||
|
||||
return Column(
|
||||
@@ -84,29 +83,13 @@ class _WorkoutSummaryState extends ConsumerState<WorkoutSummary> {
|
||||
widget._logger.warning(snapshot.stackTrace);
|
||||
return Center(child: Text('Error: ${snapshot.error}'));
|
||||
}
|
||||
|
||||
// Wait for trophies + the PowerSync streams to have produced
|
||||
// at least one value. In practice these are loaded long before
|
||||
// we reach the summary screen, but the null check keeps us
|
||||
// safe on a cold start.
|
||||
if (snapshot.connectionState == ConnectionState.waiting ||
|
||||
sessions == null ||
|
||||
exerciseState == null) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting || routine == null) {
|
||||
return const BoxedProgressIndicator();
|
||||
}
|
||||
|
||||
final session = sessions.firstWhereOrNull(
|
||||
(s) => s.routineId == routineId && s.date.isSameDayAs(clock.now()),
|
||||
final session = routine.sessions.firstWhereOrNull(
|
||||
(s) => s.date.isSameDayAs(clock.now()),
|
||||
);
|
||||
if (session != null) {
|
||||
// The session repository joins logs but doesn't hydrate the
|
||||
// exercise relation; do it here so [MuscleGroupsCard] and
|
||||
// [ExercisesCard] can read `log.exerciseObj`.
|
||||
for (final log in session.logs) {
|
||||
log.exerciseObj = exerciseState.getById(log.exerciseId);
|
||||
}
|
||||
}
|
||||
|
||||
final userTrophies = trophyState.prTrophies
|
||||
.where((t) => t.contextData?.sessionId == session?.id)
|
||||
.toList();
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (C) 2020, 2021 wger Team
|
||||
* 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.
|
||||
*
|
||||
* wger Workout Manager is distributed in the hope that it will be useful,
|
||||
* 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.
|
||||
@@ -29,6 +29,7 @@ import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/exercises/muscle.dart';
|
||||
import 'package:wger/providers/add_exercise_repository.dart';
|
||||
import 'package:wger/providers/core_data.dart';
|
||||
import 'package:wger/providers/exercise_repository.dart';
|
||||
import 'package:wger/providers/exercises.dart';
|
||||
import 'package:wger/providers/network_provider.dart';
|
||||
import 'package:wger/providers/user_profile_repository.dart';
|
||||
@@ -40,31 +41,29 @@ import 'contribute_exercise_test.mocks.dart';
|
||||
|
||||
/// Test suite for the exercise-contribution screen.
|
||||
///
|
||||
/// Exercises the widget tree with the real `AddExerciseNotifier` + a mocked
|
||||
/// repository. The notifier builds with an empty default form state.
|
||||
class _FakeExercises extends Exercises {
|
||||
@override
|
||||
Stream<ExerciseState> build() => Stream.value(const ExerciseState(<Exercise>[]));
|
||||
}
|
||||
|
||||
@GenerateMocks([AddExerciseRepository, UserProfileRepository])
|
||||
@GenerateMocks([AddExerciseRepository, UserProfileRepository, ExerciseRepository])
|
||||
void main() {
|
||||
installFakeConnectivity();
|
||||
|
||||
late MockAddExerciseRepository mockAddExerciseRepository;
|
||||
late MockUserProfileRepository mockUserProfileRepository;
|
||||
late MockExerciseRepository mockExerciseRepository;
|
||||
|
||||
setUp(() {
|
||||
mockAddExerciseRepository = MockAddExerciseRepository();
|
||||
mockUserProfileRepository = MockUserProfileRepository();
|
||||
mockExerciseRepository = MockExerciseRepository();
|
||||
when(mockUserProfileRepository.fetchProfile()).thenAnswer((_) async => tProfile1);
|
||||
when(
|
||||
mockExerciseRepository.watchAllDrift(),
|
||||
).thenAnswer((_) => Stream.value(const ExerciseState(<Exercise>[])));
|
||||
});
|
||||
|
||||
Widget createExerciseScreen({locale = 'en', bool isOnline = true}) {
|
||||
return ProviderScope(
|
||||
overrides: [
|
||||
languagesProvider.overrideWith((ref) => Stream<List<Language>>.value(<Language>[])),
|
||||
exercisesProvider.overrideWith(() => _FakeExercises()),
|
||||
exerciseRepositoryProvider.overrideWithValue(mockExerciseRepository),
|
||||
exerciseMusclesProvider.overrideWith((ref) => Stream<List<Muscle>>.value(<Muscle>[])),
|
||||
exerciseCategoriesProvider.overrideWith(
|
||||
(ref) => Stream<List<ExerciseCategory>>.value(<ExerciseCategory>[]),
|
||||
|
||||
@@ -10,6 +10,8 @@ import 'package:wger/models/exercises/exercise_submission.dart' as _i5;
|
||||
import 'package:wger/models/exercises/exercise_submission_images.dart' as _i6;
|
||||
import 'package:wger/models/user/profile.dart' as _i2;
|
||||
import 'package:wger/providers/add_exercise_repository.dart' as _i3;
|
||||
import 'package:wger/providers/exercise_repository.dart' as _i8;
|
||||
import 'package:wger/providers/exercises.dart' as _i9;
|
||||
import 'package:wger/providers/user_profile_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
@@ -109,3 +111,20 @@ class MockUserProfileRepository extends _i1.Mock implements _i7.UserProfileRepos
|
||||
)
|
||||
as _i4.Future<void>);
|
||||
}
|
||||
|
||||
/// A class which mocks [ExerciseRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockExerciseRepository extends _i1.Mock implements _i8.ExerciseRepository {
|
||||
MockExerciseRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i4.Stream<_i9.ExerciseState> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i4.Stream<_i9.ExerciseState>.empty(),
|
||||
)
|
||||
as _i4.Stream<_i9.ExerciseState>);
|
||||
}
|
||||
|
||||
@@ -19,29 +19,38 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/exercises/category.dart';
|
||||
import 'package:wger/models/exercises/equipment.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/exercises/muscle.dart';
|
||||
import 'package:wger/providers/exercise_repository.dart';
|
||||
import 'package:wger/providers/exercises.dart';
|
||||
import 'package:wger/widgets/exercises/exercises.dart';
|
||||
|
||||
import '../../test_data/exercises.dart';
|
||||
import '../fake_connectivity.dart';
|
||||
import 'exercises_detail_widget_test.mocks.dart';
|
||||
|
||||
class _FakeExercises extends Exercises {
|
||||
@override
|
||||
Stream<ExerciseState> build() => Stream.value(const ExerciseState(<Exercise>[]));
|
||||
}
|
||||
|
||||
@GenerateMocks([ExerciseRepository])
|
||||
void main() {
|
||||
installFakeConnectivity();
|
||||
|
||||
late MockExerciseRepository mockExerciseRepo;
|
||||
|
||||
setUp(() {
|
||||
mockExerciseRepo = MockExerciseRepository();
|
||||
when(
|
||||
mockExerciseRepo.watchAllDrift(),
|
||||
).thenAnswer((_) => Stream.value(const ExerciseState(<Exercise>[])));
|
||||
});
|
||||
|
||||
Widget createHomeScreen({locale = 'en'}) {
|
||||
return ProviderScope(
|
||||
overrides: [
|
||||
exercisesProvider.overrideWith(() => _FakeExercises()),
|
||||
exerciseRepositoryProvider.overrideWithValue(mockExerciseRepo),
|
||||
exerciseMusclesProvider.overrideWith((ref) => Stream<List<Muscle>>.value(<Muscle>[])),
|
||||
exerciseCategoriesProvider.overrideWith(
|
||||
(ref) => Stream<List<ExerciseCategory>>.value(<ExerciseCategory>[]),
|
||||
|
||||
42
test/exercises/exercises_detail_widget_test.mocks.dart
Normal file
42
test/exercises/exercises_detail_widget_test.mocks.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
// Mocks generated by Mockito 5.4.6 from annotations
|
||||
// in wger/test/exercises/exercises_detail_widget_test.dart.
|
||||
// Do not manually edit this file.
|
||||
|
||||
// ignore_for_file: no_leading_underscores_for_library_prefixes
|
||||
import 'dart:async' as _i3;
|
||||
|
||||
import 'package:mockito/mockito.dart' as _i1;
|
||||
import 'package:wger/providers/exercise_repository.dart' as _i2;
|
||||
import 'package:wger/providers/exercises.dart' as _i4;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
// ignore_for_file: avoid_setters_without_getters
|
||||
// ignore_for_file: comment_references
|
||||
// ignore_for_file: deprecated_member_use
|
||||
// ignore_for_file: deprecated_member_use_from_same_package
|
||||
// ignore_for_file: implementation_imports
|
||||
// ignore_for_file: invalid_use_of_visible_for_testing_member
|
||||
// ignore_for_file: must_be_immutable
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
// ignore_for_file: unnecessary_parenthesis
|
||||
// ignore_for_file: camel_case_types
|
||||
// ignore_for_file: subtype_of_sealed_class
|
||||
// ignore_for_file: invalid_use_of_internal_member
|
||||
|
||||
/// A class which mocks [ExerciseRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockExerciseRepository extends _i1.Mock implements _i2.ExerciseRepository {
|
||||
MockExerciseRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i3.Stream<_i4.ExerciseState> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i3.Stream<_i4.ExerciseState>.empty(),
|
||||
)
|
||||
as _i3.Stream<_i4.ExerciseState>);
|
||||
}
|
||||
@@ -25,11 +25,13 @@ import 'package:mockito/mockito.dart';
|
||||
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
||||
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/models/workouts/repetition_unit.dart';
|
||||
import 'package:wger/models/workouts/session.dart';
|
||||
import 'package:wger/models/workouts/weight_unit.dart';
|
||||
import 'package:wger/providers/exercise_repository.dart';
|
||||
import 'package:wger/providers/exercises.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/providers/workout_session_repository.dart';
|
||||
import 'package:wger/screens/gym_mode.dart';
|
||||
import 'package:wger/screens/routine_screen.dart';
|
||||
@@ -48,24 +50,7 @@ import '../../../test_data/routines.dart';
|
||||
import '../../fake_connectivity.dart';
|
||||
import 'gym_mode_test.mocks.dart';
|
||||
|
||||
@GenerateMocks([WorkoutSessionRepository])
|
||||
class _FakeRoutinesRiverpod extends RoutinesRiverpod {
|
||||
_FakeRoutinesRiverpod(this._routine);
|
||||
|
||||
final Routine _routine;
|
||||
|
||||
@override
|
||||
Stream<RoutinesState> build() => Stream.value(RoutinesState(routines: [_routine]));
|
||||
|
||||
@override
|
||||
Future<Routine> fetchAndSetRoutineFull(int routineId) async => _routine;
|
||||
}
|
||||
|
||||
class _FakeExercises extends Exercises {
|
||||
@override
|
||||
Stream<ExerciseState> build() => Stream.value(const ExerciseState(<Exercise>[]));
|
||||
}
|
||||
|
||||
@GenerateMocks([WorkoutSessionRepository, ExerciseRepository, RoutinesRepository])
|
||||
void main() {
|
||||
installFakeConnectivity();
|
||||
|
||||
@@ -75,6 +60,8 @@ void main() {
|
||||
final testExercises = getTestExercises();
|
||||
|
||||
final mockSessionRepo = MockWorkoutSessionRepository();
|
||||
final mockExerciseRepo = MockExerciseRepository();
|
||||
final mockRoutinesRepo = MockRoutinesRepository();
|
||||
|
||||
setUp(() {
|
||||
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
|
||||
@@ -83,14 +70,38 @@ void main() {
|
||||
controller.add(testRoutine.sessions);
|
||||
}),
|
||||
);
|
||||
when(
|
||||
mockExerciseRepo.watchAllDrift(),
|
||||
).thenAnswer((_) => Stream.value(ExerciseState(testExercises)));
|
||||
|
||||
// Drift the test routine in via the routines repository — the real
|
||||
// [RoutinesRiverpod] picks it up and exposes it through state.
|
||||
when(
|
||||
mockRoutinesRepo.watchAllDrift(),
|
||||
).thenAnswer((_) => Stream.value([testRoutine]));
|
||||
// [fetchAndSetRoutineFull] (called by [GymMode._loadGymState] before the
|
||||
// page tree is built) goes back to the server in production; here we
|
||||
// just hand back the same test routine.
|
||||
when(
|
||||
mockRoutinesRepo.fetchAndSetRoutineFullServer(any),
|
||||
).thenAnswer((_) async => testRoutine);
|
||||
});
|
||||
|
||||
Widget renderGymMode({locale = 'en'}) {
|
||||
return riverpod.ProviderScope(
|
||||
overrides: [
|
||||
routinesRiverpodProvider.overrideWith(() => _FakeRoutinesRiverpod(testRoutine)),
|
||||
exercisesProvider.overrideWith(() => _FakeExercises()),
|
||||
routinesRepositoryProvider.overrideWithValue(mockRoutinesRepo),
|
||||
exerciseRepositoryProvider.overrideWithValue(mockExerciseRepo),
|
||||
workoutSessionRepositoryProvider.overrideWithValue(mockSessionRepo),
|
||||
// The repetition + weight unit catalogues are tiny direct-Drift
|
||||
// stream providers — overriding them inline is the established
|
||||
// pattern (see also [exerciseCategoriesProvider] etc.).
|
||||
routineRepetitionUnitProvider.overrideWith(
|
||||
(ref) => Stream<List<RepetitionUnit>>.value(testRepetitionUnits),
|
||||
),
|
||||
routineWeightUnitProvider.overrideWith(
|
||||
(ref) => Stream<List<WeightUnit>>.value(testWeightUnits),
|
||||
),
|
||||
],
|
||||
child: MaterialApp(
|
||||
locale: Locale(locale),
|
||||
|
||||
@@ -3,11 +3,19 @@
|
||||
// Do not manually edit this file.
|
||||
|
||||
// ignore_for_file: no_leading_underscores_for_library_prefixes
|
||||
import 'dart:async' as _i3;
|
||||
import 'dart:async' as _i8;
|
||||
|
||||
import 'package:mockito/mockito.dart' as _i1;
|
||||
import 'package:wger/models/workouts/session.dart' as _i4;
|
||||
import 'package:wger/providers/workout_session_repository.dart' as _i2;
|
||||
import 'package:wger/models/workouts/base_config.dart' as _i6;
|
||||
import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/session.dart' as _i9;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/exercise_repository.dart' as _i10;
|
||||
import 'package:wger/providers/exercises.dart' as _i11;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i12;
|
||||
import 'package:wger/providers/workout_session_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -24,46 +32,307 @@ import 'package:wger/providers/workout_session_repository.dart' as _i2;
|
||||
// ignore_for_file: subtype_of_sealed_class
|
||||
// ignore_for_file: invalid_use_of_internal_member
|
||||
|
||||
class _FakeRoutine_0 extends _i1.SmartFake implements _i2.Routine {
|
||||
_FakeRoutine_0(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
|
||||
}
|
||||
|
||||
class _FakeDay_1 extends _i1.SmartFake implements _i3.Day {
|
||||
_FakeDay_1(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
|
||||
}
|
||||
|
||||
class _FakeSlot_2 extends _i1.SmartFake implements _i4.Slot {
|
||||
_FakeSlot_2(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
|
||||
}
|
||||
|
||||
class _FakeSlotEntry_3 extends _i1.SmartFake implements _i5.SlotEntry {
|
||||
_FakeSlotEntry_3(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
|
||||
}
|
||||
|
||||
class _FakeBaseConfig_4 extends _i1.SmartFake implements _i6.BaseConfig {
|
||||
_FakeBaseConfig_4(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
|
||||
}
|
||||
|
||||
/// A class which mocks [WorkoutSessionRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockWorkoutSessionRepository extends _i1.Mock implements _i2.WorkoutSessionRepository {
|
||||
class MockWorkoutSessionRepository extends _i1.Mock implements _i7.WorkoutSessionRepository {
|
||||
MockWorkoutSessionRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i3.Stream<List<_i4.WorkoutSession>> watchAllDrift() =>
|
||||
_i8.Stream<List<_i9.WorkoutSession>> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i3.Stream<List<_i4.WorkoutSession>>.empty(),
|
||||
returnValue: _i8.Stream<List<_i9.WorkoutSession>>.empty(),
|
||||
)
|
||||
as _i3.Stream<List<_i4.WorkoutSession>>);
|
||||
as _i8.Stream<List<_i9.WorkoutSession>>);
|
||||
|
||||
@override
|
||||
_i3.Future<void> deleteLocalDrift(String? id) =>
|
||||
_i8.Future<void> deleteLocalDrift(String? id) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteLocalDrift, [id]),
|
||||
returnValue: _i3.Future<void>.value(),
|
||||
returnValueForMissingStub: _i3.Future<void>.value(),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i3.Future<void>);
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i3.Future<void> editLocalDrift(_i4.WorkoutSession? session) =>
|
||||
_i8.Future<void> editLocalDrift(_i9.WorkoutSession? session) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editLocalDrift, [session]),
|
||||
returnValue: _i3.Future<void>.value(),
|
||||
returnValueForMissingStub: _i3.Future<void>.value(),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i3.Future<void>);
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i3.Future<void> addLocalDrift(_i4.WorkoutSession? session) =>
|
||||
_i8.Future<void> addLocalDrift(_i9.WorkoutSession? session) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addLocalDrift, [session]),
|
||||
returnValue: _i3.Future<void>.value(),
|
||||
returnValueForMissingStub: _i3.Future<void>.value(),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i3.Future<void>);
|
||||
as _i8.Future<void>);
|
||||
}
|
||||
|
||||
/// A class which mocks [ExerciseRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockExerciseRepository extends _i1.Mock implements _i10.ExerciseRepository {
|
||||
MockExerciseRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Stream<_i11.ExerciseState> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i8.Stream<_i11.ExerciseState>.empty(),
|
||||
)
|
||||
as _i8.Stream<_i11.ExerciseState>);
|
||||
}
|
||||
|
||||
/// A class which mocks [RoutinesRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockRoutinesRepository extends _i1.Mock implements _i12.RoutinesRepository {
|
||||
MockRoutinesRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> fetchAndSetRoutineFullServer(int? routineId) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchAndSetRoutineFullServer, [routineId]),
|
||||
returnValue: _i8.Future<_i2.Routine>.value(
|
||||
_FakeRoutine_0(
|
||||
this,
|
||||
Invocation.method(#fetchAndSetRoutineFullServer, [routineId]),
|
||||
),
|
||||
),
|
||||
)
|
||||
as _i8.Future<_i2.Routine>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> addRoutineServer(_i2.Routine? routine) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addRoutineServer, [routine]),
|
||||
returnValue: _i8.Future<_i2.Routine>.value(
|
||||
_FakeRoutine_0(
|
||||
this,
|
||||
Invocation.method(#addRoutineServer, [routine]),
|
||||
),
|
||||
),
|
||||
)
|
||||
as _i8.Future<_i2.Routine>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> editRoutineServer(_i2.Routine? routine) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editRoutineServer, [routine]),
|
||||
returnValue: _i8.Future<_i2.Routine>.value(
|
||||
_FakeRoutine_0(
|
||||
this,
|
||||
Invocation.method(#editRoutineServer, [routine]),
|
||||
),
|
||||
),
|
||||
)
|
||||
as _i8.Future<_i2.Routine>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> deleteRoutineServer(int? id) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteRoutineServer, [id]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Stream<List<_i2.Routine>> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i8.Stream<List<_i2.Routine>>.empty(),
|
||||
)
|
||||
as _i8.Stream<List<_i2.Routine>>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> editLocalDrift(_i2.Routine? routine) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editLocalDrift, [routine]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> deleteLocalDrift(int? id) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteLocalDrift, [id]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i3.Day> addDayServer(_i3.Day? day) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addDayServer, [day]),
|
||||
returnValue: _i8.Future<_i3.Day>.value(
|
||||
_FakeDay_1(this, Invocation.method(#addDayServer, [day])),
|
||||
),
|
||||
)
|
||||
as _i8.Future<_i3.Day>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> editDayServer(_i3.Day? day) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editDayServer, [day]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> deleteDayServer(int? dayId) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteDayServer, [dayId]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i4.Slot> addSlotServer(_i4.Slot? slot) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addSlotServer, [slot]),
|
||||
returnValue: _i8.Future<_i4.Slot>.value(
|
||||
_FakeSlot_2(this, Invocation.method(#addSlotServer, [slot])),
|
||||
),
|
||||
)
|
||||
as _i8.Future<_i4.Slot>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> deleteSlotServer(int? slotId) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteSlotServer, [slotId]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> editSlotServer(_i4.Slot? slot) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editSlotServer, [slot]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i5.SlotEntry> addSlotEntryServer(_i5.SlotEntry? entry) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addSlotEntryServer, [entry]),
|
||||
returnValue: _i8.Future<_i5.SlotEntry>.value(
|
||||
_FakeSlotEntry_3(
|
||||
this,
|
||||
Invocation.method(#addSlotEntryServer, [entry]),
|
||||
),
|
||||
),
|
||||
)
|
||||
as _i8.Future<_i5.SlotEntry>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> deleteSlotEntryServer(int? id) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteSlotEntryServer, [id]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> editSlotEntryServer(_i5.SlotEntry? entry) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editSlotEntryServer, [entry]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i6.BaseConfig> editConfigServer(
|
||||
_i6.BaseConfig? config,
|
||||
_i5.ConfigType? type,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editConfigServer, [config, type]),
|
||||
returnValue: _i8.Future<_i6.BaseConfig>.value(
|
||||
_FakeBaseConfig_4(
|
||||
this,
|
||||
Invocation.method(#editConfigServer, [config, type]),
|
||||
),
|
||||
),
|
||||
)
|
||||
as _i8.Future<_i6.BaseConfig>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i6.BaseConfig> addConfigServer(
|
||||
_i6.BaseConfig? config,
|
||||
_i5.ConfigType? type,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addConfigServer, [config, type]),
|
||||
returnValue: _i8.Future<_i6.BaseConfig>.value(
|
||||
_FakeBaseConfig_4(
|
||||
this,
|
||||
Invocation.method(#addConfigServer, [config, type]),
|
||||
),
|
||||
),
|
||||
)
|
||||
as _i8.Future<_i6.BaseConfig>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> deleteConfigServer(int? id, _i5.ConfigType? type) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteConfigServer, [id, type]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> handleConfigServer(
|
||||
_i5.SlotEntry? entry,
|
||||
num? value,
|
||||
_i5.ConfigType? type,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#handleConfigServer, [entry, value, type]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
}
|
||||
|
||||
@@ -26,9 +26,14 @@ import 'package:mockito/mockito.dart';
|
||||
import 'package:wger/core/exceptions/http_exception.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/models/workouts/session.dart';
|
||||
import 'package:wger/providers/exercise_repository.dart';
|
||||
import 'package:wger/providers/exercises.dart';
|
||||
import 'package:wger/providers/network_provider.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/providers/workout_session_repository.dart';
|
||||
import 'package:wger/screens/routine_edit_screen.dart';
|
||||
import 'package:wger/screens/routine_screen.dart';
|
||||
import 'package:wger/widgets/routines/forms/routine.dart';
|
||||
@@ -37,11 +42,13 @@ import '../../test_data/routines.dart';
|
||||
import '../fake_connectivity.dart';
|
||||
import './routine_form_test.mocks.dart';
|
||||
|
||||
@GenerateMocks([RoutinesRepository])
|
||||
@GenerateMocks([RoutinesRepository, WorkoutSessionRepository, ExerciseRepository])
|
||||
void main() {
|
||||
installFakeConnectivity();
|
||||
|
||||
late MockRoutinesRepository mockRoutinesRepository;
|
||||
late MockWorkoutSessionRepository mockSessionRepo;
|
||||
late MockExerciseRepository mockExerciseRepo;
|
||||
late StreamController<List<Routine>> routineStream;
|
||||
late Routine existingRoutine;
|
||||
late Routine newRoutine;
|
||||
@@ -68,6 +75,19 @@ void main() {
|
||||
// Edit + delete now go through Drift instead of REST.
|
||||
when(mockRoutinesRepository.editLocalDrift(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockRoutinesRepository.deleteLocalDrift(any)).thenAnswer((_) async => Future.value());
|
||||
|
||||
// RoutinesRiverpod.build() listens to workoutSessionProvider and
|
||||
// exercisesProvider for session/exercise hydration; stub the
|
||||
// repositories that back them so we don't fall through to the real
|
||||
// PowerSync DB.
|
||||
mockSessionRepo = MockWorkoutSessionRepository();
|
||||
when(
|
||||
mockSessionRepo.watchAllDrift(),
|
||||
).thenAnswer((_) => Stream.value(const <WorkoutSession>[]));
|
||||
mockExerciseRepo = MockExerciseRepository();
|
||||
when(
|
||||
mockExerciseRepo.watchAllDrift(),
|
||||
).thenAnswer((_) => Stream.value(const ExerciseState(<Exercise>[])));
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
@@ -80,6 +100,8 @@ void main() {
|
||||
return ProviderScope(
|
||||
overrides: [
|
||||
routinesRepositoryProvider.overrideWithValue(mockRoutinesRepository),
|
||||
workoutSessionRepositoryProvider.overrideWithValue(mockSessionRepo),
|
||||
exerciseRepositoryProvider.overrideWithValue(mockExerciseRepo),
|
||||
networkStatusProvider.overrideWithValue(isOnline),
|
||||
],
|
||||
child: MaterialApp(
|
||||
|
||||
@@ -9,9 +9,13 @@ import 'package:mockito/mockito.dart' as _i1;
|
||||
import 'package:wger/models/workouts/base_config.dart' as _i6;
|
||||
import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/session.dart' as _i10;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/exercise_repository.dart' as _i11;
|
||||
import 'package:wger/providers/exercises.dart' as _i12;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
import 'package:wger/providers/workout_session_repository.dart' as _i9;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -271,3 +275,64 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
}
|
||||
|
||||
/// A class which mocks [WorkoutSessionRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockWorkoutSessionRepository extends _i1.Mock implements _i9.WorkoutSessionRepository {
|
||||
MockWorkoutSessionRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Stream<List<_i10.WorkoutSession>> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i8.Stream<List<_i10.WorkoutSession>>.empty(),
|
||||
)
|
||||
as _i8.Stream<List<_i10.WorkoutSession>>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> deleteLocalDrift(String? id) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteLocalDrift, [id]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> editLocalDrift(_i10.WorkoutSession? session) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editLocalDrift, [session]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> addLocalDrift(_i10.WorkoutSession? session) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addLocalDrift, [session]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
}
|
||||
|
||||
/// A class which mocks [ExerciseRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockExerciseRepository extends _i1.Mock implements _i11.ExerciseRepository {
|
||||
MockExerciseRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Stream<_i12.ExerciseState> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i8.Stream<_i12.ExerciseState>.empty(),
|
||||
)
|
||||
as _i8.Stream<_i12.ExerciseState>);
|
||||
}
|
||||
|
||||
@@ -17,21 +17,29 @@
|
||||
*/
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_riverpod/misc.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/models/workouts/session.dart';
|
||||
import 'package:wger/providers/exercise_repository.dart';
|
||||
import 'package:wger/providers/exercises.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/providers/workout_session_repository.dart';
|
||||
|
||||
import '../../test_data/routines.dart';
|
||||
import '../fake_connectivity.dart';
|
||||
import 'routines_provider_test.mocks.dart';
|
||||
|
||||
@GenerateMocks([RoutinesRepository])
|
||||
@GenerateMocks([RoutinesRepository, WorkoutSessionRepository, ExerciseRepository])
|
||||
void main() {
|
||||
late MockRoutinesRepository mockRepo;
|
||||
late MockWorkoutSessionRepository mockSessionRepo;
|
||||
late MockExerciseRepository mockExerciseRepo;
|
||||
late Day testDay;
|
||||
|
||||
installFakeConnectivity();
|
||||
@@ -39,6 +47,18 @@ void main() {
|
||||
setUp(() {
|
||||
mockRepo = MockRoutinesRepository();
|
||||
when(mockRepo.watchAllDrift()).thenAnswer((_) => Stream.value(const <Routine>[]));
|
||||
// RoutinesRiverpod.build() listens to workoutSessionProvider and
|
||||
// exercisesProvider for session/exercise hydration; stub the
|
||||
// repositories that back them so we don't fall through to the real
|
||||
// PowerSync DB.
|
||||
mockSessionRepo = MockWorkoutSessionRepository();
|
||||
when(
|
||||
mockSessionRepo.watchAllDrift(),
|
||||
).thenAnswer((_) => Stream.value(const <WorkoutSession>[]));
|
||||
mockExerciseRepo = MockExerciseRepository();
|
||||
when(
|
||||
mockExerciseRepo.watchAllDrift(),
|
||||
).thenAnswer((_) => Stream.value(const ExerciseState(<Exercise>[])));
|
||||
testDay = Day(
|
||||
id: 15,
|
||||
routineId: 101,
|
||||
@@ -47,6 +67,13 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
/// Overrides for the providers `RoutinesRiverpod.build()` listens to so
|
||||
/// they don't reach for the real PowerSync DB.
|
||||
List<Override> ambientOverrides() => [
|
||||
workoutSessionRepositoryProvider.overrideWithValue(mockSessionRepo),
|
||||
exerciseRepositoryProvider.overrideWithValue(mockExerciseRepo),
|
||||
];
|
||||
|
||||
group('test routine methods', () {
|
||||
test('addRoutine calls repository (creation still goes via REST)', () async {
|
||||
// Arrange
|
||||
@@ -54,7 +81,10 @@ void main() {
|
||||
final created = getTestRoutine();
|
||||
when(mockRepo.addRoutineServer(any)).thenAnswer((_) async => created);
|
||||
final container = ProviderContainer.test(
|
||||
overrides: [routinesRepositoryProvider.overrideWithValue(mockRepo)],
|
||||
overrides: [
|
||||
routinesRepositoryProvider.overrideWithValue(mockRepo),
|
||||
...ambientOverrides(),
|
||||
],
|
||||
);
|
||||
|
||||
// Act
|
||||
@@ -73,7 +103,10 @@ void main() {
|
||||
when(mockRepo.addDayServer(any)).thenAnswer((_) async => testDay);
|
||||
when(mockRepo.fetchAndSetRoutineFullServer(any)).thenAnswer((_) async => getTestRoutine());
|
||||
final container = ProviderContainer.test(
|
||||
overrides: [routinesRepositoryProvider.overrideWithValue(mockRepo)],
|
||||
overrides: [
|
||||
routinesRepositoryProvider.overrideWithValue(mockRepo),
|
||||
...ambientOverrides(),
|
||||
],
|
||||
);
|
||||
|
||||
// Act
|
||||
@@ -94,7 +127,10 @@ void main() {
|
||||
).thenAnswer((_) async => getTestRoutine());
|
||||
|
||||
final container = ProviderContainer.test(
|
||||
overrides: [routinesRepositoryProvider.overrideWithValue(mockRepo)],
|
||||
overrides: [
|
||||
routinesRepositoryProvider.overrideWithValue(mockRepo),
|
||||
...ambientOverrides(),
|
||||
],
|
||||
);
|
||||
|
||||
// Act
|
||||
@@ -123,7 +159,10 @@ void main() {
|
||||
when(mockRepo.editDayServer(any)).thenAnswer((_) async => Future.value());
|
||||
|
||||
final container = ProviderContainer.test(
|
||||
overrides: [routinesRepositoryProvider.overrideWithValue(mockRepo)],
|
||||
overrides: [
|
||||
routinesRepositoryProvider.overrideWithValue(mockRepo),
|
||||
...ambientOverrides(),
|
||||
],
|
||||
);
|
||||
|
||||
// Act
|
||||
@@ -148,7 +187,10 @@ void main() {
|
||||
).thenAnswer((_) async => getTestRoutine());
|
||||
|
||||
final container = ProviderContainer.test(
|
||||
overrides: [routinesRepositoryProvider.overrideWithValue(mockRepo)],
|
||||
overrides: [
|
||||
routinesRepositoryProvider.overrideWithValue(mockRepo),
|
||||
...ambientOverrides(),
|
||||
],
|
||||
);
|
||||
|
||||
// Act
|
||||
|
||||
@@ -9,9 +9,13 @@ import 'package:mockito/mockito.dart' as _i1;
|
||||
import 'package:wger/models/workouts/base_config.dart' as _i6;
|
||||
import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/session.dart' as _i10;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/exercise_repository.dart' as _i11;
|
||||
import 'package:wger/providers/exercises.dart' as _i12;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
import 'package:wger/providers/workout_session_repository.dart' as _i9;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -271,3 +275,64 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
}
|
||||
|
||||
/// A class which mocks [WorkoutSessionRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockWorkoutSessionRepository extends _i1.Mock implements _i9.WorkoutSessionRepository {
|
||||
MockWorkoutSessionRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Stream<List<_i10.WorkoutSession>> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i8.Stream<List<_i10.WorkoutSession>>.empty(),
|
||||
)
|
||||
as _i8.Stream<List<_i10.WorkoutSession>>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> deleteLocalDrift(String? id) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#deleteLocalDrift, [id]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> editLocalDrift(_i10.WorkoutSession? session) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#editLocalDrift, [session]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
|
||||
@override
|
||||
_i8.Future<void> addLocalDrift(_i10.WorkoutSession? session) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addLocalDrift, [session]),
|
||||
returnValue: _i8.Future<void>.value(),
|
||||
returnValueForMissingStub: _i8.Future<void>.value(),
|
||||
)
|
||||
as _i8.Future<void>);
|
||||
}
|
||||
|
||||
/// A class which mocks [ExerciseRepository].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockExerciseRepository extends _i1.Mock implements _i11.ExerciseRepository {
|
||||
MockExerciseRepository() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Stream<_i12.ExerciseState> watchAllDrift() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#watchAllDrift, []),
|
||||
returnValue: _i8.Stream<_i12.ExerciseState>.empty(),
|
||||
)
|
||||
as _i8.Stream<_i12.ExerciseState>);
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ Routine getTestRoutine({List<Exercise>? exercises}) {
|
||||
slotId: 2,
|
||||
type: SlotEntryType.normal,
|
||||
order: 1,
|
||||
exerciseId: 8,
|
||||
exerciseId: testExercises[4].id!,
|
||||
repetitionUnitId: 1,
|
||||
repetitionRounding: 0.25,
|
||||
weightUnitId: 1,
|
||||
@@ -171,7 +171,7 @@ Routine getTestRoutine({List<Exercise>? exercises}) {
|
||||
slotId: 2,
|
||||
type: SlotEntryType.normal,
|
||||
order: 1,
|
||||
exerciseId: 8,
|
||||
exerciseId: testExercises[5].id!,
|
||||
repetitionUnitId: 1,
|
||||
repetitionRounding: 0.25,
|
||||
weightUnitId: 1,
|
||||
@@ -325,7 +325,7 @@ Routine getTestRoutine({List<Exercise>? exercises}) {
|
||||
exerciseIds: [testExercises[4].id!],
|
||||
setConfigs: [
|
||||
SetConfigData(
|
||||
exerciseId: 8,
|
||||
exerciseId: testExercises[4].id!,
|
||||
exercise: testExercises[4],
|
||||
slotEntryId: 1,
|
||||
nrOfSets: 1,
|
||||
@@ -432,10 +432,10 @@ Routine getTestRoutine({List<Exercise>? exercises}) {
|
||||
SlotData(
|
||||
comment: 'Make sure to go deep. Slow down, and push through your heels',
|
||||
isSuperset: false,
|
||||
exerciseIds: [8],
|
||||
exerciseIds: [testExercises[4].id!],
|
||||
setConfigs: [
|
||||
SetConfigData(
|
||||
exerciseId: 8,
|
||||
exerciseId: testExercises[4].id!,
|
||||
exercise: testExercises[4],
|
||||
slotEntryId: 1,
|
||||
nrOfSets: 4,
|
||||
|
||||
Reference in New Issue
Block a user