Skip to content

Commit

Permalink
re-add all the old c tests, now in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Sep 16, 2023
1 parent 03fcb7e commit abf5dd4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 58 deletions.
2 changes: 1 addition & 1 deletion core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mod alter;
mod automigrate;
mod backfill;
mod bootstrap;
pub mod bootstrap;
pub mod c;
mod changes_vtab;
mod changes_vtab_read;
Expand Down
1 change: 0 additions & 1 deletion core/rs/core/src/tableinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use alloc::vec::Vec;
use core::ffi::c_char;
use core::ffi::c_int;
use core::ffi::c_void;
use core::mem;
use core::mem::forget;
use num_traits::ToPrimitive;
use sqlite_nostd as sqlite;
Expand Down
20 changes: 19 additions & 1 deletion core/rs/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,23 @@ mod tests {
use super::*;

#[test]
fn test_slab_rowid() {}
fn test_slab_rowid() {
let foo_slab = slab_rowid(0, 1);
let bar_slab = slab_rowid(1, 2);
let baz_slab = slab_rowid(2, 3);

assert_eq!(foo_slab, 1);
assert_eq!(bar_slab, 2 + crate::consts::ROWID_SLAB_SIZE);
assert_eq!(baz_slab, 3 + crate::consts::ROWID_SLAB_SIZE * 2);
assert_eq!(slab_rowid(0, crate::consts::ROWID_SLAB_SIZE), 0);
assert_eq!(slab_rowid(0, crate::consts::ROWID_SLAB_SIZE + 1), 1);

let foo_slab = slab_rowid(0, crate::consts::ROWID_SLAB_SIZE + 1);
let bar_slab = slab_rowid(1, crate::consts::ROWID_SLAB_SIZE + 2);
let baz_slab = slab_rowid(2, crate::consts::ROWID_SLAB_SIZE * 2 + 3);

assert_eq!(foo_slab, 1);
assert_eq!(bar_slab, 2 + crate::consts::ROWID_SLAB_SIZE);
assert_eq!(baz_slab, 3 + crate::consts::ROWID_SLAB_SIZE * 2);
}
}
96 changes: 41 additions & 55 deletions core/rs/integration-check/tests/tableinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sqlite::Connection;
use sqlite_nostd as sqlite;

// janx hax to do an `afterAll` cleanup
integration_utils::counter_setup!(1);
integration_utils::counter_setup!(4);

fn make_err_ptr() -> *mut *mut c_char {
let mut inner_ptr: *mut c_char = std::ptr::null_mut();
Expand All @@ -21,7 +21,6 @@ fn make_site() -> *mut c_char {
inner_ptr
}

// https://github.com/vlcn-io/cr-sqlite/pull/353/files#diff-6097dc2328b422bd90f6f46c9cd066c15238a757d3a7a92fb6d1964468657f7cL1
#[test]
fn test_ensure_table_infos_are_up_to_date() {
let db = integration_utils::opendb().expect("Opened DB");
Expand Down Expand Up @@ -201,6 +200,13 @@ fn test_is_table_compatible() {
assert_eq!(is_compatible, true);

// pks + other unique indices
c.exec_safe("CREATE TABLE booz (a PRIMARY KEY, b);")
.expect("made booz");
c.exec_safe("CREATE UNIQUE INDEX booz_b ON booz (b);")
.expect("made index");
let is_compatible = crsql_core::tableinfo::is_table_compatible(raw_db, "booz", make_err_ptr())
.expect("checked if booz is compatible");
assert_eq!(is_compatible, false);

// not null and no dflt
c.exec_safe("CREATE TABLE buzz (a PRIMARY KEY, b NOT NULL);")
Expand Down Expand Up @@ -269,59 +275,39 @@ fn test_is_table_compatible() {
decrement_counter();
}

// https://github.com/vlcn-io/cr-sqlite/pull/353/files#diff-6d6b961ba2bd1905d0f357b72927a8707992abc533322fe95f7d134421919ef1R83
#[test]
fn test_create_clock_table_from_table_info() {
// static void testCreateClockTable() {
// printf("CreateClockTable\n");

// sqlite3 *db;
// int rc;
// crsql_TableInfo *tc1;
// crsql_TableInfo *tc2;
// crsql_TableInfo *tc3;
// crsql_TableInfo *tc4;
// char *err = 0;

// rc = sqlite3_open(":memory:", &db);
// sqlite3_exec(db, "CREATE TABLE foo (a, b, primary key (a, b))", 0, 0, 0);
// sqlite3_exec(db, "CREATE TABLE bar (a primary key)", 0, 0, 0);
// sqlite3_exec(db, "CREATE TABLE baz (a primary key, b)", 0, 0, 0);
// sqlite3_exec(db, "CREATE TABLE boo (a primary key, b, c)", 0, 0, 0);

// rc = crsql_pull_table_info(db, "foo", &tc1, &err);
// CHECK_OK
// rc = crsql_pull_table_info(db, "bar", &tc2, &err);
// CHECK_OK
// rc = crsql_pull_table_info(db, "baz", &tc3, &err);
// CHECK_OK
// rc = crsql_pull_table_info(db, "boo", &tc4, &err);
// CHECK_OK

// rc = crsql_create_clock_table(db, tc1, &err);
// CHECK_OK
// rc = crsql_create_clock_table(db, tc2, &err);
// CHECK_OK
// rc = crsql_create_clock_table(db, tc3, &err);
// CHECK_OK
// rc = crsql_create_clock_table(db, tc4, &err);
// CHECK_OK

// crsql_free_table_info(tc1);
// crsql_free_table_info(tc2);
// crsql_free_table_info(tc3);
// crsql_free_table_info(tc4);

// // TODO: check that the tables have the expected schema

// printf("\t\e[0;32mSuccess\e[0m\n");
// crsql_close(db);
// return;

// fail:
// printf("err: %s %d\n", err, rc);
// sqlite3_free(err);
// crsql_close(db);
// assert(rc == SQLITE_OK);
// }
let db = integration_utils::opendb().expect("Opened DB");
let c = &db.db;
let raw_db = db.db.db;

c.exec_safe("CREATE TABLE foo (a, b, primary key (a, b));")
.expect("made foo");
c.exec_safe("CREATE TABLE bar (a primary key);")
.expect("made bar");
c.exec_safe("CREATE TABLE baz (a primary key, b);")
.expect("made baz");
c.exec_safe("CREATE TABLE boo (a primary key, b, c);")
.expect("made boo");

let foo_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "foo", make_err_ptr())
.expect("pulled table info for foo");
let bar_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "bar", make_err_ptr())
.expect("pulled table info for bar");
let baz_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "baz", make_err_ptr())
.expect("pulled table info for baz");
let boo_tbl_info = crsql_core::tableinfo::pull_table_info(raw_db, "boo", make_err_ptr())
.expect("pulled table info for boo");

crsql_core::bootstrap::create_clock_table(raw_db, &foo_tbl_info, make_err_ptr())
.expect("created clock table for foo");
crsql_core::bootstrap::create_clock_table(raw_db, &bar_tbl_info, make_err_ptr())
.expect("created clock table for bar");
crsql_core::bootstrap::create_clock_table(raw_db, &baz_tbl_info, make_err_ptr())
.expect("created clock table for baz");
crsql_core::bootstrap::create_clock_table(raw_db, &boo_tbl_info, make_err_ptr())
.expect("created clock table for boo");

// todo: Check that clock tables have expected schema(s)
decrement_counter();
}

0 comments on commit abf5dd4

Please sign in to comment.