Skip to content

Commit 09b841b

Browse files
committed
add test
1 parent d764359 commit 09b841b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

packages/drift_sqlite_async/test/basic_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:sqlite_async/sqlite_async.dart';
1111
import 'package:test/test.dart';
1212

1313
import './utils/test_utils.dart';
14+
import 'generated/database.dart';
1415

1516
class EmptyDatabase extends GeneratedDatabase {
1617
EmptyDatabase(super.executor);
@@ -245,4 +246,43 @@ INSERT INTO test_data(description) VALUES('test data');
245246
expect(row, isEmpty);
246247
});
247248
});
249+
250+
test('transform table updates', () async {
251+
final path = dbPath();
252+
await cleanDb(path: path);
253+
254+
final db = await setupDatabase(path: path);
255+
final connection = SqliteAsyncDriftConnection(
256+
db,
257+
// tables with the local_ prefix are mapped to the name without the prefix
258+
transformTableUpdates: (event) {
259+
final updates = <TableUpdate>{};
260+
261+
for (final originalTableName in event.tables) {
262+
final effectiveName = originalTableName.startsWith("local_")
263+
? originalTableName.substring(6)
264+
: originalTableName;
265+
updates.add(TableUpdate(effectiveName));
266+
}
267+
268+
return updates;
269+
},
270+
);
271+
272+
// Create table with a different name than drift. (Mimicking a table name backed by a view in PowerSync with the optional sync strategy)
273+
await db.execute(
274+
'CREATE TABLE local_todos(id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT)',
275+
);
276+
277+
final dbu = TodoDatabase.fromSqliteAsyncConnection(connection);
278+
279+
final tableUpdatesFut =
280+
dbu.tableUpdates(TableUpdateQuery.onTableName("todos")).first;
281+
282+
// This insert will trigger the sqlite_async "updates" stream
283+
await db.execute("INSERT INTO local_todos(description) VALUES('Test 1')");
284+
285+
expect(await tableUpdatesFut.timeout(const Duration(seconds: 2)),
286+
{TableUpdate("todos")});
287+
});
248288
}

packages/drift_sqlite_async/test/generated/database.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class TodoItems extends Table {
1515
@DriftDatabase(tables: [TodoItems])
1616
class TodoDatabase extends _$TodoDatabase {
1717
TodoDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));
18+
19+
TodoDatabase.fromSqliteAsyncConnection(SqliteAsyncDriftConnection super.conn);
1820

1921
@override
2022
int get schemaVersion => 1;

0 commit comments

Comments
 (0)