-
Notifications
You must be signed in to change notification settings - Fork 182
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
25 changed files
with
3,955 additions
and
192 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
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use dojo::test_utils::{spawn_test_world}; | ||
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; | ||
|
||
// Utils | ||
fn deploy_world() -> IWorldDispatcher { | ||
spawn_test_world("dojo", array![]) | ||
} | ||
|
||
#[derive(Copy, Drop, Serde)] | ||
#[dojo::model] | ||
struct Foo { | ||
#[key] | ||
k1: u8, | ||
#[key] | ||
k2: felt252, | ||
v1: u128, | ||
v2: u32 | ||
} | ||
|
||
#[test] | ||
fn test_values() { | ||
let mvalues = FooValues { v1: 3, v2: 4 }; | ||
let expected_values = array![3, 4].span(); | ||
|
||
let values = FooModelValues::values(@mvalues); | ||
assert!(expected_values == values); | ||
} | ||
|
||
#[test] | ||
fn test_from_values() { | ||
let values = array![3, 4].span(); | ||
|
||
let model_values = FooModelValues::from_values(values); | ||
assert!(model_values.v1 == 3 && model_values.v2 == 4); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: "ModelValues `FooValues`: deserialization failed.")] | ||
fn test_from_values_bad_data() { | ||
let values = array![3].span(); | ||
let _ = FooModelValues::from_values(values); | ||
} | ||
|
||
#[test] | ||
fn test_get_and_update_values() { | ||
let world = deploy_world(); | ||
world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap()); | ||
|
||
let entity = Foo { k1: 1, k2: 2, v1: 3, v2: 4 }; | ||
let entity_id = entity.entity_id(); | ||
dojo::model::Model::< | ||
Foo | ||
>::set_entity(world, entity.keys(), entity.values(), entity.instance_layout()); | ||
|
||
let mut entity_values = FooModelValues::get(world, entity_id); | ||
assert!(entity.v1 == entity_values.v1 && entity.v2 == entity_values.v2); | ||
|
||
entity_values.v1 = 12; | ||
entity_values.v2 = 18; | ||
|
||
entity_values.update(world, entity_id); | ||
|
||
let read_values = FooModelValues::get(world, entity_id); | ||
assert!(read_values.v1 == entity_values.v1 && read_values.v2 == entity_values.v2); | ||
} | ||
|
||
#[test] | ||
fn test_delete_values() { | ||
let world = deploy_world(); | ||
world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap()); | ||
|
||
let entity = Foo { k1: 1, k2: 2, v1: 3, v2: 4 }; | ||
let entity_id = entity.entity_id(); | ||
dojo::model::Model::< | ||
Foo | ||
>::set_entity(world, entity.keys(), entity.values(), entity.instance_layout()); | ||
|
||
let mut entity_values = FooModelValues::get(world, entity_id); | ||
entity_values.delete(world, entity_id); | ||
|
||
let read_values = FooModelValues::get(world, entity_id); | ||
assert!(read_values.v1 == 0 && read_values.v2 == 0); | ||
} | ||
|
||
#[test] | ||
fn test_entity_and_set_entity() { | ||
let world = deploy_world(); | ||
world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap()); | ||
|
||
let entity = Foo { k1: 1, k2: 2, v1: 3, v2: 4 }; | ||
dojo::model::Model::< | ||
Foo | ||
>::set_entity(world, entity.keys(), entity.values(), entity.instance_layout()); | ||
let read_entity = dojo::model::Model::< | ||
Foo | ||
>::entity(world, entity.keys(), entity.instance_layout()); | ||
|
||
assert!( | ||
entity.k1 == read_entity.k1 | ||
&& entity.k2 == read_entity.k2 | ||
&& entity.v1 == read_entity.v1 | ||
&& entity.v2 == read_entity.v2 | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_delete_entity() { | ||
let world = deploy_world(); | ||
world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap()); | ||
|
||
let entity = Foo { k1: 1, k2: 2, v1: 3, v2: 4 }; | ||
dojo::model::Model::< | ||
Foo | ||
>::set_entity(world, entity.keys(), entity.values(), entity.instance_layout()); | ||
dojo::model::Model::<Foo>::delete_entity(world, entity.keys(), entity.instance_layout()); | ||
|
||
let read_entity = dojo::model::Model::< | ||
Foo | ||
>::entity(world, entity.keys(), entity.instance_layout()); | ||
assert!( | ||
read_entity.k1 == entity.k1 | ||
&& read_entity.k2 == entity.k2 | ||
&& read_entity.v1 == 0 | ||
&& read_entity.v2 == 0 | ||
); | ||
} |
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
Oops, something went wrong.