app/src/api/mod.rs

101 lines
3.2 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
2022-02-03 21:55:10 +00:00
#[macro_export]
#[cfg(test)]
2022-02-03 21:55:10 +00:00
macro_rules! call_endpoint {
($req:ident, $state:ident) => {{
// let subscriber = tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt::with(
// tracing_subscriber::registry(),
// tracing_subscriber::Layer::with_filter(
// tracing_subscriber::fmt::Layer::new()
// .pretty()
// .with_writer(std::io::stdout)
// .with_ansi(true),
// tracing_subscriber::filter::LevelFilter::DEBUG,
// ),
// );
// tracing::subscriber::set_global_default(subscriber)
// .expect("Unable to set a global collector");
2022-02-03 21:55:10 +00:00
let a = App::new()
.wrap(tracing_actix_web::TracingLogger::default())
.app_data($state.clone())
.service(crate::api::applications::routes())
.service(crate::api::application_category::routes());
let app = actix_web::test::init_service(a).await;
let resp = actix_web::test::call_service(&app, $req).await;
2022-02-03 21:55:10 +00:00
resp
}};
}
#[cfg(test)]
macro_rules! get_response {
($resp: ident, $type:ty) => {{
let body = test::read_body($resp).await.to_vec();
serde_json::from_slice::<$type>(&body).unwrap()
}};
}
pub mod application_category;
2022-02-03 21:55:10 +00:00
pub mod applications;
mod api_prelude {
pub use super::ListObjects;
2022-02-03 21:55:10 +00:00
pub use crate::entity::prelude::*;
pub use crate::entity::*;
pub use crate::AppState;
pub use actix_web::{delete, get, post, put, web, Error, HttpResponse, Scope};
2022-02-03 21:55:10 +00:00
pub use sea_orm::prelude::*;
pub use sea_orm::{NotSet, Set};
2022-02-03 21:55:10 +00:00
}
#[cfg(test)]
mod test_prelude {
pub use super::ListObjects;
2022-02-03 21:55:10 +00:00
pub use crate::entity::*;
pub use crate::AppState;
pub use crate::error::Result;
2022-02-03 21:55:10 +00:00
pub use actix_web::dev::ServiceResponse;
pub use actix_web::{test, web, App};
use sea_orm::sea_query::TableCreateStatement;
use sea_orm::ConnectionTrait;
use sea_orm::Database;
use sea_orm::DbBackend;
use sea_orm::Schema;
2022-02-03 21:55:10 +00:00
pub use sea_orm::{
entity::prelude::*, entity::*, tests_cfg::*, DatabaseBackend, MockDatabase, MockExecResult,
Transaction,
2022-02-03 21:55:10 +00:00
};
/// Sets up a testing state with an in-memory database and creates the scheme.
pub async fn setup_state() -> Result<actix_web::web::Data<AppState>> {
let db = Database::connect("sqlite::memory:").await?;
let schema = Schema::new(DbBackend::Sqlite);
let stmt: TableCreateStatement = schema.create_table_from_entity(application::Entity);
db.execute(db.get_database_backend().build(&stmt)).await?;
let stmt: TableCreateStatement =
schema.create_table_from_entity(application_category::Entity);
db.execute(db.get_database_backend().build(&stmt)).await?;
Ok(actix_web::web::Data::new(AppState { db }))
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListObjects<T>
where
T: Serialize,
{
items: Vec<T>,
total: usize,
}
impl<T: Serialize> ListObjects<T> {
pub fn new(items: Vec<T>, total: usize) -> Self {
Self { items, total }
}
2022-02-03 21:55:10 +00:00
}