Fix and cleanup some tests

This commit is contained in:
Roland Geider
2026-03-14 13:18:11 +01:00
parent d38a7609d8
commit e20de66cf0
19 changed files with 1075 additions and 1488 deletions

View File

@@ -1,12 +1,31 @@
/*
* 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/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart' as riverpod;
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/measurements/measurement_category.dart';
import 'package:wger/providers/auth.dart';
import 'package:wger/providers/body_weight_repository.dart';
import 'package:wger/providers/gallery.dart';
import 'package:wger/providers/measurement.dart';
import 'package:wger/providers/measurement_repository.dart';
import 'package:wger/providers/nutrition.dart';
import 'package:wger/providers/routines.dart';
import 'package:wger/providers/user.dart';
@@ -46,8 +65,10 @@ Widget createDashboardScreen({Locale? locale}) {
mockBodyWeightRepository.watchAllDrift(),
).thenAnswer((_) => Stream.value(getWeightEntries()));
final mockMeasurementProvider = MockMeasurementProvider();
when(mockMeasurementProvider.categories).thenReturn(getMeasurementCategories());
final mockMeasurementRepo = MockMeasurementRepository();
when(
mockMeasurementRepo.watchAll(),
).thenAnswer((_) => Stream<List<MeasurementCategory>>.value(getMeasurementCategories()));
final mockUserProvider = MockUserProvider();
when(mockUserProvider.profile).thenReturn(tProfile1);
@@ -55,6 +76,7 @@ Widget createDashboardScreen({Locale? locale}) {
final container = riverpod.ProviderContainer.test(
overrides: [
bodyWeightRepositoryProvider.overrideWithValue(mockBodyWeightRepository),
measurementRepositoryProvider.overrideWithValue(mockMeasurementRepo),
],
);
container.read(routinesRiverpodProvider.notifier).state = RoutinesState(
@@ -85,9 +107,6 @@ Widget createDashboardScreen({Locale? locale}) {
ChangeNotifierProvider<NutritionPlansProvider>(
create: (context) => mockNutritionProvider,
),
ChangeNotifierProvider<MeasurementProvider>(
create: (context) => mockMeasurementProvider,
),
],
child: MaterialApp(
locale: locale,

View File

@@ -1,8 +1,27 @@
/*
* 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/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/providers/measurement.dart';
import 'package:wger/models/measurements/measurement_category.dart';
import 'package:wger/providers/measurement_repository.dart';
import 'package:wger/screens/measurement_categories_screen.dart';
import 'package:wger/theme/theme.dart';
@@ -12,8 +31,16 @@ import '../test_data/measurements.dart';
Widget createMeasurementScreen({Locale? locale}) {
locale ??= const Locale('en');
final mockMeasurementProvider = MockMeasurementProvider();
when(mockMeasurementProvider.categories).thenReturn(getMeasurementCategories());
final mockMeasurementRepo = MockMeasurementRepository();
when(
mockMeasurementRepo.watchAll(),
).thenAnswer((_) => Stream<List<MeasurementCategory>>.value(getMeasurementCategories()));
final container = ProviderContainer.test(
overrides: [
measurementRepositoryProvider.overrideWithValue(mockMeasurementRepo),
],
);
return MediaQuery(
data: MediaQueryData.fromView(WidgetsBinding.instance.platformDispatcher.views.first).copyWith(
@@ -21,12 +48,8 @@ Widget createMeasurementScreen({Locale? locale}) {
viewPadding: EdgeInsets.zero,
viewInsets: EdgeInsets.zero,
),
child: MultiProvider(
providers: [
ChangeNotifierProvider<MeasurementProvider>(
create: (context) => mockMeasurementProvider,
),
],
child: UncontrolledProviderScope(
container: container,
child: MaterialApp(
locale: locale,
debugShowCheckedModeBanner: false,

View File

@@ -88,7 +88,7 @@ class RepetitionInputWidget extends ConsumerStatefulWidget {
}
class _RepetitionInputWidgetState extends ConsumerState<RepetitionInputWidget> {
final _logger = Logger('LogsRepsWidget');
final _logger = Logger('RepetitionUnitInputWidget');
late TextEditingController _controller;
num? _lastReps;

View File

@@ -86,7 +86,7 @@ class WeightInputWidget extends ConsumerStatefulWidget {
}
class _WeightInputWidgetState extends ConsumerState<WeightInputWidget> {
final _logger = Logger('LogsWeightWidget');
final _logger = Logger('WeightInputWidget');
late TextEditingController _controller;
num? _lastWeight;

View File

@@ -17,74 +17,31 @@
*/
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:provider/provider.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/measurements/measurement_category.dart';
import 'package:wger/models/measurements/measurement_entry.dart';
import 'package:wger/providers/measurement.dart';
import 'package:wger/providers/measurement_repository.dart';
import 'package:wger/screens/measurement_categories_screen.dart';
import 'package:wger/widgets/measurements/charts.dart';
import 'measurement_categories_screen_test.mocks.dart';
import '../../test_data/measurements.dart';
import 'measurement_provider_test.mocks.dart';
@GenerateMocks([MeasurementProvider])
@GenerateMocks([MeasurementRepository])
void main() {
late MeasurementProvider mockMeasurementProvider;
Widget createMeasurementScreen({locale = 'en'}) {
final mockRepo = MockMeasurementRepository();
when(
mockRepo.watchAll(),
).thenAnswer((_) => Stream<List<MeasurementCategory>>.value(getMeasurementCategories()));
setUp(() {
mockMeasurementProvider = MockMeasurementProvider();
when(mockMeasurementProvider.categories).thenReturn([
MeasurementCategory(
uuid: 1,
name: 'body fat',
unit: '%',
entries: [
MeasurementEntry(
uuid: 1,
categoryId: 1,
date: DateTime(2021, 9, 1),
value: 10,
notes: '',
),
MeasurementEntry(
uuid: 2,
categoryId: 1,
date: DateTime(2021, 9, 5),
value: 11,
notes: '',
),
],
),
MeasurementCategory(
uuid: 2,
name: 'biceps',
unit: 'cm',
entries: [
MeasurementEntry(
uuid: 3,
categoryId: 2,
date: DateTime(2021, 9, 1),
value: 30,
notes: '',
),
MeasurementEntry(
uuid: 4,
categoryId: 2,
date: DateTime(2021, 9, 5),
value: 40,
notes: '',
),
],
),
]);
});
Widget createHomeScreen({locale = 'en'}) {
return ChangeNotifierProvider<MeasurementProvider>(
create: (context) => mockMeasurementProvider,
return ProviderScope(
overrides: [
measurementRepositoryProvider.overrideWithValue(mockRepo),
],
child: MaterialApp(
locale: Locale(locale),
localizationsDelegates: AppLocalizations.localizationsDelegates,
@@ -95,12 +52,12 @@ void main() {
}
testWidgets('Test the widgets on the measurement category screen', (WidgetTester tester) async {
await tester.pumpWidget(createHomeScreen());
await tester.pumpWidget(createMeasurementScreen());
await tester.pumpAndSettle();
expect(find.text('Measurements'), findsOneWidget);
expect(find.text('body fat'), findsOneWidget);
expect(find.text('biceps'), findsOneWidget);
expect(find.text('Body fat'), findsOneWidget);
expect(find.text('Biceps'), findsOneWidget);
expect(find.byType(Card), findsNWidgets(2));
expect(find.byType(MeasurementChartWidgetFl), findsNWidgets(2));
});

View File

@@ -3,14 +3,12 @@
// Do not manually edit this file.
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i5;
import 'dart:ui' as _i7;
import 'dart:async' as _i3;
import 'package:mockito/mockito.dart' as _i1;
import 'package:wger/models/measurements/measurement_category.dart' as _i3;
import 'package:wger/models/measurements/measurement_entry.dart' as _i6;
import 'package:wger/providers/base_provider.dart' as _i2;
import 'package:wger/providers/measurement.dart' as _i4;
import 'package:wger/models/measurements/measurement_category.dart' as _i4;
import 'package:wger/models/measurements/measurement_entry.dart' as _i5;
import 'package:wger/providers/measurement_repository.dart' as _i2;
// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
@@ -27,178 +25,85 @@ import 'package:wger/providers/measurement.dart' as _i4;
// ignore_for_file: subtype_of_sealed_class
// ignore_for_file: invalid_use_of_internal_member
class _FakeWgerBaseProvider_0 extends _i1.SmartFake implements _i2.WgerBaseProvider {
_FakeWgerBaseProvider_0(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeMeasurementCategory_1 extends _i1.SmartFake implements _i3.MeasurementCategory {
_FakeMeasurementCategory_1(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
/// A class which mocks [MeasurementProvider].
/// A class which mocks [MeasurementRepository].
///
/// See the documentation for Mockito's code generation for more information.
class MockMeasurementProvider extends _i1.Mock implements _i4.MeasurementProvider {
MockMeasurementProvider() {
class MockMeasurementRepository extends _i1.Mock implements _i2.MeasurementRepository {
MockMeasurementRepository() {
_i1.throwOnMissingStub(this);
}
@override
_i2.WgerBaseProvider get baseProvider =>
_i3.Stream<List<_i4.MeasurementCategory>> watchAll() =>
(super.noSuchMethod(
Invocation.getter(#baseProvider),
returnValue: _FakeWgerBaseProvider_0(
this,
Invocation.getter(#baseProvider),
),
Invocation.method(#watchAll, []),
returnValue: _i3.Stream<List<_i4.MeasurementCategory>>.empty(),
)
as _i2.WgerBaseProvider);
as _i3.Stream<List<_i4.MeasurementCategory>>);
@override
List<_i3.MeasurementCategory> get categories =>
(super.noSuchMethod(
Invocation.getter(#categories),
returnValue: <_i3.MeasurementCategory>[],
)
as List<_i3.MeasurementCategory>);
@override
bool get hasListeners =>
(super.noSuchMethod(Invocation.getter(#hasListeners), returnValue: false) as bool);
@override
void clear() => super.noSuchMethod(
Invocation.method(#clear, []),
returnValueForMissingStub: null,
);
@override
_i3.MeasurementCategory findCategoryById(int? id) =>
(super.noSuchMethod(
Invocation.method(#findCategoryById, [id]),
returnValue: _FakeMeasurementCategory_1(
this,
Invocation.method(#findCategoryById, [id]),
),
)
as _i3.MeasurementCategory);
@override
_i5.Future<void> fetchAndSetCategories() =>
(super.noSuchMethod(
Invocation.method(#fetchAndSetCategories, []),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> fetchAndSetCategoryEntries(int? id) =>
(super.noSuchMethod(
Invocation.method(#fetchAndSetCategoryEntries, [id]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> fetchAndSetAllCategoriesAndEntries() =>
(super.noSuchMethod(
Invocation.method(#fetchAndSetAllCategoriesAndEntries, []),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> addCategory(_i3.MeasurementCategory? category) =>
(super.noSuchMethod(
Invocation.method(#addCategory, [category]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> deleteCategory(int? id) =>
(super.noSuchMethod(
Invocation.method(#deleteCategory, [id]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> editCategory(int? id, String? newName, String? newUnit) =>
(super.noSuchMethod(
Invocation.method(#editCategory, [id, newName, newUnit]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> addEntry(_i6.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#addEntry, [entry]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> deleteEntry(int? id, int? categoryId) =>
(super.noSuchMethod(
Invocation.method(#deleteEntry, [id, categoryId]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> editEntry(
int? id,
int? categoryId,
num? newValue,
String? newNotes,
DateTime? newDate,
_i3.Stream<_i4.MeasurementCategory?> watchLocalDriftCategoryByUuid(
String? uuid,
) =>
(super.noSuchMethod(
Invocation.method(#editEntry, [
id,
categoryId,
newValue,
newNotes,
newDate,
]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
Invocation.method(#watchLocalDriftCategoryByUuid, [uuid]),
returnValue: _i3.Stream<_i4.MeasurementCategory?>.empty(),
)
as _i5.Future<void>);
as _i3.Stream<_i4.MeasurementCategory?>);
@override
void addListener(_i7.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(#addListener, [listener]),
returnValueForMissingStub: null,
);
_i3.Future<void> deleteLocalDrift(String? uuid) =>
(super.noSuchMethod(
Invocation.method(#deleteLocalDrift, [uuid]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
void removeListener(_i7.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(#removeListener, [listener]),
returnValueForMissingStub: null,
);
_i3.Future<void> updateLocalDrift(_i5.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDrift, [entry]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
void dispose() => super.noSuchMethod(
Invocation.method(#dispose, []),
returnValueForMissingStub: null,
);
_i3.Future<void> addLocalDrift(_i5.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#addLocalDrift, [entry]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
void notifyListeners() => super.noSuchMethod(
Invocation.method(#notifyListeners, []),
returnValueForMissingStub: null,
);
_i3.Future<void> deleteLocalDriftCategory(String? uuid) =>
(super.noSuchMethod(
Invocation.method(#deleteLocalDriftCategory, [uuid]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> updateLocalDriftCategory(
_i4.MeasurementCategory? category,
) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDriftCategory, [category]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> addLocalDriftCategory(_i4.MeasurementCategory? category) =>
(super.noSuchMethod(
Invocation.method(#addLocalDriftCategory, [category]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
}

View File

@@ -19,115 +19,28 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:wger/core/exceptions/no_such_entry_exception.dart';
import 'package:wger/models/measurements/measurement_category.dart';
import 'package:wger/models/measurements/measurement_entry.dart';
import '../../test_data/measurements.dart';
void main() {
final List<MeasurementEntry> tMeasurementEntries = [
MeasurementEntry(
uuid: 1234,
categoryId: 123,
date: DateTime(2021, 7, 22),
value: 83,
notes: 'notes',
),
];
late MeasurementCategory category;
final MeasurementEntry tMeasurementEntry = MeasurementEntry(
uuid: 1234,
categoryId: 123,
date: DateTime(2021, 7, 22),
value: 83,
notes: 'notes',
);
const int tMeasurementEntryId = 1234;
final MeasurementCategory tMeasurementCategory = MeasurementCategory(
uuid: 123,
name: 'Bizeps',
unit: 'cm',
entries: tMeasurementEntries,
);
final Map<String, dynamic> tMeasurementEntryMap = {
'id': 1234,
'category': 123,
'date': '2021-07-22',
'value': 83,
'notes': 'notes',
};
final Map<String, dynamic> tMeasurementCategoryMap = {
'id': 123,
'name': 'Bizeps',
'unit': 'cm',
'entries': [tMeasurementEntryMap],
};
final Map<String, dynamic> tMeasurementCategoryMaptoJson = {
'id': 123,
'name': 'Bizeps',
'unit': 'cm',
'entries': null,
};
group('fromJson()', () {
test('should convert a JSON map to a MeasurementCategory object', () {
// act
final result = MeasurementCategory.fromJson(tMeasurementCategoryMap);
// assert
expect(result, tMeasurementCategory);
});
});
group('toJson()', () {
test('should convert a MeasurementCategory object to a JSON map', () {
// act
final result = tMeasurementCategory.toJson();
// assert
expect(result, tMeasurementCategoryMaptoJson);
});
});
group('copyWith()', () {
test('should copyWith objects of this class', () {
// arrange
final MeasurementCategory tMeasurementCategoryCopied = MeasurementCategory(
uuid: 1234,
name: 'Coolness',
unit: 'lp',
entries: tMeasurementEntries,
);
// act
final result = tMeasurementCategory.copyWith(
uuid: 1234,
name: 'Coolness',
unit: 'lp',
entries: tMeasurementEntries,
);
// assert
expect(result, tMeasurementCategoryCopied);
});
setUp(() {
category = getMeasurementCategories()[0];
});
group('findEntryById()', () {
test('should find an entry in the entries list', () {
// arrange
// act
final result = tMeasurementCategory.findEntryByUuid(tMeasurementEntryId);
final result = category.findEntryByUuid('1');
// assert
expect(result, tMeasurementEntry);
expect(result.id, 1);
});
test('should throw a NoSuchEntryException if no MeasurementEntry was found', () {
// act & assert
expect(() => tMeasurementCategory.findEntryByUuid(83), throwsA(isA<NoSuchEntryException>()));
expect(() => category.findEntryByUuid('abc'), throwsA(isA<NoSuchEntryException>()));
});
});
}

View File

@@ -17,61 +17,45 @@
*/
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:provider/provider.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/measurements/measurement_category.dart';
import 'package:wger/models/measurements/measurement_entry.dart';
import 'package:wger/providers/measurement.dart';
import 'package:wger/providers/measurement_repository.dart';
import 'package:wger/providers/nutrition.dart';
import 'package:wger/screens/measurement_entries_screen.dart';
import '../nutrition/nutritional_plan_form_test.mocks.dart';
import 'measurement_categories_screen_test.mocks.dart';
import '../../test_data/measurements.dart';
import 'measurement_entries_screen_test.mocks.dart';
@GenerateMocks([MeasurementRepository, NutritionPlansProvider])
void main() {
late MockMeasurementProvider mockMeasurementProvider;
late MockMeasurementRepository mockMeasurementRepo;
late MockNutritionPlansProvider mockNutritionPlansProvider;
setUp(() {
mockMeasurementProvider = MockMeasurementProvider();
when(mockMeasurementProvider.findCategoryById(any)).thenReturn(
MeasurementCategory(
uuid: 1,
name: 'body fat',
unit: '%',
entries: [
MeasurementEntry(
uuid: 1,
categoryId: 1,
date: DateTime(2021, 8, 1),
value: 10.2,
notes: '',
),
MeasurementEntry(
uuid: 1,
categoryId: 1,
date: DateTime(2021, 8, 10),
value: 18.1,
notes: 'a',
),
],
),
);
mockMeasurementRepo = MockMeasurementRepository();
when(
mockMeasurementRepo.watchLocalDriftCategoryByUuid(any),
).thenAnswer((_) => Stream<MeasurementCategory>.value(getMeasurementCategories()[0]));
mockNutritionPlansProvider = MockNutritionPlansProvider();
when(mockNutritionPlansProvider.currentPlan).thenReturn(null);
when(mockNutritionPlansProvider.items).thenReturn([]);
});
Widget createHomeScreen({locale = 'en'}) {
Widget createEntriesScreen({locale = 'en'}) {
final key = GlobalKey<NavigatorState>();
return ChangeNotifierProvider<NutritionPlansProvider>(
create: (context) => mockNutritionPlansProvider,
child: ChangeNotifierProvider<MeasurementProvider>(
create: (context) => mockMeasurementProvider,
return ProviderScope(
overrides: [
measurementRepositoryProvider.overrideWithValue(mockMeasurementRepo),
],
child: ChangeNotifierProvider<NutritionPlansProvider>(
create: (context) => mockNutritionPlansProvider,
child: MaterialApp(
locale: Locale(locale),
localizationsDelegates: AppLocalizations.localizationsDelegates,
@@ -80,7 +64,7 @@ void main() {
home: TextButton(
onPressed: () => key.currentState!.push(
MaterialPageRoute<void>(
settings: const RouteSettings(arguments: 1),
settings: const RouteSettings(arguments: '1'),
builder: (_) => const MeasurementEntriesScreen(),
),
),
@@ -92,32 +76,34 @@ void main() {
}
testWidgets('Test the widgets on the measurement entries screen', (WidgetTester tester) async {
await tester.pumpWidget(createHomeScreen());
await tester.pumpWidget(createEntriesScreen());
await tester.tap(find.byType(TextButton));
await tester.pumpAndSettle();
// Nav bar
expect(find.text('body fat'), findsOneWidget);
expect(find.text('Body fat'), findsOneWidget);
// Entries
expect(find.text('15 %'), findsNWidgets(1));
expect(find.text('30 %'), findsNWidgets(1));
});
testWidgets('Tests the localization of dates - EN', (WidgetTester tester) async {
await tester.pumpWidget(createHomeScreen());
await tester.pumpWidget(createEntriesScreen());
await tester.tap(find.byType(TextButton));
await tester.pumpAndSettle();
// From the entries list and from the chart
expect(find.text('8/1/2021'), findsWidgets);
expect(find.text('8/10/2021'), findsWidgets);
expect(find.text('9/10/2022'), findsWidgets);
expect(find.text('10/5/2022'), findsWidgets);
});
testWidgets('Tests the localization of dates - DE', (WidgetTester tester) async {
await tester.pumpWidget(createHomeScreen(locale: 'de'));
await tester.pumpWidget(createEntriesScreen(locale: 'de'));
await tester.tap(find.byType(TextButton));
await tester.pumpAndSettle();
expect(find.text('1.8.2021'), findsWidgets);
expect(find.text('10.8.2021'), findsWidgets);
// From the entries list and from the chart
expect(find.text('10.9.2022'), findsWidgets);
expect(find.text('5.10.2022'), findsWidgets);
});
}

View File

@@ -0,0 +1,516 @@
// Mocks generated by Mockito 5.4.6 from annotations
// in wger/test/measurements/measurement_entries_screen_test.dart.
// Do not manually edit this file.
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i9;
import 'dart:ui' as _i13;
import 'package:mockito/mockito.dart' as _i1;
import 'package:wger/database/ingredients/ingredients_database.dart' as _i3;
import 'package:wger/models/measurements/measurement_category.dart' as _i10;
import 'package:wger/models/measurements/measurement_entry.dart' as _i11;
import 'package:wger/models/nutrition/ingredient.dart' as _i7;
import 'package:wger/models/nutrition/meal.dart' as _i5;
import 'package:wger/models/nutrition/meal_item.dart' as _i6;
import 'package:wger/models/nutrition/nutritional_plan.dart' as _i4;
import 'package:wger/providers/base_provider.dart' as _i2;
import 'package:wger/providers/measurement_repository.dart' as _i8;
import 'package:wger/providers/nutrition.dart' as _i12;
// 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
class _FakeWgerBaseProvider_0 extends _i1.SmartFake implements _i2.WgerBaseProvider {
_FakeWgerBaseProvider_0(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeIngredientDatabase_1 extends _i1.SmartFake implements _i3.IngredientDatabase {
_FakeIngredientDatabase_1(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeNutritionalPlan_2 extends _i1.SmartFake implements _i4.NutritionalPlan {
_FakeNutritionalPlan_2(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}
class _FakeMeal_3 extends _i1.SmartFake implements _i5.Meal {
_FakeMeal_3(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
class _FakeMealItem_4 extends _i1.SmartFake implements _i6.MealItem {
_FakeMealItem_4(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
class _FakeIngredient_5 extends _i1.SmartFake implements _i7.Ingredient {
_FakeIngredient_5(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
/// A class which mocks [MeasurementRepository].
///
/// See the documentation for Mockito's code generation for more information.
class MockMeasurementRepository extends _i1.Mock implements _i8.MeasurementRepository {
MockMeasurementRepository() {
_i1.throwOnMissingStub(this);
}
@override
_i9.Stream<List<_i10.MeasurementCategory>> watchAll() =>
(super.noSuchMethod(
Invocation.method(#watchAll, []),
returnValue: _i9.Stream<List<_i10.MeasurementCategory>>.empty(),
)
as _i9.Stream<List<_i10.MeasurementCategory>>);
@override
_i9.Stream<_i10.MeasurementCategory?> watchLocalDriftCategoryByUuid(
String? uuid,
) =>
(super.noSuchMethod(
Invocation.method(#watchLocalDriftCategoryByUuid, [uuid]),
returnValue: _i9.Stream<_i10.MeasurementCategory?>.empty(),
)
as _i9.Stream<_i10.MeasurementCategory?>);
@override
_i9.Future<void> deleteLocalDrift(String? uuid) =>
(super.noSuchMethod(
Invocation.method(#deleteLocalDrift, [uuid]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> updateLocalDrift(_i11.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDrift, [entry]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> addLocalDrift(_i11.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#addLocalDrift, [entry]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> deleteLocalDriftCategory(String? uuid) =>
(super.noSuchMethod(
Invocation.method(#deleteLocalDriftCategory, [uuid]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> updateLocalDriftCategory(
_i10.MeasurementCategory? category,
) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDriftCategory, [category]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> addLocalDriftCategory(_i10.MeasurementCategory? category) =>
(super.noSuchMethod(
Invocation.method(#addLocalDriftCategory, [category]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
}
/// A class which mocks [NutritionPlansProvider].
///
/// See the documentation for Mockito's code generation for more information.
class MockNutritionPlansProvider extends _i1.Mock implements _i12.NutritionPlansProvider {
MockNutritionPlansProvider() {
_i1.throwOnMissingStub(this);
}
@override
_i2.WgerBaseProvider get baseProvider =>
(super.noSuchMethod(
Invocation.getter(#baseProvider),
returnValue: _FakeWgerBaseProvider_0(
this,
Invocation.getter(#baseProvider),
),
)
as _i2.WgerBaseProvider);
@override
_i3.IngredientDatabase get database =>
(super.noSuchMethod(
Invocation.getter(#database),
returnValue: _FakeIngredientDatabase_1(
this,
Invocation.getter(#database),
),
)
as _i3.IngredientDatabase);
@override
List<_i7.Ingredient> get ingredients =>
(super.noSuchMethod(
Invocation.getter(#ingredients),
returnValue: <_i7.Ingredient>[],
)
as List<_i7.Ingredient>);
@override
List<_i4.NutritionalPlan> get items =>
(super.noSuchMethod(
Invocation.getter(#items),
returnValue: <_i4.NutritionalPlan>[],
)
as List<_i4.NutritionalPlan>);
@override
set database(_i3.IngredientDatabase? value) => super.noSuchMethod(
Invocation.setter(#database, value),
returnValueForMissingStub: null,
);
@override
set ingredients(List<_i7.Ingredient>? value) => super.noSuchMethod(
Invocation.setter(#ingredients, value),
returnValueForMissingStub: null,
);
@override
bool get hasListeners =>
(super.noSuchMethod(Invocation.getter(#hasListeners), returnValue: false) as bool);
@override
void clear() => super.noSuchMethod(
Invocation.method(#clear, []),
returnValueForMissingStub: null,
);
@override
_i4.NutritionalPlan findById(int? id) =>
(super.noSuchMethod(
Invocation.method(#findById, [id]),
returnValue: _FakeNutritionalPlan_2(
this,
Invocation.method(#findById, [id]),
),
)
as _i4.NutritionalPlan);
@override
_i5.Meal? findMealById(int? id) =>
(super.noSuchMethod(Invocation.method(#findMealById, [id])) as _i5.Meal?);
@override
_i9.Future<void> fetchAndSetAllPlansSparse() =>
(super.noSuchMethod(
Invocation.method(#fetchAndSetAllPlansSparse, []),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> fetchAndSetAllPlansFull() =>
(super.noSuchMethod(
Invocation.method(#fetchAndSetAllPlansFull, []),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<_i4.NutritionalPlan> fetchAndSetPlanSparse(int? planId) =>
(super.noSuchMethod(
Invocation.method(#fetchAndSetPlanSparse, [planId]),
returnValue: _i9.Future<_i4.NutritionalPlan>.value(
_FakeNutritionalPlan_2(
this,
Invocation.method(#fetchAndSetPlanSparse, [planId]),
),
),
)
as _i9.Future<_i4.NutritionalPlan>);
@override
_i9.Future<_i4.NutritionalPlan> fetchAndSetPlanFull(int? planId) =>
(super.noSuchMethod(
Invocation.method(#fetchAndSetPlanFull, [planId]),
returnValue: _i9.Future<_i4.NutritionalPlan>.value(
_FakeNutritionalPlan_2(
this,
Invocation.method(#fetchAndSetPlanFull, [planId]),
),
),
)
as _i9.Future<_i4.NutritionalPlan>);
@override
_i9.Future<_i4.NutritionalPlan> addPlan(_i4.NutritionalPlan? planData) =>
(super.noSuchMethod(
Invocation.method(#addPlan, [planData]),
returnValue: _i9.Future<_i4.NutritionalPlan>.value(
_FakeNutritionalPlan_2(
this,
Invocation.method(#addPlan, [planData]),
),
),
)
as _i9.Future<_i4.NutritionalPlan>);
@override
_i9.Future<void> editPlan(_i4.NutritionalPlan? plan) =>
(super.noSuchMethod(
Invocation.method(#editPlan, [plan]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> deletePlan(int? id) =>
(super.noSuchMethod(
Invocation.method(#deletePlan, [id]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<_i5.Meal> addMeal(_i5.Meal? meal, int? planId) =>
(super.noSuchMethod(
Invocation.method(#addMeal, [meal, planId]),
returnValue: _i9.Future<_i5.Meal>.value(
_FakeMeal_3(this, Invocation.method(#addMeal, [meal, planId])),
),
)
as _i9.Future<_i5.Meal>);
@override
_i9.Future<_i5.Meal> editMeal(_i5.Meal? meal) =>
(super.noSuchMethod(
Invocation.method(#editMeal, [meal]),
returnValue: _i9.Future<_i5.Meal>.value(
_FakeMeal_3(this, Invocation.method(#editMeal, [meal])),
),
)
as _i9.Future<_i5.Meal>);
@override
_i9.Future<void> deleteMeal(_i5.Meal? meal) =>
(super.noSuchMethod(
Invocation.method(#deleteMeal, [meal]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<_i6.MealItem> addMealItem(
_i6.MealItem? mealItem,
_i5.Meal? meal,
) =>
(super.noSuchMethod(
Invocation.method(#addMealItem, [mealItem, meal]),
returnValue: _i9.Future<_i6.MealItem>.value(
_FakeMealItem_4(
this,
Invocation.method(#addMealItem, [mealItem, meal]),
),
),
)
as _i9.Future<_i6.MealItem>);
@override
_i9.Future<void> deleteMealItem(_i6.MealItem? mealItem) =>
(super.noSuchMethod(
Invocation.method(#deleteMealItem, [mealItem]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> clearIngredientCache() =>
(super.noSuchMethod(
Invocation.method(#clearIngredientCache, []),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> cacheIngredient(
_i7.Ingredient? ingredient, {
_i3.IngredientDatabase? database,
}) =>
(super.noSuchMethod(
Invocation.method(
#cacheIngredient,
[ingredient],
{#database: database},
),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<_i7.Ingredient> fetchIngredient(
int? ingredientId, {
_i3.IngredientDatabase? database,
}) =>
(super.noSuchMethod(
Invocation.method(
#fetchIngredient,
[ingredientId],
{#database: database},
),
returnValue: _i9.Future<_i7.Ingredient>.value(
_FakeIngredient_5(
this,
Invocation.method(
#fetchIngredient,
[ingredientId],
{#database: database},
),
),
),
)
as _i9.Future<_i7.Ingredient>);
@override
_i9.Future<void> fetchIngredientsFromCache() =>
(super.noSuchMethod(
Invocation.method(#fetchIngredientsFromCache, []),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<List<_i7.Ingredient>> searchIngredient(
String? name, {
String? languageCode = 'en',
_i12.IngredientSearchLanguage? searchLanguage = _i12.IngredientSearchLanguage.current,
bool? isVegan = false,
bool? isVegetarian = false,
}) =>
(super.noSuchMethod(
Invocation.method(
#searchIngredient,
[name],
{
#languageCode: languageCode,
#searchLanguage: searchLanguage,
#isVegan: isVegan,
#isVegetarian: isVegetarian,
},
),
returnValue: _i9.Future<List<_i7.Ingredient>>.value(
<_i7.Ingredient>[],
),
)
as _i9.Future<List<_i7.Ingredient>>);
@override
_i9.Future<_i7.Ingredient?> searchIngredientWithBarcode(String? barcode) =>
(super.noSuchMethod(
Invocation.method(#searchIngredientWithBarcode, [barcode]),
returnValue: _i9.Future<_i7.Ingredient?>.value(),
)
as _i9.Future<_i7.Ingredient?>);
@override
_i9.Future<void> logMealToDiary(_i5.Meal? meal, DateTime? mealDateTime) =>
(super.noSuchMethod(
Invocation.method(#logMealToDiary, [meal, mealDateTime]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> logIngredientToDiary(
_i6.MealItem? mealItem,
int? planId, [
DateTime? dateTime,
]) =>
(super.noSuchMethod(
Invocation.method(#logIngredientToDiary, [
mealItem,
planId,
dateTime,
]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> deleteLog(int? logId, int? planId) =>
(super.noSuchMethod(
Invocation.method(#deleteLog, [logId, planId]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
_i9.Future<void> fetchAndSetLogs(_i4.NutritionalPlan? plan) =>
(super.noSuchMethod(
Invocation.method(#fetchAndSetLogs, [plan]),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
)
as _i9.Future<void>);
@override
void addListener(_i13.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(#addListener, [listener]),
returnValueForMissingStub: null,
);
@override
void removeListener(_i13.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(#removeListener, [listener]),
returnValueForMissingStub: null,
);
@override
void dispose() => super.noSuchMethod(
Invocation.method(#dispose, []),
returnValueForMissingStub: null,
);
@override
void notifyListeners() => super.noSuchMethod(
Invocation.method(#notifyListeners, []),
returnValueForMissingStub: null,
);
}

View File

@@ -1,105 +0,0 @@
/*
* 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_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:wger/providers/measurement_notifier.dart';
import 'package:wger/providers/measurement_repository.dart';
import '../../test_data/measurements.dart';
import 'measurement_entry_notifier_test.mocks.dart';
@GenerateMocks([MeasurementRepository])
void main() {
late MockMeasurementRepository mockRepo;
late ProviderContainer container;
setUp(() {
mockRepo = MockMeasurementRepository();
// Default stubs
when(mockRepo.watchAll()).thenAnswer((_) => Stream.value([]));
when(mockRepo.deleteLocalDrift(any)).thenAnswer((_) async {});
when(mockRepo.updateLocalDrift(any)).thenAnswer((_) async {});
when(mockRepo.addLocalDrift(any)).thenAnswer((_) async {});
when(mockRepo.deleteLocalDriftCategory(any)).thenAnswer((_) async {});
when(mockRepo.updateLocalDriftCategory(any)).thenAnswer((_) async {});
when(mockRepo.addLocalDriftCategory(any)).thenAnswer((_) async {});
container = ProviderContainer(
overrides: [
measurementRepositoryProvider.overrideWithValue(mockRepo),
],
);
});
group('MeasurementProvider', () {
// Entries
test('deleteEntry calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
await notifier.deleteEntry('123');
verify(mockRepo.deleteLocalDrift('123')).called(1);
});
test('updateEntry calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
await notifier.updateEntry(testMeasurementEntry1);
verify(mockRepo.updateLocalDrift(testMeasurementEntry1)).called(1);
});
test('addEntry calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
await notifier.addEntry(testMeasurementEntry1);
verify(mockRepo.addLocalDrift(testMeasurementEntry1)).called(1);
});
// Categories
test('deleteCategory calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
await notifier.deleteCategory('cat1');
verify(mockRepo.deleteLocalDriftCategory('cat1')).called(1);
});
test('updateCategory calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
final category = getMeasurementCategories().first;
await notifier.updateCategory(category);
verify(mockRepo.updateLocalDriftCategory(category)).called(1);
});
test('addCategory calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
final category = getMeasurementCategories().first;
await notifier.addCategory(category);
verify(mockRepo.addLocalDriftCategory(category)).called(1);
});
test('build watches repository stream', () async {
container.read(measurementProvider);
verify(mockRepo.watchAll()).called(1);
});
});
}

View File

@@ -1,109 +0,0 @@
// Mocks generated by Mockito 5.4.6 from annotations
// in wger/test/measurements/measurement_entry_notifier_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/models/measurements/measurement_category.dart' as _i4;
import 'package:wger/models/measurements/measurement_entry.dart' as _i5;
import 'package:wger/providers/measurement_repository.dart' as _i2;
// 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 [MeasurementRepository].
///
/// See the documentation for Mockito's code generation for more information.
class MockMeasurementRepository extends _i1.Mock implements _i2.MeasurementRepository {
MockMeasurementRepository() {
_i1.throwOnMissingStub(this);
}
@override
_i3.Stream<List<_i4.MeasurementCategory>> watchAll() =>
(super.noSuchMethod(
Invocation.method(#watchAll, []),
returnValue: _i3.Stream<List<_i4.MeasurementCategory>>.empty(),
)
as _i3.Stream<List<_i4.MeasurementCategory>>);
@override
_i3.Stream<_i4.MeasurementCategory?> watchLocalDriftCategoryByUuid(
String? uuid,
) =>
(super.noSuchMethod(
Invocation.method(#watchLocalDriftCategoryByUuid, [uuid]),
returnValue: _i3.Stream<_i4.MeasurementCategory?>.empty(),
)
as _i3.Stream<_i4.MeasurementCategory?>);
@override
_i3.Future<void> deleteLocalDrift(String? uuid) =>
(super.noSuchMethod(
Invocation.method(#deleteLocalDrift, [uuid]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> updateLocalDrift(_i5.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDrift, [entry]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> addLocalDrift(_i5.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#addLocalDrift, [entry]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> deleteLocalDriftCategory(String? uuid) =>
(super.noSuchMethod(
Invocation.method(#deleteLocalDriftCategory, [uuid]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> updateLocalDriftCategory(
_i4.MeasurementCategory? category,
) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDriftCategory, [category]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> addLocalDriftCategory(_i4.MeasurementCategory? category) =>
(super.noSuchMethod(
Invocation.method(#addLocalDriftCategory, [category]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
}

View File

@@ -1,78 +0,0 @@
/*
* 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:wger/models/measurements/measurement_entry.dart';
void main() {
final MeasurementEntry tMeasurementEntry = MeasurementEntry(
uuid: 1234,
categoryId: 123,
date: DateTime(2021, 7, 22),
value: 83,
notes: 'notes',
);
final Map<String, dynamic> tMeasurementEntryMap = {
'id': 1234,
'category': 123,
'date': '2021-07-22',
'value': 83,
'notes': 'notes',
};
test('should convert a JSON map to a MeasurementEntry object', () {
// act
final result = MeasurementEntry.fromJson(tMeasurementEntryMap);
// assert
expect(result, tMeasurementEntry);
});
test('should convert a MeasurementEntry object to a JSON map', () {
// act
final result = tMeasurementEntry.toJson();
// assert
expect(result, tMeasurementEntryMap);
});
test('should copyWith objects of this class', () {
// arrange
final MeasurementEntry tMeasurementEntryCopied = MeasurementEntry(
uuid: 83,
categoryId: 17,
date: DateTime(1960),
value: 93,
notes: 'Interesting',
);
// act
final result = tMeasurementEntry.copyWith(
uuid: 83,
category: 17,
date: DateTime(1960),
value: 93,
notes: 'Interesting',
);
// assert
expect(result, tMeasurementEntryCopied);
});
}

View File

@@ -16,671 +16,90 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:convert';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:wger/core/exceptions/http_exception.dart';
import 'package:wger/core/exceptions/no_such_entry_exception.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/models/measurements/measurement_category.dart';
import 'package:wger/models/measurements/measurement_entry.dart';
import 'package:wger/providers/base_provider.dart';
import 'package:wger/providers/measurement.dart';
import 'package:wger/providers/measurement_notifier.dart';
import 'package:wger/providers/measurement_repository.dart';
import '../fixtures/fixture_reader.dart';
import '../../test_data/measurements.dart';
import 'measurement_provider_test.mocks.dart';
@GenerateMocks([WgerBaseProvider])
@GenerateMocks([MeasurementRepository])
void main() {
late MeasurementProvider measurementProvider;
late MockWgerBaseProvider mockWgerBaseProvider;
const String categoryUrl = 'measurement-category';
const String entryUrl = 'measurement';
final Uri tCategoryUri = Uri();
final Map<String, dynamic> tMeasurementCategoryMap = jsonDecode(
fixture('measurement/measurement_category_entries.json'),
);
final Uri tCategoryEntriesUri = Uri(
scheme: 'http',
host: 'tedmosbyisajerk.com',
path: 'api/v2/$entryUrl/',
query: 'category=1',
);
const int tCategoryId = 1;
const MeasurementCategory tMeasurementCategory = MeasurementCategory(
uuid: 1,
name: 'Strength',
unit: 'kN',
);
final List<MeasurementCategory> tMeasurementCategories = [
const MeasurementCategory(uuid: 1, name: 'Strength', unit: 'kN'),
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
];
final Map<String, dynamic> tMeasurementCategoriesMap = jsonDecode(
fixture('measurement/measurement_categories.json'),
);
late MockMeasurementRepository mockRepo;
late ProviderContainer container;
setUp(() {
mockWgerBaseProvider = MockWgerBaseProvider();
measurementProvider = MeasurementProvider(mockWgerBaseProvider);
mockRepo = MockMeasurementRepository();
when(mockWgerBaseProvider.makeUrl(any)).thenReturn(tCategoryUri);
when(
mockWgerBaseProvider.makeUrl(any, id: anyNamed('id'), query: anyNamed('query')),
).thenReturn(tCategoryUri);
when(
mockWgerBaseProvider.fetchPaginated(any),
).thenAnswer((realInvocation) => Future.value(tMeasurementCategoriesMap['results']));
// Default stubs
when(mockRepo.watchAll()).thenAnswer((_) => Stream.value([]));
when(mockRepo.deleteLocalDrift(any)).thenAnswer((_) async {});
when(mockRepo.updateLocalDrift(any)).thenAnswer((_) async {});
when(mockRepo.addLocalDrift(any)).thenAnswer((_) async {});
when(
mockWgerBaseProvider.makeUrl(entryUrl, query: anyNamed('query')),
).thenReturn(tCategoryEntriesUri);
when(
mockWgerBaseProvider.makeUrl(entryUrl, id: anyNamed('id'), query: anyNamed('query')),
).thenReturn(tCategoryEntriesUri);
when(
mockWgerBaseProvider.fetchPaginated(tCategoryEntriesUri),
).thenAnswer((realInvocation) => Future.value(tMeasurementCategoryMap['results']));
});
when(mockRepo.deleteLocalDriftCategory(any)).thenAnswer((_) async {});
when(mockRepo.updateLocalDriftCategory(any)).thenAnswer((_) async {});
when(mockRepo.addLocalDriftCategory(any)).thenAnswer((_) async {});
group('clear()', () {
test('should clear the categories list', () async {
// arrange
await measurementProvider.fetchAndSetCategories();
// assert
expect(measurementProvider.categories.isEmpty, false);
// act
measurementProvider.clear();
// assert
expect(measurementProvider.categories.isEmpty, true);
});
});
group('findCategoryById()', () {
test('should return a category for an id', () async {
// arrange
await measurementProvider.fetchAndSetCategories();
// act
final result = measurementProvider.findCategoryById(1);
// assert
expect(result, tMeasurementCategory);
});
test('should throw a NoResultException if no category is found', () {
// act & assert
expect(() => measurementProvider.findCategoryById(3), throwsA(isA<NoSuchEntryException>()));
});
});
group('fetchAndSetCategories()', () {
test('should make a request url', () async {
// act
await measurementProvider.fetchAndSetCategories();
// assert
verify(mockWgerBaseProvider.makeUrl(categoryUrl, query: {'limit': API_MAX_PAGE_SIZE}));
});
test('should fetch data from api', () async {
// act
await measurementProvider.fetchAndSetCategories();
// assert
verify(mockWgerBaseProvider.fetchPaginated(tCategoryUri));
});
test('should set categories', () async {
// act
await measurementProvider.fetchAndSetCategories();
// assert
expect(measurementProvider.categories, tMeasurementCategories);
});
});
group('fetchAndSetCategoryEntries()', () {
setUp(() async {
await measurementProvider.fetchAndSetCategories();
});
test('should make a uri from a category id', () async {
// act
await measurementProvider.fetchAndSetCategoryEntries(tCategoryId);
// assert
verify(
mockWgerBaseProvider.makeUrl(
entryUrl,
query: {'category': tCategoryId.toString(), 'limit': API_MAX_PAGE_SIZE},
),
);
});
test('should fetch categories entries for id', () async {
// act
await measurementProvider.fetchAndSetCategoryEntries(tCategoryId);
// assert
verify(mockWgerBaseProvider.fetchPaginated(tCategoryEntriesUri));
});
test('should add entries to category in list', () async {
// arrange
final List<MeasurementCategory> tMeasurementCategories = [
MeasurementCategory(
uuid: 1,
name: 'Strength',
unit: 'kN',
entries: [
MeasurementEntry(
uuid: 1,
categoryId: 1,
date: DateTime(2021, 7, 21),
value: 10,
notes: 'Some important notes',
),
MeasurementEntry(
uuid: 2,
categoryId: 1,
date: DateTime(2021, 7, 10),
value: 15.00,
notes: '',
),
],
),
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
];
// act
await measurementProvider.fetchAndSetCategoryEntries(tCategoryId);
// assert
expect(measurementProvider.categories, tMeasurementCategories);
});
});
group('addCategory()', () {
const MeasurementCategory tMeasurementCategoryWithoutId = MeasurementCategory(
uuid: null,
name: 'Strength',
unit: 'kN',
);
final Map<String, dynamic> tMeasurementCategoryMap = jsonDecode(
fixture('measurement/measurement_category.json'),
);
final Map<String, dynamic> tMeasurementCategoryMapWithoutId = jsonDecode(
fixture('measurement/measurement_category_without_id_to_json.json'),
);
final List<MeasurementCategory> tMeasurementCategoriesAdded = [
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
const MeasurementCategory(uuid: 1, name: 'Strength', unit: 'kN'),
const MeasurementCategory(uuid: 1, name: 'Strength', unit: 'kN'),
];
setUp(() {
when(
mockWgerBaseProvider.post(any, any),
).thenAnswer((realInvocation) => Future.value(tMeasurementCategoryMap));
});
test("should post the MeasurementCategorie's Map", () async {
// act
await measurementProvider.addCategory(tMeasurementCategoryWithoutId);
// assert
verify(mockWgerBaseProvider.post(tMeasurementCategoryMapWithoutId, tCategoryUri));
});
test(
'should add the result from the post call to the categories List and sort the list by alphabetical order',
() async {
// arrange
await measurementProvider.fetchAndSetCategories();
// act
await measurementProvider.addCategory(tMeasurementCategoryWithoutId);
// assert
expect(measurementProvider.categories, tMeasurementCategoriesAdded);
},
container = ProviderContainer(
overrides: [
measurementRepositoryProvider.overrideWithValue(mockRepo),
],
);
});
group('deleteCategory()', () {
setUp(() async {
await measurementProvider.fetchAndSetCategories();
});
test(
'should remove a MeasurementCategory from the categories list for an id and call the api to remove the MeasurementCategory',
() async {
// arrange
when(
mockWgerBaseProvider.deleteRequest(any, any),
).thenAnswer((realInvocation) => Future.value(Response('', 200)));
final List<MeasurementCategory> tMeasurementCategoriesOneDeleted = [
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
];
// act
await measurementProvider.deleteCategory(tCategoryId);
// assert
verify(mockWgerBaseProvider.deleteRequest('measurement-category', tCategoryId));
expect(measurementProvider.categories, tMeasurementCategoriesOneDeleted);
},
);
test('should throw a NoSuchEntryException if no category is found', () {
// act & assert
expect(() => measurementProvider.deleteCategory(83), throwsA(isA<NoSuchEntryException>()));
group('MeasurementProvider', () {
// Entries
test('deleteEntry calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
await notifier.deleteEntry('123');
verify(mockRepo.deleteLocalDrift('123')).called(1);
});
test(
'should re-add the "removed" MeasurementCategory and relay the exception on WgerHttpException',
() {
// arrange
when(mockWgerBaseProvider.deleteRequest(any, any)).thenThrow(WgerHttpException.fromMap({}));
test('updateEntry calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
// act & assert
expect(
() => measurementProvider.deleteCategory(tCategoryId),
throwsA(isA<WgerHttpException>()),
);
expect(measurementProvider.categories, tMeasurementCategories);
},
);
});
group('editCategory()', () {
const String tCategoryEditedName = 'Triceps';
const String tCategoryEditedUnit = 'm';
final Map<String, dynamic> tCategoryMapEditedToJson = jsonDecode(
fixture('measurement/measurement_category_edited_to_json.json'),
);
final Map<String, dynamic> tCategoryMapEdited = jsonDecode(
fixture('measurement/measurement_category_edited.json'),
);
setUp(() async {
when(
mockWgerBaseProvider.patch(any, any),
).thenAnswer((realInvocation) => Future.value(tCategoryMapEdited));
await measurementProvider.fetchAndSetCategories();
});
test('should add the new MeasurementCategory and remove the old one', () async {
// arrange
final List<MeasurementCategory> tMeasurementCategoriesEdited = [
const MeasurementCategory(uuid: 1, name: 'Triceps', unit: 'm'),
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
];
// act
await measurementProvider.editCategory(tCategoryId, tCategoryEditedName, tCategoryEditedUnit);
// assert
expect(measurementProvider.categories, tMeasurementCategoriesEdited);
await notifier.updateEntry(testMeasurementEntry1);
verify(mockRepo.updateLocalDrift(testMeasurementEntry1)).called(1);
});
test("should throw a NoSuchEntryException if category doesn't exist", () {
// act & assert
expect(
() => measurementProvider.editCategory(83, tCategoryEditedName, tCategoryEditedUnit),
throwsA(isA<NoSuchEntryException>()),
);
test('addEntry calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
await notifier.addEntry(testMeasurementEntry1);
verify(mockRepo.addLocalDrift(testMeasurementEntry1)).called(1);
});
test('should call api to patch the category', () async {
// act
await measurementProvider.editCategory(tCategoryId, tCategoryEditedName, tCategoryEditedUnit);
// assert
verify(mockWgerBaseProvider.patch(tCategoryMapEditedToJson, tCategoryUri));
// Categories
test('deleteCategory calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
await notifier.deleteCategory('cat1');
verify(mockRepo.deleteLocalDriftCategory('cat1')).called(1);
});
test('should keep categories list as is on WgerHttpException', () {
// arrange
when(mockWgerBaseProvider.patch(any, any)).thenThrow(WgerHttpException.fromMap({}));
test('updateCategory calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
final category = getMeasurementCategories().first;
// act & assert
expect(
() => measurementProvider.editCategory(
tCategoryId,
tCategoryEditedName,
tCategoryEditedUnit,
),
throwsA(isA<WgerHttpException>()),
);
expect(measurementProvider.categories, tMeasurementCategories);
});
});
group('addEntry()', () {
final MeasurementEntry tMeasurementEntry = MeasurementEntry(
uuid: 3,
categoryId: 1,
date: DateTime(2021, 7, 9),
value: 15.00,
notes: '',
);
final MeasurementEntry tMeasurementEntryWithoutId = MeasurementEntry(
uuid: null,
categoryId: 1,
date: DateTime(2021, 7, 9),
value: 15.0,
notes: '',
);
final List<MeasurementCategory> tMeasurementCategories = [
MeasurementCategory(
uuid: 1,
name: 'Strength',
unit: 'kN',
entries: [
MeasurementEntry(
uuid: 1,
categoryId: 1,
date: DateTime(2021, 7, 21),
value: 10,
notes: 'Some important notes',
),
MeasurementEntry(
uuid: 2,
categoryId: 1,
date: DateTime(2021, 7, 10),
value: 15.00,
notes: '',
),
tMeasurementEntry,
],
),
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
];
setUp(() async {
await measurementProvider.fetchAndSetCategories();
final Map<String, dynamic> measurementEntryMap = jsonDecode(
fixture('measurement/measurement_entry.json'),
);
when(
mockWgerBaseProvider.post(any, any),
).thenAnswer((realInvocation) => Future.value(measurementEntryMap));
await notifier.updateCategory(category);
verify(mockRepo.updateLocalDriftCategory(category)).called(1);
});
test('should make the post url', () async {
// act
await measurementProvider.addEntry(tMeasurementEntryWithoutId);
test('addCategory calls repository', () async {
final notifier = container.read(measurementProvider.notifier);
final category = getMeasurementCategories().first;
// assert
verify(mockWgerBaseProvider.makeUrl(entryUrl));
await notifier.addCategory(category);
verify(mockRepo.addLocalDriftCategory(category)).called(1);
});
test('should post the MeasurementEntryMap', () async {
// arrange
final Map<String, dynamic> measurementEntryMapWithoutId = jsonDecode(
fixture('measurement/measurement_entry_without_id.json'),
);
test('build watches repository stream', () async {
container.read(measurementProvider);
// act
await measurementProvider.addEntry(tMeasurementEntryWithoutId);
// assert
verify(mockWgerBaseProvider.post(measurementEntryMapWithoutId, tCategoryEntriesUri));
});
test(
"should add MeasurementEntry to its MeasurementCategory in the categories List and sort the category's list by date",
() async {
// arrange
await measurementProvider.fetchAndSetCategoryEntries(tCategoryId);
// act
await measurementProvider.addEntry(tMeasurementEntryWithoutId);
// assert
expect(measurementProvider.categories, tMeasurementCategories);
},
);
test('should throw a NoSuchEntryException if no category is found', () {
// arrange
final MeasurementEntry tMeasurementEntryWrongCategory = MeasurementEntry(
uuid: 3,
categoryId: 83,
date: DateTime(2021, 7, 9),
value: 15.00,
notes: '',
);
final Map<String, dynamic> measurementEntryMapWrongCategory = jsonDecode(
fixture('measurement/measurement_entry_wrong_category.json'),
);
when(
mockWgerBaseProvider.post(any, any),
).thenAnswer((realInvocation) => Future.value(measurementEntryMapWrongCategory));
// act & assert
expect(
() => measurementProvider.addEntry(tMeasurementEntryWrongCategory),
throwsA(isA<NoSuchEntryException>()),
);
});
});
group('deleteEntry()', () {
const int tEntryId = 2;
final List<MeasurementCategory> tMeasurementCategories = [
MeasurementCategory(
uuid: 1,
name: 'Strength',
unit: 'kN',
entries: [
MeasurementEntry(
uuid: 1,
categoryId: 1,
date: DateTime(2021, 7, 21),
value: 10,
notes: 'Some important notes',
),
],
),
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
];
setUp(() async {
await measurementProvider.fetchAndSetCategories();
await measurementProvider.fetchAndSetCategoryEntries(tCategoryId);
when(
mockWgerBaseProvider.deleteRequest(any, any),
).thenAnswer((realInvocation) => Future.value(Response('', 200)));
});
test("should remove a MeasurementEntry from the category's entries List for an id", () async {
// act
await measurementProvider.deleteEntry(tEntryId, tCategoryId);
// assert
expect(measurementProvider.categories, tMeasurementCategories);
});
test("should throw a NoSuchEntryException if the category isn't found", () {
// act & assert
expect(
() => measurementProvider.deleteEntry(tEntryId, 83),
throwsA(isA<NoSuchEntryException>()),
);
});
test(
"should throw a NoSuchEntryException if the entry in the categories entries List isn't found",
() {
// act & assert
expect(
() => measurementProvider.deleteEntry(83, tCategoryId),
throwsA(isA<NoSuchEntryException>()),
);
},
);
test('should call the api to remove the MeasurementEntry', () async {
// act
await measurementProvider.deleteEntry(tEntryId, tCategoryId);
// assert
verify(mockWgerBaseProvider.deleteRequest(entryUrl, tEntryId));
});
test(
'should re-add the "removed" MeasurementEntry and throw a WgerHttpException if the api call fails',
() {
// arrange
final List<MeasurementCategory> tMeasurementCategories = [
MeasurementCategory(
uuid: 1,
name: 'Strength',
unit: 'kN',
entries: [
MeasurementEntry(
uuid: 1,
categoryId: 1,
date: DateTime(2021, 7, 21),
value: 10,
notes: 'Some important notes',
),
MeasurementEntry(
uuid: 2,
categoryId: 1,
date: DateTime(2021, 7, 10),
value: 15.00,
notes: '',
),
],
),
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
];
when(mockWgerBaseProvider.deleteRequest(any, any)).thenThrow(WgerHttpException.fromMap({}));
// act & assert
expect(
() => measurementProvider.deleteEntry(tEntryId, tCategoryId),
throwsA(isA<WgerHttpException>()),
);
expect(measurementProvider.categories, tMeasurementCategories);
},
);
});
group('editEntry()', () {
// remove the old MeasurementEntry
// should call api to patch the entry
// should add the new MeasurementEntry from the api call
// notifyListeners()
// should re-add the old MeasurementEntry and remove the new one if call to api fails
// notifyListeners()
const int tEntryId = 1;
const num tEntryEditedValue = 23;
final DateTime tEntryEditedDate = DateTime(2021, 07, 21);
const String tEntryEditedNote = 'I just wanted to edit this to see what happens';
final Map<String, dynamic> tEntryMapEdited = jsonDecode(
fixture('measurement/measurement_entry_edited.json'),
);
setUp(() async {
when(
mockWgerBaseProvider.patch(any, any),
).thenAnswer((realInvocation) => Future.value(tEntryMapEdited));
await measurementProvider.fetchAndSetCategories();
await measurementProvider.fetchAndSetCategoryEntries(1);
});
test('should add the new MeasurementEntry and remove the old one', () async {
// arrange
final List<MeasurementCategory> tMeasurementCategoriesEdited = [
MeasurementCategory(
uuid: 1,
name: 'Strength',
unit: 'kN',
entries: [
MeasurementEntry(
uuid: 1,
categoryId: 1,
date: DateTime(2021, 7, 21),
value: 23,
notes: 'I just wanted to edit this to see what happens',
),
MeasurementEntry(
uuid: 2,
categoryId: 1,
date: DateTime(2021, 7, 10),
value: 15.00,
notes: '',
),
],
),
const MeasurementCategory(uuid: 2, name: 'Biceps', unit: 'cm'),
];
// act
await measurementProvider.editEntry(
tEntryId,
tCategoryId,
tEntryEditedValue,
tEntryEditedNote,
tEntryEditedDate,
);
// assert
expect(measurementProvider.categories, tMeasurementCategoriesEdited);
});
test("should throw a NoSuchEntryException if category doesn't exist", () {
// act & assert
expect(
() => measurementProvider.editEntry(
tEntryId,
83,
tEntryEditedValue,
tEntryEditedNote,
tEntryEditedDate,
),
throwsA(isA<NoSuchEntryException>()),
);
});
test("should throw a NoSuchEntryException if entry doesn't exist", () {
// act & assert
expect(
() => measurementProvider.editEntry(
83,
tCategoryId,
tEntryEditedValue,
tEntryEditedNote,
tEntryEditedDate,
),
throwsA(isA<NoSuchEntryException>()),
);
});
test('should call api to patch the entry', () async {
// act
await measurementProvider.editEntry(
tEntryId,
tCategoryId,
tEntryEditedValue,
tEntryEditedNote,
tEntryEditedDate,
);
// assert
verify(mockWgerBaseProvider.patch(tEntryMapEdited, tCategoryEntriesUri));
verify(mockRepo.watchAll()).called(1);
});
});
}

View File

@@ -3,12 +3,12 @@
// Do not manually edit this file.
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i5;
import 'dart:async' as _i3;
import 'package:http/http.dart' as _i3;
import 'package:mockito/mockito.dart' as _i1;
import 'package:wger/providers/auth.dart' as _i2;
import 'package:wger/providers/base_provider.dart' as _i4;
import 'package:wger/models/measurements/measurement_category.dart' as _i4;
import 'package:wger/models/measurements/measurement_entry.dart' as _i5;
import 'package:wger/providers/measurement_repository.dart' as _i2;
// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
@@ -25,168 +25,85 @@ import 'package:wger/providers/base_provider.dart' as _i4;
// ignore_for_file: subtype_of_sealed_class
// ignore_for_file: invalid_use_of_internal_member
class _FakeAuthProvider_0 extends _i1.SmartFake implements _i2.AuthProvider {
_FakeAuthProvider_0(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
class _FakeClient_1 extends _i1.SmartFake implements _i3.Client {
_FakeClient_1(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
class _FakeUri_2 extends _i1.SmartFake implements Uri {
_FakeUri_2(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
class _FakeResponse_3 extends _i1.SmartFake implements _i3.Response {
_FakeResponse_3(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
/// A class which mocks [WgerBaseProvider].
/// A class which mocks [MeasurementRepository].
///
/// See the documentation for Mockito's code generation for more information.
class MockWgerBaseProvider extends _i1.Mock implements _i4.WgerBaseProvider {
MockWgerBaseProvider() {
class MockMeasurementRepository extends _i1.Mock implements _i2.MeasurementRepository {
MockMeasurementRepository() {
_i1.throwOnMissingStub(this);
}
@override
_i2.AuthProvider get auth =>
_i3.Stream<List<_i4.MeasurementCategory>> watchAll() =>
(super.noSuchMethod(
Invocation.getter(#auth),
returnValue: _FakeAuthProvider_0(this, Invocation.getter(#auth)),
Invocation.method(#watchAll, []),
returnValue: _i3.Stream<List<_i4.MeasurementCategory>>.empty(),
)
as _i2.AuthProvider);
as _i3.Stream<List<_i4.MeasurementCategory>>);
@override
_i3.Client get client =>
(super.noSuchMethod(
Invocation.getter(#client),
returnValue: _FakeClient_1(this, Invocation.getter(#client)),
)
as _i3.Client);
@override
set auth(_i2.AuthProvider? value) => super.noSuchMethod(
Invocation.setter(#auth, value),
returnValueForMissingStub: null,
);
@override
set client(_i3.Client? value) => super.noSuchMethod(
Invocation.setter(#client, value),
returnValueForMissingStub: null,
);
@override
Map<String, String> getDefaultHeaders({
bool? includeAuth = false,
String? language,
}) =>
(super.noSuchMethod(
Invocation.method(#getDefaultHeaders, [], {
#includeAuth: includeAuth,
#language: language,
}),
returnValue: <String, String>{},
)
as Map<String, String>);
@override
Uri makeUrl(
String? path, {
int? id,
String? objectMethod,
Map<String, dynamic>? query,
}) =>
(super.noSuchMethod(
Invocation.method(
#makeUrl,
[path],
{#id: id, #objectMethod: objectMethod, #query: query},
),
returnValue: _FakeUri_2(
this,
Invocation.method(
#makeUrl,
[path],
{#id: id, #objectMethod: objectMethod, #query: query},
),
),
)
as Uri);
@override
_i5.Future<dynamic> fetch(
Uri? uri, {
int? maxRetries = 3,
Duration? initialDelay = const Duration(milliseconds: 250),
Duration? timeout = const Duration(seconds: 15),
String? language,
}) =>
(super.noSuchMethod(
Invocation.method(
#fetch,
[uri],
{
#maxRetries: maxRetries,
#initialDelay: initialDelay,
#timeout: timeout,
#language: language,
},
),
returnValue: _i5.Future<dynamic>.value(),
)
as _i5.Future<dynamic>);
@override
_i5.Future<List<dynamic>> fetchPaginated(
Uri? uri, {
String? language,
Duration? timeout = const Duration(seconds: 15),
}) =>
(super.noSuchMethod(
Invocation.method(
#fetchPaginated,
[uri],
{#language: language, #timeout: timeout},
),
returnValue: _i5.Future<List<dynamic>>.value(<dynamic>[]),
)
as _i5.Future<List<dynamic>>);
@override
_i5.Future<Map<String, dynamic>> post(Map<String, dynamic>? data, Uri? uri) =>
(super.noSuchMethod(
Invocation.method(#post, [data, uri]),
returnValue: _i5.Future<Map<String, dynamic>>.value(
<String, dynamic>{},
),
)
as _i5.Future<Map<String, dynamic>>);
@override
_i5.Future<Map<String, dynamic>> patch(
Map<String, dynamic>? data,
Uri? uri,
_i3.Stream<_i4.MeasurementCategory?> watchLocalDriftCategoryByUuid(
String? uuid,
) =>
(super.noSuchMethod(
Invocation.method(#patch, [data, uri]),
returnValue: _i5.Future<Map<String, dynamic>>.value(
<String, dynamic>{},
),
Invocation.method(#watchLocalDriftCategoryByUuid, [uuid]),
returnValue: _i3.Stream<_i4.MeasurementCategory?>.empty(),
)
as _i5.Future<Map<String, dynamic>>);
as _i3.Stream<_i4.MeasurementCategory?>);
@override
_i5.Future<_i3.Response> deleteRequest(String? url, int? id) =>
_i3.Future<void> deleteLocalDrift(String? uuid) =>
(super.noSuchMethod(
Invocation.method(#deleteRequest, [url, id]),
returnValue: _i5.Future<_i3.Response>.value(
_FakeResponse_3(
this,
Invocation.method(#deleteRequest, [url, id]),
),
),
Invocation.method(#deleteLocalDrift, [uuid]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i5.Future<_i3.Response>);
as _i3.Future<void>);
@override
_i3.Future<void> updateLocalDrift(_i5.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDrift, [entry]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> addLocalDrift(_i5.MeasurementEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#addLocalDrift, [entry]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> deleteLocalDriftCategory(String? uuid) =>
(super.noSuchMethod(
Invocation.method(#deleteLocalDriftCategory, [uuid]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> updateLocalDriftCategory(
_i4.MeasurementCategory? category,
) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDriftCategory, [category]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
@override
_i3.Future<void> addLocalDriftCategory(_i4.MeasurementCategory? category) =>
(super.noSuchMethod(
Invocation.method(#addLocalDriftCategory, [category]),
returnValue: _i3.Future<void>.value(),
returnValueForMissingStub: _i3.Future<void>.value(),
)
as _i3.Future<void>);
}

View File

@@ -2,15 +2,18 @@ import 'dart:convert';
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:wger/database/ingredients/ingredients_database.dart';
import 'package:wger/models/nutrition/ingredient.dart';
import 'package:wger/models/nutrition/nutritional_plan.dart';
import 'package:wger/providers/base_provider.dart';
import 'package:wger/providers/nutrition.dart';
import '../fixtures/fixture_reader.dart';
import '../measurements/measurement_provider_test.mocks.dart';
import 'nutrition_provider_test.mocks.dart';
@GenerateMocks([WgerBaseProvider])
void main() {
final now = DateTime.now();
late NutritionPlansProvider nutritionProvider;

View File

@@ -0,0 +1,192 @@
// Mocks generated by Mockito 5.4.6 from annotations
// in wger/test/nutrition/nutrition_provider_test.dart.
// Do not manually edit this file.
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i5;
import 'package:http/http.dart' as _i3;
import 'package:mockito/mockito.dart' as _i1;
import 'package:wger/providers/auth.dart' as _i2;
import 'package:wger/providers/base_provider.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
class _FakeAuthProvider_0 extends _i1.SmartFake implements _i2.AuthProvider {
_FakeAuthProvider_0(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
class _FakeClient_1 extends _i1.SmartFake implements _i3.Client {
_FakeClient_1(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
class _FakeUri_2 extends _i1.SmartFake implements Uri {
_FakeUri_2(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
class _FakeResponse_3 extends _i1.SmartFake implements _i3.Response {
_FakeResponse_3(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}
/// A class which mocks [WgerBaseProvider].
///
/// See the documentation for Mockito's code generation for more information.
class MockWgerBaseProvider extends _i1.Mock implements _i4.WgerBaseProvider {
MockWgerBaseProvider() {
_i1.throwOnMissingStub(this);
}
@override
_i2.AuthProvider get auth =>
(super.noSuchMethod(
Invocation.getter(#auth),
returnValue: _FakeAuthProvider_0(this, Invocation.getter(#auth)),
)
as _i2.AuthProvider);
@override
_i3.Client get client =>
(super.noSuchMethod(
Invocation.getter(#client),
returnValue: _FakeClient_1(this, Invocation.getter(#client)),
)
as _i3.Client);
@override
set auth(_i2.AuthProvider? value) => super.noSuchMethod(
Invocation.setter(#auth, value),
returnValueForMissingStub: null,
);
@override
set client(_i3.Client? value) => super.noSuchMethod(
Invocation.setter(#client, value),
returnValueForMissingStub: null,
);
@override
Map<String, String> getDefaultHeaders({
bool? includeAuth = false,
String? language,
}) =>
(super.noSuchMethod(
Invocation.method(#getDefaultHeaders, [], {
#includeAuth: includeAuth,
#language: language,
}),
returnValue: <String, String>{},
)
as Map<String, String>);
@override
Uri makeUrl(
String? path, {
int? id,
String? objectMethod,
Map<String, dynamic>? query,
}) =>
(super.noSuchMethod(
Invocation.method(
#makeUrl,
[path],
{#id: id, #objectMethod: objectMethod, #query: query},
),
returnValue: _FakeUri_2(
this,
Invocation.method(
#makeUrl,
[path],
{#id: id, #objectMethod: objectMethod, #query: query},
),
),
)
as Uri);
@override
_i5.Future<dynamic> fetch(
Uri? uri, {
int? maxRetries = 3,
Duration? initialDelay = const Duration(milliseconds: 250),
Duration? timeout = const Duration(seconds: 15),
String? language,
}) =>
(super.noSuchMethod(
Invocation.method(
#fetch,
[uri],
{
#maxRetries: maxRetries,
#initialDelay: initialDelay,
#timeout: timeout,
#language: language,
},
),
returnValue: _i5.Future<dynamic>.value(),
)
as _i5.Future<dynamic>);
@override
_i5.Future<List<dynamic>> fetchPaginated(
Uri? uri, {
String? language,
Duration? timeout = const Duration(seconds: 15),
}) =>
(super.noSuchMethod(
Invocation.method(
#fetchPaginated,
[uri],
{#language: language, #timeout: timeout},
),
returnValue: _i5.Future<List<dynamic>>.value(<dynamic>[]),
)
as _i5.Future<List<dynamic>>);
@override
_i5.Future<Map<String, dynamic>> post(Map<String, dynamic>? data, Uri? uri) =>
(super.noSuchMethod(
Invocation.method(#post, [data, uri]),
returnValue: _i5.Future<Map<String, dynamic>>.value(
<String, dynamic>{},
),
)
as _i5.Future<Map<String, dynamic>>);
@override
_i5.Future<Map<String, dynamic>> patch(
Map<String, dynamic>? data,
Uri? uri,
) =>
(super.noSuchMethod(
Invocation.method(#patch, [data, uri]),
returnValue: _i5.Future<Map<String, dynamic>>.value(
<String, dynamic>{},
),
)
as _i5.Future<Map<String, dynamic>>);
@override
_i5.Future<_i3.Response> deleteRequest(String? url, int? id) =>
(super.noSuchMethod(
Invocation.method(#deleteRequest, [url, id]),
returnValue: _i5.Future<_i3.Response>.value(
_FakeResponse_3(
this,
Invocation.method(#deleteRequest, [url, id]),
),
),
)
as _i5.Future<_i3.Response>);
}

View File

@@ -24,7 +24,6 @@ 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:provider/provider.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/workouts/routine.dart';
import 'package:wger/providers/network_provider.dart';
@@ -77,26 +76,23 @@ void main() {
workoutLogRepositoryProvider.overrideWithValue(mockWorkoutLogRepository),
trophyRepositoryProvider.overrideWithValue(mockRepository),
],
child: ChangeNotifierProvider<RoutinesProvider>(
create: (context) => mockRoutinesProvider,
child: MaterialApp(
locale: Locale(locale),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
navigatorKey: key,
home: TextButton(
onPressed: () => key.currentState!.push(
MaterialPageRoute<void>(
settings: RouteSettings(arguments: routine.id),
builder: (_) => const WorkoutLogsScreen(),
),
child: MaterialApp(
locale: Locale(locale),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
navigatorKey: key,
home: TextButton(
onPressed: () => key.currentState!.push(
MaterialPageRoute<void>(
settings: RouteSettings(arguments: routine.id),
builder: (_) => const WorkoutLogsScreen(),
),
child: const SizedBox(),
),
routes: {
RoutineScreen.routeName: (ctx) => const WorkoutLogsScreen(),
},
child: const SizedBox(),
),
routes: {
RoutineScreen.routeName: (ctx) => const WorkoutLogsScreen(),
},
),
);
}

View File

@@ -27,7 +27,7 @@ import 'package:wger/screens/routine_screen.dart';
import 'package:wger/widgets/routines/forms/weight.dart';
void main() {
int? result;
WeightUnit? result;
const unit1 = WeightUnit(id: 1, name: 'kg');
const unit2 = WeightUnit(id: 2, name: 'donkeys');

View File

@@ -20,6 +20,7 @@ import 'package:wger/models/measurements/measurement_category.dart';
import 'package:wger/models/measurements/measurement_entry.dart';
final testMeasurementEntry1 = MeasurementEntry(
id: 1,
uuid: '1',
categoryId: 1,
date: DateTime(2022, 9, 10),
@@ -27,6 +28,7 @@ final testMeasurementEntry1 = MeasurementEntry(
notes: '',
);
final testMeasurementEntry2 = MeasurementEntry(
id: 2,
uuid: '2',
categoryId: 1,
date: DateTime(2022, 10, 5),
@@ -34,6 +36,7 @@ final testMeasurementEntry2 = MeasurementEntry(
notes: '',
);
final testMeasurementEntry3 = MeasurementEntry(
id: 3,
uuid: '3',
categoryId: 1,
date: DateTime(2022, 10, 10),
@@ -41,6 +44,7 @@ final testMeasurementEntry3 = MeasurementEntry(
notes: '',
);
final testMeasurementEntry4 = MeasurementEntry(
id: 4,
uuid: '4',
categoryId: 1,
date: DateTime(2022, 11, 1),
@@ -48,6 +52,7 @@ final testMeasurementEntry4 = MeasurementEntry(
notes: '',
);
final testMeasurementEntry5 = MeasurementEntry(
id: 5,
uuid: '5',
categoryId: 1,
date: DateTime(2022, 11, 10),
@@ -55,6 +60,7 @@ final testMeasurementEntry5 = MeasurementEntry(
notes: '',
);
final testMeasurementEntry6 = MeasurementEntry(
id: 6,
uuid: '6',
categoryId: 1,
date: DateTime(2022, 11, 15),
@@ -62,20 +68,47 @@ final testMeasurementEntry6 = MeasurementEntry(
notes: '',
);
List<MeasurementCategory> getMeasurementCategories() {
final category = MeasurementCategory(
uuid: '1',
name: 'Body fat',
unit: '%',
entries: [
testMeasurementEntry1,
testMeasurementEntry2,
testMeasurementEntry3,
testMeasurementEntry4,
testMeasurementEntry5,
testMeasurementEntry6,
],
);
final testMeasurementEntry7 = MeasurementEntry(
id: 7,
uuid: '7',
categoryId: 2,
date: DateTime(2026, 02, 1),
value: 40,
notes: '',
);
return [category];
final testMeasurementEntry8 = MeasurementEntry(
id: 8,
uuid: '8',
categoryId: 2,
date: DateTime(2026, 03, 14),
value: 45,
notes: '',
);
List<MeasurementCategory> getMeasurementCategories() {
return [
MeasurementCategory(
uuid: '1',
name: 'Body fat',
unit: '%',
entries: [
testMeasurementEntry1,
testMeasurementEntry2,
testMeasurementEntry3,
testMeasurementEntry4,
testMeasurementEntry5,
testMeasurementEntry6,
],
),
MeasurementCategory(
uuid: '1',
name: 'Biceps',
unit: 'cm',
entries: [
testMeasurementEntry7,
testMeasurementEntry8,
],
),
];
}