dashboard/app/test/food_barcode_test.dart
bashynx ad4b6aa22f Food: logged entries follow their food; barcodes for My foods
- Saving one of My foods or a meal works every entry logged from it out again
  for that entry's grams, on any day. Only the entries that change are
  written, so the rest aren't synced again (db._follow, app _follow).
- My foods can have a barcode (8-14 digits, one food per barcode). A scanned
  or typed barcode finds My foods first (leading zeros ignored), then the food
  database and kept products; when nothing matches, "New food with barcode"
  opens the food editor with it filled in.
- App 1.1.3 and 1.1.4 changelog entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 14:08:32 +02:00

40 lines
2.3 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// My foods with the pack's barcode (food_model.dart saveMyFood / myFoodsByBarcode = the dashboard's db.clean_food /
// my_foods_by_barcode): kept, found by it (leading zeros or not), checked, one food per barcode, left alone when not given.
import 'dart:io';
import 'package:dash_app/health/food_model.dart';
import 'package:dash_app/sync/store.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
final store = SyncStore('${Directory.systemTemp.createTempSync('barcode').path}/d.db', 'test00000000');
test('kept and found by it', () {
final (u, err) = saveMyFood(store, name: 'Chicory syrup (4Slim)', per100: {'kcal': 161, 'fiber': 71}, portions: [],
barcode: '8595245311155');
expect(err, isNull);
expect(myFoodsByBarcode(store, '8595245311155').single.myUid, u);
expect(myFoodsByBarcode(store, '08595245311155').single.name, 'Chicory syrup (4Slim)');
expect(myFoodByUid(store, u!)!.barcode, '8595245311155');
expect(myFoodsByBarcode(store, '1234567890123'), isEmpty);
});
test('checked: digits only, 8–14, one food per barcode', () {
expect(saveMyFood(store, name: 'A', per100: {'kcal': 1}, portions: [], barcode: '12ab5678').$2, 'A barcode is 8–14 digits.');
expect(saveMyFood(store, name: 'B', per100: {'kcal': 1}, portions: [], barcode: '1234567').$2, 'A barcode is 8–14 digits.');
expect(saveMyFood(store, name: 'C', per100: {'kcal': 1}, portions: [], barcode: '8595245311155').$2, contains('has that barcode already'));
});
test('an edit without it keeps it; empty clears it; a deleted food is not found', () {
final u = myFoodsByBarcode(store, '8595245311155').single.myUid!;
saveMyFood(store, uid: u, name: 'Chicory syrup (4Slim)', per100: {'kcal': 160}, portions: []);
expect(store.row('food.food', u)!['barcode'], '8595245311155');
saveMyFood(store, uid: u, name: 'Chicory syrup (4Slim)', per100: {'kcal': 160}, portions: [], barcode: ' 8595245311155 ');
expect(store.row('food.food', u)!['barcode'], '8595245311155');
deleteMyFood(store, u);
expect(myFoodsByBarcode(store, '8595245311155'), isEmpty);
final (v, _) = saveMyFood(store, name: 'D', per100: {'kcal': 1}, portions: [], barcode: '8595245311155');
saveMyFood(store, uid: v, name: 'D', per100: {'kcal': 1}, portions: [], barcode: '');
expect(store.row('food.food', v!)!['barcode'], isNull);
});
}