Skip to main content

allsource_core/
test_utils.rs

1//! Shared test fixture builders for integration tests and cross-crate use.
2//!
3//! This module is public so that dependent crates (e.g. `allsource-performance`)
4//! can import ready-made fixtures instead of duplicating test data.
5
6use crate::{
7    domain::{
8        entities::{Event, Tenant, tenant::TenantQuotas},
9        value_objects::{EntityId, EventType, TenantId},
10    },
11    store::{EventStore, EventStoreConfig},
12};
13
14/// Create a minimal test event with sensible defaults.
15pub fn test_event(entity_id: &str, event_type: &str) -> Event {
16    Event::from_strings(
17        event_type.to_string(),
18        entity_id.to_string(),
19        "default".to_string(),
20        serde_json::json!({"test": true}),
21        None,
22    )
23    .expect("test event should be valid")
24}
25
26/// Create a test event with a custom JSON payload.
27pub fn test_event_with_payload(
28    entity_id: &str,
29    event_type: &str,
30    payload: serde_json::Value,
31) -> Event {
32    Event::from_strings(
33        event_type.to_string(),
34        entity_id.to_string(),
35        "default".to_string(),
36        payload,
37        None,
38    )
39    .expect("test event should be valid")
40}
41
42/// Create a test event scoped to a specific tenant.
43pub fn test_event_for_tenant(entity_id: &str, event_type: &str, tenant_id: &str) -> Event {
44    Event::from_strings(
45        event_type.to_string(),
46        entity_id.to_string(),
47        tenant_id.to_string(),
48        serde_json::json!({"test": true}),
49        None,
50    )
51    .expect("test event should be valid")
52}
53
54/// Create an in-memory EventStore (no persistence).
55pub fn test_store() -> EventStore {
56    EventStore::new()
57}
58
59/// Create an EventStore backed by a temporary directory for persistence tests.
60pub fn test_store_with_persistence(dir: &std::path::Path) -> EventStore {
61    EventStore::with_config(EventStoreConfig::with_persistence(dir))
62}
63
64/// Create a test tenant with standard quotas.
65pub fn test_tenant(id: &str, name: &str) -> Tenant {
66    let tenant_id = TenantId::new(id.to_string()).expect("valid tenant id");
67    Tenant::new(tenant_id, name.to_string(), TenantQuotas::standard()).expect("valid tenant")
68}
69
70/// Pre-built value objects for quick use in tests.
71pub fn test_event_type(name: &str) -> EventType {
72    EventType::new(name.to_string()).expect("valid event type")
73}
74
75pub fn test_entity_id(id: &str) -> EntityId {
76    EntityId::new(id.to_string()).expect("valid entity id")
77}
78
79pub fn test_tenant_id(id: &str) -> TenantId {
80    TenantId::new(id.to_string()).expect("valid tenant id")
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn test_fixtures_create_valid_objects() {
89        let event = test_event("user-1", "user.created");
90        assert_eq!(event.entity_id.as_str(), "user-1");
91        assert_eq!(event.event_type.as_str(), "user.created");
92
93        let event2 = test_event_with_payload(
94            "order-1",
95            "order.placed",
96            serde_json::json!({"amount": 100}),
97        );
98        assert_eq!(event2.entity_id.as_str(), "order-1");
99
100        let event3 = test_event_for_tenant("item-1", "item.added", "acme");
101        assert_eq!(event3.tenant_id.as_str(), "acme");
102    }
103
104    #[test]
105    fn test_store_fixture() {
106        let store = test_store();
107        let event = test_event("e-1", "test.event");
108        store.ingest(&event).unwrap();
109        assert_eq!(store.stats().total_events, 1);
110    }
111
112    #[test]
113    fn test_value_object_fixtures() {
114        let et = test_event_type("user.created");
115        assert_eq!(et.as_str(), "user.created");
116
117        let eid = test_entity_id("user-123");
118        assert_eq!(eid.as_str(), "user-123");
119
120        let tid = test_tenant_id("acme");
121        assert_eq!(tid.as_str(), "acme");
122    }
123}