use crate::values::{ChangeOp, Relation};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelationChange {
pub operation: ChangeOp,
pub relation: Relation,
}
impl RelationChange {
pub fn new(operation: ChangeOp, relation: Relation) -> Self {
Self {
operation,
relation,
}
}
pub fn create(relation: Relation) -> Self {
Self::new(ChangeOp::Create, relation)
}
pub fn update(relation: Relation) -> Self {
Self::new(ChangeOp::Update, relation)
}
pub fn delete(relation: Relation) -> Self {
Self::new(ChangeOp::Delete, relation)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::values::{ThingRef, Value};
use std::collections::HashMap;
#[test]
fn test_create_relation_change() {
let rel = Relation::new(
"book_tags",
Value::Int64(1),
ThingRef::new("books", Value::Int64(10)),
ThingRef::new("tags", Value::Int64(20)),
HashMap::new(),
);
let change = RelationChange::create(rel);
assert_eq!(change.operation, ChangeOp::Create);
assert_eq!(change.relation.relation_type, "book_tags");
}
#[test]
fn test_delete_relation_change() {
let rel = Relation::new(
"book_tags",
Value::Int64(1),
ThingRef::new("books", Value::Int64(10)),
ThingRef::new("tags", Value::Int64(20)),
HashMap::new(),
);
let change = RelationChange::delete(rel);
assert_eq!(change.operation, ChangeOp::Delete);
}
}