Skip to content

fix: unit tests for python values #452

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

TwistingTwists
Copy link

fixes #438

Copy link
Member

@badmonster0 badmonster0 left a comment

Choose a reason for hiding this comment

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

Thanks for adding this test! Appreciate the effort!

let int_value = value::Value::Basic(value::BasicValue::Int64(42));
let int_type = schema::ValueType::Basic(schema::BasicValueType::Int64);
let py_obj = value_to_py_object(py, &int_value).unwrap();
let roundtrip_value = value_from_py_object(&int_type, &py_obj).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

value_from_py_object and value_to_py_object are functions internal to this module. Pythonized is the API exposed by the module. Ideally we want to test on the behavior of the public API.

You can also consider create a helper function as testutil to do this roundtrip conversion and validation (e.g. take a type + a value as input, do the roundtrip conversion internally and assert on equality), so that there will be less boilerplates in each specific test.

Copy link
Author

Choose a reason for hiding this comment

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

Ah. Right. Thanks for pointing me in right direction!

let roundtrip_value = value_from_py_object(&struct_type, &py_obj).unwrap();
assert_eq!(struct_value, roundtrip_value, "Struct roundtrip failed");
});
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we also cover Table types? Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

Yes. Surely!

@TwistingTwists
Copy link
Author

@badmonster0 can you please review this once?

Copy link
Member

@badmonster0 badmonster0 left a comment

Choose a reason for hiding this comment

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

Hi, sorry I missed your reply a few days ago, and just notice.

It's very close! Thanks for your patience.

fn assert_roundtrip_conversion(original_value: &value::Value, value_type: &schema::ValueType) {
Python::with_gil(|py| {
// Convert Rust value to Python object
let pythonized_value = Pythonized(original_value.clone());
Copy link
Member

Choose a reason for hiding this comment

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

Actually I think I made a mistake in my last round of comments. Apologize for that..

There're actually two sets of conversion mechanism defined in this package:

  • For conversion of some meta objects, e.g. configs (settings, specs, etc.), Pythonized is used.
  • For conversion of data values in CocoIndex, value_to_py_object() and value_from_py_object() are used.

Really sorry for the confusion. Your previous code was actually correct in using value_from_py_object() and value_to_py_object().

With these, we don't need to add Deserialize for existing types.

Copy link
Author

Choose a reason for hiding this comment

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

Ah. Thanks for clarification. I'll revert related changes. to use value_to_py_object and value_from_py_object


println!("Roundtripped value: {:?}", roundtripped_value);
// Compare values
match (&original_value, &roundtripped_value) {
Copy link
Member

Choose a reason for hiding this comment

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

Since PartialEq is already provided for value types, I think we can directly compare these by == or assert_eq!?

Copy link
Author

Choose a reason for hiding this comment

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

Will look into it. Did not realise that there is PartialEq already.

@@ -1,13 +1,13 @@
use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
Copy link
Member

Choose a reason for hiding this comment

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

is Clone here needed by any new code?

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.

Unittest for Value Conversion
2 participants