Skip to content

Commit

Permalink
use tempfile for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Sep 27, 2024
1 parent 13b1ba7 commit 0c31327
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 39 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/torii/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ dojo-test-utils = { path = "../../dojo-test-utils" }
dojo-utils.workspace = true
katana-runner.workspace = true
scarb.workspace = true
tempfile.workspace = true
46 changes: 19 additions & 27 deletions crates/torii/core/src/sql_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use dojo_world::contracts::world::{WorldContract, WorldContractReader};
use katana_runner::RunnerCtx;
use scarb::compiler::Profile;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::SqlitePool;
use starknet::accounts::Account;
use starknet::core::types::{Call, Felt};
use starknet::core::utils::{get_contract_address, get_selector_from_name};
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Provider};
use starknet_crypto::poseidon_hash_many;
use tempfile::NamedTempFile;
use tokio::sync::broadcast;

use crate::engine::{Engine, EngineConfig, Processors};
Expand All @@ -29,6 +31,20 @@ use crate::processors::store_update_member::StoreUpdateMemberProcessor;
use crate::processors::store_update_record::StoreUpdateRecordProcessor;
use crate::sql::Sql;

pub async fn setup_sqlite_pool() -> Result<SqlitePool, Box<dyn std::error::Error>> {
let tempfile = NamedTempFile::new().unwrap();
let path = tempfile.path().to_string_lossy();
let options = SqliteConnectOptions::from_str(&path).unwrap().create_if_missing(true);
let pool = SqlitePoolOptions::new()
.min_connections(1)
.idle_timeout(None)
.max_lifetime(None)
.connect_with(options)
.await?;
sqlx::migrate!("../migrations").run(&pool).await?;
Ok(pool)
}

pub async fn bootstrap_engine<P>(
world: WorldContractReader<P>,
db: Sql,
Expand Down Expand Up @@ -69,15 +85,7 @@ where
#[tokio::test(flavor = "multi_thread")]
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn test_load_from_remote(sequencer: &RunnerCtx) {
let options = SqliteConnectOptions::from_str("").unwrap().create_if_missing(true);
let pool = SqlitePoolOptions::new()
.min_connections(1)
.idle_timeout(None)
.max_lifetime(None)
.connect_with(options)
.await
.unwrap();
sqlx::migrate!("../migrations").run(&pool).await.unwrap();
let pool = setup_sqlite_pool().await.unwrap();

let setup = CompilerTestSetup::from_examples("../../dojo-core", "../../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);
Expand Down Expand Up @@ -224,15 +232,7 @@ async fn test_load_from_remote(sequencer: &RunnerCtx) {
#[tokio::test(flavor = "multi_thread")]
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn test_load_from_remote_del(sequencer: &RunnerCtx) {
let options = SqliteConnectOptions::from_str("").unwrap().create_if_missing(true);
let pool = SqlitePoolOptions::new()
.min_connections(1)
.idle_timeout(None)
.max_lifetime(None)
.connect_with(options)
.await
.unwrap();
sqlx::migrate!("../migrations").run(&pool).await.unwrap();
let pool = setup_sqlite_pool().await.unwrap();

let setup = CompilerTestSetup::from_examples("../../dojo-core", "../../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);
Expand Down Expand Up @@ -332,15 +332,7 @@ async fn test_load_from_remote_del(sequencer: &RunnerCtx) {
#[tokio::test(flavor = "multi_thread")]
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn test_update_with_set_record(sequencer: &RunnerCtx) {
let options = SqliteConnectOptions::from_str("").unwrap().create_if_missing(true);
let pool = SqlitePoolOptions::new()
.min_connections(1)
.idle_timeout(None)
.max_lifetime(None)
.connect_with(options)
.await
.unwrap();
sqlx::migrate!("../migrations").run(&pool).await.unwrap();
let pool = setup_sqlite_pool().await.unwrap();

let setup = CompilerTestSetup::from_examples("../../dojo-core", "../../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);
Expand Down
1 change: 1 addition & 0 deletions crates/torii/graphql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ scarb.workspace = true
serial_test = "2.0.0"
starknet-crypto.workspace = true
starknet.workspace = true
tempfile.workspace = true
6 changes: 4 additions & 2 deletions crates/torii/graphql/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use starknet::core::types::{Call, Felt, InvokeTransactionResult};
use starknet::macros::selector;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Provider};
use tempfile::NamedTempFile;
use tokio::sync::broadcast;
use tokio_stream::StreamExt;
use torii_core::engine::{Engine, EngineConfig, Processors};
Expand Down Expand Up @@ -276,8 +277,9 @@ pub async fn model_fixtures(db: &mut Sql) {
}

pub async fn spinup_types_test() -> Result<SqlitePool> {
// change sqlite::memory: to sqlite:~/.test.db to dump database to disk
let options = SqliteConnectOptions::from_str("")?.create_if_missing(true).with_regexp();
let tempfile = NamedTempFile::new().unwrap();
let path = tempfile.path().to_string_lossy();
let options = SqliteConnectOptions::from_str(&path).unwrap().create_if_missing(true).with_regexp();
let pool = SqlitePoolOptions::new()
.min_connections(1)
.idle_timeout(None)
Expand Down
10 changes: 1 addition & 9 deletions crates/torii/grpc/src/server/tests/entities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,7 @@ use crate::types::schema::Entity;
#[tokio::test(flavor = "multi_thread")]
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn test_entities_queries(sequencer: &RunnerCtx) {
let options = SqliteConnectOptions::from_str("").unwrap().create_if_missing(true).with_regexp();
let pool = SqlitePoolOptions::new()
.min_connections(1)
.idle_timeout(None)
.max_lifetime(None)
.connect_with(options)
.await
.unwrap();
sqlx::migrate!("../migrations").run(&pool).await.unwrap();
let pool = setup_sqlite_pool().await.unwrap();

let setup = CompilerTestSetup::from_examples("../../dojo-core", "../../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);
Expand Down
5 changes: 4 additions & 1 deletion crates/torii/libp2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ mod test {
use starknet::providers::JsonRpcClient;
use starknet::signers::SigningKey;
use starknet_crypto::Felt;
use tempfile::NamedTempFile;
use tokio::select;
use tokio::sync::broadcast;
use tokio::time::sleep;
Expand All @@ -549,7 +550,9 @@ mod test {
.try_init();

// Database
let options = <SqliteConnectOptions as std::str::FromStr>::from_str("")
let tempfile = NamedTempFile::new().unwrap();
let path = tempfile.path().to_string_lossy();
let options = <SqliteConnectOptions as std::str::FromStr>::from_str(&path)
.unwrap()
.create_if_missing(true);
let pool = SqlitePoolOptions::new()
Expand Down

0 comments on commit 0c31327

Please sign in to comment.