dashboard/app/test/food_follow_test.dart
bashynx eeca189b7f Food: all products, barcode prefixes, meals that follow their ingredients
- "All products" lists what's here first (My foods, kept products, the food
  database), then Open Food Facts; if the archive can't be reached, what's
  here is still shown. A My food picked there is used as it is.
- The start of a barcode (4+ digits) finds the products whose barcode starts
  with it: in the search box, in all products and in the server's search
  (an index range, so it stays fast).
- A meal made with a changed food is worked out again, and so are meals made
  with that meal and their logged entries (db.follow_meals, app _followMeals).
- Saving a food that didn't change writes nothing, so nothing is synced again.
- Food search knows a few words in other languages: stomach / žalúdok /
  pipotă find gizzards, kurací / pui chicken, morčací / curcan turkey.
- App 1.1.5 changelog entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 15:41:23 +02:00

68 lines
4.4 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.

// Entries logged from one of My foods follow its edits (food_model.dart _follow = the dashboard's db._follow):
// a meal's new recipe or a food's new values / name re-work them for their grams; others aren't touched.
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('follow').path}/d.db', 'test00000000');
test('editing a food re-works its entries', () {
final (u, err) = saveMyFood(store, name: 'Tvaroh', per100: {'kcal': 200, 'protein': 14, 'fat': 15}, portions: []);
expect(err, isNull);
final f = myFoodByUid(store, u!)!;
saveFromFood(store, u: 'e1', day: '2026-10-26', meal: 'lunch', food: f, amount: 100, unit: 'g');
saveFromFood(store, u: 'e2', day: '2026-10-27', meal: 'lunch', food: f, amount: 150, unit: 'g');
saveTotals(store, u: 'e3', day: '2026-10-27', meal: 'lunch', name: 'Tvaroh', values: {'kcal': 300});
final h3 = store.rows('food.entry').firstWhere((r) => r['_u'] == 'e3')['_h'];
saveMyFood(store, uid: u, name: 'Tvaroh (homemade)', per100: {'kcal': 212, 'protein': 14, 'fat': 16}, portions: []);
expect(store.row('food.entry', 'e1')!['kcal'], 212);
expect(store.row('food.entry', 'e2')!['kcal'], 318);
expect(store.row('food.entry', 'e2')!['fat'], 24);
expect(store.row('food.entry', 'e2')!['name'], 'Tvaroh (homemade)');
expect(store.row('food.entry', 'e3')!['kcal'], 300); // typed numbers, no food: left alone
expect(store.rows('food.entry').firstWhere((r) => r['_u'] == 'e3')['_h'], h3);
});
test('unchanged entries are not re-sent', () {
final (u, _) = saveMyFood(store, name: 'Casein', per100: {'kcal': 390}, portions: []);
saveFromFood(store, u: 'c1', day: '2026-10-26', meal: 'breakfast', food: myFoodByUid(store, u!)!, amount: 30, unit: 'g');
final h = store.rows('food.entry').firstWhere((r) => r['_u'] == 'c1')['_h'];
saveMyFood(store, uid: u, name: 'Casein', per100: {'kcal': 390}, portions: [], note: 'a note');
expect(store.rows('food.entry').firstWhere((r) => r['_u'] == 'c1')['_h'], h);
});
test('a meal’s new recipe re-works its entries', () {
final (oil, _) = saveMyFood(store, name: 'Oil', per100: {'kcal': 900, 'fat': 100}, portions: []);
final (veg, _) = saveMyFood(store, name: 'Veg', per100: {'kcal': 30, 'fat': 0}, portions: []);
Ingredient ing(String uid, double g) => Ingredient.food(myFoodByUid(store, uid)!, g, 'g');
final (soup, err) = saveMeal(store, name: 'Soup', ings: [ing(oil!, 30), ing(veg!, 970)], portions: []);
expect(err, isNull);
saveFromFood(store, u: 's1', day: '2026-10-27', meal: 'lunch', food: myFoodByUid(store, soup!)!, amount: 400, unit: 'g');
expect(store.row('food.entry', 's1')!['kcal'], closeTo(400 * (27000 + 29100) / 100000, 0.01));
saveMeal(store, uid: soup, name: 'Soup', ings: [ing(oil, 10), ing(veg, 970)], portions: []);
expect(store.row('food.entry', 's1')!['kcal'], closeTo(400 * (9000 + 29100) / 98000, 0.01));
});
test('a meal made with a changed food is worked out again, its entries too', () {
final (curd, _) = saveMyFood(store, name: 'Curd', per100: {'kcal': 100, 'protein': 12}, portions: []);
final (flour, _) = saveMyFood(store, name: 'Flour', per100: {'kcal': 350}, portions: []);
Ingredient ing(String uid, double g) => Ingredient.food(myFoodByUid(store, uid)!, g, 'g');
final (strudel, _) = saveMeal(store, name: 'Strudel', ings: [ing(curd!, 300), ing(flour!, 200)], portions: []);
final (plate, _) = saveMeal(store, name: 'Plate', ings: [ing(strudel!, 250), ing(flour, 50)], portions: []);
saveFromFood(store, u: 'p1', day: '2026-10-27', meal: 'dinner', food: myFoodByUid(store, plate!)!, amount: 150, unit: 'g');
expect(store.row('food.food', strudel)!['kcal'], closeTo((300 + 700) / 5, 0.001));
final hFlour = store.row('food.food', flour)!['_h'];
saveMyFood(store, uid: curd, name: 'Curd', per100: {'kcal': 200, 'protein': 12}, portions: []);
expect(store.row('food.food', strudel)!['kcal'], closeTo((600 + 700) / 5, 0.001));
final plateKcal = (250 * 260 / 100 + 175) * 100 / 300;
expect(store.row('food.food', plate)!['kcal'], closeTo(plateKcal, 0.001));
expect(store.row('food.entry', 'p1')!['kcal'], closeTo(plateKcal * 1.5, 0.01));
expect(store.row('food.food', flour)!['_h'], hFlour); // not an ingredient's meal: untouched
});
}