From e6a870f21e05d1946d07d13912158f3c43d59783 Mon Sep 17 00:00:00 2001 From: GianMarco Date: Sat, 25 Nov 2023 21:12:36 -0500 Subject: [PATCH] Update Graphql test (#1204) * Basic migration * add primative data types and enum * fmt * Clean fn model_fixtures(), clean/refactor susbcription_tests --- crates/torii/graphql/src/tests/mod.rs | 83 ++++---- .../graphql/src/tests/subscription_test.rs | 181 +++++++++++------- 2 files changed, 141 insertions(+), 123 deletions(-) diff --git a/crates/torii/graphql/src/tests/mod.rs b/crates/torii/graphql/src/tests/mod.rs index 2576c5227c..3a07eff19a 100644 --- a/crates/torii/graphql/src/tests/mod.rs +++ b/crates/torii/graphql/src/tests/mod.rs @@ -189,75 +189,56 @@ pub async fn run_graphql_subscription( pub async fn model_fixtures(db: &mut Sql) { db.register_model( Ty::Struct(Struct { - name: "Moves".to_string(), + name: "Record".to_string(), children: vec![ Member { - name: "player".to_string(), - key: true, - ty: Ty::Primitive(Primitive::ContractAddress(None)), - }, - Member { - name: "remaining".to_string(), - key: false, - ty: Ty::Primitive(Primitive::U8(None)), - }, - Member { - name: "last_direction".to_string(), + name: "depth".to_string(), key: false, ty: Ty::Enum(Enum { - name: "Direction".to_string(), + name: "Depth".to_string(), option: None, options: vec![ - EnumOption { name: "None".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Left".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Right".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Up".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Down".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "Zero".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "One".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "Two".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "Three".to_string(), ty: Ty::Tuple(vec![]) }, ], }), }, - ], - }), - vec![], - FieldElement::ONE, - 0, - 0, - ) - .await - .unwrap(); - - db.register_model( - Ty::Struct(Struct { - name: "Position".to_string(), - children: vec![ Member { - name: "player".to_string(), + name: "record_id".to_string(), key: true, - ty: Ty::Primitive(Primitive::ContractAddress(None)), + ty: Ty::Primitive(Primitive::U32(None)), }, Member { - name: "vec".to_string(), + name: "type_u16".to_string(), key: false, - ty: Ty::Struct(Struct { - name: "Vec2".to_string(), - children: vec![ - Member { - name: "x".to_string(), - key: false, - ty: Ty::Primitive(Primitive::U32(None)), - }, - Member { - name: "y".to_string(), - key: false, - ty: Ty::Primitive(Primitive::U32(None)), - }, - ], - }), + ty: Ty::Primitive(Primitive::U16(None)), + }, + Member { + name: "type_u64".to_string(), + key: false, + ty: Ty::Primitive(Primitive::U64(None)), + }, + Member { + name: "type_bool".to_string(), + key: false, + ty: Ty::Primitive(Primitive::Bool(None)), + }, + Member { + name: "type_felt".to_string(), + key: false, + ty: Ty::Primitive(Primitive::Felt252(None)), + }, + Member { + name: "type_contract_address".to_string(), + key: true, + ty: Ty::Primitive(Primitive::ContractAddress(None)), }, ], }), vec![], - FieldElement::TWO, + FieldElement::ONE, 0, 0, ) diff --git a/crates/torii/graphql/src/tests/subscription_test.rs b/crates/torii/graphql/src/tests/subscription_test.rs index e126404119..4c48dc93ba 100644 --- a/crates/torii/graphql/src/tests/subscription_test.rs +++ b/crates/torii/graphql/src/tests/subscription_test.rs @@ -22,6 +22,7 @@ mod tests { model_fixtures(&mut db).await; // 0. Preprocess expected entity value + let model_name = "Record".to_string(); let key = vec![FieldElement::ONE]; let entity_id = format!("{:#x}", poseidon_hash_many(&key)); let keys_str = key.iter().map(|k| format!("{:#x}", k)).collect::>().join(","); @@ -29,11 +30,16 @@ mod tests { "entityUpdated": { "id": entity_id, "keys":vec![keys_str], - "model_names": "Moves", + "model_names": model_name, "models" : [{ - "player": format!("{:#x}", FieldElement::ONE), - "remaining": 10, - "last_direction": "Left" + "__typename": model_name, + "depth": "Zero", + "record_id": 0, + "type_u16": 1, + "type_u64": 1, + "type_bool": true, + "type_felt": format!("{:#x}", FieldElement::from(1u128)), + "type_contract_address": format!("{:#x}", FieldElement::ONE) }] } }); @@ -43,35 +49,54 @@ mod tests { // 1. Open process and sleep.Go to execute subscription tokio::time::sleep(Duration::from_secs(1)).await; - // Set entity with one moves model + // Set entity with one Record model db.set_entity( Ty::Struct(Struct { - name: "Moves".to_string(), + name: model_name, children: vec![ Member { - name: "player".to_string(), - key: true, - ty: Ty::Primitive(Primitive::ContractAddress(Some(FieldElement::ONE))), + name: "depth".to_string(), + key: false, + ty: Ty::Enum(Enum { + name: "Depth".to_string(), + option: Some(0), + options: vec![ + EnumOption { name: "Zero".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "One".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "Two".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "Three".to_string(), ty: Ty::Tuple(vec![]) }, + ], + }), }, Member { - name: "remaining".to_string(), + name: "record_id".to_string(), key: false, - ty: Ty::Primitive(Primitive::U8(Some(10))), + ty: Ty::Primitive(Primitive::U8(Some(0))), }, Member { - name: "last_direction".to_string(), + name: "type_u16".to_string(), key: false, - ty: Ty::Enum(Enum { - name: "Direction".to_string(), - option: Some(1), - options: vec![ - EnumOption { name: "None".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Left".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Right".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Up".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Down".to_string(), ty: Ty::Tuple(vec![]) }, - ], - }), + ty: Ty::Primitive(Primitive::U16(Some(1))), + }, + Member { + name: "type_u64".to_string(), + key: false, + ty: Ty::Primitive(Primitive::U64(Some(1))), + }, + Member { + name: "type_bool".to_string(), + key: false, + ty: Ty::Primitive(Primitive::Bool(Some(true))), + }, + Member { + name: "type_felt".to_string(), + key: false, + ty: Ty::Primitive(Primitive::Felt252(Some(FieldElement::from(1u128)))), + }, + Member { + name: "type_contract_address".to_string(), + key: true, + ty: Ty::Primitive(Primitive::ContractAddress(Some(FieldElement::ONE))), }, ], }), @@ -79,12 +104,11 @@ mod tests { ) .await .unwrap(); - // 3. fn publish() is called from state.set_entity() tx.send(()).await.unwrap(); }); - // 2. The subscription is executed and it is listeing, waiting for publish() to be executed + // 2. The subscription is executed and it is listening, waiting for publish() to be executed let response_value = run_graphql_subscription( &pool, r#"subscription { @@ -93,17 +117,22 @@ mod tests { keys model_names models { - ... on Moves { - player - remaining - last_direction + __typename + ... on Record { + depth + record_id + type_u16 + type_u64 + type_bool + type_felt + type_contract_address } } } }"#, ) .await; - // 4. The subcription has received the message from publish() + // 4. The subscription has received the message from publish() // 5. Compare values assert_eq!(expected_value, response_value); rx.recv().await.unwrap(); @@ -116,6 +145,7 @@ mod tests { model_fixtures(&mut db).await; // 0. Preprocess expected entity value + let model_name = "Record".to_string(); let key = vec![FieldElement::ONE]; let entity_id = format!("{:#x}", poseidon_hash_many(&key)); let keys_str = key.iter().map(|k| format!("{:#x}", k)).collect::>().join(","); @@ -123,11 +153,13 @@ mod tests { "entityUpdated": { "id": entity_id, "keys":vec![keys_str], - "model_names": "Moves", + "model_names": model_name, "models" : [{ - "player": format!("{:#x}", FieldElement::ONE), - "remaining": 10, - "last_direction": "Left" + "__typename": model_name, + "depth": "Zero", + "record_id": 0, + "type_felt": format!("{:#x}", FieldElement::from(1u128)), + "type_contract_address": format!("{:#x}", FieldElement::ONE) }] } }); @@ -137,35 +169,39 @@ mod tests { // 1. Open process and sleep.Go to execute subscription tokio::time::sleep(Duration::from_secs(1)).await; - // Set entity with one moves model + // Set entity with one Record model db.set_entity( Ty::Struct(Struct { - name: "Moves".to_string(), + name: model_name, children: vec![ Member { - name: "player".to_string(), - key: true, - ty: Ty::Primitive(Primitive::ContractAddress(Some(FieldElement::ONE))), + name: "depth".to_string(), + key: false, + ty: Ty::Enum(Enum { + name: "Depth".to_string(), + option: Some(0), + options: vec![ + EnumOption { name: "Zero".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "One".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "Two".to_string(), ty: Ty::Tuple(vec![]) }, + EnumOption { name: "Three".to_string(), ty: Ty::Tuple(vec![]) }, + ], + }), }, Member { - name: "remaining".to_string(), + name: "record_id".to_string(), key: false, - ty: Ty::Primitive(Primitive::U8(Some(10))), + ty: Ty::Primitive(Primitive::U32(Some(0))), }, Member { - name: "last_direction".to_string(), + name: "type_felt".to_string(), key: false, - ty: Ty::Enum(Enum { - name: "Direction".to_string(), - option: Some(1), - options: vec![ - EnumOption { name: "None".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Left".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Right".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Up".to_string(), ty: Ty::Tuple(vec![]) }, - EnumOption { name: "Down".to_string(), ty: Ty::Tuple(vec![]) }, - ], - }), + ty: Ty::Primitive(Primitive::Felt252(Some(FieldElement::from(1u128)))), + }, + Member { + name: "type_contract_address".to_string(), + key: true, + ty: Ty::Primitive(Primitive::ContractAddress(Some(FieldElement::ONE))), }, ], }), @@ -173,24 +209,25 @@ mod tests { ) .await .unwrap(); - // 3. fn publish() is called from state.set_entity() tx.send(()).await.unwrap(); }); - // 2. The subscription is executed and it is listeing, waiting for publish() to be executed + // 2. The subscription is executed and it is listening, waiting for publish() to be executed let response_value = run_graphql_subscription( &pool, r#"subscription { entityUpdated(id: "0x579e8877c7755365d5ec1ec7d3a94a457eff5d1f40482bbe9729c064cdead2") { - id + id keys model_names models { - ... on Moves { - player - remaining - last_direction + __typename + ... on Record { + depth + record_id + type_felt + type_contract_address } } } @@ -208,11 +245,11 @@ mod tests { async fn test_model_subscription(pool: SqlitePool) { let mut db = Sql::new(pool.clone(), FieldElement::ZERO).await.unwrap(); // 0. Preprocess model value - let name = "Moves".to_string(); - let model_id = name.clone(); + let model_name = "Subrecord".to_string(); + let model_id = model_name.clone(); let class_hash = FieldElement::TWO; let expected_value: async_graphql::Value = value!({ - "modelRegistered": { "id": model_id, "name":name } + "modelRegistered": { "id": model_id, "name":model_name } }); let (tx, mut rx) = mpsc::channel(7); @@ -221,11 +258,11 @@ mod tests { tokio::time::sleep(Duration::from_secs(1)).await; let model = Ty::Struct(Struct { - name: "Moves".to_string(), + name: model_name, children: vec![Member { - name: "player".to_string(), + name: "subrecord_id".to_string(), key: true, - ty: Ty::Primitive(Primitive::ContractAddress(None)), + ty: Ty::Primitive(Primitive::U32(None)), }], }); db.register_model(model, vec![], class_hash, 0, 0).await.unwrap(); @@ -257,11 +294,11 @@ mod tests { async fn test_model_subscription_with_id(pool: SqlitePool) { let mut db = Sql::new(pool.clone(), FieldElement::ZERO).await.unwrap(); // 0. Preprocess model value - let name = "Test".to_string(); - let model_id = name.clone(); + let model_name = "Subrecord".to_string(); + let model_id = model_name.clone(); let class_hash = FieldElement::TWO; let expected_value: async_graphql::Value = value!({ - "modelRegistered": { "id": model_id, "name":name } + "modelRegistered": { "id": model_id, "name":model_name } }); let (tx, mut rx) = mpsc::channel(7); @@ -270,9 +307,9 @@ mod tests { tokio::time::sleep(Duration::from_secs(1)).await; let model = Ty::Struct(Struct { - name: "Test".to_string(), + name: model_name, children: vec![Member { - name: "test".into(), + name: "type_u8".into(), key: false, ty: Ty::Primitive(Primitive::U8(None)), }], @@ -288,7 +325,7 @@ mod tests { &pool, r#" subscription { - modelRegistered(id: "Test") { + modelRegistered(id: "Subrecord") { id, name } }"#,