Skip to content

Commit e574015

Browse files
committed
Tests
1 parent 819c3cf commit e574015

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ homepage = "https://powersync.com"
3737
repository = "https://github.com/powersync-ja/powersync-sqlite-core"
3838

3939
[workspace.dependencies]
40-
sqlite_nostd = { path = "./sqlite-rs-embedded/sqlite_nostd" }
40+
sqlite_nostd = { path="./sqlite-rs-embedded/sqlite_nostd" }

crates/core/src/json_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
8181

8282
db.create_function_v2(
8383
"powersync_strip_subtype",
84-
-1,
84+
1,
8585
sqlite::UTF8 | sqlite::DETERMINISTIC | SQLITE_SUBTYPE | SQLITE_RESULT_SUBTYPE,
8686
None,
8787
Some(powersync_strip_subtype),

crates/core/src/views.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -415,26 +415,17 @@ fn json_object_fragment<'a>(
415415
let mut column_names_quoted: Vec<String> = alloc::vec![];
416416
while let Some(column) = columns.next() {
417417
let name = &*column.name;
418-
let quoted = match &*column.type_name {
419-
// We really want the individual columns here to appear as they show up in the database.
420-
// For text columns however, it's possible that e.g. NEW.column was created by a JSON
421-
// function, meaning that it has a JSON subtype active - causing the json_object() call
422-
// we're about to emit to include it as a subobject instead of a string.
423-
"TEXT" | "text" => format!(
424-
"{:}, powersync_strip_subtype({:}.{:})",
425-
QuotedString(name),
426-
prefix,
427-
quote_identifier(name)
428-
),
429-
_ => format!(
430-
"{:}, {:}.{:}",
431-
QuotedString(name),
432-
prefix,
433-
quote_identifier(name)
434-
),
435-
};
436418

437-
column_names_quoted.push(quoted);
419+
// We really want the individual columns here to appear as they show up in the database.
420+
// For text columns however, it's possible that e.g. NEW.column was created by a JSON
421+
// function, meaning that it has a JSON subtype active - causing the json_object() call
422+
// we're about to emit to include it as a subobject instead of a string.
423+
column_names_quoted.push(format!(
424+
"{:}, powersync_strip_subtype({:}.{:})",
425+
QuotedString(name),
426+
prefix,
427+
quote_identifier(name)
428+
));
438429
}
439430

440431
// SQLITE_MAX_COLUMN - 1 (because of the id column)

dart/test/crud_test.dart

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ void main() {
662662
expect(db.select('SELECT * FROM ps_crud'), hasLength(1));
663663
});
664664

665-
test('json values are included as text', () {
665+
test('preserves values in text column', () {
666666
db
667667
..execute('select powersync_replace_schema(?)', [
668668
json.encode({
@@ -675,31 +675,44 @@ void main() {
675675
}
676676
]
677677
})
678-
])
679-
..execute('INSERT INTO items (id, col) VALUES (uuid(), json_object())');
678+
]);
679+
680+
db.execute('INSERT INTO items (id, col) VALUES (uuid(), json_object())');
681+
final [insert] = db.select('SELECT data FROM ps_crud');
682+
expect(json.decode(insert['data']), containsPair('data', {'col': '{}'}));
683+
db.execute('DELETE FROM ps_crud');
680684

685+
db.execute('UPDATE items SET col = NULL;');
681686
final [update] = db.select('SELECT data FROM ps_crud');
682-
expect(json.decode(update['data']), containsPair('data', {'col': '{}'}));
687+
expect(json.decode(update['data']), containsPair('data', {'col': null}));
688+
db.execute('DELETE FROM ps_crud');
683689
});
684690

685-
test('null values are included as null', () {
691+
test('preserves mismatched type', () {
686692
db
687693
..execute('select powersync_replace_schema(?)', [
688694
json.encode({
689695
'tables': [
690696
{
691697
'name': 'items',
692698
'columns': [
693-
{'name': 'col', 'type': 'text'}
699+
{'name': 'col', 'type': 'int'}
694700
],
695701
}
696702
]
697703
})
698704
])
699-
..execute('INSERT INTO items (id, col) VALUES (uuid(), null)');
700-
701-
final [update] = db.select('SELECT data FROM ps_crud');
702-
expect(json.decode(update['data']), containsPair('data', {}));
705+
..execute('insert into items (id, col) values (uuid(), json_object())')
706+
..execute('insert into items (id, col) values (uuid(), null)')
707+
..execute('insert into items (id, col) values (uuid(), ?)',
708+
['not an integer']);
709+
710+
final data = db.select('SELECT data FROM ps_crud');
711+
expect(data.map((row) => jsonDecode(row['data'])), [
712+
containsPair('data', {'col': '{}'}),
713+
containsPair('data', {}),
714+
containsPair('data', {'col': 'not an integer'}),
715+
]);
703716
});
704717
});
705718
}

0 commit comments

Comments
 (0)