Skip to content

Commit 73bcfdc

Browse files
committed
Expand tests
1 parent 9b6ae2d commit 73bcfdc

6 files changed

Lines changed: 42 additions & 15 deletions

File tree

crates/core/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ pub enum RawPowerSyncError {
248248
libversion_number: c_int,
249249
libversion: &'static str,
250250
},
251-
#[error("{function_name} may only be called in transactions.")]
252-
MustBeCalledInTransaction { function_name: &'static str },
251+
#[error("This function may only be called in transactions.")]
252+
MustBeCalledInTransaction,
253253
}
254254

255255
#[derive(Debug)]

crates/core/src/schema/management.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ fn powersync_replace_schema_impl(
266266
args: &[*mut sqlite::value],
267267
) -> Result<String, PowerSyncError> {
268268
let db = ctx.db_handle();
269-
verify_in_transaction(db, "powersync_replace_schema")?;
269+
verify_in_transaction(db)?;
270270

271271
let schema = args[0].text();
272272
let state = unsafe { DatabaseState::from_context(&ctx) };

crates/core/src/sync/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub fn register(db: *mut sqlite::sqlite3, state: Rc<DatabaseState>) -> Result<()
206206
) -> () {
207207
let result = (|| -> Result<(), PowerSyncError> {
208208
let db = ctx.db_handle();
209-
verify_in_transaction(db, "powersync_control")?;
209+
verify_in_transaction(db)?;
210210

211211
let state = unsafe { DatabaseState::from_context(&ctx) };
212212
let args = sqlite::args!(argc, argv);

crates/core/src/utils/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ use crate::error::{PowerSyncError, RawPowerSyncError};
1212
use uuid::Uuid;
1313

1414
#[cold]
15-
fn must_be_in_tx_error(function_name: &'static str) -> PowerSyncError {
16-
return RawPowerSyncError::MustBeCalledInTransaction { function_name }.into();
15+
fn must_be_in_tx_error() -> PowerSyncError {
16+
return RawPowerSyncError::MustBeCalledInTransaction.into();
1717
}
1818

1919
#[inline]
20-
pub fn verify_in_transaction(
21-
db: *mut sqlite3,
22-
function_name: &'static str,
23-
) -> Result<(), PowerSyncError> {
20+
pub fn verify_in_transaction(db: *mut sqlite3) -> Result<(), PowerSyncError> {
2421
if db.get_autocommit() {
25-
return Err(must_be_in_tx_error(function_name));
22+
return Err(must_be_in_tx_error());
2623
}
2724

2825
Ok(())

crates/core/src/view_admin.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn powersync_init_impl(
3636
_args: &[*mut sqlite::value],
3737
) -> Result<String, PowerSyncError> {
3838
let db = ctx.db_handle();
39-
verify_in_transaction(db, "powersync_init")?;
39+
verify_in_transaction(db)?;
4040
powersync_migrate(ctx, LATEST_VERSION)?;
4141

4242
// Register the powersync_internal_close vtab to implement a "pre-close hook".
@@ -54,7 +54,7 @@ fn powersync_test_migration_impl(
5454
args: &[*mut sqlite::value],
5555
) -> Result<String, PowerSyncError> {
5656
let db = ctx.db_handle();
57-
verify_in_transaction(db, "powersync_test_migration")?;
57+
verify_in_transaction(db)?;
5858

5959
let target_version = args[0].int();
6060
powersync_migrate(ctx, target_version)?;
@@ -73,7 +73,7 @@ fn powersync_clear_impl(
7373
args: &[*mut sqlite::value],
7474
) -> Result<String, PowerSyncError> {
7575
let local_db = ctx.db_handle();
76-
verify_in_transaction(local_db, "powersync_clear")?;
76+
verify_in_transaction(local_db)?;
7777
let state = unsafe { DatabaseState::from_context(&ctx) };
7878

7979
let flags = PowerSyncClearFlags(args[0].int());
@@ -180,7 +180,7 @@ fn powersync_trigger_resync_impl(
180180
args: &[*mut sqlite::value],
181181
) -> Result<String, PowerSyncError> {
182182
let local_db = ctx.db_handle();
183-
verify_in_transaction(local_db, "powersync_trigger_resync")?;
183+
verify_in_transaction(local_db)?;
184184

185185
let state = unsafe { DatabaseState::from_context(&ctx) };
186186
trigger_resync(local_db, state)?;

dart/test/error_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
import 'package:sqlite3/common.dart';
24
import 'package:test/test.dart';
35

@@ -12,6 +14,34 @@ void main() {
1214
db = openTestDatabase();
1315
});
1416

17+
test('not in transaction', () {
18+
void expectErrorOutsideOfTransaction(String sql, String functionName,
19+
[List<Object?> args = const []]) {
20+
expect(
21+
() => db.execute(sql, args),
22+
throwsA(isSqliteException(
23+
21,
24+
'$functionName: This function may only be called in transactions.',
25+
)),
26+
);
27+
}
28+
29+
expectErrorOutsideOfTransaction(
30+
'SELECT powersync_init()', 'powersync_init');
31+
db.executeInTx('SELECT powersync_init()');
32+
33+
expectErrorOutsideOfTransaction('SELECT powersync_control(?, ?)',
34+
'powersync_control', ['STOP', null]);
35+
expectErrorOutsideOfTransaction('SELECT powersync_replace_schema(?)',
36+
'powersync_replace_schema', [json.encode({})]);
37+
expectErrorOutsideOfTransaction('SELECT powersync_test_migration(?)',
38+
'powersync_test_migration', [123]);
39+
expectErrorOutsideOfTransaction(
40+
'SELECT powersync_clear(?)', 'powersync_clear', [0]);
41+
expectErrorOutsideOfTransaction(
42+
'SELECT powersync_trigger_resync(TRUE)', 'powersync_trigger_resync');
43+
});
44+
1545
test('contain inner SQLite descriptions', () {
1646
// Create a wrong migrations table for the core extension to trip over.
1747
db.execute('CREATE TABLE IF NOT EXISTS ps_migration(foo TEXT)');

0 commit comments

Comments
 (0)