Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re added members and converted ModelPtr to a struct #2634

Merged
merged 20 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,8 @@ rpassword = "7.2.0"
rstest = "0.18.2"
rstest_reuse = "0.6.0"
salsa = "0.16.1"
scarb = { git = "https://github.com/remybar/scarb", rev = "fbad7d8d6bdb763b413055d8d9f8b7995672f145" }
scarb-ui = { git = "https://github.com/remybar/scarb", rev = "fbad7d8d6bdb763b413055d8d9f8b7995672f145" }
#scarb = { git = "https://github.com/dojoengine/scarb", branch = "dojo-284" }
#scarb-ui = { git = "https://github.com/dojoengine/scarb", branch = "dojo-284" }
scarb = { git = "https://github.com/dojoengine/scarb", rev = "b9965b7e2f0d97f2a97f18ca9a75bac541de7d84" }
scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "b9965b7e2f0d97f2a97f18ca9a75bac541de7d84" }
semver = "1.0.5"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = { version = "1.0", features = [ "arbitrary_precision" ] }
Expand Down
6 changes: 3 additions & 3 deletions crates/dojo/core-cairo-test/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ mod tests {
mod event;
}

// mod model {
// mod model;
// }
mod model {
mod model;
}

mod storage {
mod database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub mod bar {
let mut world = self.world(@"dojo");
let ptr = ModelPtr::<
Foo
>::Id(core::poseidon::poseidon_hash_span([get_caller_address().into()].span()));
> { id: core::poseidon::poseidon_hash_span([get_caller_address().into()].span()) };
world.erase_model_ptr(ptr);
}
}
Expand Down
140 changes: 78 additions & 62 deletions crates/dojo/core-cairo-test/src/tests/model/model.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use dojo::model::{Model, ModelValue, ModelStorage, ModelValueStorage, ModelMemberStorage};
use dojo::world::{IWorldDispatcherTrait, WorldStorageTrait, WorldStorage};

use crate::tests::helpers::{deploy_world};
use dojo::model::{Model, ModelValue, ModelStorage, ModelValueStorage};
use dojo::world::WorldStorage;
use dojo_cairo_test::{spawn_test_world, NamespaceDef, TestResource};

#[derive(Copy, Drop, Serde, Debug)]
#[dojo::model]
Expand All @@ -26,6 +25,19 @@ struct Foo2 {
v2: u32
}

fn namespace_def() -> NamespaceDef {
NamespaceDef {
namespace: "dojo_cairo_test", resources: [
TestResource::Model(m_Foo::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Model(m_Foo2::TEST_CLASS_HASH.try_into().unwrap()),
].span()
}
}

fn spawn_foo_world() -> WorldStorage {
spawn_test_world([namespace_def()].span())
}

#[test]
fn test_model_definition() {
let definition = dojo::model::Model::<Foo>::definition();
Expand Down Expand Up @@ -64,73 +76,61 @@ fn test_from_values_bad_data() {
}

#[test]
fn test_get_and_update_model_value() {
let world = deploy_world();
world.register_model("dojo", foo::TEST_CLASS_HASH.try_into().unwrap());

let mut world = WorldStorageTrait::new(world, "dojo");
fn test_read_and_update_model_value() {
let mut world = spawn_foo_world();

let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
world.write_model(@foo);

let entity_id = foo.entity_id();
let mut model_value: FooValue = world.read_model_value(foo.key());
let mut model_value: FooValue = world.read_value(foo.key());
assert_eq!(model_value.v1, foo.v1);
assert_eq!(model_value.v2, foo.v2);

model_value.v1 = 12;
model_value.v2 = 18;

world.write_model_value_from_id(entity_id, @model_value);
world.write_value_from_id(entity_id, @model_value);

let read_values: FooValue = world.read_model_value(foo.key());
let read_values: FooValue = world.read_value(foo.key());
assert!(read_values.v1 == model_value.v1 && read_values.v2 == model_value.v2);
}

#[test]
fn test_delete_model_value() {
let world = deploy_world();
world.register_model("dojo", foo::TEST_CLASS_HASH.try_into().unwrap());

let mut world = WorldStorageTrait::new(world, "dojo");
let mut world = spawn_foo_world();

let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
world.write_model(@foo);

let entity_id = foo.entity_id();
ModelStorage::<WorldStorage, Foo>::erase_model(ref world, @foo);

let read_values: FooValue = world.read_model_value_from_id(entity_id);
let read_values: FooValue = world.read_value_from_id(entity_id);
assert!(read_values.v1 == 0 && read_values.v2 == 0);
}

#[test]
fn test_get_and_set_field_name() {
let world = deploy_world();
world.register_model("dojo", foo::TEST_CLASS_HASH.try_into().unwrap());

let mut world = WorldStorageTrait::new(world, "dojo");
fn test_read_and_write_field_name() {
let mut world = spawn_foo_world();

let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
world.write_model(@foo);

// Inference fails here, we need something better without too generics
// which also fails.
let v1 = world.read_member(foo.key(), selector!("v1"));
let v1 = world.read_member(foo.ptr(), selector!("v1"));
assert!(foo.v1 == v1);

world.write_member_from_id(foo.entity_id(), selector!("v1"), 42);
world.write_member(foo.ptr(), selector!("v1"), 42);

let v1 = world.read_member_from_id(foo.key(), selector!("v1"));
let v1 = world.read_member(foo.ptr(), selector!("v1"));
assert!(v1 == 42);
}

#[test]
fn test_get_and_set_from_model() {
let world = deploy_world();
world.register_model("dojo", foo::TEST_CLASS_HASH.try_into().unwrap());

let mut world = WorldStorageTrait::new(world, "dojo");
fn test_read_and_write_from_model() {
let mut world = spawn_foo_world();

let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
world.write_model(@foo);
Expand All @@ -142,10 +142,7 @@ fn test_get_and_set_from_model() {

#[test]
fn test_delete_from_model() {
let world = deploy_world();
world.register_model("dojo", foo::TEST_CLASS_HASH.try_into().unwrap());

let mut world = WorldStorageTrait::new(world, "dojo");
let mut world = spawn_foo_world();

let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
world.write_model(@foo);
Expand All @@ -156,41 +153,60 @@ fn test_delete_from_model() {
}

#[test]
fn test_get_and_set_member_from_model() {
let world = deploy_world();
world.register_model("dojo", foo::TEST_CLASS_HASH.try_into().unwrap());

let mut world = WorldStorageTrait::new(world, "dojo");

fn test_model_ptr_from_key() {
let mut world = spawn_foo_world();
let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let ptr = Model::<Foo>::ptr_from_key(foo.key());
world.write_model(@foo);

let key: (u8, felt252) = foo.key();
let v1: u128 = world.read_member(key, selector!("v1"));

assert!(v1 == 3);

world.write_member(key, selector!("v1"), 42);
let foo: Foo = world.read_model(key);
assert!(foo.v1 == 42);
let v1 = world.read_member(ptr, selector!("v1"));
assert!(foo.v1 == v1);
}

#[test]
fn test_get_and_set_field_name_from_model() {
let world = deploy_world();
world.register_model("dojo", foo::TEST_CLASS_HASH.try_into().unwrap());

let mut world = WorldStorageTrait::new(world, "dojo");

fn test_model_ptr_from_keys() {
let mut world = spawn_foo_world();
let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let ptr = Model::<Foo>::ptr_from_keys(foo.keys());
world.write_model(@foo);
let v1 = world.read_member(ptr, selector!("v1"));
assert!(foo.v1 == v1);
}

// Currently we don't have automatic field id computation. To be done.
// @remy/@ben.

let v1 = world.read_member((foo.k1, foo.k2), selector!("v1"));
assert!(v1 == 3);
#[test]
fn test_model_ptr_from_entity_id() {
let mut world = spawn_foo_world();
let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let ptr = Model::<Foo>::ptr_from_id(foo.entity_id());
world.write_model(@foo);
let v1 = world.read_member(ptr, selector!("v1"));
assert!(foo.v1 == v1);
}

world.write_member((foo.k1, foo.k2), selector!("v1"), 42);
assert!(v1 == 42);
#[test]
fn test_models_ptrs_from_key() {
let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let foo2 = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let keys = [foo.key(), foo2.key()].span();
let ptrs = Model::<Foo>::ptrs_from_key(keys);
assert_eq!(foo.entity_id(), *ptrs.at(0).id);
assert_eq!(foo2.entity_id(), *ptrs.at(1).id);
}
#[test]
fn test_models_ptrs_from_keys() {
let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let foo2 = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let keys = [foo.keys(), foo2.keys()].span();
let ptrs = Model::<Foo>::ptrs_from_keys(keys);
assert_eq!(foo.entity_id(), *ptrs.at(0).id);
assert_eq!(foo2.entity_id(), *ptrs.at(1).id);
}
#[test]
fn test_models_ptrs_from_entity_id() {
let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let foo2 = Foo { k1: 1, k2: 2, v1: 3, v2: 4 };
let keys = [foo.entity_id(), foo2.entity_id()].span();
let ptrs = Model::<Foo>::ptrs_from_id(keys);
assert_eq!(foo.entity_id(), *ptrs.at(0).id);
assert_eq!(foo2.entity_id(), *ptrs.at(1).id);
}

6 changes: 2 additions & 4 deletions crates/dojo/core/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub mod model {
pub use definition::{ModelIndex, ModelDefinition, ModelDef};

pub mod model;
pub use model::{Model, KeyParser};
pub use model::{Model, KeyParser, ModelPtr};

pub mod model_value;
pub use model_value::{ModelValue, ModelValueKey};
Expand All @@ -54,9 +54,7 @@ pub mod model {
pub use metadata::ResourceMetadata;

pub mod storage;
pub use storage::{
ModelStorage, ModelStorageTest, ModelValueStorage, ModelValueStorageTest, ModelPtr,
};
pub use storage::{ModelStorage, ModelStorageTest, ModelValueStorage, ModelValueStorageTest,};
}

pub mod storage {
Expand Down
Loading
Loading