Add tests for CompressedCRDTOperation (#2189)

This commit is contained in:
Brendan Allan 2024-03-12 08:39:57 +08:00 committed by GitHub
parent a5f74b19c2
commit d1fa6af7be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 182 additions and 27 deletions

View file

@ -23,13 +23,12 @@ repository = "https://github.com/spacedriveapp/spacedrive"
[workspace.dependencies]
# First party dependencies
prisma-client-rust = { git = "https://github.com/spacedriveapp/prisma-client-rust", rev = "f99d6f5566570f3ab1edecb7a172ad25b03d95af", features = [
"rspc",
"sqlite-create-many",
"migrations",
"sqlite",
], default-features = false }
prisma-client-rust-cli = { git = "https://github.com/spacedriveapp/prisma-client-rust", rev = "f99d6f5566570f3ab1edecb7a172ad25b03d95af", features = [
"rspc",
"specta",
"sqlite-create-many",
"migrations",
"sqlite",

View file

@ -59,7 +59,7 @@ image = { workspace = true }
normpath = { workspace = true, features = ["localization"] }
once_cell = { workspace = true }
pin-project-lite = { workspace = true }
prisma-client-rust = { workspace = true }
prisma-client-rust = { workspace = true, features = ["rspc"] }
regex = { workspace = true }
reqwest = { workspace = true, features = ["json", "native-tls-vendored"] }
rmp-serde = { workspace = true }

View file

@ -12,6 +12,6 @@ rmp-serde = "1.1.2"
rmpv = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
specta = { workspace = true, features = ["uuid", "uhlc"] }
specta = { workspace = true, features = ["uuid", "uhlc", "serde_json"] }
uhlc = { workspace = true }
uuid = { workspace = true, features = ["serde", "v4"] }

View file

@ -8,7 +8,9 @@ pub type CompressedCRDTOperationsForModel = Vec<(rmpv::Value, Vec<CompressedCRDT
/// Stores a bunch of CRDTOperations in a more memory-efficient form for sending to the cloud.
#[derive(Serialize, Deserialize)]
pub struct CompressedCRDTOperations(Vec<(Uuid, Vec<(String, CompressedCRDTOperationsForModel)>)>);
pub struct CompressedCRDTOperations(
pub(self) Vec<(Uuid, Vec<(String, CompressedCRDTOperationsForModel)>)>,
);
impl CompressedCRDTOperations {
pub fn new(ops: Vec<CRDTOperation>) -> Self {
@ -109,3 +111,157 @@ impl From<CRDTOperation> for CompressedCRDTOperation {
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn compress() {
let instance = Uuid::new_v4();
let uncompressed = vec![
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "Object".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "Object".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
];
let CompressedCRDTOperations(compressed) = CompressedCRDTOperations::new(uncompressed);
assert_eq!(&compressed[0].1[0].0, "FilePath");
assert_eq!(&compressed[0].1[1].0, "Object");
assert_eq!(&compressed[0].1[2].0, "FilePath");
assert_eq!(compressed[0].1[0].1[0].1.len(), 3);
assert_eq!(compressed[0].1[1].1[0].1.len(), 2);
assert_eq!(compressed[0].1[2].1[0].1.len(), 2);
}
#[test]
fn into_ops() {
let compressed = CompressedCRDTOperations(vec![(
Uuid::new_v4(),
vec![
(
"FilePath".to_string(),
vec![(
rmpv::Value::Nil,
vec![
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
],
)],
),
(
"Object".to_string(),
vec![(
rmpv::Value::Nil,
vec![
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
],
)],
),
(
"FilePath".to_string(),
vec![(
rmpv::Value::Nil,
vec![
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
],
)],
),
],
)]);
let uncompressed = compressed.into_ops();
assert_eq!(uncompressed.len(), 7);
assert_eq!(uncompressed[2].model, "FilePath");
assert_eq!(uncompressed[4].model, "Object");
assert_eq!(uncompressed[6].model, "FilePath");
}
}