mirror of
https://github.com/danbulant/convex-macros
synced 2026-05-19 03:58:31 +00:00
104 lines
No EOL
2.9 KiB
Rust
104 lines
No EOL
2.9 KiB
Rust
use convex::Value;
|
|
use maplit::btreemap;
|
|
use ragkit_convex_macros::convex_model;
|
|
use serde_json::json;
|
|
|
|
#[test]
|
|
fn basic_string_array() {
|
|
convex_model!(Model { a: v.array(v.string()) });
|
|
|
|
let convex_data = Value::Object(btreemap! {
|
|
"a".into() => Value::Array(vec![Value::String("apple".into()), Value::String("banana".into())]),
|
|
});
|
|
let json_data = json!({
|
|
"a": ["apple", "banana"],
|
|
});
|
|
|
|
let model = Model::from_convex_value(&convex_data);
|
|
assert!(model.is_ok());
|
|
let model = model.unwrap();
|
|
assert_eq!(vec!["apple".to_string(), "banana".to_string()], model.a);
|
|
assert_eq!(json_data, json!(model));
|
|
}
|
|
|
|
#[test]
|
|
fn union_array() {
|
|
convex_model!(Model { a: v.array(v.union(v.string(), v.number())) });
|
|
|
|
let convex_data = Value::Object(btreemap! {
|
|
"a".into() => Value::Array(vec![Value::String("apple".into()), Value::Float64(42.)]),
|
|
});
|
|
let json_data = json!({
|
|
"a": ["apple", 42.],
|
|
});
|
|
|
|
let model = Model::from_convex_value(&convex_data);
|
|
assert!(model.is_ok());
|
|
let model = model.unwrap();
|
|
// assert_eq!(vec!["apple".to_string(), 42], model.a);
|
|
assert_eq!(json_data, json!(model));
|
|
}
|
|
|
|
#[test]
|
|
fn union_array_objects() {
|
|
convex_model!(Model {
|
|
a: v.array(
|
|
v.union(
|
|
v.object({
|
|
typ: v.literal("http"),
|
|
hostname: v.string(),
|
|
port: v.number()
|
|
}),
|
|
v.object({
|
|
typ: v.literal("tcp"),
|
|
port: v.number()
|
|
})
|
|
)
|
|
)
|
|
});
|
|
|
|
let convex_data = Value::Object(btreemap! {
|
|
"a".into() => Value::Array(vec![
|
|
Value::Object(btreemap! {
|
|
"typ".into() => Value::String("http".into()),
|
|
"hostname".into() => Value::String("example.com".into()),
|
|
"port".into() => Value::Float64(80.0),
|
|
}),
|
|
Value::Object(btreemap! {
|
|
"typ".into() => Value::String("tcp".into()),
|
|
"port".into() => Value::Float64(8080.0),
|
|
}),
|
|
]),
|
|
});
|
|
let json_data = json!({
|
|
"a": [
|
|
{
|
|
"typ": "http",
|
|
"hostname": "example.com",
|
|
"port": 80.0,
|
|
},
|
|
{
|
|
"typ": "tcp",
|
|
"port": 8080.0,
|
|
},
|
|
],
|
|
});
|
|
let model = Model::from_convex_value(&convex_data);
|
|
assert!(model.is_ok());
|
|
let model = model.unwrap();
|
|
assert_eq!(
|
|
vec![
|
|
ModelA::Variant1(ModelAVariant1 {
|
|
typ: "http".to_string(),
|
|
hostname: "example.com".to_string(),
|
|
port: 80.0,
|
|
}),
|
|
ModelA::Variant2(ModelAVariant2 {
|
|
typ: "tcp".to_string(),
|
|
port: 8080.0,
|
|
}),
|
|
],
|
|
model.a
|
|
);
|
|
assert_eq!(json_data, json!(model));
|
|
} |