mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
feat(test): add ability to use test env without schedule
This commit is contained in:
@@ -2,23 +2,46 @@
|
||||
pub(crate) mod tests {
|
||||
use crate::app_state::{AppState, Schedule, app_state};
|
||||
use crate::parser::tests::test_result;
|
||||
use crate::utility::mutex::MutexScope;
|
||||
use actix_web::web;
|
||||
use std::default::Default;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
pub fn test_env() {
|
||||
dotenvy::from_path(".env.test").expect("Failed to load test environment file");
|
||||
}
|
||||
|
||||
pub async fn test_app_state() -> web::Data<AppState> {
|
||||
let state = app_state().await;
|
||||
let mut schedule_lock = state.schedule.lock().unwrap();
|
||||
pub enum TestScheduleType {
|
||||
None,
|
||||
Local,
|
||||
}
|
||||
|
||||
*schedule_lock = Some(Schedule {
|
||||
etag: "".to_string(),
|
||||
fetched_at: Default::default(),
|
||||
updated_at: Default::default(),
|
||||
parsed_at: Default::default(),
|
||||
data: test_result().unwrap(),
|
||||
pub struct TestAppStateParams {
|
||||
pub schedule: TestScheduleType,
|
||||
}
|
||||
|
||||
impl Default for TestAppStateParams {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
schedule: TestScheduleType::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn test_app_state(params: TestAppStateParams) -> web::Data<AppState> {
|
||||
let state = app_state().await;
|
||||
|
||||
state.schedule.scope(|schedule| {
|
||||
*schedule = match params.schedule {
|
||||
TestScheduleType::None => None,
|
||||
TestScheduleType::Local => Some(Schedule {
|
||||
etag: "".to_string(),
|
||||
fetched_at: Default::default(),
|
||||
updated_at: Default::default(),
|
||||
parsed_at: Default::default(),
|
||||
data: test_result().unwrap(),
|
||||
}),
|
||||
}
|
||||
});
|
||||
|
||||
state.clone()
|
||||
@@ -27,6 +50,9 @@ pub(crate) mod tests {
|
||||
pub async fn static_app_state() -> web::Data<AppState> {
|
||||
static STATE: OnceCell<web::Data<AppState>> = OnceCell::const_new();
|
||||
|
||||
STATE.get_or_init(|| test_app_state()).await.clone()
|
||||
STATE
|
||||
.get_or_init(|| test_app_state(Default::default()))
|
||||
.await
|
||||
.clone()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user