Files
flutter/test/exercises/exercise_provider_test.dart
2026-05-03 17:54:07 +02:00

643 lines
21 KiB
Dart

/*
* 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 'dart:convert';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.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/core/exceptions/no_such_entry_exception.dart';
import 'package:wger/database/exercises/exercise_database.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/shared_preferences.dart';
import 'package:wger/models/core/search_options.dart';
import 'package:wger/models/exercises/category.dart';
import 'package:wger/models/exercises/equipment.dart';
import 'package:wger/models/exercises/exercise_filters.dart';
import 'package:wger/models/exercises/muscle.dart';
import 'package:wger/providers/exercises.dart';
import '../../test_data/exercises.dart' as data;
import '../../test_data/exercises.dart';
import '../fixtures/fixture_reader.dart';
import '../measurements/measurement_provider_test.mocks.dart';
void main() {
late MockWgerBaseProvider mockBaseProvider;
late ExercisesProvider provider;
const String categoryUrl = 'exercisecategory';
const String exerciseInfoUrl = 'exerciseinfo';
const String muscleUrl = 'muscle';
const String equipmentUrl = 'equipment';
const String languageUrl = 'language';
const String exerciseSearchUrl = 'exerciseinfo';
final Uri tCategoryEntriesUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$categoryUrl/',
);
final Uri tExerciseInfoUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$exerciseInfoUrl/1/',
);
final Uri tMuscleEntriesUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$muscleUrl/',
);
final Uri tEquipmentEntriesUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$equipmentUrl/',
);
final Uri tLanguageEntriesUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$languageUrl/',
);
final Uri tSearchByNameUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$exerciseSearchUrl/',
);
const category1 = ExerciseCategory(id: 1, name: 'Arms');
const muscle1 = Muscle(id: 1, name: 'Biceps brachii', nameEn: 'Biceps', isFront: true);
final Map<String, dynamic> tCategoryMap = jsonDecode(
fixture('exercises/category_entries.json'),
);
final Map<String, dynamic> tMuscleMap = jsonDecode(
fixture('exercises/muscles_entries.json'),
);
final Map<String, dynamic> tEquipmentMap = jsonDecode(
fixture('exercises/equipment_entries.json'),
);
final Map<String, dynamic> tLanguageMap = jsonDecode(
fixture('exercises/language_entries.json'),
);
final Map<String, dynamic> tExerciseInfoMap = jsonDecode(
fixture('exercises/exerciseinfo_response.json'),
);
setUpAll(() {
// Needs to be configured here, setUp runs on every test, setUpAll only once
//await ServiceLocator().configure();
});
setUp(() {
mockBaseProvider = MockWgerBaseProvider();
provider = ExercisesProvider(
mockBaseProvider,
database: ExerciseDatabase.inMemory(NativeDatabase.memory()),
);
provider.languages = [...testLanguages];
/// Replacement for SharedPreferences.setMockInitialValues()
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
// Mock categories
when(
mockBaseProvider.makeUrl(categoryUrl, query: anyNamed('query')),
).thenReturn(tCategoryEntriesUri);
when(
mockBaseProvider.fetchPaginated(tCategoryEntriesUri),
).thenAnswer((_) => Future.value(tCategoryMap['results']));
// Mock muscles
when(
mockBaseProvider.makeUrl(muscleUrl, query: anyNamed('query')),
).thenReturn(tMuscleEntriesUri);
when(
mockBaseProvider.fetchPaginated(tMuscleEntriesUri),
).thenAnswer((_) => Future.value(tMuscleMap['results']));
// Mock equipment
when(
mockBaseProvider.makeUrl(equipmentUrl, query: anyNamed('query')),
).thenReturn(tEquipmentEntriesUri);
when(
mockBaseProvider.fetchPaginated(tEquipmentEntriesUri),
).thenAnswer((_) => Future.value(tEquipmentMap['results']));
// Mock languages
when(
mockBaseProvider.makeUrl(languageUrl, query: anyNamed('query')),
).thenReturn(tLanguageEntriesUri);
when(
mockBaseProvider.fetchPaginated(tLanguageEntriesUri),
).thenAnswer((_) => Future.value(tLanguageMap['results']));
// Mock base info response
when(mockBaseProvider.makeUrl(exerciseInfoUrl, id: 1)).thenReturn(tExerciseInfoUri);
when(mockBaseProvider.makeUrl(exerciseInfoUrl, id: 2)).thenReturn(tExerciseInfoUri);
when(
mockBaseProvider.fetch(tExerciseInfoUri),
).thenAnswer((_) => Future.value(tExerciseInfoMap));
});
group('findCategoryById()', () {
test('should return a category for an id', () async {
// arrange
await provider.fetchAndSetCategoriesFromApi();
// act
final result = provider.findCategoryById(1);
// assert
expect(result, category1);
});
test('should throw a NoResultException if no category is found', () {
// act & assert
expect(() => provider.findCategoryById(3), throwsA(isA<NoSuchEntryException>()));
});
});
group('findMuscleById()', () {
test('should return a muscle for an id', () async {
// arrange
await provider.fetchAndSetMusclesFromApi();
// act
final result = provider.findMuscleById(1);
// assert
expect(result, muscle1);
});
test('should throw a NoResultException if no muscle is found', () {
// act & assert
expect(() => provider.findMuscleById(3), throwsA(isA<NoSuchEntryException>()));
});
});
group('findEquipmentById()', () {
test('should return an equipment for an id', () async {
// arrange
await provider.fetchAndSetEquipmentsFromApi();
// act
final result = provider.findEquipmentById(1);
// assert
expect(result, tEquipment1);
});
test('should throw a NoResultException if no equipment is found', () {
// act & assert
expect(() => provider.findEquipmentById(10), throwsA(isA<NoSuchEntryException>()));
});
});
group('findLanguageById()', () {
test('should return a language for an id', () async {
// arrange
await provider.fetchAndSetLanguagesFromApi();
// act
final result = provider.findLanguageById(1);
// assert
expect(result, tLanguage1);
});
test('should throw a NoResultException if no equipment is found', () {
// act & assert
expect(() => provider.findLanguageById(10), throwsA(isA<NoSuchEntryException>()));
});
});
group('findByFilters', () {
test('Filters are null', () async {
// arrange
Filters? currentFilters;
// arrange and act
await provider.setFilters(currentFilters);
// assert
verifyNever(provider.baseProvider.fetch(tSearchByNameUri));
expect(provider.filteredExercises, isEmpty);
});
group('Filters are not null', () {
late Filters filters;
setUp(() {
SharedPreferences.setMockInitialValues({});
filters = Filters(
exerciseCategories: FilterCategory<ExerciseCategory>(title: 'Muscle Groups', items: {}),
equipment: FilterCategory<Equipment>(title: 'Equipment', items: {}),
);
provider.exercises = data.getTestExercises();
});
test('Nothing is selected with no search term', () async {
// arrange
final Filters currentFilters = filters;
// act
await provider.setFilters(currentFilters);
// assert
verifyNever(provider.baseProvider.fetch(tSearchByNameUri));
expect(provider.filteredExercises, data.getTestExercises());
});
test('A muscle is selected with no search term. Should find results', () async {
// arrange
final Filters tFilters = filters.copyWith(
exerciseCategories: filters.exerciseCategories.copyWith(items: {data.tCategory1: true}),
);
// act
await provider.setFilters(tFilters);
// assert
verifyNever(provider.baseProvider.fetch(tSearchByNameUri));
expect(provider.filteredExercises, [data.getTestExercises()[0]]);
});
test('A muscle is selected with no search term. Should not find results', () async {
// arrange
final Filters tFilters = filters.copyWith(
exerciseCategories: filters.exerciseCategories.copyWith(items: {data.tCategory5: true}),
);
// act
await provider.setFilters(tFilters);
// assert
verifyNever(provider.baseProvider.fetch(tSearchByNameUri));
expect(provider.filteredExercises, isEmpty);
});
test('An equipment is selected with no search term. Should find results', () async {
// arrange
final Filters tFilters = filters.copyWith(
equipment: filters.equipment.copyWith(items: {data.tEquipment1: true}),
);
// act
await provider.setFilters(tFilters);
// assert
verifyNever(provider.baseProvider.fetch(tSearchByNameUri));
expect(provider.filteredExercises, [data.getTestExercises()[0]]);
});
test('An equipment is selected with no search term. Should not find results', () async {
// arrange
final Filters tFilters = filters.copyWith(
equipment: filters.equipment.copyWith(items: {data.tEquipment3: true}),
);
// act
await provider.setFilters(tFilters);
// assert
verifyNever(provider.baseProvider.fetch(tSearchByNameUri));
expect(provider.filteredExercises, isEmpty);
});
test('A muscle and equipment is selected and there is a match', () async {
// arrange
final Filters tFilters = filters.copyWith(
exerciseCategories: filters.exerciseCategories.copyWith(items: {data.tCategory2: true}),
equipment: filters.equipment.copyWith(items: {data.tEquipment2: true}),
);
// act
await provider.setFilters(tFilters);
// assert
verifyNever(provider.baseProvider.fetch(tSearchByNameUri));
expect(provider.filteredExercises, [data.getTestExercises()[1]]);
});
test('A muscle and equipment is selected but no match', () async {
// arrange
final Filters tFilters = filters.copyWith(
exerciseCategories: filters.exerciseCategories.copyWith(items: {data.tCategory2: true}),
equipment: filters.equipment.copyWith(items: {tEquipment1: true}),
);
// act
await provider.setFilters(tFilters);
// assert
verifyNever(provider.baseProvider.fetch(tSearchByNameUri));
expect(provider.filteredExercises, isEmpty);
});
group('Search term', () {
late Uri tSearchByNameUri;
setUp(() {
const String tSearchTerm = 'press';
const String tSearchLanguage = 'en';
final Map<String, dynamic> query = {
'name__search': tSearchTerm,
'language__code': tSearchLanguage,
'limit': '100',
};
tSearchByNameUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$exerciseSearchUrl/',
queryParameters: query,
);
final exerciseInfoEntry =
jsonDecode(
fixture('exercises/exerciseinfo_response.json'),
)
as Map<String, dynamic>;
// Build a paginated exerciseinfo response with two results that
// have the categories used by the test exercises so that the
// category filter test below works correctly.
final Map<String, dynamic> tSearchResponse = {
'count': 2,
'next': null,
'previous': null,
'results': [
{
...exerciseInfoEntry,
'id': 1,
'category': {'id': 1, 'name': 'Arms'},
},
{
...exerciseInfoEntry,
'id': 2,
'category': {'id': 2, 'name': 'Legs'},
},
],
};
// Mock exercise search
when(
mockBaseProvider.makeUrl(
exerciseSearchUrl,
query: {
'name__search': tSearchTerm,
'language__code': tSearchLanguage,
'limit': '100',
},
),
).thenReturn(tSearchByNameUri);
when(mockBaseProvider.fetch(tSearchByNameUri)).thenAnswer((_) async => tSearchResponse);
});
test('Should find results from search term', () async {
// arrange
final Filters tFilters = filters.copyWith(searchTerm: 'press');
// act
await provider.setFilters(tFilters);
// assert
verify(provider.baseProvider.fetch(tSearchByNameUri)).called(1);
expect(provider.filteredExercises.length, 2);
expect(provider.filteredExercises.map((e) => e.id).toList(), [1, 2]);
});
test('Should find items from selection but should filter them by search term', () async {
// arrange
final Filters tFilters = filters.copyWith(
searchTerm: 'press',
exerciseCategories: filters.exerciseCategories.copyWith(items: {data.tCategory3: true}),
);
// act
await provider.setFilters(tFilters);
// assert
verify(provider.baseProvider.fetch(tSearchByNameUri)).called(1);
expect(provider.filteredExercises, isEmpty);
});
});
});
});
group('searchExerciseWithSearchMode', () {
final Uri tFakeSearchUri = Uri(
scheme: 'http',
host: 'localhost',
path: 'api/v2/$exerciseSearchUrl/',
);
setUp(() {
// Generic catch-all stub for the search endpoint — actual query content
// is captured per test via `captureAnyNamed('query')` below.
when(
mockBaseProvider.makeUrl(exerciseSearchUrl, query: anyNamed('query')),
).thenReturn(tFakeSearchUri);
when(
mockBaseProvider.fetch(tFakeSearchUri),
).thenAnswer((_) async => {'results': <dynamic>[]});
});
/// Returns the most recent query map passed to `makeUrl(exerciseSearchUrl)`.
Map<String, dynamic> captureLastQuery() {
return verify(
mockBaseProvider.makeUrl(exerciseSearchUrl, query: captureAnyNamed('query')),
).captured.last
as Map<String, dynamic>;
}
test('returns empty list and skips the API call when name is too short', () async {
final result = await provider.searchExerciseWithSearchMode('a');
expect(result, isEmpty);
verifyNever(mockBaseProvider.makeUrl(exerciseSearchUrl, query: anyNamed('query')));
});
test('fulltext mode sends name__search and not name__exact', () async {
await provider.searchExerciseWithSearchMode('press', languageCode: 'de');
final query = captureLastQuery();
expect(query['name__search'], 'press');
expect(query.containsKey('name__exact'), isFalse);
});
test('exact mode sends name__exact and not name__search', () async {
await provider.searchExerciseWithSearchMode(
'bench press',
languageCode: 'de',
searchMode: ExerciseSearchMode.exact,
);
final query = captureLastQuery();
expect(query['name__exact'], 'bench press');
expect(query.containsKey('name__search'), isFalse);
});
test('searchLanguage current sends only the locale code', () async {
await provider.searchExerciseWithSearchMode(
'press',
languageCode: 'de',
searchLanguage: SearchLanguage.current,
);
expect(captureLastQuery()['language__code'], 'de');
});
test('searchLanguage currentAndEnglish appends en to a non-English locale', () async {
await provider.searchExerciseWithSearchMode(
'press',
languageCode: 'de',
searchLanguage: SearchLanguage.currentAndEnglish,
);
expect(captureLastQuery()['language__code'], 'de,en');
});
test('searchLanguage currentAndEnglish on en locale only sends en (no duplicate)', () async {
await provider.searchExerciseWithSearchMode(
'press',
languageCode: 'en',
searchLanguage: SearchLanguage.currentAndEnglish,
);
expect(captureLastQuery()['language__code'], 'en');
});
test('searchLanguage all omits the language__code param', () async {
await provider.searchExerciseWithSearchMode(
'press',
languageCode: 'de',
searchLanguage: SearchLanguage.all,
);
expect(captureLastQuery().containsKey('language__code'), isFalse);
});
test('empty categories omits the category__in param', () async {
await provider.searchExerciseWithSearchMode('press', languageCode: 'de');
expect(captureLastQuery().containsKey('category__in'), isFalse);
});
test('a single category is sent as category__in with one id', () async {
const abs = ExerciseCategory(id: 1, name: 'Abs');
await provider.searchExerciseWithSearchMode(
'press',
languageCode: 'de',
categories: {abs},
);
expect(captureLastQuery()['category__in'], '1');
});
test('multiple categories are sent as comma-separated ids in category__in', () async {
const abs = ExerciseCategory(id: 1, name: 'Abs');
const arms = ExerciseCategory(id: 2, name: 'Arms');
await provider.searchExerciseWithSearchMode(
'press',
languageCode: 'de',
categories: {abs, arms},
);
// The set is unordered, so check membership rather than the literal string.
final ids = (captureLastQuery()['category__in'] as String).split(',').toSet();
expect(ids, {'1', '2'});
});
test('limit param is always sent', () async {
await provider.searchExerciseWithSearchMode('press', languageCode: 'de');
expect(captureLastQuery()['limit'], '100');
});
});
group('local prefs', () {
test('initCacheTimesLocalPrefs correctly initalises the cache values', () async {
// arrange
const initValue = '2023-01-01T00:00:00.000';
final prefs = PreferenceHelper.asyncPref;
// act
await provider.initCacheTimesLocalPrefs();
// assert
expect(await prefs.getString(PREFS_LAST_UPDATED_MUSCLES), initValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_EQUIPMENT), initValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_CATEGORIES), initValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_LANGUAGES), initValue);
});
test('calling initCacheTimesLocalPrefs again does nothing', () async {
// arrange
final prefs = PreferenceHelper.asyncPref;
const newValue = '2023-10-10T01:18:35.000';
// act
await provider.initCacheTimesLocalPrefs();
await prefs.setString(PREFS_LAST_UPDATED_MUSCLES, newValue);
await prefs.setString(PREFS_LAST_UPDATED_EQUIPMENT, newValue);
await prefs.setString(PREFS_LAST_UPDATED_CATEGORIES, newValue);
await prefs.setString(PREFS_LAST_UPDATED_LANGUAGES, newValue);
await provider.initCacheTimesLocalPrefs();
// Assert
expect(await prefs.getString(PREFS_LAST_UPDATED_MUSCLES), newValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_EQUIPMENT), newValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_CATEGORIES), newValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_LANGUAGES), newValue);
});
test('calling initCacheTimesLocalPrefs with forceInit replaces the date', () async {
// arrange
final prefs = PreferenceHelper.asyncPref;
const initValue = '2023-01-01T00:00:00.000';
const newValue = '2023-10-10T01:18:35.000';
// act
await provider.initCacheTimesLocalPrefs();
await prefs.setString(PREFS_LAST_UPDATED_MUSCLES, newValue);
await prefs.setString(PREFS_LAST_UPDATED_EQUIPMENT, newValue);
await prefs.setString(PREFS_LAST_UPDATED_CATEGORIES, newValue);
await prefs.setString(PREFS_LAST_UPDATED_LANGUAGES, newValue);
await provider.initCacheTimesLocalPrefs(forceInit: true);
// Assert
expect(await prefs.getString(PREFS_LAST_UPDATED_MUSCLES), initValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_EQUIPMENT), initValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_CATEGORIES), initValue);
expect(await prefs.getString(PREFS_LAST_UPDATED_LANGUAGES), initValue);
});
});
}