-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_scalar_custom_mappings_tests.rs
84 lines (69 loc) · 2.28 KB
/
struct_scalar_custom_mappings_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::proto;
use proto_mapper::derive::ProtoMap;
use proto_mapper::{ProtoMap, ProtoMapScalar, ProtoScalar};
use super::custom_mappings::{uuid_as_bytes, uuid_as_string};
use uuid::Uuid;
#[derive(Debug, ProtoMap, Eq, PartialEq)]
#[proto_map(source = "proto::protobuf::EntityUuids")]
struct EntityUuids {
#[proto_map(scalar, with = "uuid_as_string")]
uuid_str: Uuid,
#[proto_map(scalar, with = "uuid_as_string")]
opt_uuid_str: Option<Uuid>,
#[proto_map(scalar, with = "uuid_as_bytes")]
uuid_bytes: Uuid,
#[proto_map(scalar, with = "uuid_as_bytes")]
opt_uuid_bytes: Option<Uuid>,
}
#[test]
fn entity_round_trip() {
let original = EntityUuids {
uuid_str: Uuid::new_v4(),
opt_uuid_str: Some(Uuid::new_v4()),
uuid_bytes: Uuid::new_v4(),
opt_uuid_bytes: Some(Uuid::new_v4()),
};
let p = original.to_proto();
let tested = EntityUuids::from_proto(p).unwrap();
assert_eq!(tested, original);
}
#[test]
fn proto_entity_round_trip() {
let original = proto::protobuf::EntityUuids {
uuid_str: Uuid::new_v4().to_string(),
opt_uuid_str: Uuid::new_v4().to_string(),
uuid_bytes: Uuid::new_v4().as_bytes().to_vec(),
opt_uuid_bytes: Uuid::new_v4().as_bytes().to_vec(),
..Default::default()
};
let e = EntityUuids::from_proto(original.clone()).unwrap();
assert!(e.opt_uuid_str.is_some());
assert!(e.opt_uuid_bytes.is_some());
let tested = e.to_proto();
assert_eq!(tested, original);
}
#[test]
fn entity_optional_missing_round_trip() {
let original = EntityUuids {
uuid_str: Uuid::new_v4(),
opt_uuid_str: None,
uuid_bytes: Uuid::new_v4(),
opt_uuid_bytes: None,
};
let p = original.to_proto();
let tested = EntityUuids::from_proto(p).unwrap();
assert_eq!(tested, original);
}
#[test]
fn proto_entity_optional_missing_round_trip() {
let original = proto::protobuf::EntityUuids {
uuid_str: Uuid::new_v4().to_string(),
uuid_bytes: Uuid::new_v4().as_bytes().to_vec(),
..Default::default()
};
let e = EntityUuids::from_proto(original.clone()).unwrap();
assert!(e.opt_uuid_str.is_none());
assert!(e.opt_uuid_bytes.is_none());
let tested = e.to_proto();
assert_eq!(tested, original);
}