|
| 1 | +use tempfile::TempDir; |
| 2 | + |
| 3 | +use crate::common::{ExecRows, TempDatabase}; |
| 4 | + |
| 5 | +#[turso_macros::test()] |
| 6 | +fn test_snapshot_basic(db: TempDatabase) { |
| 7 | + let conn = db.connect_limbo(); |
| 8 | + |
| 9 | + // Create table and insert data |
| 10 | + conn.execute("CREATE TABLE t(x INTEGER PRIMARY KEY, y TEXT)") |
| 11 | + .unwrap(); |
| 12 | + conn.execute("INSERT INTO t VALUES (1, 'hello'), (2, 'world')") |
| 13 | + .unwrap(); |
| 14 | + |
| 15 | + // Create snapshot |
| 16 | + let temp_dir = TempDir::new().unwrap(); |
| 17 | + let snapshot_path = temp_dir.path().join("snapshot.db"); |
| 18 | + |
| 19 | + conn.snapshot(snapshot_path.to_str().unwrap()).unwrap(); |
| 20 | + |
| 21 | + // Verify snapshot exists |
| 22 | + assert!(snapshot_path.exists()); |
| 23 | + |
| 24 | + // Open snapshot and verify data |
| 25 | + let snapshot_db = TempDatabase::new_with_existent(&snapshot_path); |
| 26 | + let snapshot_conn = snapshot_db.connect_limbo(); |
| 27 | + let rows: Vec<(i64, String)> = snapshot_conn.exec_rows("SELECT x, y FROM t ORDER BY x"); |
| 28 | + assert_eq!( |
| 29 | + rows, |
| 30 | + vec![(1, "hello".to_string()), (2, "world".to_string())] |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +#[turso_macros::test()] |
| 35 | +fn test_snapshot_with_pending_wal_data(db: TempDatabase) { |
| 36 | + let conn = db.connect_limbo(); |
| 37 | + conn.execute("CREATE TABLE t(x INTEGER PRIMARY KEY, y BLOB)") |
| 38 | + .unwrap(); |
| 39 | + |
| 40 | + // Insert enough data to create multiple WAL frames |
| 41 | + for i in 0..100 { |
| 42 | + conn.execute(format!("INSERT INTO t VALUES ({i}, randomblob(1000))")) |
| 43 | + .unwrap(); |
| 44 | + } |
| 45 | + |
| 46 | + // Verify WAL has frames before snapshot |
| 47 | + let wal_state = conn.wal_state().unwrap(); |
| 48 | + assert!( |
| 49 | + wal_state.max_frame > 0, |
| 50 | + "WAL should have frames before snapshot" |
| 51 | + ); |
| 52 | + |
| 53 | + // Create snapshot |
| 54 | + let temp_dir = TempDir::new().unwrap(); |
| 55 | + let snapshot_path = temp_dir.path().join("snapshot.db"); |
| 56 | + |
| 57 | + conn.snapshot(snapshot_path.to_str().unwrap()).unwrap(); |
| 58 | + |
| 59 | + // Verify all data is in snapshot |
| 60 | + let snapshot_db = TempDatabase::new_with_existent(&snapshot_path); |
| 61 | + let snapshot_conn = snapshot_db.connect_limbo(); |
| 62 | + let count: Vec<(i64,)> = snapshot_conn.exec_rows("SELECT COUNT(*) FROM t"); |
| 63 | + assert_eq!(count[0].0, 100); |
| 64 | +} |
0 commit comments