Flutter-Ernaehrungsapp/test/statistics_test.dart

99 lines
3.8 KiB
Dart
Raw Permalink Normal View History

2023-06-25 14:43:07 +02:00
import 'package:ernaehrung/helper/format_helper.dart';
import 'package:ernaehrung/models/food.dart';
import 'package:ernaehrung/pages/nav_pages/subpages/progress_page/sub_components/charts/sub_components/tabs/timespan_tabs.dart';
import 'package:ernaehrung/services/statistics.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-25 14:43:07 +02:00
// TO RUN TESTS COMMENT initBoxes() (services/statistics.dart line 21) out -> HIVE behavior is tested in other tests
late DataService dataService;
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 {
2023-06-25 14:43:07 +02:00
dataService = DataService.instance;
2023-06-16 11:44:42 +02:00
});
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', () {
2023-06-25 14:43:07 +02:00
List<int> day = dataService.getTimestampsByTimestampAndTimespan(TimeSpan.day,getTimestampFromNow());
2023-06-16 11:44:42 +02:00
expect(31, day.length);
2023-06-25 14:43:07 +02:00
List<int> week = dataService.getTimestampsByTimestampAndTimespan(TimeSpan.week,getTimestampFromNow());
2023-06-16 11:44:42 +02:00
expect(7, week.length);
2023-06-25 14:43:07 +02:00
List<int> month = dataService.getTimestampsByTimestampAndTimespan(TimeSpan.month,getTimestampFromNow());
2023-06-16 11:44:42 +02:00
expect(31, month.length);
2023-06-25 14:43:07 +02:00
List<int> invalidTimestamp = dataService.getTimestampsByTimestampAndTimespan(TimeSpan.month,9999999999999);
2023-06-16 11:44:42 +02:00
expect(0, invalidTimestamp.length);
});
test('check if dates are returned correctly from timestamp', () {
2023-06-25 14:43:07 +02:00
int day = dataService.getDayAsIntFromTimestamp(getTimestampFromNow());
2023-06-16 11:44:42 +02:00
expect(DateTime.now().day, day);
2023-06-25 14:43:07 +02:00
int month = dataService.getMonthAsIntFromTimestamp(getTimestampFromNow());
2023-06-16 11:44:42 +02:00
expect(DateTime.now().month, month);
2023-06-25 14:43:07 +02:00
int year = dataService.getYearAsIntFromTimestamp(getTimestampFromNow());
2023-06-16 11:44:42 +02:00
expect(DateTime.now().year, year);
});
test('check if date comparison is right', () {
int today = getTimestampFromNow();
2023-06-25 14:43:07 +02:00
expect(true,dataService.isDateEqual(today, today));
2023-06-16 11:44:42 +02:00
int other = getTimeStampForDateTime(DateTime.utc(2010));
2023-06-25 14:43:07 +02:00
expect(false,dataService.isDateEqual(today, other));
expect(false,dataService.isDateEqual(9999999999999, today));
expect(false,dataService.isDateEqual(other, 9999999999999));
expect(false,dataService.isDateEqual(other, -1));
expect(false,dataService.isDateEqual(other, 0));
2023-06-16 11:44:42 +02:00
});
test('check if calories are summed up correctly for given food list',() {
2023-06-25 14:43:07 +02:00
expect(1500, dataService.getSumOfCaloriesByFoodList(getFoodListValid()));
expect(() => dataService.getSumOfCaloriesByFoodList(getInvalidFoodList()),throwsException);
2023-06-16 11:44:42 +02:00
});
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
});
}