Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: fix sort order in recent meals #104

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]));
});
});
}
Loading