database: add open table tests

This commit is contained in:
hinto.janai 2024-06-13 16:27:43 -04:00
parent 4a04625a8b
commit 3dab3f234f
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
3 changed files with 76 additions and 28 deletions

52
Cargo.lock generated
View file

@ -559,6 +559,32 @@ dependencies = [
"tracing",
]
[[package]]
name = "cuprate-database"
version = "0.0.1"
dependencies = [
"bitflags 2.5.0",
"bytemuck",
"bytes",
"cfg-if",
"cuprate-helper",
"cuprate-test-utils",
"cuprate-types",
"curve25519-dalek",
"heed",
"hex",
"hex-literal",
"monero-pruning",
"monero-serai",
"page_size",
"paste",
"pretty_assertions",
"redb",
"serde",
"tempfile",
"thiserror",
]
[[package]]
name = "cuprate-fast-sync"
version = "0.1.0"
@ -733,32 +759,6 @@ dependencies = [
"parking_lot_core",
]
[[package]]
name = "database"
version = "0.0.0"
dependencies = [
"bitflags 2.5.0",
"bytemuck",
"bytes",
"cfg-if",
"cuprate-helper",
"cuprate-test-utils",
"cuprate-types",
"curve25519-dalek",
"heed",
"hex",
"hex-literal",
"monero-pruning",
"monero-serai",
"page_size",
"paste",
"pretty_assertions",
"redb",
"serde",
"tempfile",
"thiserror",
]
[[package]]
name = "diff"
version = "0.1.13"

View file

@ -32,6 +32,17 @@ Which reads as:
1. You open a particular `Table` from that `Environment`, getting a `Database`
1. You can now read/write data from/to that `Database`
# Concrete types
You should _not_ rely on the concrete type of any abstracted backend.
For example, when using the `heed` backend, [`Env`]'s associated [`TxRw`] type
is `RefCell<heed::RwTxn<'_>>`. In order to ensure compatibility with other backends
and to not create backend-specific code, you should _not_ refer to that concrete type.
Use generics and trait notation in these situations:
- `impl<T: TxRw> Trait for Object`
- `fn() -> impl TxRw`
# `ConcreteEnv`
This crate exposes [`ConcreteEnv`], which is a non-generic/non-dynamic,
concrete object representing a database [`Env`]ironment.
@ -108,7 +119,8 @@ impl cuprate_database::Table for Table {
// Open up a transaction + tables for writing.
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw()?;
env_inner.create_db::<Table>(&tx_rw)?; // we must create it or the next line will panic.
// We must create the table first or the next line will error.
env_inner.create_db::<Table>(&tx_rw)?;
let mut table = env_inner.open_db_rw::<Table>(&tx_rw)?;
// Write data to the table.

View file

@ -71,7 +71,7 @@ fn open_db() {
/// Assert that opening a read-only table before creating errors.
#[test]
fn open_uncreated_table() {
fn open_ro_uncreated_table() {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_ro = env_inner.tx_ro().unwrap();
@ -81,6 +81,42 @@ fn open_uncreated_table() {
assert!(matches!(error, Err(RuntimeError::TableNotFound)));
}
/// Assert that opening a read/write table before creating is OK.
#[test]
fn open_rw_uncreated_table() {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw().unwrap();
// Open uncreated table.
let _table = env_inner.open_db_rw::<TestTable>(&tx_rw).unwrap();
}
/// Assert that opening a read-only table after creating is OK.
#[test]
fn open_ro_created_table() {
let (env, _tempdir) = tmp_concrete_env();
let env_inner = env.env_inner();
// Assert uncreated table errors.
{
let tx_ro = env_inner.tx_ro().unwrap();
let error = env_inner.open_db_ro::<TestTable>(&tx_ro);
assert!(matches!(error, Err(RuntimeError::TableNotFound)));
}
// Create table.
{
let tx_rw = env_inner.tx_rw().unwrap();
env_inner.create_db::<TestTable>(&tx_rw).unwrap();
TxRw::commit(tx_rw).unwrap();
}
// Assert created table is now OK.
let tx_ro = env_inner.tx_ro().unwrap();
let _table = env_inner.open_db_ro::<TestTable>(&tx_ro).unwrap();
}
/// Test `Env` resizes.
#[test]
fn resize() {