Skip to content

Commit

Permalink
add primative data types and enum
Browse files Browse the repository at this point in the history
  • Loading branch information
gianalarcon committed Nov 24, 2023
1 parent 0a7f9dd commit 86a1e96
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 18 deletions.
54 changes: 54 additions & 0 deletions crates/torii/graphql/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ pub async fn model_fixtures(db: &mut Sql) {
Ty::Struct(Struct {
name: "Record".to_string(),
children: vec![
Member {
name: "depth".to_string(),
key: false,
ty: Ty::Enum(Enum {
name: "Depth".to_string(),
option: None,
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: "record_id".to_string(),
key: true,
Expand All @@ -201,6 +215,46 @@ pub async fn model_fixtures(db: &mut Sql) {
key: false,
ty: Ty::Primitive(Primitive::U8(None)),
},
Member {
name: "type_u16".to_string(),
key: false,
ty: Ty::Primitive(Primitive::U16(None)),
},
Member {
name: "type_u32".to_string(),
key: false,
ty: Ty::Primitive(Primitive::U32(None)),
},
Member {
name: "type_u64".to_string(),
key: false,
ty: Ty::Primitive(Primitive::U64(None)),
},
// Member {
// name: "type_u128".to_string(),
// key: false,
// ty: Ty::Primitive(Primitive::U128(None)),
// },
// Member {
// name: "type_u256".to_string(),
// key: false,
// ty: Ty::Primitive(Primitive::U256(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_class_hash".to_string(),
// key: true,
// ty: Ty::Primitive(Primitive::ClassHash(None)),
// },
Member {
name: "type_contract_address".to_string(),
key: true,
Expand Down
88 changes: 70 additions & 18 deletions crates/torii/graphql/src/tests/subscription_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ mod tests {
"keys":vec![keys_str],
"model_names": "Record",
"models" : [{
"type_contract_address": format!("{:#x}", FieldElement::ONE),
"record_id": 0,
"type_u8": 10
"__typename": "Record",
"depth": "Zero",
"record_id": 0,
"type_u8": 1,
"type_u16": 1,
"type_u32": 1,
"type_u64": 1,
"type_bool": true,
"type_felt": format!("{:#x}", FieldElement::from(1u128)),
"type_contract_address": format!("{:#x}", FieldElement::ONE)

}]
}
});
Expand All @@ -43,15 +51,24 @@ 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: "Record".to_string(),
children: vec![
Member {
name: "type_contract_address".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: "record_id".to_string(),
Expand All @@ -61,15 +78,44 @@ mod tests {
Member {
name: "type_u8".to_string(),
key: false,
ty: Ty::Primitive(Primitive::U8(Some(10))),
ty: Ty::Primitive(Primitive::U8(Some(1))),
},
Member {
name: "type_u16".to_string(),
key: false,
ty: Ty::Primitive(Primitive::U16(Some(1))),
},
Member {
name: "type_u32".to_string(),
key: false,
ty: Ty::Primitive(Primitive::U32(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))),
},
],
}),
&format!("0x{:064x}:0x{:04x}:0x{:04x}", 0, 0, 0),
)
.await
.unwrap();
// 3. fn publish() is called from state.set_entity()

tx.send(()).await.unwrap();
});
Expand All @@ -83,25 +129,31 @@ mod tests {
keys
model_names
models {
__typename
... on Record {
type_contract_address
depth
record_id
type_u8
type_u16
type_u32
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();
}

#[sqlx::test(migrations = "../migrations")]
#[serial]
//#[ignore]
async fn test_entity_subscription_with_id(pool: SqlitePool) {
let mut db = Sql::new(pool.clone(), FieldElement::ZERO).await.unwrap();

Expand All @@ -118,7 +170,7 @@ mod tests {
"models" : [{
"type_contract_address": format!("{:#x}", FieldElement::ONE),
"record_id": 0,
"type_u8": 10
"type_u8": 1
}]
}
});
Expand Down Expand Up @@ -146,7 +198,7 @@ mod tests {
Member {
name: "type_u8".to_string(),
key: false,
ty: Ty::Primitive(Primitive::U8(Some(10))),
ty: Ty::Primitive(Primitive::U8(Some(1))),
},
],
}),
Expand Down Expand Up @@ -238,7 +290,7 @@ 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 name = "Subrecord".to_string();
let model_id = name.clone();
let class_hash = FieldElement::TWO;
let expected_value: async_graphql::Value = value!({
Expand All @@ -251,9 +303,9 @@ mod tests {
tokio::time::sleep(Duration::from_secs(1)).await;

let model = Ty::Struct(Struct {
name: "Test".to_string(),
name: "Subrecord".to_string(),
children: vec![Member {
name: "test".into(),
name: "type_u8".into(),
key: false,
ty: Ty::Primitive(Primitive::U8(None)),
}],
Expand All @@ -269,7 +321,7 @@ mod tests {
&pool,
r#"
subscription {
modelRegistered(id: "Test") {
modelRegistered(id: "Subrecord") {
id, name
}
}"#,
Expand Down

0 comments on commit 86a1e96

Please sign in to comment.