Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(torii): use correct query_type #2476

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl Sql {
Argument::String(event_id.to_string()),
Argument::String(utc_dt_string_from_timestamp(block_timestamp)),
],
QueryType::Other,
QueryType::EventMessage(entity.clone()),
);
self.query_queue.enqueue(
"INSERT INTO event_model (entity_id, model_id) VALUES (?, ?) ON CONFLICT(entity_id, \
Expand Down
57 changes: 57 additions & 0 deletions crates/torii/libp2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ mod test {
use crypto_bigint::U256;
use dojo_types::primitive::Primitive;
use dojo_types::schema::{Enum, EnumOption, Member, Struct, Ty};
use dojo_world::contracts::abi::model::Layout;
use futures::StreamExt;
use katana_runner::KatanaRunner;
use serde_json::Number;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use starknet::core::types::Felt;
use torii_core::simple_broker::SimpleBroker;
use torii_core::sql::Sql;
use torii_core::types::EventMessage;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

Expand Down Expand Up @@ -680,6 +686,57 @@ mod test {
}
}

// Test to verify that setting an entity message in the SQL database
// triggers a publish event on the broker
#[tokio::test]
async fn test_entity_message_trigger_publish() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt()
.with_env_filter("torii::relay::client=debug,torii::relay::server=debug")
.try_init();

let options = <SqliteConnectOptions as std::str::FromStr>::from_str("sqlite::memory:")
.unwrap()
.create_if_missing(true);
let pool = SqlitePoolOptions::new().max_connections(5).connect_with(options).await.unwrap();
sqlx::migrate!("../migrations").run(&pool).await.unwrap();

let mut db = Sql::new(pool.clone(), Felt::ZERO).await.unwrap();
let mut broker = SimpleBroker::<EventMessage>::subscribe();

let entity = Ty::Struct(Struct { name: "Message".to_string(), children: vec![] });
db.register_model(
"test_namespace",
entity.clone(),
Layout::Fixed(vec![]),
Felt::ZERO,
Felt::ZERO,
0,
0,
0,
)
.await?;

// FIXME: register_model and set_event_message handle the name and namespace of entity type
// differently.
let entity =
Ty::Struct(Struct { name: "test_namespace-Message".to_string(), children: vec![] });
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved

// Set the event message in the database
db.set_event_message(entity, "some_entity_id", 0).await?;
db.query_queue.execute_all().await?;

// Check if a message was published to the broker
tokio::select! {
Some(message) = broker.next() => {
println!("Received message: {:?}", message);
Ok(())
},
_ = tokio::time::sleep(std::time::Duration::from_secs(5)) => {
Err("Timeout: No message received".into())
}
}
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_client_connection_wasm() -> Result<(), Box<dyn Error>> {
Expand Down
Loading