|
| 1 | +#![cfg(test)] |
| 2 | + |
| 3 | +//! Minimal smoke tests for postcard serialisation. |
| 4 | +//! These verify the bincode → postcard migration works correctly. |
| 5 | +
|
| 6 | +use crate::query::Value; |
| 7 | + |
| 8 | +/// Verify roundtrip works for all Value variants. |
| 9 | +#[test] |
| 10 | +fn test_value_roundtrip_all_variants() { |
| 11 | + let values = vec![ |
| 12 | + Value::Null, |
| 13 | + Value::Integer(0), |
| 14 | + Value::Integer(i64::MAX), |
| 15 | + Value::Integer(i64::MIN), |
| 16 | + Value::Real(std::f64::consts::PI), |
| 17 | + Value::Real(f64::MAX), |
| 18 | + Value::Text(String::new()), |
| 19 | + Value::Text("hello 🦀".to_string()), |
| 20 | + Value::Blob(vec![]), |
| 21 | + Value::Blob(vec![0u8, 255u8]), |
| 22 | + ]; |
| 23 | + |
| 24 | + for value in &values { |
| 25 | + let encoded = postcard::to_stdvec(value).expect("encode failed"); |
| 26 | + let decoded: Value = postcard::from_bytes(&encoded).expect("decode failed"); |
| 27 | + assert_eq!(format!("{:?}", value), format!("{:?}", decoded)); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// Verify large blobs don't hit size limits. |
| 32 | +#[test] |
| 33 | +fn test_large_blob_roundtrip() { |
| 34 | + let value = Value::Blob(vec![42u8; 1_000_000]); |
| 35 | + let encoded = postcard::to_stdvec(&value).expect("encode failed"); |
| 36 | + let decoded: Value = postcard::from_bytes(&encoded).expect("decode failed"); |
| 37 | + assert!(matches!(decoded, Value::Blob(b) if b.len() == 1_000_000)); |
| 38 | +} |
| 39 | + |
| 40 | +/// Verify postcard errors convert correctly to our error type. |
| 41 | +#[test] |
| 42 | +fn test_postcard_error_conversion() { |
| 43 | + let garbage = vec![0xff, 0xfe]; |
| 44 | + let result: Result<Value, postcard::Error> = postcard::from_bytes(&garbage); |
| 45 | + |
| 46 | + if let Err(e) = result { |
| 47 | + let crate_error = crate::error::Error::from(e); |
| 48 | + assert!( |
| 49 | + matches!(crate_error, crate::error::Error::Internal(msg) if msg.contains("Unexpected")) |
| 50 | + ); |
| 51 | + } |
| 52 | +} |
0 commit comments