diff --git a/simulator/main.rs b/simulator/main.rs index fb3892c4d..2d72c81b8 100644 --- a/simulator/main.rs +++ b/simulator/main.rs @@ -1,9 +1,8 @@ use clap::Parser; use generation::plan::{Interaction, InteractionPlan, ResultSet}; -use generation::{pick_index, Arbitrary, ArbitraryFrom}; +use generation::{pick_index, ArbitraryFrom}; use limbo_core::{Connection, Database, Result, RowResult, IO}; -use model::query::{Create, Query}; -use model::table::{Column, Name, Table, Value}; +use model::table::Value; use rand::prelude::*; use rand_chacha::ChaCha8Rng; use runner::cli::SimulatorCLI; @@ -18,7 +17,6 @@ use tempfile::TempDir; mod generation; mod model; -mod properties; mod runner; #[allow(clippy::arc_with_non_send_sync)] @@ -289,100 +287,3 @@ fn compare_equal_rows(a: &[Vec], b: &[Vec]) { } } } - -fn maybe_add_table(env: &mut SimulatorEnv, conn: &mut Rc) -> Result<()> { - if env.tables.len() < env.opts.max_tables { - let table = Table { - rows: Vec::new(), - name: Name::arbitrary(&mut env.rng).0, - columns: (1..env.rng.gen_range(1..128)) - .map(|_| Column::arbitrary(&mut env.rng)) - .collect(), - }; - let query = Query::Create(Create { - table: table.clone(), - }); - let rows = get_all_rows(env, conn, query.to_string().as_str())?; - log::debug!("{:?}", rows); - let rows = get_all_rows( - env, - conn, - format!( - "SELECT sql FROM sqlite_schema WHERE type IN ('table', 'index') AND name = '{}';", - table.name - ) - .as_str(), - )?; - log::debug!("{:?}", rows); - assert!(rows.len() == 1); - let as_text = match &rows[0][0] { - Value::Text(t) => t, - _ => unreachable!(), - }; - assert!( - *as_text != query.to_string(), - "table was not inserted correctly" - ); - env.tables.push(table); - } - Ok(()) -} - -fn get_all_rows( - env: &mut SimulatorEnv, - conn: &mut Rc, - query: &str, -) -> Result>> { - log::info!("running query '{}'", &query[0..query.len().min(4096)]); - let mut out = Vec::new(); - let rows = conn.query(query); - if rows.is_err() { - let err = rows.err(); - log::error!( - "Error running query '{}': {:?}", - &query[0..query.len().min(4096)], - err - ); - return Err(err.unwrap()); - } - let rows = rows.unwrap(); - assert!(rows.is_some()); - let mut rows = rows.unwrap(); - 'rows_loop: loop { - env.io.inject_fault(env.rng.gen_ratio(1, 10000)); - match rows.next_row()? { - RowResult::Row(row) => { - let mut r = Vec::new(); - for el in &row.values { - let v = match el { - limbo_core::Value::Null => Value::Null, - limbo_core::Value::Integer(i) => Value::Integer(*i), - limbo_core::Value::Float(f) => Value::Float(*f), - limbo_core::Value::Text(t) => Value::Text(t.to_string()), - limbo_core::Value::Blob(b) => Value::Blob(b.to_vec()), - }; - r.push(v); - } - - out.push(r); - } - RowResult::IO => { - env.io.inject_fault(env.rng.gen_ratio(1, 10000)); - if env.io.run_once().is_err() { - log::info!("query inject fault"); - break 'rows_loop; - } - } - RowResult::Interrupt => { - break; - } - RowResult::Done => { - break; - } - RowResult::Busy => { - // for now let's retry? - } - } - } - Ok(out) -} diff --git a/simulator/properties.rs b/simulator/properties.rs deleted file mode 100644 index a6536d1d8..000000000 --- a/simulator/properties.rs +++ /dev/null @@ -1,78 +0,0 @@ -use std::rc::Rc; - -use limbo_core::Connection; -use rand::Rng; - -use crate::{ - compare_equal_rows, - generation::ArbitraryFrom, - get_all_rows, - model::{ - query::{Insert, Predicate, Query, Select}, - table::Value, - }, - SimulatorEnv, -}; - -pub fn property_insert_select(env: &mut SimulatorEnv, conn: &mut Rc) { - // Get a random table - let table = env.rng.gen_range(0..env.tables.len()); - - // Pick a random column - let column_index = env.rng.gen_range(0..env.tables[table].columns.len()); - let column = &env.tables[table].columns[column_index].clone(); - - let mut rng = env.rng.clone(); - - // Generate a random value of the column type - let value = Value::arbitrary_from(&mut rng, &column.column_type); - - // Create a whole new row - let mut row = Vec::new(); - for (i, column) in env.tables[table].columns.iter().enumerate() { - if i == column_index { - row.push(value.clone()); - } else { - let value = Value::arbitrary_from(&mut rng, &column.column_type); - row.push(value); - } - } - - // Insert the row - let query = Query::Insert(Insert { - table: env.tables[table].name.clone(), - values: row.clone(), - }); - let _ = get_all_rows(env, conn, query.to_string().as_str()).unwrap(); - // Shadow operation on the table - env.tables[table].rows.push(row.clone()); - - // Create a query that selects the row - let query = Query::Select(Select { - table: env.tables[table].name.clone(), - predicate: Predicate::Eq(column.name.clone(), value), - }); - - // Get all rows - let rows = get_all_rows(env, conn, query.to_string().as_str()).unwrap(); - - // Check that the row is there - assert!(rows.iter().any(|r| r == &row)); -} - -pub fn property_select_all(env: &mut SimulatorEnv, conn: &mut Rc) { - // Get a random table - let table = env.rng.gen_range(0..env.tables.len()); - - // Create a query that selects all rows - let query = Query::Select(Select { - table: env.tables[table].name.clone(), - predicate: Predicate::And(Vec::new()), - }); - - // Get all rows - let rows = get_all_rows(env, conn, query.to_string().as_str()).unwrap(); - - // Make sure the rows are the same - compare_equal_rows(&rows, &env.tables[table].rows); -}