cultcache-rs: catalog_members test mirrors every member shape (rs-member-rows-vectors)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-27 22:21:33 +01:00
commit 262df11aa1

View file

@ -0,0 +1,134 @@
//! `tests/vectors/catalog-members`: a record that holds a value in every member shape, in the two stores the vector folder
//! holds. `csharp-members-store.cc` is written by the C# reference and publishes one catalog row per member;
//! `rust-members-empty-store.cc` is written by this runtime as it stood at e503edf2, whose catalog entry holds `members []`.
//! Rust writes the second only under `CULTCACHE_WRITE_VECTORS=1` and it is never regenerated: it pins the bytes old Rust
//! wrote, so a runtime that reads a store with no member rows keeps reading one. Both stores must read back the same record.
use std::path::PathBuf;
use cultcache_rs::{CultCache, CultSlots, CultUnion, DatabaseEntry, SingleFileMessagePackBackingStore};
#[derive(Clone, Debug, PartialEq, CultSlots)]
struct CatalogMemberMark {
#[cultcache(key = 0)]
label: String,
#[cultcache(key = 1)]
weight: i32,
}
#[derive(Clone, Debug, PartialEq, CultSlots)]
struct CatalogMemberTextArm {
#[cultcache(key = 0)]
text: String,
}
#[derive(Clone, Debug, PartialEq, CultSlots)]
struct CatalogMemberCountArm {
#[cultcache(key = 0)]
count: i32,
}
#[derive(Clone, Debug, PartialEq, CultUnion)]
enum CatalogMemberArm {
#[cultcache(key = 0)]
Text(CatalogMemberTextArm),
#[cultcache(key = 1)]
Count(CatalogMemberCountArm),
}
#[derive(Clone, Debug, PartialEq, DatabaseEntry)]
#[cultcache(type = "tests.catalog_members", schema = "tests.catalog_members")]
struct CatalogMembers {
#[cultcache(key = 0, name)]
title: String,
#[cultcache(key = 1, index = "sku")]
sku: String,
#[cultcache(key = 2)]
count: i32,
#[cultcache(key = 3)]
total: u64,
#[cultcache(key = 4)]
ratio: f64,
#[cultcache(key = 5)]
scale: f32,
#[cultcache(key = 6)]
flag: bool,
#[cultcache(key = 7)]
maybe_count: Option<i32>,
#[cultcache(key = 8)]
note: Option<String>,
#[cultcache(key = 9)]
tags: Vec<String>,
#[cultcache(key = 10)]
labels: Vec<String>,
#[cultcache(key = 11, bytes)]
raw: Vec<u8>,
#[cultcache(key = 12)]
chunks: Vec<serde_bytes::ByteBuf>,
#[cultcache(key = 13)]
weights: std::collections::BTreeMap<String, i32>,
#[cultcache(key = 14)]
mark: CatalogMemberMark,
#[cultcache(key = 15)]
arm: CatalogMemberArm,
#[cultcache(key = 17)]
late: i64,
}
fn expected() -> CatalogMembers {
CatalogMembers {
title: "Brass lantern".into(),
sku: "LAN-0042".into(),
count: -7,
total: 10_000_000_000,
ratio: 0.25,
scale: 1.5,
flag: true,
maybe_count: Some(3),
note: Some("handle with care".into()),
tags: vec!["fragile".into(), "lit".into()],
labels: vec!["a".into(), "b".into(), "c".into()],
raw: vec![0, 1, 2, 254, 255],
chunks: vec![serde_bytes::ByteBuf::from(vec![9, 8]), serde_bytes::ByteBuf::from(vec![7])],
weights: [("brass".to_string(), 12), ("glass".to_string(), 3)].into_iter().collect(),
mark: CatalogMemberMark { label: "stamp".into(), weight: 5 },
arm: CatalogMemberArm::Count(CatalogMemberCountArm { count: 21 }),
late: -9_000_000_000,
}
}
fn vectors() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/vectors/catalog-members")
}
fn read_from(path: &std::path::Path) -> CatalogMembers {
let dir = tempfile::tempdir().unwrap();
let copy = dir.path().join("store.cc");
std::fs::copy(path, &copy).unwrap();
let mut cache = CultCache::new();
cache.register_entry_type::<CatalogMembers>().unwrap();
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&copy)).unwrap();
cache.pull_all_backing_stores().unwrap();
cache.get::<CatalogMembers>("m:1").unwrap().expect("m:1 is in the store")
}
#[test]
fn the_members_empty_store_vector() {
let path = vectors().join("rust-members-empty-store.cc");
if std::env::var("CULTCACHE_WRITE_VECTORS").as_deref() == Ok("1") {
std::fs::create_dir_all(vectors()).unwrap();
let _ = std::fs::remove_file(&path);
let mut cache = CultCache::new();
cache.register_entry_type::<CatalogMembers>().unwrap();
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap();
cache.pull_all_backing_stores().unwrap();
cache.put("m:1", &expected()).unwrap();
return;
}
assert_eq!(read_from(&path), expected());
}
#[test]
fn a_csharp_written_store_reads_in_rust() {
assert_eq!(read_from(&vectors().join("csharp-members-store.cc")), expected());
}