veilid/veilid-flutter/example/integration_test/test_table_db.dart

134 lines
4.0 KiB
Dart
Raw Normal View History

2024-03-16 23:57:46 -04:00
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:veilid/veilid.dart';
2024-05-02 14:15:42 -04:00
const testDb = '__dart_test_db';
const testNonexistentDb = '__dart_test_nonexistent_db';
2024-03-16 23:57:46 -04:00
Future<void> testDeleteTableDbNonExistent() async {
expect(await Veilid.instance.deleteTableDB(testNonexistentDb), isFalse);
}
Future<void> testOpenDeleteTableDb() async {
// delete test db if it exists
await Veilid.instance.deleteTableDB(testDb);
final tdb = await Veilid.instance.openTableDB(testDb, 1);
try {
2024-05-02 14:15:42 -04:00
await expectLater(() async => Veilid.instance.deleteTableDB(testDb),
2024-03-16 23:57:46 -04:00
throwsA(isA<VeilidAPIException>()));
} finally {
tdb.close();
}
expect(await Veilid.instance.deleteTableDB(testDb), isTrue);
}
Future<void> testOpenTwiceTableDb() async {
// delete test db if it exists
await Veilid.instance.deleteTableDB(testDb);
final tdb = await Veilid.instance.openTableDB(testDb, 1);
final tdb2 = await Veilid.instance.openTableDB(testDb, 1);
// delete should fail because open
2024-05-02 14:15:42 -04:00
await expectLater(() async => Veilid.instance.deleteTableDB(testDb),
2024-03-16 23:57:46 -04:00
throwsA(isA<VeilidAPIException>()));
tdb.close();
// delete should fail because open
2024-05-02 14:15:42 -04:00
await expectLater(() async => Veilid.instance.deleteTableDB(testDb),
2024-03-16 23:57:46 -04:00
throwsA(isA<VeilidAPIException>()));
tdb2.close();
// delete should now succeed
expect(await Veilid.instance.deleteTableDB(testDb), isTrue);
}
Future<void> testOpenTwiceTableDbStoreLoad() async {
// delete test db if it exists
await Veilid.instance.deleteTableDB(testDb);
final tdb = await Veilid.instance.openTableDB(testDb, 1);
try {
final tdb2 = await Veilid.instance.openTableDB(testDb, 1);
try {
// store into first db copy
2024-05-02 14:15:42 -04:00
await tdb.store(0, utf8.encode('asdf'), utf8.encode('1234'));
2024-03-16 23:57:46 -04:00
// load from second db copy
expect(
2024-05-02 14:15:42 -04:00
await tdb2.load(0, utf8.encode('asdf')), equals(utf8.encode('1234')));
2024-03-16 23:57:46 -04:00
} finally {
tdb2.close();
}
} finally {
tdb.close();
}
// delete should now succeed
expect(await Veilid.instance.deleteTableDB(testDb), isTrue);
}
Future<void> testOpenTwiceTableDbStoreDeleteLoad() async {
// delete test db if it exists
await Veilid.instance.deleteTableDB(testDb);
final tdb = await Veilid.instance.openTableDB(testDb, 1);
try {
final tdb2 = await Veilid.instance.openTableDB(testDb, 1);
try {
// store into first db copy
2024-05-02 14:15:42 -04:00
await tdb.store(0, utf8.encode('asdf'), utf8.encode('1234'));
2024-03-16 23:57:46 -04:00
// delete from second db copy and clean up
2024-05-02 14:15:42 -04:00
await tdb2.delete(0, utf8.encode('asdf'));
2024-03-16 23:57:46 -04:00
} finally {
tdb2.close();
}
// load from first db copy
2024-05-02 14:15:42 -04:00
expect(await tdb.load(0, utf8.encode('asdf')), isNull);
2024-03-16 23:57:46 -04:00
} finally {
tdb.close();
}
// delete should now succeed
expect(await Veilid.instance.deleteTableDB(testDb), isTrue);
}
Future<void> testResizeTableDb() async {
// delete test db if it exists
await Veilid.instance.deleteTableDB(testDb);
final tdb = await Veilid.instance.openTableDB(testDb, 1);
try {
// reopen the db with more columns should fail if it is already open
2024-05-02 14:15:42 -04:00
await expectLater(() async => Veilid.instance.openTableDB(testDb, 2),
2024-03-16 23:57:46 -04:00
throwsA(isA<VeilidAPIException>()));
} finally {
tdb.close();
}
final tdb2 = await Veilid.instance.openTableDB(testDb, 2);
try {
// write something to second column
2024-05-02 14:15:42 -04:00
await tdb2.store(1, utf8.encode('qwer'), utf8.encode('5678'));
2024-03-16 23:57:46 -04:00
// reopen the db with fewer columns
final tdb3 = await Veilid.instance.openTableDB(testDb, 1);
try {
// Should fail access to second column
2024-05-02 14:15:42 -04:00
await expectLater(() async => tdb3.load(1, utf8.encode('qwer')),
2024-03-16 23:57:46 -04:00
throwsA(isA<VeilidAPIException>()));
// Should succeed with access to second column
expect(
2024-05-02 14:15:42 -04:00
await tdb2.load(1, utf8.encode('qwer')), equals(utf8.encode('5678')));
2024-03-16 23:57:46 -04:00
} finally {
tdb3.close();
}
} finally {
tdb2.close();
}
// delete should now succeed
expect(await Veilid.instance.deleteTableDB(testDb), isTrue);
}