⎇ d3649bfd1a80153652c2a028315369bb4b184024
crates/modules/nakui/core/tests/surreal_persist.rs
Historia
Quién tocó qué
crudo
chore: absorbe nakui (ERP matemático) en modules/nakui
1//! Persistence test for SurrealStore against the RocksDB backend.
2//!
3//! Gated behind the `persistent` Cargo feature because RocksDB is a heavy
4//! native dep (~5 min to compile cold). Run with:
5//! cargo test --features persistent --test surreal_persist
6
7#![cfg(feature = "persistent")]
8
9use std::path::PathBuf;
10
11use nakui_core::store::Store;
12use nakui_core::surreal_store::SurrealStore;
13use serde_json::{Value, json};
14use uuid::Uuid;
15
16fn fresh_db_path() -> PathBuf {
17 std::env::temp_dir().join(format!("nakui_persist_{}", Uuid::new_v4()))
18}
19
20#[test]
21fn data_survives_close_and_reopen() {
22 let path = fresh_db_path();
23 let id = Uuid::new_v4();
24
25 {
26 let mut store = SurrealStore::new_persistent(&path).expect("open persistent");
27 store.seed(
28 "Caja",
29 id,
30 json!({
31 "id": id.to_string(),
32 "name": "persisted",
33 "saldo": 12_345_i64,
34 "currency": "USD",
35 }),
36 );
37 // Drop store; runtime + db released.
38 }
39
40 {
41 let store = SurrealStore::new_persistent(&path).expect("reopen persistent");
42 let loaded = store
43 .load("Caja", id)
44 .expect("record must survive reopen");
45 assert_eq!(
46 loaded.get("saldo").and_then(Value::as_i64),
47 Some(12_345),
48 "saldo persisted"
49 );
50 assert_eq!(
51 loaded.get("currency").and_then(Value::as_str),
52 Some("USD"),
53 "currency persisted"
54 );
55 }
56
57 let _ = std::fs::remove_dir_all(&path);
58}
59
60#[test]
61fn applied_ops_persist_across_reopens() {
62 use nakui_core::delta::{FieldOp, FieldPath};
63
64 let path = fresh_db_path();
65 let id = Uuid::new_v4();
66
67 {
68 let mut store = SurrealStore::new_persistent(&path).expect("open");
69 store.seed(
70 "Caja",
71 id,
72 json!({"id": id.to_string(), "saldo": 100_i64, "currency": "USD"}),
73 );
74 store
75 .apply(&[FieldOp::Set {
76 path: FieldPath {
77 entity: "Caja".into(),
78 id,
79 field: "saldo".into(),
80 },
81 value: json!(999_i64),
82 }])
83 .expect("apply Set");
84 }
85
86 {
87 let store = SurrealStore::new_persistent(&path).expect("reopen");
88 let v = store.load("Caja", id).expect("present");
89 assert_eq!(
90 v.get("saldo").and_then(Value::as_i64),
91 Some(999),
92 "Set op persisted across restart"
93 );
94 }
95
96 let _ = std::fs::remove_dir_all(&path);
97}