109 lines
2.6 KiB
Dart
109 lines
2.6 KiB
Dart
import 'dart:io';
|
|
import 'dart:math';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:receipity/services/product_repository.dart';
|
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
|
|
var _dbSeq = 0;
|
|
var _sqliteReady = false;
|
|
|
|
void ensureTestSqlite() {
|
|
if (_sqliteReady) return;
|
|
sqfliteFfiInit();
|
|
databaseFactory = databaseFactoryFfi;
|
|
_sqliteReady = true;
|
|
}
|
|
|
|
ProductRepository memoryRepository() {
|
|
ensureTestSqlite();
|
|
final dir = Directory(p.join('.dart_tool', 'receipity_test_db'))
|
|
..createSync(recursive: true);
|
|
final name =
|
|
'db_${DateTime.now().microsecondsSinceEpoch}_${_dbSeq++}_${Random().nextInt(1 << 32)}.sqlite';
|
|
return ProductRepository(databasePath: p.join(dir.path, name));
|
|
}
|
|
|
|
Future<void> pumpUntilFound(
|
|
WidgetTester tester,
|
|
Finder finder, {
|
|
int attempts = 40,
|
|
}) async {
|
|
for (var i = 0; i < attempts; i++) {
|
|
await tester.pump();
|
|
if (finder.evaluate().isNotEmpty) return;
|
|
await tester.runAsync(
|
|
() => Future<void>.delayed(const Duration(milliseconds: 20)),
|
|
);
|
|
}
|
|
throw TestFailure('Timed out waiting for $finder');
|
|
}
|
|
|
|
const _seedTs = '2026-08-01T00:00:00.000';
|
|
|
|
Future<void> seedCaseConflictCatalog(ProductRepository repository) async {
|
|
await repository.replaceAllData(
|
|
darkMode: false,
|
|
supermarkets: [
|
|
{'id': 1, 'name': 'Jumbo', 'color': 0xFFEEC21B, 'sort_order': 0},
|
|
],
|
|
products: [
|
|
{
|
|
'id': 1,
|
|
'name': 'Broccoli',
|
|
'normalized_name': 'BROCCOLI_A',
|
|
'last_price': 1.5,
|
|
'times_seen': 1,
|
|
'favourite': 0,
|
|
'supermarket': 'Jumbo',
|
|
'created_at': _seedTs,
|
|
'updated_at': _seedTs,
|
|
},
|
|
{
|
|
'id': 2,
|
|
'name': 'BROCCOLI',
|
|
'normalized_name': 'BROCCOLI_B',
|
|
'last_price': 1.8,
|
|
'times_seen': 1,
|
|
'favourite': 0,
|
|
'supermarket': 'Jumbo',
|
|
'created_at': _seedTs,
|
|
'updated_at': _seedTs,
|
|
},
|
|
],
|
|
shops: [
|
|
{
|
|
'id': 1,
|
|
'supermarket': 'Jumbo',
|
|
'shopped_at': '2026-08-01T00:00:00.000',
|
|
'total': 1.5,
|
|
'created_at': _seedTs,
|
|
},
|
|
{
|
|
'id': 2,
|
|
'supermarket': 'Jumbo',
|
|
'shopped_at': '2026-08-02T00:00:00.000',
|
|
'total': 1.8,
|
|
'created_at': _seedTs,
|
|
},
|
|
],
|
|
shopItems: [
|
|
{
|
|
'id': 1,
|
|
'shop_id': 1,
|
|
'product_id': 1,
|
|
'name': 'Broccoli',
|
|
'price': 1.5,
|
|
},
|
|
{
|
|
'id': 2,
|
|
'shop_id': 2,
|
|
'product_id': 2,
|
|
'name': 'BROCCOLI',
|
|
'price': 1.8,
|
|
},
|
|
],
|
|
);
|
|
}
|