Skip to content

Commit

Permalink
Merge pull request #104 from pektinasen/62_recent_sort_order
Browse files Browse the repository at this point in the history
bug: fix sort order in recent meals
  • Loading branch information
simonoppowa authored Sep 27, 2024
2 parents c340483 + 3f208be commit 25a7361
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/core/data/data_source/intake_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class IntakeDataSource {
Future<List<IntakeDBO>> getRecentlyAddedIntake({int number = 100}) async {
final intakeList = _intakeBox.values.toList();

// sort list by date and filter unique intake
// sort list by date (newest first) and filter unique intake
intakeList
.sort((a, b) => a.dateTime.compareTo(b.dateTime));
.sort((a, b) => (-1) * a.dateTime.compareTo(b.dateTime));

final filterCodes = <String>{};
final uniqueIntake = intakeList
Expand Down
35 changes: 35 additions & 0 deletions test/fixture/meal_entity_fixtures.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:opennutritracker/features/add_meal/domain/entity/meal_entity.dart';
import 'package:opennutritracker/features/add_meal/domain/entity/meal_nutriments_entity.dart';

class MealEntityFixtures {
static final mealOne = MealEntity(
code: "1",
name: null,
url: null,
mealQuantity: null,
mealUnit: 'g',
servingQuantity: null,
servingUnit: 'g',
nutriments: MealNutrimentsEntity.empty(),
source: MealSourceEntity.custom);
static final mealTwo = MealEntity(
code: "2",
name: null,
url: null,
mealQuantity: null,
mealUnit: 'g',
servingQuantity: null,
servingUnit: 'g',
nutriments: MealNutrimentsEntity.empty(),
source: MealSourceEntity.custom);
static final mealThree = MealEntity(
code: "3",
name: null,
url: null,
mealQuantity: null,
mealUnit: 'g',
servingQuantity: null,
servingUnit: 'g',
nutriments: MealNutrimentsEntity.empty(),
source: MealSourceEntity.custom);
}
64 changes: 64 additions & 0 deletions test/unit_test/intake_repository_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:opennutritracker/core/data/data_source/intake_data_source.dart';
import 'package:opennutritracker/core/data/dbo/intake_dbo.dart';
import 'package:opennutritracker/core/data/dbo/intake_type_dbo.dart';
import 'package:opennutritracker/core/data/dbo/meal_dbo.dart';
import 'package:opennutritracker/core/data/dbo/meal_nutriments_dbo.dart';
import 'package:opennutritracker/core/data/repository/intake_repository.dart';
import 'package:opennutritracker/core/domain/entity/intake_entity.dart';
import 'package:opennutritracker/core/domain/entity/intake_type_entity.dart';

import '../fixture/meal_entity_fixtures.dart';

void main() {

group('IntakeRepository test', () {
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized();
// final temp = await getTemporaryDirectory();
Hive.init(".");
Hive.registerAdapter(IntakeDBOAdapter());
Hive.registerAdapter(IntakeTypeDBOAdapter());
Hive.registerAdapter(MealDBOAdapter());
Hive.registerAdapter(MealSourceDBOAdapter());
Hive.registerAdapter(MealNutrimentsDBOAdapter());
});

tearDown(() {
Hive.deleteFromDisk();
});

test('returns last added first', () async {
final box = await Hive.openBox<IntakeDBO>('intake_test');

final repo = IntakeRepository(IntakeDataSource(box));


await repo.addIntake(IntakeEntity(
id: "1",
unit: "g",
amount: 1,
type: IntakeTypeEntity.breakfast,
meal: MealEntityFixtures.mealOne,
dateTime: DateTime.utc(2024, 1, 1, 0, 0, 0)));
await repo.addIntake(IntakeEntity(
id: "2",
unit: "g",
amount: 1,
type: IntakeTypeEntity.breakfast,
meal: MealEntityFixtures.mealTwo,
dateTime: DateTime.utc(2024, 1, 2, 0, 0, 0)));
await repo.addIntake(IntakeEntity(
id: "3",
unit: "g",
amount: 1,
type: IntakeTypeEntity.breakfast,
meal: MealEntityFixtures.mealThree,
dateTime: DateTime.utc(2024, 1, 3, 0, 0, 0)));

final recents = (await repo.getRecentIntake()).map((e) => e.id).toList();
expect(recents, List.from(["3", "2", "1"]));
});
});
}

0 comments on commit 25a7361

Please sign in to comment.