From 300f278ff302757f49e09a6c99c1040ad2a6fe2c Mon Sep 17 00:00:00 2001 From: Nikita Sivukhin Date: Sun, 2 Feb 2025 19:34:15 +0400 Subject: [PATCH] use TempDatabase from commons in tests/ --- tests/Cargo.toml | 4 --- tests/integration/common.rs | 7 ++++ .../fuzz/grammar_generator.rs | 0 tests/{ => integration}/fuzz/mod.rs | 32 ++++++++++++------- tests/integration/mod.rs | 1 + 5 files changed, 29 insertions(+), 15 deletions(-) rename tests/{ => integration}/fuzz/grammar_generator.rs (100%) rename tests/{ => integration}/fuzz/mod.rs (85%) diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 3ee12139f..f3e67d73a 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -14,10 +14,6 @@ path = "lib.rs" name = "integration_tests" path = "integration/mod.rs" -[[test]] -name = "fuzz_tests" -path = "fuzz/mod.rs" - [dependencies] anyhow = "1.0.75" clap = { version = "4.5", features = ["derive"] } diff --git a/tests/integration/common.rs b/tests/integration/common.rs index 07c840b23..9fbb9eb41 100644 --- a/tests/integration/common.rs +++ b/tests/integration/common.rs @@ -12,6 +12,13 @@ pub struct TempDatabase { #[allow(dead_code, clippy::arc_with_non_send_sync)] impl TempDatabase { + pub fn new_empty() -> Self { + let mut path = TempDir::new().unwrap().into_path(); + path.push("test.db"); + let io: Arc = Arc::new(limbo_core::PlatformIO::new().unwrap()); + + Self { path, io } + } pub fn new(table_sql: &str) -> Self { let mut path = TempDir::new().unwrap().into_path(); path.push("test.db"); diff --git a/tests/fuzz/grammar_generator.rs b/tests/integration/fuzz/grammar_generator.rs similarity index 100% rename from tests/fuzz/grammar_generator.rs rename to tests/integration/fuzz/grammar_generator.rs diff --git a/tests/fuzz/mod.rs b/tests/integration/fuzz/mod.rs similarity index 85% rename from tests/fuzz/mod.rs rename to tests/integration/fuzz/mod.rs index 3dc40f7b4..91e8c5f2f 100644 --- a/tests/fuzz/mod.rs +++ b/tests/integration/fuzz/mod.rs @@ -2,14 +2,25 @@ pub mod grammar_generator; #[cfg(test)] mod tests { - use std::{rc::Rc, sync::Arc}; + use std::rc::Rc; - use limbo_core::Database; use rand::SeedableRng; use rand_chacha::ChaCha8Rng; use rusqlite::params; - use crate::grammar_generator::{rand_int, GrammarGenerator}; + use crate::{ + common::TempDatabase, + fuzz::grammar_generator::{rand_int, GrammarGenerator}, + }; + + fn rng_from_time() -> (ChaCha8Rng, u64) { + let seed = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + let rng = ChaCha8Rng::seed_from_u64(seed); + (rng, seed) + } fn sqlite_exec_row(conn: &rusqlite::Connection, query: &str) -> Vec { let mut stmt = conn.prepare(&query).unwrap(); @@ -56,9 +67,8 @@ mod tests { #[test] pub fn arithmetic_expression_fuzz_ex1() { - let io = Arc::new(limbo_core::PlatformIO::new().unwrap()); - let limbo_db = Database::open_file(io, ":memory:").unwrap(); - let limbo_conn = limbo_db.connect(); + let db = TempDatabase::new_empty(); + let limbo_conn = db.connect_limbo(); let sqlite_conn = rusqlite::Connection::open_in_memory().unwrap(); for query in [ @@ -118,13 +128,13 @@ mod tests { let sql = g.create().concat(" ").push_str("SELECT").push(expr).build(); - let io = Arc::new(limbo_core::PlatformIO::new().unwrap()); - let limbo_db = Database::open_file(io, ":memory:").unwrap(); - let limbo_conn = limbo_db.connect(); + let db = TempDatabase::new_empty(); + let limbo_conn = db.connect_limbo(); let sqlite_conn = rusqlite::Connection::open_in_memory().unwrap(); - let mut rng = ChaCha8Rng::seed_from_u64(0); - for _ in 0..16 * 1024 { + let (mut rng, seed) = rng_from_time(); + println!("seed: {}", seed); + for _ in 0..1024 { let query = g.generate(&mut rng, sql, 50); let limbo = limbo_exec_row(&limbo_conn, &query); let sqlite = sqlite_exec_row(&sqlite_conn, &query); diff --git a/tests/integration/mod.rs b/tests/integration/mod.rs index 221ca089f..01bb224dd 100644 --- a/tests/integration/mod.rs +++ b/tests/integration/mod.rs @@ -1,4 +1,5 @@ mod common; mod functions; +mod fuzz; mod pragma; mod query_processing;