Tests: Fix scope of store

This commit is contained in:
Lennart
2025-12-31 01:05:56 +01:00
parent f778c470d0
commit 4b8a8c61f2
14 changed files with 103 additions and 316 deletions

View File

@@ -1,12 +1,12 @@
use criterion::{Criterion, criterion_group, criterion_main};
use rustical_ical::{CalendarObject, CalendarObjectType};
use rustical_store::{Calendar, CalendarMetadata, CalendarStore};
use rustical_store_sqlite::tests::get_test_calendar_store;
use rustical_store_sqlite::tests::test_store_context;
fn benchmark(c: &mut Criterion) {
let runtime = tokio::runtime::Runtime::new().unwrap();
let cal_store = runtime.block_on(async {
let cal_store = get_test_calendar_store().await;
let cal_store = test_store_context().await.cal_store;
cal_store
.insert_calendar(Calendar {

View File

@@ -27,7 +27,7 @@ impl TryFrom<AddressObjectRow> for AddressObject {
}
}
#[derive(Debug, Constructor)]
#[derive(Debug, Clone, Constructor)]
pub struct SqliteAddressbookStore {
db: SqlitePool,
sender: Sender<CollectionOperation>,

View File

@@ -87,7 +87,7 @@ impl From<CalendarRow> for Calendar {
}
}
#[derive(Debug, Constructor)]
#[derive(Debug, Clone, Constructor)]
pub struct SqliteCalendarStore {
db: SqlitePool,
sender: Sender<CollectionOperation>,

View File

@@ -24,7 +24,7 @@ pub(crate) enum ChangeOperation {
Delete,
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct SqliteStore {
db: SqlitePool,
}

View File

@@ -41,7 +41,7 @@ impl TryFrom<PrincipalRow> for Principal {
}
}
#[derive(Debug, Constructor)]
#[derive(Debug, Clone, Constructor)]
pub struct SqlitePrincipalStore {
db: SqlitePool,
}

View File

@@ -1,17 +1,17 @@
#[cfg(test)]
mod tests {
use crate::{addressbook_store::SqliteAddressbookStore, tests::get_test_addressbook_store};
use crate::tests::{TestStoreContext, test_store_context};
use rstest::rstest;
use rustical_store::{Addressbook, AddressbookStore};
#[rstest]
#[tokio::test]
async fn test_addressbook_store(
#[from(get_test_addressbook_store)]
#[from(test_store_context)]
#[future]
addr_store: SqliteAddressbookStore,
context: TestStoreContext,
) {
let addr_store = addr_store.await;
let addr_store = context.await.addr_store;
let cal = Addressbook {
id: "addr".to_string(),

View File

@@ -1,17 +1,19 @@
#[cfg(test)]
mod tests {
use crate::{calendar_store::SqliteCalendarStore, tests::get_test_calendar_store};
use crate::tests::{TestStoreContext, test_store_context};
use rstest::rstest;
use rustical_store::{Calendar, CalendarMetadata, CalendarStore};
#[rstest]
#[tokio::test]
async fn test_calendar_store(
#[from(get_test_calendar_store)]
#[future]
cal_store: SqliteCalendarStore,
#[from(test_store_context)]
context: TestStoreContext,
) {
let cal_store = cal_store.await;
let TestStoreContext { cal_store, .. } = context.await;
let cal_store = cal_store;
let cal = Calendar {
principal: "fake-user".to_string(),

View File

@@ -2,61 +2,59 @@ use crate::{
SqliteStore, addressbook_store::SqliteAddressbookStore, calendar_store::SqliteCalendarStore,
principal_store::SqlitePrincipalStore,
};
use rstest::fixture;
use rustical_store::auth::{AuthenticationProvider, Principal, PrincipalType};
use sqlx::SqlitePool;
use tokio::sync::OnceCell;
static DB: OnceCell<SqlitePool> = OnceCell::const_new();
mod addressbook_store;
mod calendar_store;
async fn get_test_db() -> SqlitePool {
DB.get_or_init(async || {
let db = SqlitePool::connect("sqlite::memory:").await.unwrap();
sqlx::migrate!("./migrations").run(&db).await.unwrap();
let db = SqlitePool::connect("sqlite::memory:").await.unwrap();
sqlx::migrate!("./migrations").run(&db).await.unwrap();
// Populate with test data
let principal_store = SqlitePrincipalStore::new(db.clone());
principal_store
.insert_principal(
Principal {
id: "user".to_owned(),
displayname: None,
memberships: vec![],
password: None,
principal_type: PrincipalType::Individual,
},
false,
)
.await
.unwrap();
principal_store
.add_app_token("user", "test".to_string(), "pass".to_string())
.await
.unwrap();
// Populate with test data
let principal_store = SqlitePrincipalStore::new(db.clone());
principal_store
.insert_principal(
Principal {
id: "user".to_owned(),
displayname: None,
memberships: vec![],
password: None,
principal_type: PrincipalType::Individual,
},
false,
)
.await
.unwrap();
principal_store
.add_app_token("user", "test".to_string(), "pass".to_string())
.await
.unwrap();
db
})
.await
.clone()
db
}
#[rstest::fixture]
pub async fn get_test_addressbook_store() -> SqliteAddressbookStore {
let (send, _recv) = tokio::sync::mpsc::channel(1000);
SqliteAddressbookStore::new(get_test_db().await, send)
#[derive(Debug, Clone)]
pub struct TestStoreContext {
pub db: SqlitePool,
pub addr_store: SqliteAddressbookStore,
pub cal_store: SqliteCalendarStore,
pub principal_store: SqlitePrincipalStore,
pub sub_store: SqliteStore,
}
#[rstest::fixture]
pub async fn get_test_calendar_store() -> SqliteCalendarStore {
let (send, _recv) = tokio::sync::mpsc::channel(1000);
SqliteCalendarStore::new(get_test_db().await, send)
}
#[rstest::fixture]
pub async fn get_test_subscription_store() -> SqliteStore {
SqliteStore::new(get_test_db().await)
}
#[rstest::fixture]
pub async fn get_test_principal_store() -> SqlitePrincipalStore {
SqlitePrincipalStore::new(get_test_db().await)
#[fixture]
pub async fn test_store_context() -> TestStoreContext {
let (send_addr, _recv) = tokio::sync::mpsc::channel(1);
let (send_cal, _recv) = tokio::sync::mpsc::channel(1);
let db = get_test_db().await;
TestStoreContext {
db: db.clone(),
addr_store: SqliteAddressbookStore::new(db.clone(), send_addr),
cal_store: SqliteCalendarStore::new(db.clone(), send_cal),
principal_store: SqlitePrincipalStore::new(db.clone()),
sub_store: SqliteStore::new(db),
}
}