Skip to content

Commit 05d400c

Browse files
committed
Proto get properties conversions test
Signed-off-by: Luca Arato <luca.arato@secomind.com>
1 parent 64e23ae commit 05d400c

File tree

2 files changed

+359
-63
lines changed

2 files changed

+359
-63
lines changed

src/transport/grpc/convert.rs

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,10 @@ impl From<Value> for ProtoPayload {
461461

462462
#[cfg(test)]
463463
mod test {
464+
use core::panic;
465+
464466
use astarte_message_hub_proto::{
465-
astarte_data_type_individual::IndividualData, astarte_message::Payload as ProtoPayload,
466-
AstarteMessage,
467+
astarte_data_type_individual::IndividualData, AstarteMessage, InterfaceProperties,
467468
};
468469
use chrono::{DateTime, Utc};
469470

@@ -1334,4 +1335,113 @@ mod test {
13341335

13351336
assert_eq!(IndividualData::AstarteDouble(expected_data), double_data);
13361337
}
1338+
1339+
fn make_messagehub_property(
1340+
path: &str,
1341+
value: Option<AstarteType>,
1342+
) -> astarte_message_hub_proto::Property {
1343+
astarte_message_hub_proto::Property {
1344+
path: path.to_owned(),
1345+
value: Some(value.map_or(
1346+
astarte_message_hub_proto::property::Value::AstarteUnset(
1347+
astarte_message_hub_proto::AstarteUnset {},
1348+
),
1349+
|v| astarte_message_hub_proto::property::Value::AstarteProperty(v.into()),
1350+
)),
1351+
}
1352+
}
1353+
1354+
#[test]
1355+
fn map_property_to_astarte_type() {
1356+
let value: AstarteType = AstarteType::String("test".to_owned());
1357+
1358+
let prop = make_messagehub_property("/path11", Some(value.clone()));
1359+
1360+
let astarte_type = AstarteType::try_from(prop).unwrap();
1361+
1362+
assert_eq!(value, astarte_type);
1363+
}
1364+
1365+
#[test]
1366+
fn map_property_to_astarte_type_error() {
1367+
let prop = make_messagehub_property("/path11", None);
1368+
1369+
let astarte_type_err = AstarteType::try_from(prop);
1370+
1371+
assert!(matches!(
1372+
astarte_type_err,
1373+
Err(MessageHubProtoError::ExpectedField(..))
1374+
));
1375+
}
1376+
1377+
#[test]
1378+
fn from_message_hub_stored_properties_to_internal_ok() {
1379+
const INTERFACE_1: &str = "com.test.interface1";
1380+
const INTERFACE_2: &str = "com.test.interface2";
1381+
1382+
let interface_properties_map = vec![
1383+
(
1384+
INTERFACE_1.to_owned(),
1385+
InterfaceProperties {
1386+
ownership: astarte_message_hub_proto::Ownership::Device.into(),
1387+
version_major: 0,
1388+
properties: vec![
1389+
make_messagehub_property(
1390+
"/path11",
1391+
Some(AstarteType::String("test".to_owned())),
1392+
),
1393+
make_messagehub_property("/path12", Some(AstarteType::Integer(0))),
1394+
],
1395+
},
1396+
),
1397+
(
1398+
INTERFACE_2.to_owned(),
1399+
InterfaceProperties {
1400+
ownership: astarte_message_hub_proto::Ownership::Server.into(),
1401+
version_major: 0,
1402+
properties: vec![
1403+
make_messagehub_property(
1404+
"/path21",
1405+
Some(AstarteType::BinaryBlob(vec![0, 54, 0, 23])),
1406+
),
1407+
make_messagehub_property(
1408+
"/path22",
1409+
Some(AstarteType::Double(std::f64::consts::PI)),
1410+
),
1411+
],
1412+
},
1413+
),
1414+
]
1415+
.into_iter()
1416+
.collect();
1417+
1418+
let message_hub_stored_properties: astarte_message_hub_proto::StoredProperties =
1419+
astarte_message_hub_proto::StoredProperties {
1420+
interface_properties: interface_properties_map,
1421+
};
1422+
1423+
let inner_vec = map_set_stored_properties(message_hub_stored_properties).unwrap();
1424+
1425+
assert_eq!(inner_vec.len(), 4);
1426+
1427+
assert_eq!(
1428+
inner_vec
1429+
.iter()
1430+
.filter(
1431+
|p| p.interface == INTERFACE_1 && (p.path == "/path11" || p.path == "/path12")
1432+
)
1433+
.count(),
1434+
2
1435+
);
1436+
1437+
assert_eq!(
1438+
inner_vec
1439+
.iter()
1440+
.filter(
1441+
|p| p.interface == INTERFACE_2 && (p.path == "/path21" || p.path == "/path22")
1442+
)
1443+
.count(),
1444+
2
1445+
);
1446+
}
13371447
}

0 commit comments

Comments
 (0)