Skip to content

Commit 4e47df8

Browse files
fix: unit tests for python values
1 parent ba46f66 commit 4e47df8

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ name = "cocoindex_engine"
1414
crate-type = ["cdylib"]
1515

1616
[dependencies]
17-
pyo3 = { version = "0.24.1", features = ["chrono"] }
17+
pyo3 = { version = "0.24.1", features = ["chrono", "auto-initialize"] }
1818
pythonize = "0.24.0"
1919
pyo3-async-runtimes = { version = "0.24.0", features = ["tokio-runtime"] }
2020

src/base/value.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl KeyValue {
340340
}
341341
}
342342

343-
#[derive(Debug, Clone)]
343+
#[derive(Debug, Clone, PartialEq)]
344344
pub enum BasicValue {
345345
Bytes(Bytes),
346346
Str(Arc<str>),
@@ -511,7 +511,7 @@ impl BasicValue {
511511
}
512512
}
513513

514-
#[derive(Debug, Clone, Default)]
514+
#[derive(Debug, Clone, Default, PartialEq)]
515515
pub enum Value<VS = ScopeValue> {
516516
#[default]
517517
Null,
@@ -747,7 +747,7 @@ impl<VS> Value<VS> {
747747
}
748748
}
749749

750-
#[derive(Debug, Clone)]
750+
#[derive(Debug, Clone, PartialEq)]
751751
pub struct FieldValues<VS = ScopeValue> {
752752
pub fields: Vec<Value<VS>>,
753753
}
@@ -821,7 +821,7 @@ where
821821
}
822822
}
823823

824-
#[derive(Debug, Clone, Serialize)]
824+
#[derive(Debug, Clone, Serialize, PartialEq)]
825825
pub struct ScopeValue(pub FieldValues);
826826

827827
impl Deref for ScopeValue {

src/py/convert.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,89 @@ pub fn value_from_py_object<'py>(
226226
};
227227
Ok(result)
228228
}
229+
230+
231+
#[cfg(test)]
232+
mod tests {
233+
use super::*;
234+
use crate::base::{schema, value};
235+
use pyo3::Python;
236+
use std::sync::Arc;
237+
238+
#[test]
239+
fn test_roundtrip_basic_values() {
240+
Python::with_gil(|py| {
241+
// Test Int64
242+
let int_value = value::Value::Basic(value::BasicValue::Int64(42));
243+
let int_type = schema::ValueType::Basic(schema::BasicValueType::Int64);
244+
let py_obj = value_to_py_object(py, &int_value).unwrap();
245+
let roundtrip_value = value_from_py_object(&int_type, &py_obj).unwrap();
246+
assert_eq!(int_value, roundtrip_value, "Int64 roundtrip failed");
247+
248+
// Test String
249+
let str_value = value::Value::Basic(value::BasicValue::Str(Arc::from("test string")));
250+
let str_type = schema::ValueType::Basic(schema::BasicValueType::Str);
251+
let py_obj = value_to_py_object(py, &str_value).unwrap();
252+
let roundtrip_value = value_from_py_object(&str_type, &py_obj).unwrap();
253+
assert_eq!(str_value, roundtrip_value, "String roundtrip failed");
254+
255+
// Test Bool
256+
let bool_value = value::Value::Basic(value::BasicValue::Bool(true));
257+
let bool_type = schema::ValueType::Basic(schema::BasicValueType::Bool);
258+
let py_obj = value_to_py_object(py, &bool_value).unwrap();
259+
let roundtrip_value = value_from_py_object(&bool_type, &py_obj).unwrap();
260+
assert_eq!(bool_value, roundtrip_value, "Bool roundtrip failed");
261+
});
262+
}
263+
264+
#[test]
265+
fn test_roundtrip_struct() {
266+
Python::with_gil(|py| {
267+
// Create a struct schema with multiple fields
268+
let struct_schema = schema::StructSchema {
269+
description: Some(Arc::from("Test struct")),
270+
fields: Arc::new(vec![
271+
schema::FieldSchema {
272+
name: "id".into(),
273+
value_type: schema::EnrichedValueType {
274+
typ: schema::ValueType::Basic(schema::BasicValueType::Int64),
275+
nullable: false,
276+
attrs: Default::default(),
277+
},
278+
},
279+
schema::FieldSchema {
280+
name: "name".into(),
281+
value_type: schema::EnrichedValueType {
282+
typ: schema::ValueType::Basic(schema::BasicValueType::Str),
283+
nullable: false,
284+
attrs: Default::default(),
285+
},
286+
},
287+
schema::FieldSchema {
288+
name: "active".into(),
289+
value_type: schema::EnrichedValueType {
290+
typ: schema::ValueType::Basic(schema::BasicValueType::Bool),
291+
nullable: false,
292+
attrs: Default::default(),
293+
},
294+
},
295+
]),
296+
};
297+
298+
// Create a struct value matching the schema
299+
let struct_value = value::Value::Struct(value::FieldValues {
300+
fields: vec![
301+
value::Value::Basic(value::BasicValue::Int64(1)),
302+
value::Value::Basic(value::BasicValue::Str(Arc::from("test"))),
303+
value::Value::Basic(value::BasicValue::Bool(true)),
304+
],
305+
});
306+
307+
// Perform roundtrip conversion
308+
let struct_type = schema::ValueType::Struct(struct_schema);
309+
let py_obj = value_to_py_object(py, &struct_value).unwrap();
310+
let roundtrip_value = value_from_py_object(&struct_type, &py_obj).unwrap();
311+
assert_eq!(struct_value, roundtrip_value, "Struct roundtrip failed");
312+
});
313+
}
314+
}

0 commit comments

Comments
 (0)