Add tests for insert

next
Sayan Nandan 1 year ago
parent 2488053318
commit b714647905
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -52,6 +52,7 @@ pub fn insert(gns: &GlobalNS, insert: InsertStatement) -> DatabaseResult<()> {
})
}
// TODO(@ohsayan): optimize null case
fn prepare_insert(
model: &ModelData,
fields: &Fields,

@ -116,6 +116,15 @@ impl Row {
pub fn d_data(&self) -> &RwLock<RowData> {
self.rc.data()
}
#[cfg(test)]
pub fn cloned_data(&self) -> Vec<(Box<str>, Datacell)> {
self.d_data()
.read()
.fields()
.st_iter_kv()
.map(|(id, data)| (id.clone(), data.clone()))
.collect()
}
}
impl Row {

@ -27,7 +27,7 @@
use crate::engine::{
core::{
model::{alt::AlterPlan, ModelData},
tests::model::{create, exec_create},
tests::ddl_model::{create, exec_create},
GlobalNS,
},
error::DatabaseResult,

@ -134,7 +134,7 @@ mod exec {
use crate::engine::{
core::{
model::{DeltaVersion, Field, Layer},
tests::model::{exec_create_new_space, with_model},
tests::ddl_model::{exec_create_new_space, with_model},
GlobalNS,
},
data::tag::{DataTag, FullTag},

@ -0,0 +1,68 @@
/*
* Created on Tue May 09 2023
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
* NoSQL database written by Sayan Nandan ("the Author") with the
* vision to provide flexibility in data modelling without compromising
* on performance, queryability or scalability.
*
* Copyright (c) 2023, Sayan Nandan <ohsayan@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
use crate::engine::{core::GlobalNS, data::cell::Datacell};
#[derive(sky_macros::Wrapper)]
struct Tuple(Vec<(Box<str>, Datacell)>);
#[test]
fn insert_simple() {
let gns = GlobalNS::empty();
super::exec_insert(
&gns,
"create model myspace.mymodel(username: string, password: string)",
"insert into myspace.mymodel('sayan', 'pass123')",
"sayan",
|row| {
assert_veceq_transposed!(row.cloned_data(), Tuple(pairvec!(("password", "pass123"))));
},
)
.unwrap();
}
#[test]
fn insert_with_null() {
let gns = GlobalNS::empty();
super::exec_insert(
&gns,
"create model myspace.mymodel(username: string, null useless_password: string, null useless_email: string, null useless_random_column: uint64)",
"insert into myspace.mymodel('sayan', null, null, null)",
"sayan",
|row| {
assert_veceq_transposed!(
row.cloned_data(),
Tuple(
pairvec!(
("useless_password", Datacell::null()),
("useless_email", Datacell::null()),
("useless_random_column", Datacell::null())
)
)
)
}
).unwrap();
}

@ -0,0 +1,65 @@
/*
* Created on Tue May 09 2023
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
* NoSQL database written by Sayan Nandan ("the Author") with the
* vision to provide flexibility in data modelling without compromising
* on performance, queryability or scalability.
*
* Copyright (c) 2023, Sayan Nandan <ohsayan@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
mod insert;
use crate::engine::{
core::{dml, index::Row, model::ModelData, GlobalNS},
data::lit::LitIR,
error::DatabaseResult,
ql::{ast::parse_ast_node_full, dml::ins::InsertStatement, tests::lex_insecure},
sync,
};
pub(self) fn exec_insert<T>(
gns: &GlobalNS,
model: &str,
insert: &str,
key_name: &str,
f: impl Fn(Row) -> T,
) -> DatabaseResult<T> {
if !gns.spaces().read().contains_key("myspace") {
gns.test_new_empty_space("myspace");
}
let lex_create_model = lex_insecure(model.as_bytes()).unwrap();
let stmt_create_model = parse_ast_node_full(&lex_create_model[2..]).unwrap();
ModelData::exec_create(gns, stmt_create_model)?;
let lex_insert = lex_insecure(insert.as_bytes()).unwrap();
let stmt_insert = parse_ast_node_full::<InsertStatement>(&lex_insert[1..]).unwrap();
let entity = stmt_insert.entity();
dml::insert(gns, stmt_insert)?;
let guard = sync::atm::cpin();
gns.with_model(entity, |mdl| {
let _irm = mdl.intent_read_model();
let row = mdl
.primary_index()
.select(LitIR::from(key_name), &guard)
.unwrap()
.clone();
drop(guard);
Ok(f(row))
})
}

@ -24,6 +24,6 @@
*
*/
// ddl
mod model;
mod space;
mod ddl_model;
mod ddl_space;
mod dml;

@ -147,7 +147,6 @@ macro_rules! into_dict {
}
#[cfg(test)]
#[allow(unused_macros)]
macro_rules! into_vec {
($($val:expr),* $(,)?) => { vec![$(::core::convert::From::from($val),)*]};
macro_rules! pairvec {
($($x:expr),*) => {{ let mut v = Vec::new(); $( let (a, b) = $x; v.push((a.into(), b.into())); )* v }};
}

Loading…
Cancel
Save