cyberhybridhub/server/test/integration/spelling_words_api_test.dart
2026-06-07 03:10:21 +00:00

85 lines
2.5 KiB
Dart

@Tags(<String>['integration', 'postgres'])
library;
import 'package:cyberhybridhub_server/spelling/spelling_items_db.dart';
import 'package:test/test.dart';
import '../helpers/test_db.dart';
void main() {
TestDb? testDb;
setUpAll(() async {
testDb = await TestDb.open();
});
tearDown(() async {
if (testDb != null) {
await testDb!.truncateTradingTables();
await testDb!.connection.execute('DELETE FROM spelling_items');
await testDb!.connection.execute('DELETE FROM spelling_words');
await testDb!.spellingItemsDb.ensureCatalogSeeded();
}
});
tearDownAll(() async {
await testDb?.close();
});
test('addCanonicalWord and listCanonicalWords', () async {
if (testDb == null) {
markTestSkipped('Set DATABASE_URL or TEST_DATABASE_URL for integration tests');
return;
}
final SpellingItemsDb db = testDb!.spellingItemsDb;
final bool added = await db.addCanonicalWord('customword');
expect(added, isTrue);
final bool duplicate = await db.addCanonicalWord('customword');
expect(duplicate, isFalse);
final List<String> words = await db.listCanonicalWords();
expect(words, contains('customword'));
});
test('importCanonicalWords adds and skips duplicates', () async {
if (testDb == null) {
markTestSkipped('Set DATABASE_URL or TEST_DATABASE_URL for integration tests');
return;
}
final SpellingItemsDb db = testDb!.spellingItemsDb;
final ({int added, int skipped}) result = await db.importCanonicalWords(
<String>['alpha', 'beta', 'alpha', '', ' '],
);
expect(result.added, 2);
expect(result.skipped, 1);
final List<String> words = await db.listCanonicalWords();
expect(words, contains('alpha'));
expect(words, contains('beta'));
});
test('deleteCanonicalWord deletes word and cascades progress and links', () async {
if (testDb == null) {
markTestSkipped('Set DATABASE_URL or TEST_DATABASE_URL for integration tests');
return;
}
final SpellingItemsDb db = testDb!.spellingItemsDb;
final bool added = await db.addCanonicalWord('deleteme');
expect(added, isTrue);
final List<String> wordsBefore = await db.listCanonicalWords();
expect(wordsBefore, contains('deleteme'));
final bool deleted = await db.deleteCanonicalWord('deleteme');
expect(deleted, isTrue);
final List<String> wordsAfter = await db.listCanonicalWords();
expect(wordsAfter, isNot(contains('deleteme')));
});
}