Skip to content

Commit

Permalink
fix(torii): graphql tests failing (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody authored Sep 8, 2023
1 parent b99ba3d commit 1d3f47d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
17 changes: 9 additions & 8 deletions crates/torii/graphql/src/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Entity {
pub struct Moves {
pub __typename: String,
pub remaining: u32,
pub last_direction: u8,
pub entity: Option<Entity>,
}

Expand All @@ -46,7 +47,7 @@ pub struct Position {
pub entity: Option<Entity>,
}

pub enum Direction {
pub enum Paginate {
Forward,
Backward,
}
Expand Down Expand Up @@ -74,9 +75,9 @@ pub async fn entity_fixtures(pool: &SqlitePool) {
let state = init(pool).await;

// Set entity with one moves component
// remaining: 10
// remaining: 10, last_direction: 0
let key = vec![FieldElement::ONE];
let moves_values = vec![FieldElement::from_hex_be("0xa").unwrap()];
let moves_values = vec![FieldElement::from_hex_be("0xa").unwrap(), FieldElement::ZERO];
state.set_entity("Moves".to_string(), key, moves_values.clone()).await.unwrap();

// Set entity with one position component
Expand All @@ -90,11 +91,11 @@ pub async fn entity_fixtures(pool: &SqlitePool) {
state.set_entity("Position".to_string(), key, position_values.clone()).await.unwrap();

// Set an entity with both moves and position components
// remaining: 1
// remaining: 1, last_direction: 0
// x: 69
// y: 42
let key = vec![FieldElement::THREE];
let moves_values = vec![FieldElement::from_hex_be("0x1").unwrap()];
let moves_values = vec![FieldElement::from_hex_be("0x1").unwrap(), FieldElement::ZERO];
let position_values = vec![
FieldElement::from_hex_be("0x45").unwrap(),
FieldElement::from_hex_be("0x2a").unwrap(),
Expand All @@ -120,12 +121,12 @@ pub async fn init(pool: &SqlitePool) -> Sql {
pub async fn paginate(
pool: &SqlitePool,
cursor: Option<String>,
direction: Direction,
direction: Paginate,
page_size: usize,
) -> Connection<Entity> {
let (first_last, before_after) = match direction {
Direction::Forward => ("first", "after"),
Direction::Backward => ("last", "before"),
Paginate::Forward => ("first", "after"),
Paginate::Backward => ("last", "before"),
};

let cursor = cursor.map_or(String::new(), |c| format!(", {before_after}: \"{c}\""));
Expand Down
1 change: 1 addition & 0 deletions crates/torii/graphql/src/tests/components_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod tests {
node {
__typename
remaining
last_direction
}
cursor
}
Expand Down
12 changes: 6 additions & 6 deletions crates/torii/graphql/src/tests/entities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod tests {
use starknet_crypto::{poseidon_hash_many, FieldElement};

use crate::tests::common::{
entity_fixtures, paginate, run_graphql_query, Direction, Entity, Moves, Position,
entity_fixtures, paginate, run_graphql_query, Entity, Moves, Paginate, Position,
};

#[sqlx::test(migrations = "../migrations")]
Expand Down Expand Up @@ -41,6 +41,7 @@ mod tests {
__typename
... on Moves {{
remaining
last_direction
}}
... on Position {{
x
Expand Down Expand Up @@ -73,27 +74,26 @@ mod tests {
let page_size = 2;

// Forward pagination
let entities_connection = paginate(&pool, None, Direction::Forward, page_size).await;
let entities_connection = paginate(&pool, None, Paginate::Forward, page_size).await;
assert_eq!(entities_connection.total_count, 3);
assert_eq!(entities_connection.edges.len(), page_size);

let cursor: String = entities_connection.edges[0].cursor.clone();
let next_cursor: String = entities_connection.edges[1].cursor.clone();
let entities_connection =
paginate(&pool, Some(cursor), Direction::Forward, page_size).await;
let entities_connection = paginate(&pool, Some(cursor), Paginate::Forward, page_size).await;
assert_eq!(entities_connection.total_count, 3);
assert_eq!(entities_connection.edges.len(), page_size);
assert_eq!(entities_connection.edges[0].cursor, next_cursor);

// Backward pagination
let entities_connection = paginate(&pool, None, Direction::Backward, page_size).await;
let entities_connection = paginate(&pool, None, Paginate::Backward, page_size).await;
assert_eq!(entities_connection.total_count, 3);
assert_eq!(entities_connection.edges.len(), page_size);

let cursor: String = entities_connection.edges[0].cursor.clone();
let next_cursor: String = entities_connection.edges[1].cursor.clone();
let entities_connection =
paginate(&pool, Some(cursor), Direction::Backward, page_size).await;
paginate(&pool, Some(cursor), Paginate::Backward, page_size).await;
assert_eq!(entities_connection.total_count, 3);
assert_eq!(entities_connection.edges.len(), page_size);
assert_eq!(entities_connection.edges[0].cursor, next_cursor);
Expand Down
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/tests/subscription_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ mod tests {
tokio::time::sleep(Duration::from_secs(1)).await;

// Set entity with one moves component
// remaining: 10
let moves_values = vec![FieldElement::from_hex_be("0xa").unwrap()];
// remaining: 10, last_direction: 0
let moves_values = vec![FieldElement::from_hex_be("0xa").unwrap(), FieldElement::ZERO];
state.set_entity("Moves".to_string(), key, moves_values).await.unwrap();
// 3. fn publish() is called from state.set_entity()

Expand Down

0 comments on commit 1d3f47d

Please sign in to comment.