Sur 229 kvs integration tests ()

This commit is contained in:
Przemyslaw Hugh Kaznowski 2023-05-31 08:36:23 +01:00 committed by GitHub
parent e835d27ecc
commit b2adca1851
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 1 deletions
lib/src/kvs

View file

@ -314,7 +314,7 @@ impl Transaction {
#[cfg(test)]
mod tests {
use crate::kvs::tests::transaction::verify_transaction_isolation;
use crate::kvs::tests::tx_isolation::transaction::verify_transaction_isolation;
use temp_dir::TempDir;
// https://github.com/surrealdb/surrealdb/issues/76

2
lib/src/kvs/tests/mod.rs Normal file
View file

@ -0,0 +1,2 @@
mod tb;
pub(crate) mod tx_isolation;

103
lib/src/kvs/tests/tb.rs Normal file
View file

@ -0,0 +1,103 @@
#[cfg(feature = "kv-mem")]
pub(crate) mod table {
use crate::err::Error;
use crate::key::tb;
use crate::key::tb::Tb;
use crate::kvs::Datastore;
use crate::sql::statements::DefineTableStatement;
struct TestContext {
db: Datastore,
}
async fn init() -> Result<TestContext, Error> {
let db = Datastore::new("memory").await?;
return Ok(TestContext {
db,
});
}
#[tokio::test]
#[rustfmt::skip]
async fn table_definitions_can_be_scanned() {
// Setup
let test = match init().await {
Ok(ctx) => ctx,
Err(e) => panic!("{:?}", e),
};
let mut tx = match test.db.transaction(true, false).await {
Ok(tx) => tx,
Err(e) => panic!("{:?}", e),
};
// Create a table definition
let namespace = "test_namespace";
let database = "test_database";
let table = "test_table";
let key = Tb::new(namespace, database, table);
let value = DefineTableStatement {
name: Default::default(),
drop: false,
full: false,
view: None,
permissions: Default::default(),
};
match tx.set(&key, &value).await {
Ok(_) => {}
Err(e) => panic!("{:?}", e),
};
// Validate with scan
match tx.scan(tb::prefix(namespace, database)..tb::suffix(namespace, database), 1000).await {
Ok(scan) => {
assert_eq!(scan.len(), 1);
let read = DefineTableStatement::from(&scan[0].1);
assert_eq!(&read, &value);
}
Err(e) => panic!("{:?}", e),
}
}
#[tokio::test]
async fn table_definitions_can_be_deleted() {
// Setup
let test = match init().await {
Ok(ctx) => ctx,
Err(e) => panic!("{:?}", e),
};
let mut tx = match test.db.transaction(true, false).await {
Ok(tx) => tx,
Err(e) => panic!("{:?}", e),
};
// Create a table definition
let namespace = "test_namespace";
let database = "test_database";
let table = "test_table";
let key = Tb::new(namespace, database, table);
let value = DefineTableStatement {
name: Default::default(),
drop: false,
full: false,
view: None,
permissions: Default::default(),
};
match tx.set(&key, &value).await {
Ok(_) => {}
Err(e) => panic!("{:?}", e),
};
// Validate delete
match tx.del(&key).await {
Ok(_) => {}
Err(e) => panic!("{:?}", e),
};
// Should not exist
match tx.get(&key).await {
Ok(None) => {}
Ok(Some(o)) => panic!("Should not exist but was {:?}", o),
Err(e) => panic!("Unexpected error on get {:?}", e),
};
}
}