automerge/automerge-protocol/tests/serde_round_trip.rs
Andrew Jeffery 9f554252d0
Use tinyvec for actor id (#179)
* Use tinyvec for actor id

Most of the time users will not be using their own identifiers and so
use random uuids. These can fit nicely on the stack for a speedup.
TinyVec allows us to capture this mostly stack, sometimes heap
behaviour.

* Use git version of tinyvec for arbitrary
2021-07-15 10:18:07 +01:00

61 lines
1.6 KiB
Rust

extern crate automerge_protocol as amp;
use maplit::hashmap;
// This was not caught in the proptests
#[test]
fn test_msgpack_roundtrip_diff() {
let actor = amp::ActorId::from("bd1850df21004038a8141a98473ff142".as_bytes());
let diff = amp::RootDiff {
props: hashmap! {
"bird".into() => hashmap! {
actor.op_id_at(1) => "magpie".into()
}
},
};
let serialized = rmp_serde::to_vec_named(&diff).unwrap();
let deserialized: amp::RootDiff = rmp_serde::from_slice(&serialized).unwrap();
assert_eq!(diff, deserialized);
}
#[test]
fn patch_roundtrip() {
let patch_json = r#"{
"clock": {
"7b7723afd9e6480397a4d467b7693156": 1
},
"deps": [
"822845b4bac583c5fc67fb60937cfb814cd79d85e8dfdbdafc75424ec573d898"
],
"maxOp": 4,
"pendingChanges": 0,
"diffs": {
"objectId": "_root",
"type": "map",
"props": {
"todos": {
"1@7b7723afd9e6480397a4d467b7693156": {
"objectId": "1@7b7723afd9e6480397a4d467b7693156",
"type": "list",
"edits": [
{
"action": "multi-insert",
"index": 0,
"elemId": "2@7b7723afd9e6480397a4d467b7693156",
"datatype": "int",
"values": [
1,
2,
3
]
}
]
}
}
}
}
}"#;
let patch: amp::Patch = serde_json::from_str(patch_json).unwrap();
let new_patch_json = serde_json::to_string_pretty(&patch).unwrap();
let new_patch: amp::Patch = serde_json::from_str(&new_patch_json).unwrap();
assert_eq!(patch, new_patch);
}