feat(test): add ability to use test env without schedule

This commit is contained in:
2025-05-25 15:48:10 +04:00
parent fceffb900d
commit 234055eaeb
4 changed files with 39 additions and 13 deletions

View File

@@ -150,7 +150,7 @@ mod tests {
use std::fmt::Write; use std::fmt::Write;
async fn sign_in_client(data: Request) -> ServiceResponse { async fn sign_in_client(data: Request) -> ServiceResponse {
let app = test_app(test_app_state().await, sign_in).await; let app = test_app(test_app_state(Default::default()).await, sign_in).await;
let req = test::TestRequest::with_uri("/sign-in") let req = test::TestRequest::with_uri("/sign-in")
.method(Method::POST) .method(Method::POST)

View File

@@ -255,7 +255,7 @@ mod tests {
} }
async fn sign_up_client(data: SignUpPartial) -> ServiceResponse { async fn sign_up_client(data: SignUpPartial) -> ServiceResponse {
let app = test_app(test_app_state().await, sign_up).await; let app = test_app(test_app_state(Default::default()).await, sign_up).await;
let req = test::TestRequest::with_uri("/sign-up") let req = test::TestRequest::with_uri("/sign-up")
.method(Method::POST) .method(Method::POST)

View File

@@ -2,9 +2,9 @@ mod cache_status;
mod group; mod group;
mod group_names; mod group_names;
mod schedule; mod schedule;
mod schema;
mod teacher; mod teacher;
mod teacher_names; mod teacher_names;
mod schema;
mod update_download_url; mod update_download_url;
pub use cache_status::*; pub use cache_status::*;

View File

@@ -2,23 +2,46 @@
pub(crate) mod tests { pub(crate) mod tests {
use crate::app_state::{AppState, Schedule, app_state}; use crate::app_state::{AppState, Schedule, app_state};
use crate::parser::tests::test_result; use crate::parser::tests::test_result;
use crate::utility::mutex::MutexScope;
use actix_web::web; use actix_web::web;
use std::default::Default;
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
pub fn test_env() { pub fn test_env() {
dotenvy::from_path(".env.test").expect("Failed to load test environment file"); dotenvy::from_path(".env.test").expect("Failed to load test environment file");
} }
pub async fn test_app_state() -> web::Data<AppState> { pub enum TestScheduleType {
let state = app_state().await; None,
let mut schedule_lock = state.schedule.lock().unwrap(); Local,
}
*schedule_lock = Some(Schedule { pub struct TestAppStateParams {
etag: "".to_string(), pub schedule: TestScheduleType,
fetched_at: Default::default(), }
updated_at: Default::default(),
parsed_at: Default::default(), impl Default for TestAppStateParams {
data: test_result().unwrap(), 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() state.clone()
@@ -27,6 +50,9 @@ pub(crate) mod tests {
pub async fn static_app_state() -> web::Data<AppState> { pub async fn static_app_state() -> web::Data<AppState> {
static STATE: OnceCell<web::Data<AppState>> = OnceCell::const_new(); 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()
} }
} }