Skip to content

Commit

Permalink
fix: sql tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Sep 27, 2024
1 parent ef9fafc commit 4ec379c
Showing 1 changed file with 24 additions and 43 deletions.
67 changes: 24 additions & 43 deletions crates/torii/core/src/sql_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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};
Expand All @@ -31,7 +30,7 @@ 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>> {
pub async fn setup_sqlite_pool(world_address: Felt) -> Result<Sql, 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);
Expand All @@ -42,7 +41,16 @@ pub async fn setup_sqlite_pool() -> Result<SqlitePool, Box<dyn std::error::Error
.connect_with(options)
.await?;
sqlx::migrate!("../migrations").run(&pool).await?;
Ok(pool)

let (shutdown_tx, _) = broadcast::channel(1);
let (mut executor, sender) = Executor::new(pool.clone(), shutdown_tx.clone()).await.unwrap();
tokio::spawn(async move {
executor.run().await.unwrap();
});

let db = Sql::new(pool.clone(), world_address, sender).await.unwrap();

Ok(db)
}

pub async fn bootstrap_engine<P>(
Expand Down Expand Up @@ -85,8 +93,6 @@ 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 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 @@ -151,27 +157,20 @@ async fn test_load_from_remote(sequencer: &RunnerCtx) {
TransactionWaiter::new(tx.transaction_hash, &provider).await.unwrap();

let world_reader = WorldContractReader::new(strat.world_address, Arc::clone(&provider));

let (shutdown_tx, _) = broadcast::channel(1);
let (mut executor, sender) = Executor::new(pool.clone(), shutdown_tx.clone()).await.unwrap();
tokio::spawn(async move {
executor.run().await.unwrap();
});

let db = Sql::new(pool.clone(), world_reader.address, sender).await.unwrap();
let db = setup_sqlite_pool(world_reader.address).await.unwrap();

let _ = bootstrap_engine(world_reader, db.clone(), provider).await.unwrap();

let _block_timestamp = 1710754478_u64;
let models = sqlx::query("SELECT * FROM models").fetch_all(&pool).await.unwrap();
let models = sqlx::query("SELECT * FROM models").fetch_all(&db.pool).await.unwrap();
assert_eq!(models.len(), 10);

let (id, name, namespace, packed_size, unpacked_size): (String, String, String, u8, u8) =
sqlx::query_as(
"SELECT id, name, namespace, packed_size, unpacked_size FROM models WHERE name = \
'Position'",
)
.fetch_one(&pool)
.fetch_one(&db.pool)
.await
.unwrap();

Expand All @@ -186,7 +185,7 @@ async fn test_load_from_remote(sequencer: &RunnerCtx) {
"SELECT id, name, namespace, packed_size, unpacked_size FROM models WHERE name = \
'Moves'",
)
.fetch_one(&pool)
.fetch_one(&db.pool)
.await
.unwrap();

Expand All @@ -201,7 +200,7 @@ async fn test_load_from_remote(sequencer: &RunnerCtx) {
"SELECT id, name, namespace, packed_size, unpacked_size FROM models WHERE name = \
'PlayerConfig'",
)
.fetch_one(&pool)
.fetch_one(&db.pool)
.await
.unwrap();

Expand All @@ -211,8 +210,8 @@ async fn test_load_from_remote(sequencer: &RunnerCtx) {
assert_eq!(packed_size, 0);
assert_eq!(unpacked_size, 0);

assert_eq!(count_table("entities", &pool).await, 2);
assert_eq!(count_table("event_messages", &pool).await, 1);
assert_eq!(count_table("entities", &db.pool).await, 2);
assert_eq!(count_table("event_messages", &db.pool).await, 1);

let (id, keys): (String, String) = sqlx::query_as(
format!(
Expand All @@ -221,7 +220,7 @@ async fn test_load_from_remote(sequencer: &RunnerCtx) {
)
.as_str(),
)
.fetch_one(&pool)
.fetch_one(&db.pool)
.await
.unwrap();

Expand All @@ -232,8 +231,6 @@ 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 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 @@ -310,20 +307,13 @@ async fn test_load_from_remote_del(sequencer: &RunnerCtx) {
TransactionWaiter::new(res.transaction_hash, &provider).await.unwrap();

let world_reader = WorldContractReader::new(strat.world_address, Arc::clone(&provider));

let (shutdown_tx, _) = broadcast::channel(1);
let (mut executor, sender) = Executor::new(pool.clone(), shutdown_tx.clone()).await.unwrap();
tokio::spawn(async move {
executor.run().await.unwrap();
});

let db = Sql::new(pool.clone(), world_reader.address, sender).await.unwrap();
let db = setup_sqlite_pool(world_reader.address).await.unwrap();

let _ = bootstrap_engine(world_reader, db.clone(), provider).await;

assert_eq!(count_table("dojo_examples-PlayerConfig", &pool).await, 0);
assert_eq!(count_table("dojo_examples-PlayerConfig$favorite_item", &pool).await, 0);
assert_eq!(count_table("dojo_examples-PlayerConfig$items", &pool).await, 0);
assert_eq!(count_table("dojo_examples-PlayerConfig", &db.pool).await, 0);
assert_eq!(count_table("dojo_examples-PlayerConfig$favorite_item", &db.pool).await, 0);
assert_eq!(count_table("dojo_examples-PlayerConfig$items", &db.pool).await, 0);

// TODO: check how we can have a test that is more chronological with Torii re-syncing
// to ensure we can test intermediate states.
Expand All @@ -332,8 +322,6 @@ 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 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 @@ -398,14 +386,7 @@ async fn test_update_with_set_record(sequencer: &RunnerCtx) {
TransactionWaiter::new(move_res.transaction_hash, &provider).await.unwrap();

let world_reader = WorldContractReader::new(strat.world_address, Arc::clone(&provider));

let (shutdown_tx, _) = broadcast::channel(1);
let (mut executor, sender) = Executor::new(pool.clone(), shutdown_tx.clone()).await.unwrap();
tokio::spawn(async move {
executor.run().await.unwrap();
});

let db = Sql::new(pool.clone(), world_reader.address, sender).await.unwrap();
let db = setup_sqlite_pool(world_reader.address).await.unwrap();

let _ = bootstrap_engine(world_reader, db.clone(), Arc::clone(&provider)).await.unwrap();
}
Expand Down

0 comments on commit 4ec379c

Please sign in to comment.