Flutter-Ernaehrungsapp/test/statistics_test.dart

99 lines
3.8 KiB
Dart
Raw Normal View History

2023-06-16 11:44:42 +02:00
import 'package:ernaehrung/android/components/meal_page_text/days_component.dart';
import 'package:ernaehrung/android/config/format_helper.dart';
import 'package:ernaehrung/android/config/statistics.dart';
import 'package:ernaehrung/android/models/food.dart';
2023-06-16 11:47:52 +02:00
import 'package:flutter_dotenv/flutter_dotenv.dart';
2023-06-16 11:44:42 +02:00
import 'package:flutter_test/flutter_test.dart';
2023-06-16 11:47:52 +02:00
late StatisticsService statisticsService;
2023-06-16 11:44:42 +02:00
Future<void> main() async {
2023-06-16 11:47:52 +02:00
await dotenv.load(fileName: ".env");
2023-06-16 11:44:42 +02:00
setUp(() async {
statisticsService = StatisticsService.instance;
});
tearDown(() async {
//await Hive.close(); // Close Hive at the end of tests
});
getTimestampFromNow(){
DateTime now = DateTime.now();
return now.millisecondsSinceEpoch.toInt() ~/ 1000;
}
getTimeStampForDateTime(DateTime date){
return date.millisecondsSinceEpoch.toInt() ~/ 1000;
}
getFoodListValid(){
// 1500kcal
return [
Food(167511, "Testfood","",300,0,0,0,0,0),
Food(167512, "TestfoodTestfoodTestfoodTestfood","",300,0,0,0,0,0),
Food(167512, "","",300,0,0,0,0,0),
Food(167512, "","",300,0,0,0,0,0),
Food(167512, "","",300,0,0,0,0,0)
];
}
getInvalidFoodList(){
return [
Food(167512, "","",300,0,0,0,0,0),
Food(167512, "","",300,0,0,0,0,0),
Food(167512, "","",300,0,0,0,0,0),
Food(167512, "","","300",0,0,0,0,0),
Food(167512, "","",-200,0,0,0,0,0)
];
}
test('check if given timestamp returns valid timespan or catches invalid timestamp', () {
List<int> day = statisticsService.getTimestampsByTimestampAndTimespan(TimeSpan.day,getTimestampFromNow());
expect(31, day.length);
List<int> week = statisticsService.getTimestampsByTimestampAndTimespan(TimeSpan.week,getTimestampFromNow());
expect(7, week.length);
List<int> month = statisticsService.getTimestampsByTimestampAndTimespan(TimeSpan.month,getTimestampFromNow());
expect(31, month.length);
List<int> invalidTimestamp = statisticsService.getTimestampsByTimestampAndTimespan(TimeSpan.month,9999999999999);
expect(0, invalidTimestamp.length);
});
test('check if dates are returned correctly from timestamp', () {
int day = statisticsService.getDayAsIntFromTimestamp(getTimestampFromNow());
expect(DateTime.now().day, day);
int month = statisticsService.getMonthAsIntFromTimestamp(getTimestampFromNow());
expect(DateTime.now().month, month);
int year = statisticsService.getYearAsIntFromTimestamp(getTimestampFromNow());
expect(DateTime.now().year, year);
});
test('check if date comparison is right', () {
int today = getTimestampFromNow();
expect(true,statisticsService.isDateEqual(today, today));
int other = getTimeStampForDateTime(DateTime.utc(2010));
expect(false,statisticsService.isDateEqual(today, other));
expect(false,statisticsService.isDateEqual(9999999999999, today));
expect(false,statisticsService.isDateEqual(other, 9999999999999));
expect(false,statisticsService.isDateEqual(other, -1));
expect(false,statisticsService.isDateEqual(other, 0));
});
test('check if calories are summed up correctly for given food list',() {
expect(1500, statisticsService.getSumOfCaloriesByFoodList(getFoodListValid()));
expect(() => statisticsService.getSumOfCaloriesByFoodList(getInvalidFoodList()),throwsException);
});
test('check if format helper are working right',() {
expect(2,getListOfDistinctElements(getFoodListValid()).length);
expect(3,getMapOfDistinctElementsWithCounterAndCalories(getFoodListValid()).keys.length);
2023-06-16 12:03:04 +02:00
expect("Testfood",getToastFoodNameString(getFoodListValid()[0],null));
expect("TestfoodTestfoodTestfo ...",getToastFoodNameString(getFoodListValid()[1],null));
2023-06-16 11:44:42 +02:00
Food food = getFoodListValid()[0];
2023-06-16 12:03:04 +02:00
expect("Testfood 2 x 600 kcal", getFoodListStringByFood(food.name, 2, 600,null));
2023-06-16 11:44:42 +02:00
});
}