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:
@@ -150,7 +150,7 @@ mod tests {
|
||||
use std::fmt::Write;
|
||||
|
||||
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")
|
||||
.method(Method::POST)
|
||||
|
||||
@@ -255,7 +255,7 @@ mod tests {
|
||||
}
|
||||
|
||||
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")
|
||||
.method(Method::POST)
|
||||
|
||||
@@ -2,9 +2,9 @@ mod cache_status;
|
||||
mod group;
|
||||
mod group_names;
|
||||
mod schedule;
|
||||
mod schema;
|
||||
mod teacher;
|
||||
mod teacher_names;
|
||||
mod schema;
|
||||
mod update_download_url;
|
||||
|
||||
pub use cache_status::*;
|
||||
|
||||
@@ -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 {
|
||||
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