112 lines
3.5 KiB
Dart
112 lines
3.5 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:groci/models/line_item.dart';
|
|
|
|
import 'test_helpers.dart';
|
|
|
|
void main() {
|
|
ensureTestSqlite();
|
|
|
|
test(
|
|
'deleting a product keeps the trip line and drops the catalog row',
|
|
() async {
|
|
final repository = memoryRepository();
|
|
final shopId = await repository.saveShop(
|
|
supermarket: 'Jumbo',
|
|
shoppedAt: DateTime(2026, 8, 12),
|
|
items: [LineItem(id: '1', name: 'Milk', price: 1.5)],
|
|
);
|
|
|
|
final products = await repository.getAll();
|
|
expect(products, hasLength(1));
|
|
await repository.deleteProduct(products.single.id);
|
|
|
|
expect(await repository.getAll(), isEmpty);
|
|
final items = await repository.getShopItems(shopId);
|
|
expect(items, hasLength(1));
|
|
expect(items.single.name, 'Milk');
|
|
expect(items.single.productId, isNull);
|
|
},
|
|
);
|
|
|
|
test('addShopItem appends a line and updates the trip total', () async {
|
|
final repository = memoryRepository();
|
|
final shopId = await repository.saveShop(
|
|
supermarket: 'Jumbo',
|
|
shoppedAt: DateTime(2026, 8, 12),
|
|
items: [LineItem(id: '1', name: 'Milk', price: 1.5)],
|
|
);
|
|
|
|
await repository.addShopItem(
|
|
shopId: shopId,
|
|
item: LineItem(id: '2', name: 'Bread', price: 2),
|
|
);
|
|
|
|
final shop = (await repository.getShop(shopId))!;
|
|
expect(shop.total, 3.5);
|
|
final items = await repository.getShopItems(shopId);
|
|
expect(items, hasLength(2));
|
|
expect(items.map((item) => item.name), ['Milk', 'Bread']);
|
|
expect((await repository.getAll()).map((product) => product.name).toSet(), {
|
|
'Milk',
|
|
'Bread',
|
|
});
|
|
});
|
|
|
|
test('getLastTripSupermarkets lists unique stores newest first', () async {
|
|
final repository = memoryRepository();
|
|
await repository.saveShop(
|
|
supermarket: 'Jumbo',
|
|
shoppedAt: DateTime(2026, 8, 1),
|
|
items: [LineItem(id: '1', name: 'Milk', price: 1.5)],
|
|
);
|
|
await repository.saveShop(
|
|
supermarket: 'Albert Heijn',
|
|
shoppedAt: DateTime(2026, 8, 12),
|
|
items: [LineItem(id: '2', name: 'Bread', price: 2)],
|
|
);
|
|
|
|
expect(await repository.getLastTripSupermarkets(), [
|
|
'Albert Heijn',
|
|
'Jumbo',
|
|
]);
|
|
});
|
|
|
|
test('wipeAllData clears products, trips, lists, and settings', () async {
|
|
final repository = memoryRepository();
|
|
await repository.saveShop(
|
|
supermarket: 'Jumbo',
|
|
shoppedAt: DateTime(2026, 8, 12),
|
|
items: [LineItem(id: '1', name: 'Milk', price: 1.5)],
|
|
);
|
|
await repository.createShoppingList('Weekly');
|
|
await repository.setDarkMode(true);
|
|
|
|
await repository.wipeAllData();
|
|
|
|
expect(await repository.getAll(), isEmpty);
|
|
expect(await repository.getShops(), isEmpty);
|
|
expect(await repository.getShoppingLists(), isEmpty);
|
|
expect(await repository.getDarkMode(), isFalse);
|
|
expect((await repository.getSupermarkets()).length, greaterThan(0));
|
|
});
|
|
|
|
test('deleting all products leaves shopping trips in place', () async {
|
|
final repository = memoryRepository();
|
|
await repository.saveShop(
|
|
supermarket: 'Jumbo',
|
|
shoppedAt: DateTime(2026, 8, 12),
|
|
items: [
|
|
LineItem(id: '1', name: 'Milk', price: 1.5),
|
|
LineItem(id: '2', name: 'Bread', price: 2),
|
|
],
|
|
);
|
|
await repository.deleteAllProducts();
|
|
|
|
expect(await repository.getAll(), isEmpty);
|
|
expect(await repository.getShops(), hasLength(1));
|
|
final overview = await repository.getOverview();
|
|
expect(overview.productCount, 0);
|
|
expect(overview.tripCount, 1);
|
|
});
|
|
}
|