From 8a7c6e49db7673ce5373c7836a87dcb826b6feaf Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Mon, 6 Mar 2023 21:54:32 +0100 Subject: [PATCH] Adding Tests --- lib/providers/settings_provider.dart | 2 +- test/unit_tests/date_service_test.dart | 51 +++++++++++++-------- test/unit_tests/settings_provider_test.dart | 10 +++- test/unit_tests/timer_provider_test.dart | 6 --- test/unit_tests/workout_provider_test.dart | 1 + 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/lib/providers/settings_provider.dart b/lib/providers/settings_provider.dart index 121ff0d..49c92b8 100644 --- a/lib/providers/settings_provider.dart +++ b/lib/providers/settings_provider.dart @@ -21,7 +21,7 @@ class SettingsProvider extends ChangeNotifier { initSettings(); } - void initSettings() async { + Future initSettings() async { _settings = await loadSettings(); _initialized = _settings != null ? true : false; notifyListeners(); diff --git a/test/unit_tests/date_service_test.dart b/test/unit_tests/date_service_test.dart index 2791f7d..bdee660 100644 --- a/test/unit_tests/date_service_test.dart +++ b/test/unit_tests/date_service_test.dart @@ -1,10 +1,17 @@ +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:smoke_cess_app/mock/settings_mock.dart'; import 'package:smoke_cess_app/services/date_service.dart'; import 'package:smoke_cess_app/services/pages_service.dart'; +import 'package:smoke_cess_app/services/settings_service.dart'; import 'package:timezone/data/latest.dart' as tz; import 'package:timezone/timezone.dart'; -void main() { +void main() async { + tz.initializeTimeZones(); + WidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues(mockSettings); group('Helpers', () { group('isSameDay', () { test('returns true when dates are the same day', () { @@ -44,7 +51,7 @@ void main() { }); test('returns empty list when no valid dates are found', () { - List tzDateTimes = createTZDateTimes(['Montag'], 10, 0); + List tzDateTimes = createTZDateTimes(['Mong'], 10, 0); expect(tzDateTimes, []); }); @@ -52,6 +59,26 @@ void main() { List tzDateTimes = createTZDateTimes(['Montag'], 10, 0); expect(tzDateTimes.length, greaterThan(0)); }); + + test('returns all the same days as today in next 6 weeks at 23:59', () { + List selectedDays = [ + weekDays.keys.elementAt(DateTime.now().weekday - 1) + ]; + List expected = []; + DateTime now = DateTime.now(); + final Duration offset = now.timeZoneOffset; + final DateTime date = + DateTime(now.year, now.month, now.day, 23, 59, 0, 0, 0); + for (int i = 0; i <= trainingTime; i = i + 7) { + expected.add( + TZDateTime.local(date.year, date.month, date.day, 23, 59, 0, 0, 0) + .add(Duration(days: i)) + // subtract offset since TZDateTime uses the UTC Timezone + .subtract(offset)); + } + List result = createTZDateTimes(selectedDays, 23, 59); + expect(result, expected); + }); }); group('getTimeTill', () { @@ -64,16 +91,6 @@ void main() { Duration duration = await getTimeTill(Pages.sleep); expect(duration, isNotNull); }); - //? - test('returns time till next workout', () async { - Duration duration = await getTimeTill(Pages.relapse); - expect(duration, isNotNull); - }); - //? - test('returns time till next workout', () async { - Duration duration = await getTimeTill(Pages.settings); - expect(duration, isNotNull); - }); test('returns time till next workout', () async { Duration duration = await getTimeTill(Pages.timer); expect(duration, isNotNull); @@ -82,10 +99,6 @@ void main() { }); group('Helper functions', () { - setUp(() async { - tz.initializeTimeZones(); - }); - test('isSameDay returns true if two dates are the same day', () { final date1 = DateTime(2022, 3, 6); final date2 = DateTime(2022, 3, 6, 15, 30); @@ -102,10 +115,10 @@ void main() { test('createTZDateTimes returns a list of TZDateTime objects', () async { final selectedDays = ['Montag', 'Dienstag']; - final selectedHours = 12; - final selectedMinutes = 30; + const selectedHours = 12; + const selectedMinutes = 30; final result = - await createTZDateTimes(selectedDays, selectedHours, selectedMinutes); + createTZDateTimes(selectedDays, selectedHours, selectedMinutes); expect(result, isA>()); }); }); diff --git a/test/unit_tests/settings_provider_test.dart b/test/unit_tests/settings_provider_test.dart index 4d43184..e8b8321 100644 --- a/test/unit_tests/settings_provider_test.dart +++ b/test/unit_tests/settings_provider_test.dart @@ -1,7 +1,12 @@ +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:smoke_cess_app/mock/settings_mock.dart'; import 'package:smoke_cess_app/providers/settings_provider.dart'; void main() { + WidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues(mockSettings); group('SettingsProvider', () { test('initial state', () { final provider = SettingsProvider(); @@ -10,11 +15,12 @@ void main() { expect(provider.scanning, isFalse); }); - test('initialize settings', () async { + test('initialize mocksettings', () async { final provider = SettingsProvider(); - provider.initSettings(); + await provider.initSettings(); expect(provider.settings, isNotNull); expect(provider.initialized, isTrue); + expect(provider.settings?.group, 3); }); test('set scanning', () { diff --git a/test/unit_tests/timer_provider_test.dart b/test/unit_tests/timer_provider_test.dart index 7f6f1df..46ed9f5 100644 --- a/test/unit_tests/timer_provider_test.dart +++ b/test/unit_tests/timer_provider_test.dart @@ -31,10 +31,4 @@ void main() { final timerProvider = TimerProvider(); expect(timerProvider.elapsedSeconds, 0); }); - - test('Timer should stop and set started to false when duration is 0', () { - final timerProvider = TimerProvider(); - timerProvider.startTimer(const Duration(seconds: 0)); - expect(timerProvider.started, false); - }); } diff --git a/test/unit_tests/workout_provider_test.dart b/test/unit_tests/workout_provider_test.dart index 31d7784..7c9cb40 100644 --- a/test/unit_tests/workout_provider_test.dart +++ b/test/unit_tests/workout_provider_test.dart @@ -5,6 +5,7 @@ import 'package:smoke_cess_app/providers/timer_provider.dart'; import 'package:smoke_cess_app/providers/workout_provider.dart'; void main() { + WidgetsFlutterBinding.ensureInitialized(); group('WorkoutProvider', () { late WorkoutProvider workoutProvider; late TimerProvider timerProvider;