-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,37 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use serde::Deserialize; | ||
use sqlx::SqlitePool; | ||
|
||
use crate::graphql::schema::build_schema; | ||
use crate::tests::common::run_graphql_query; | ||
|
||
#[sqlx::test(migrations = "./migrations", fixtures("components"))] | ||
async fn test_dynamic_component(pool: SqlitePool) { | ||
let schema = build_schema(&pool).await.expect("failed to build schema"); | ||
#[derive(Deserialize)] | ||
struct Game { | ||
name: String, | ||
is_finished: bool, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Stats { | ||
health: i64, | ||
mana: i64, | ||
} | ||
|
||
#[sqlx::test(migrations = "./migrations", fixtures("entities", "components"))] | ||
async fn test_storage_components(pool: SqlitePool) { | ||
let _ = pool.acquire().await; | ||
|
||
let query = "{ game(id: 1) { name is_finished } stats(id: 1) { health mana } }"; | ||
let value = run_graphql_query(&pool, query).await; | ||
|
||
let game = value.get("game").ok_or("no game found").unwrap(); | ||
let game: Game = serde_json::from_value(game.clone()).unwrap(); | ||
let stats = value.get("stats").ok_or("no stats found").unwrap(); | ||
let stats: Stats = serde_json::from_value(stats.clone()).unwrap(); | ||
|
||
assert!(!game.is_finished); | ||
assert_eq!(game.name, "0x594F4C4F"); | ||
assert_eq!(stats.health, 42); | ||
assert_eq!(stats.mana, 69); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,37 @@ | ||
INSERT INTO components (id, name, address, class_hash, transaction_hash, storage_definition) | ||
VALUES ('component_1', 'Game', '0x0', '0x0', '0x0', | ||
'[{"name":"is_finished","type":"Boolean","slot":0,"offset":0}]'); | ||
'[{"name":"name","type":"FieldElement","slot":0,"offset":0},{"name":"is_finished","type":"Boolean","slot":0,"offset":0}]'); | ||
|
||
INSERT INTO components (id, name, address, class_hash, transaction_hash, storage_definition) | ||
VALUES ('component_2', 'Stats', '0x0', '0x0', '0x0', | ||
'[{"name":"health","type":"u8","slot":0,"offset":0},{"name":"mana","type":"u8","slot":0,"offset":0}]'); | ||
|
||
INSERT INTO components (id, name, address, class_hash, transaction_hash, storage_definition) | ||
VALUES ('component_3', 'Cash', '0x0', '0x0', '0x0', | ||
'[{"name":"amount","type":"u32","slot":0,"offset":0}]'); | ||
|
||
CREATE TABLE storage_game ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT NOT NULL, | ||
is_finished BOOLEAN NOT NULL, | ||
version TEXT NOT NULL, | ||
entity_id TEXT NOT NULL, | ||
component_id TEXT NOT NULL, | ||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
FOREIGN KEY (entity_id) REFERENCES entities(id), | ||
FOREIGN KEY (component_id) REFERENCES components(id) | ||
); | ||
CREATE TABLE storage_stats ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
health INTEGER NOT NULL, | ||
mana INTEGER NOT NULL, | ||
version TEXT NOT NULL, | ||
entity_id TEXT NOT NULL, | ||
component_id TEXT NOT NULL, | ||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
FOREIGN KEY (entity_id) REFERENCES entities(id), | ||
FOREIGN KEY (component_id) REFERENCES components(id) | ||
); | ||
|
||
INSERT INTO storage_game (id, name, is_finished, version, entity_id, component_id, created_at) | ||
VALUES (1, '0x594F4C4F', 0, '0.0.0', 'entity_1', 'component_1', '2023-05-19T21:04:04Z'); | ||
INSERT INTO storage_stats (id, health, mana, version, entity_id, component_id, created_at) | ||
VALUES (1, 42, 69, '0.0.0', 'entity_2', 'component_2', '2023-05-19T21:05:44Z'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters