Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion rust/rsmgp-sys/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ pub(crate) unsafe fn mgp_raw_value_to_value(
match invoke_mgp_func!(mgp_value_type, ffi::mgp_value_get_type, value).unwrap() {
mgp_value_type::MGP_VALUE_TYPE_NULL => Ok(Value::Null),
mgp_value_type::MGP_VALUE_TYPE_BOOL => Ok(Value::Bool(
invoke_mgp_func!(::std::os::raw::c_int, ffi::mgp_value_get_bool, value).unwrap() == 0,
invoke_mgp_func!(::std::os::raw::c_int, ffi::mgp_value_get_bool, value).unwrap() != 0,
)),
mgp_value_type::MGP_VALUE_TYPE_INT => Ok(Value::Int(
invoke_mgp_func!(i64, ffi::mgp_value_get_int, value).unwrap(),
Expand Down
179 changes: 179 additions & 0 deletions rust/rsmgp-sys/src/value/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ fn test_make_false_bool_mgp_value() {
});
}

#[test]
#[serial]
fn test_convert_false_bool_mgp_value() {
mock_mgp_once!(mgp_value_get_bool_context, |_, out| {
unsafe {
*out = 0;
}
mgp_error::MGP_ERROR_NO_ERROR
});
mock_mgp_once!(mgp_value_get_type_context, |_, out| {
unsafe {
*out = mgp_value_type::MGP_VALUE_TYPE_BOOL;
}
mgp_error::MGP_ERROR_NO_ERROR
});

with_dummy!(|memgraph: &Memgraph| {
unsafe {
let res = mgp_raw_value_to_value(null_mut(), &memgraph);
match res {
Ok(value) => match value {
Value::Bool(value) => assert!(value == false, "Wrong boolean value"),
_ => {
assert!(false, "Value is not a Bool")
}
},
_ => {
assert!(false, "Failed to convert raw value")
}
}
}
});
}

#[test]
#[serial]
fn test_make_true_bool_mgp_value() {
Expand All @@ -63,6 +97,40 @@ fn test_make_true_bool_mgp_value() {
});
}

#[test]
#[serial]
fn test_convert_true_bool_mgp_value() {
mock_mgp_once!(mgp_value_get_bool_context, |_, out| {
unsafe {
*out = 1;
}
mgp_error::MGP_ERROR_NO_ERROR
});
mock_mgp_once!(mgp_value_get_type_context, |_, out| {
unsafe {
*out = mgp_value_type::MGP_VALUE_TYPE_BOOL;
}
mgp_error::MGP_ERROR_NO_ERROR
});

with_dummy!(|memgraph: &Memgraph| {
unsafe {
let res = mgp_raw_value_to_value(null_mut(), &memgraph);
match res {
Ok(value) => match value {
Value::Bool(value) => assert!(value == true, "Wrong boolean value"),
_ => {
assert!(false, "Value is not a Bool")
}
},
_ => {
assert!(false, "Failed to convert raw value")
}
}
}
});
}

#[test]
#[serial]
fn test_make_int_mgp_value() {
Expand All @@ -77,6 +145,40 @@ fn test_make_int_mgp_value() {
});
}

#[test]
#[serial]
fn test_convert_int_mgp_value() {
mock_mgp_once!(mgp_value_get_int_context, |_, out| {
unsafe {
*out = 123;
}
mgp_error::MGP_ERROR_NO_ERROR
});
mock_mgp_once!(mgp_value_get_type_context, |_, out| {
unsafe {
*out = mgp_value_type::MGP_VALUE_TYPE_INT;
}
mgp_error::MGP_ERROR_NO_ERROR
});

with_dummy!(|memgraph: &Memgraph| {
unsafe {
let res = mgp_raw_value_to_value(null_mut(), &memgraph);
match res {
Ok(value) => match value {
Value::Int(value) => assert!(value == 123, "Wrong integer value"),
_ => {
assert!(false, "Value is not an Int")
}
},
_ => {
assert!(false, "Failed to convert raw value")
}
}
}
});
}

#[test]
#[serial]
fn test_make_double_mgp_value() {
Expand All @@ -91,6 +193,40 @@ fn test_make_double_mgp_value() {
});
}

#[test]
#[serial]
fn test_convert_double_mgp_value() {
mock_mgp_once!(mgp_value_get_double_context, |_, out| {
unsafe {
*out = 1.23;
}
mgp_error::MGP_ERROR_NO_ERROR
});
mock_mgp_once!(mgp_value_get_type_context, |_, out| {
unsafe {
*out = mgp_value_type::MGP_VALUE_TYPE_DOUBLE;
}
mgp_error::MGP_ERROR_NO_ERROR
});

with_dummy!(|memgraph: &Memgraph| {
unsafe {
let res = mgp_raw_value_to_value(null_mut(), &memgraph);
match res {
Ok(value) => match value {
Value::Float(value) => assert!(value == 1.23, "Wrong double value"),
_ => {
assert!(false, "Value is not a Double")
}
},
_ => {
assert!(false, "Failed to convert raw value")
}
}
}
});
}

#[test]
#[serial]
fn test_make_string_mgp_value() {
Expand All @@ -105,6 +241,49 @@ fn test_make_string_mgp_value() {
});
}

#[test]
#[serial]
fn test_convert_string_mgp_value() {
let cstr = CString::new("a string").expect("CString::new failed");
mock_mgp_once!(mgp_value_get_string_context, move |_, out| {
unsafe {
*out = cstr.as_ptr() as *const i8;
}
mgp_error::MGP_ERROR_NO_ERROR
});
mock_mgp_once!(mgp_value_get_type_context, |_, out| {
unsafe {
*out = mgp_value_type::MGP_VALUE_TYPE_STRING;
}
mgp_error::MGP_ERROR_NO_ERROR
});

with_dummy!(|memgraph: &Memgraph| {
unsafe {
let res = mgp_raw_value_to_value(null_mut(), &memgraph);
match res {
Ok(value) => match value {
Value::String(value) => match value.to_str() {
Ok(vs) => match vs {
"a string" => {}
_ => assert!(false, "Wrong string value"),
},
_ => {
assert!(false, "Value is not a String")
}
},
_ => {
assert!(false, "Value is not a String")
}
},
_ => {
assert!(false, "Failed to convert raw value")
}
}
}
});
}

#[test]
#[serial]
fn test_make_list_mgp_value() {
Expand Down