use happenstance_sync_testkit::{IngestFixture, Violation};
use happenstance_testkit::Capability;
use sqlx::PgPool;
use tokio::sync::OnceCell;
use super::server;
use crate::event_store::PostgresEventStore;
#[derive(Debug)]
struct PostgresIngestFixture {
pool: OnceCell<PgPool>,
}
impl PostgresIngestFixture {
fn new() -> Self {
Self {
pool: OnceCell::new(),
}
}
}
impl PostgresIngestFixture {
async fn pool(&self) -> &PgPool {
self.pool.get_or_init(server::fresh_pool).await
}
}
impl IngestFixture for PostgresIngestFixture {
type Store = PostgresEventStore;
const MID_GROUP_FAULT: Capability = Capability::SUPPORTED;
const MAX_EVENT_DATA_LEN: Option<usize> = Some(PostgresEventStore::MAX_EVENT_DATA_LEN);
const MAX_TAGS_PER_EVENT: Option<usize> = Some(PostgresEventStore::MAX_TAGS_PER_EVENT);
async fn connect(&self) -> Self::Store {
let pool = self.pool().await.clone();
PostgresEventStore::new(pool)
}
async fn arm_mid_group_fault(&self, after: usize) -> Result<(), Violation> {
let threshold = after
.checked_add(1)
.and_then(|row| i64::try_from(row).ok())
.ok_or_else(|| {
Violation::new("SY-30", format!("a cut at row {after} overflows an i64"))
})?;
let sql = format!(
"CREATE OR REPLACE FUNCTION mid_group_fault() RETURNS trigger AS $fn$ \
DECLARE seen bigint; \
BEGIN \
seen := coalesce(nullif(current_setting('hs.mid_group_seen', true), ''), \
'0')::bigint + 1; \
PERFORM set_config('hs.mid_group_seen', seen::text, true); \
IF seen = {threshold} THEN \
RAISE EXCEPTION 'the fixture armed a mid-group fault'; \
END IF; \
RETURN NEW; \
END $fn$ LANGUAGE plpgsql; \
DROP TRIGGER IF EXISTS mid_group_fault ON event; \
CREATE TRIGGER mid_group_fault AFTER INSERT ON event \
FOR EACH ROW EXECUTE FUNCTION mid_group_fault();"
);
sqlx::raw_sql(&sql)
.execute(self.pool().await)
.await
.map(|_| ())
.map_err(|error| {
Violation::new(
"SY-30",
format!("the Postgres fixture could not arm its trigger: {error}"),
)
})
}
}
macro_rules! emit_ignored_ingest_tokio {
($($name:ident),* $(,)?) => {
$(
#[tokio::test]
#[ignore = "needs a live Postgres; run with `-- --ignored` (starts a container, or set HAPPENSTANCE_POSTGRES_URL)"]
async fn $name() -> ::core::result::Result<(), ::happenstance_sync_testkit::Violation> {
::happenstance_sync_testkit::ingest_rules::$name(__ingest_fixture)
.await?
.report(::core::stringify!($name));
Ok(())
}
)*
};
}
pub(crate) use emit_ignored_ingest_tokio;
happenstance_sync_testkit::ingest_store_conformance!(
mod_name = ingest_conformance,
emit = super::emit_ignored_ingest_tokio,
fixture = super::PostgresIngestFixture::new()
);
macro_rules! emit_ignored_write_group_tokio {
($($name:ident),* $(,)?) => {
$(
#[tokio::test]
#[ignore = "needs a live Postgres; run with `-- --ignored` (starts a container, or set HAPPENSTANCE_POSTGRES_URL)"]
async fn $name() -> ::core::result::Result<(), ::happenstance_sync_testkit::Violation> {
::happenstance_sync_testkit::write_group_rules::$name(__write_group_fixture)
.await?
.report(::core::stringify!($name));
Ok(())
}
)*
};
}
pub(crate) use emit_ignored_write_group_tokio;
happenstance_sync_testkit::write_group_store_conformance!(
mod_name = write_group_conformance,
emit = super::emit_ignored_write_group_tokio,
fixture = super::PostgresIngestFixture::new()
);