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

fix(dojo-core): add test to showcase serialization issue on options #3036

Merged
merged 1 commit into from
Feb 15, 2025

Conversation

glihm
Copy link
Collaborator

@glihm glihm commented Feb 14, 2025

@remybar this is the issue mentioned by message.

Let's see what could be the best solution for that, of it's something we need to be aware of, and accept the behavior.

But this is very error prone for users. We will document it if we don't have an evident fix without breaking storage backward compatibility.

Summary by CodeRabbit

  • Refactor
    • Streamlined module export management for improved maintainability.
  • New Features
    • Extended data modeling with additional types to support more complex scenarios.
  • Tests
    • Introduced a new test ensuring proper handling of models with optional and enumerated fields.

@glihm glihm changed the title fix: add test to showcase serialization issue on options fix(dojo-core): add test to showcase serialization issue on options Feb 14, 2025
Copy link

coderabbitai bot commented Feb 14, 2025

Ohayo, sensei!

Walkthrough

This pull request makes three main changes. It simplifies the export of public items in the core library by replacing an explicit list with a wildcard export. Additionally, it introduces two new public types—a new enum (EnumOne) and a struct (WithOptionAndEnums)—in the helpers module. Finally, a new test function (write_read_option_enums) is added to verify that models containing options and enums are read correctly.

Changes

File(s) Change Summary
crates/dojo/.../lib.cairo Replaced explicit public exports in the helpers module with a wildcard export statement (pub use helpers::*;).
crates/dojo/.../helpers/helpers.cairo Added two new public entities: an enum EnumOne (with three variants) and a struct WithOptionAndEnums (with fields including an optional), with relevant attributes.
crates/dojo/.../world/storage.cairo Added a new test function write_read_option_enums that verifies reading a model with enum and option fields, including necessary import updates.

Sequence Diagram(s)

sequenceDiagram
    participant Test as write_read_option_enums
    participant World as World/Storage
    participant Model as Model Data

    Test->>World: Initialize world and request model (key=1)
    World->>Model: Fetch model data with enum & option fields
    Model-->>World: Return model (EnumOne::One, Option::Some(0))
    World-->>Test: Provide model for assertion
Loading

Suggested labels

contributor

Suggested reviewers

  • kariy

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
crates/dojo/core-cairo-test/src/tests/helpers/helpers.cairo (1)

45-52: Consider documenting the serialization behavior, sensei!

Since this struct is designed to showcase a serialization issue with options, it would be helpful to add documentation explaining the expected behavior and known limitations.

Add documentation like this:

 #[derive(Drop, Serde, Debug)]
 #[dojo::model]
+/// Struct used to demonstrate Option serialization behavior.
+/// Note: Currently, Option::None is serialized as Option::Some(0),
+/// which may lead to unexpected behavior.
 pub struct WithOptionAndEnums {
     #[key]
     pub id: u32,
     pub a: EnumOne,
     pub b: Option<u32>,
 }
crates/dojo/core-cairo-test/src/tests/world/storage.cairo (1)

126-137: Excellent test case for documenting the serialization issue, sensei!

The test effectively demonstrates the Option serialization behavior. However, we could enhance it by:

  1. Testing both Some and None cases
  2. Adding more detailed documentation about the workaround

Consider expanding the test like this:

 #[test]
 fn write_read_option_enums() {
     let (mut world, _) = deploy_world_and_foo();
 
-    let key: u32 = 1;
+    // Test Option::None (currently serializes as Some(0))
+    let none_key: u32 = 1;
+    let wo: WithOptionAndEnums = world.read_model(none_key);
+    assert_eq!(wo.a, EnumOne::One);
+    assert_eq!(wo.b, Option::Some(0), "Option::None serializes as Some(0)");
 
-    let wo: WithOptionAndEnums = world.read_model(key);
-    assert_eq!(wo.a, EnumOne::One);
-    // Should have been `Option::None`. Need to find a way to mitigate this issue
-    // using a `DojoOption` converter or customizing the serialization.
-    assert_eq!(wo.b, Option::Some(0));
+    // Test Option::Some
+    let some_key: u32 = 2;
+    let model = WithOptionAndEnums {
+        id: some_key,
+        a: EnumOne::Two(42),
+        b: Option::Some(42)
+    };
+    world.write_model(@model);
+    
+    let wo: WithOptionAndEnums = world.read_model(some_key);
+    assert_eq!(wo.a, EnumOne::Two(42));
+    assert_eq!(wo.b, Option::Some(42), "Option::Some serializes correctly");
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de7eddd and 3948c69.

📒 Files selected for processing (3)
  • crates/dojo/core-cairo-test/src/lib.cairo (1 hunks)
  • crates/dojo/core-cairo-test/src/tests/helpers/helpers.cairo (1 hunks)
  • crates/dojo/core-cairo-test/src/tests/world/storage.cairo (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: ensure-wasm
  • GitHub Check: docs
  • GitHub Check: build
🔇 Additional comments (2)
crates/dojo/core-cairo-test/src/lib.cairo (1)

46-46: Ohayo! LGTM on the wildcard export, sensei!

The change to use pub use helpers::* simplifies the exports, which is acceptable in a test context.

crates/dojo/core-cairo-test/src/tests/helpers/helpers.cairo (1)

38-43: Ohayo! Nice enum definition, sensei!

The EnumOne enum is well-structured with appropriate derive macros and varied variant types, making it perfect for testing serialization.

Copy link

codecov bot commented Feb 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 56.30%. Comparing base (de7eddd) to head (3948c69).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3036      +/-   ##
==========================================
- Coverage   56.31%   56.30%   -0.01%     
==========================================
  Files         437      437              
  Lines       59013    59013              
==========================================
- Hits        33232    33229       -3     
- Misses      25781    25784       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@glihm glihm merged commit 418044e into main Feb 15, 2025
14 of 15 checks passed
@glihm glihm deleted the dev/test-option-issue branch February 15, 2025 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant