Photos: - Repository addPhoto/removePhoto; VarietyDetail.photos (list); loads all photos. - Detail: a 140×140 photo carousel with page dots, delete-current, and add-photo (gallery); empty state shows an add-photo button. - watchInventory now re-emits on attachment + species changes (StreamGroup), so a photo added on the detail screen refreshes the inventory avatar immediately (fixes avatars not updating). Also carries the concurrent harvest-date work: lots gain an optional harvest month (schemaVersion 3 + from2To3 migration, drift_schema_v3 exported), a month/year harvest picker, and localized month names. 48 tests green (add/remove photos, avatar reactivity, v1→v3 migration); Linux runs.
215 lines
6.7 KiB
Dart
215 lines
6.7 KiB
Dart
import 'dart:typed_data';
|
|
|
|
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 'package:tane/db/enums.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
|
|
|
|
// The inventory list row also surfaces the scientific name (subtitle).
|
|
final row = (await repo.watchInventory().first).single;
|
|
expect(row.scientificName, 'Solanum lycopersicum');
|
|
},
|
|
);
|
|
|
|
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');
|
|
});
|
|
|
|
test(
|
|
'addGerminationTest surfaces the latest derived rate on the lot',
|
|
() async {
|
|
final id = await repo.addQuickVariety(label: 'Maize');
|
|
final lotId = await repo.addLot(varietyId: id, harvestYear: 2024);
|
|
await repo.addGerminationTest(
|
|
lotId: lotId,
|
|
testedOn: 1000,
|
|
sampleSize: 20,
|
|
germinatedCount: 18,
|
|
);
|
|
|
|
final lot = (await repo.watchVariety(id).first)!.lots.single;
|
|
expect(lot.germinationTests, hasLength(1));
|
|
expect(lot.latestGerminationRate, closeTo(0.9, 1e-9));
|
|
},
|
|
);
|
|
|
|
test('watchVariety reacts to a germination test being added', () async {
|
|
final id = await repo.addQuickVariety(label: 'Maize');
|
|
final lotId = await repo.addLot(varietyId: id);
|
|
final queue = StreamQueue(repo.watchVariety(id));
|
|
|
|
final initial = await queue.next;
|
|
expect(initial!.lots.single.germinationTests, isEmpty);
|
|
|
|
await repo.addGerminationTest(
|
|
lotId: lotId,
|
|
sampleSize: 10,
|
|
germinatedCount: 9,
|
|
);
|
|
|
|
final updated = await queue.next;
|
|
expect(updated!.lots.single.latestGerminationRate, closeTo(0.9, 1e-9));
|
|
|
|
await queue.cancel();
|
|
});
|
|
|
|
test('updateLot changes a lot\'s year, type and quantity', () async {
|
|
final id = await repo.addQuickVariety(label: 'Maize');
|
|
final lotId = await repo.addLot(
|
|
varietyId: id,
|
|
harvestYear: 2020,
|
|
quantity: const Quantity(kind: QuantityKind.packet, count: 1),
|
|
);
|
|
|
|
await repo.updateLot(
|
|
lotId: lotId,
|
|
type: LotType.plant,
|
|
harvestYear: 2024,
|
|
quantity: const Quantity(kind: QuantityKind.plant, count: 3),
|
|
);
|
|
|
|
final lot = (await repo.watchVariety(id).first)!.lots.single;
|
|
expect(lot.type, LotType.plant);
|
|
expect(lot.harvestYear, 2024);
|
|
expect(lot.quantity?.kind, QuantityKind.plant);
|
|
expect(lot.quantity?.count, 3);
|
|
});
|
|
|
|
test('softDeleteLot removes the lot from the variety', () async {
|
|
final id = await repo.addQuickVariety(label: 'Maize');
|
|
final lotId = await repo.addLot(varietyId: id, harvestYear: 2024);
|
|
|
|
await repo.softDeleteLot(lotId);
|
|
|
|
expect((await repo.watchVariety(id).first)!.lots, isEmpty);
|
|
});
|
|
|
|
test('add and remove vernacular names', () async {
|
|
final id = await repo.addQuickVariety(label: 'Maize');
|
|
final nameId = await repo.addVernacularName(id, 'Maíz');
|
|
|
|
expect(
|
|
(await repo.watchVariety(id).first)!.vernacularNames.map((n) => n.name),
|
|
['Maíz'],
|
|
);
|
|
|
|
await repo.removeVernacularName(nameId);
|
|
expect((await repo.watchVariety(id).first)!.vernacularNames, isEmpty);
|
|
});
|
|
|
|
test('add and remove photos (multiple, ordered)', () async {
|
|
final id = await repo.addQuickVariety(label: 'Maize');
|
|
final p1 = await repo.addPhoto(id, Uint8List.fromList([1, 2, 3]));
|
|
await repo.addPhoto(id, Uint8List.fromList([4, 5, 6]));
|
|
|
|
expect((await repo.watchVariety(id).first)!.photos.length, 2);
|
|
|
|
await repo.removePhoto(p1);
|
|
final photos = (await repo.watchVariety(id).first)!.photos;
|
|
expect(photos.map((p) => p.bytes.toList()), [
|
|
[4, 5, 6],
|
|
]);
|
|
});
|
|
|
|
test('watchInventory avatar refreshes when a photo is added later', () async {
|
|
final id = await repo.addQuickVariety(label: 'Maize');
|
|
final queue = StreamQueue(repo.watchInventory());
|
|
|
|
expect((await queue.next).single.photo, isNull);
|
|
|
|
await repo.addPhoto(id, Uint8List.fromList([1, 2, 3]));
|
|
expect((await queue.next).single.photo, isNotNull);
|
|
|
|
await queue.cancel();
|
|
});
|
|
}
|