Add a small curated catalog of Iberian horticultural species and let a variety be linked to it from the edit sheet. - assets/catalog/species.json: 14 species with botanical family and ES/EN common names (wikidata_qid/gbif_key deferred to the varilla enrichment). - SpeciesRepository: idempotent seedBundled (is_bundled rows, keyed by scientific name) + search by scientific/common name with a locale-best label. Seeded on startup from DI. - VarietyRepository.linkSpecies: sets species_id and prefills category from the species' family when empty (never overwrites an existing category). VarietyDetail now carries the scientific name. - Edit sheet gains a live species-search field; the detail view shows the scientific name (italic). i18n strings added (ES/EN). Tests: catalog parse, idempotent seeding, search by scientific/common name, linkSpecies prefill semantics, and a widget test for the autocomplete → link → scientific-name-shown flow. Full suite: 32 passing, 0 skipped.
100 lines
3.1 KiB
Dart
100 lines
3.1 KiB
Dart
import 'package:async/async.dart';
|
|
import 'package:commons_core/commons_core.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:tane/data/species_repository.dart';
|
|
import 'package:tane/data/variety_repository.dart';
|
|
import 'package:tane/db/database.dart';
|
|
|
|
import '../support/test_support.dart';
|
|
|
|
const _tomatoSeed = SpeciesSeed(
|
|
scientificName: 'Solanum lycopersicum',
|
|
family: 'Solanaceae',
|
|
commonNames: {
|
|
'en': ['Tomato'],
|
|
},
|
|
);
|
|
|
|
void main() {
|
|
late AppDatabase db;
|
|
late VarietyRepository repo;
|
|
|
|
setUp(() {
|
|
db = newTestDatabase();
|
|
repo = newTestRepository(db);
|
|
});
|
|
tearDown(() => db.close());
|
|
|
|
test('watchVariety emits null for a missing variety', () async {
|
|
expect(await repo.watchVariety('does-not-exist').first, isNull);
|
|
});
|
|
|
|
test('watchVariety reacts to a lot being added', () async {
|
|
final id = await repo.addQuickVariety(label: 'Maize', category: 'Poaceae');
|
|
final queue = StreamQueue(repo.watchVariety(id));
|
|
|
|
final initial = await queue.next;
|
|
expect(initial!.label, 'Maize');
|
|
expect(initial.lots, isEmpty);
|
|
|
|
await repo.addLot(
|
|
varietyId: id,
|
|
harvestYear: 2024,
|
|
quantity: const Quantity(kind: QuantityKind.cob),
|
|
);
|
|
|
|
final updated = await queue.next;
|
|
expect(updated!.lots.single.harvestYear, 2024);
|
|
expect(updated.lots.single.quantity?.kind, QuantityKind.cob);
|
|
|
|
await queue.cancel();
|
|
});
|
|
|
|
test('updateVariety changes scalar fields (last-writer-wins)', () async {
|
|
final id = await repo.addQuickVariety(label: 'Old name');
|
|
await repo.updateVariety(id: id, label: 'New name', notes: 'a note');
|
|
|
|
final detail = await repo.watchVariety(id).first;
|
|
expect(detail!.label, 'New name');
|
|
expect(detail.notes, 'a note');
|
|
});
|
|
|
|
test(
|
|
'softDeleteVariety hides it from watchVariety and the inventory',
|
|
() async {
|
|
final id = await repo.addQuickVariety(label: 'Doomed');
|
|
await repo.softDeleteVariety(id);
|
|
|
|
expect(await repo.watchVariety(id).first, isNull);
|
|
expect(await repo.watchInventory().first, isEmpty);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'linkSpecies exposes the scientific name and prefills empty category',
|
|
() async {
|
|
final species = newTestSpeciesRepository(db);
|
|
await species.seedBundled(const [_tomatoSeed]);
|
|
final match = (await species.search('tomato')).single;
|
|
|
|
final id = await repo.addQuickVariety(label: "Grandma's tomato");
|
|
await repo.linkSpecies(id, match.id);
|
|
|
|
final detail = await repo.watchVariety(id).first;
|
|
expect(detail!.scientificName, 'Solanum lycopersicum');
|
|
expect(detail.category, 'Solanaceae'); // prefilled from family
|
|
},
|
|
);
|
|
|
|
test('linkSpecies does not overwrite an existing category', () async {
|
|
final species = newTestSpeciesRepository(db);
|
|
await species.seedBundled(const [_tomatoSeed]);
|
|
final match = (await species.search('tomato')).single;
|
|
|
|
final id = await repo.addQuickVariety(label: 'T', category: 'My tomatoes');
|
|
await repo.linkSpecies(id, match.id);
|
|
|
|
final detail = await repo.watchVariety(id).first;
|
|
expect(detail!.category, 'My tomatoes');
|
|
});
|
|
}
|