rudeshark.net/packages/backend/native-utils/crates/model/tests/common.rs

100 lines
3.1 KiB
Rust
Raw Normal View History

2023-05-27 11:50:07 +02:00
extern crate model;
mod repository;
use chrono::Utc;
use model::entity::{antenna, sea_orm_active_enums::AntennaSrcEnum, user};
use sea_orm::{
ActiveModelTrait, ActiveValue::Set, DatabaseConnection, DbErr, EntityTrait, TransactionTrait,
};
use std::env;
use util::{
id::{create_id, init_id},
random::gen_string,
};
/// Insert predefined entries in the database.
async fn prepare() {
let conn_uri = env::var("DATABASE_URL")
.unwrap_or("postgres://calckey:calckey@localhost/calckey".to_string());
database::init_database(conn_uri)
.await
.expect("Unable to initialize database connection");
let db = database::get_database().expect("Unable to get database connection from pool");
setup_model(db).await;
}
/// Delete all entries in the database.
async fn cleanup() {
let db = database::get_database().unwrap();
db.transaction::<_, (), DbErr>(|txn| {
Box::pin(async move {
user::Entity::delete_many().exec(txn).await.unwrap();
antenna::Entity::delete_many().exec(txn).await.unwrap();
Ok(())
})
})
.await
.expect("Unable to delete predefined models");
}
async fn setup_model(db: &DatabaseConnection) {
init_id(12);
db.transaction::<_, (), DbErr>(|txn| {
Box::pin(async move {
let user_id = create_id().unwrap();
let name = "Alice";
let user_model = user::ActiveModel {
id: Set(user_id.to_owned()),
created_at: Set(Utc::now().into()),
username: Set(name.to_lowercase().to_string()),
username_lower: Set(name.to_lowercase().to_string()),
name: Set(Some(name.to_string())),
token: Set(Some(gen_string(16))),
is_admin: Set(true),
..Default::default()
};
user_model.insert(txn).await?;
let antenna_model = antenna::ActiveModel {
id: Set(create_id().unwrap()),
created_at: Set(Utc::now().into()),
user_id: Set(user_id.to_owned()),
name: Set("Test Antenna".to_string()),
src: Set(AntennaSrcEnum::All),
2023-05-31 18:09:30 +02:00
keywords: Set(vec![
vec!["foo".to_string(), "bar".to_string()],
vec!["foobar".to_string()],
]
.into()),
exclude_keywords: Set(vec![
vec!["abc".to_string()],
vec!["def".to_string(), "ghi".to_string()],
]
.into()),
2023-05-27 11:50:07 +02:00
with_file: Set(false),
notify: Set(true),
case_sensitive: Set(true),
with_replies: Set(false),
..Default::default()
};
antenna_model.insert(txn).await?;
Ok(())
})
})
.await
.expect("Unable to setup predefined models");
}
2023-05-31 18:24:02 +02:00
mod int_test {
2023-05-27 12:52:15 +02:00
use super::{cleanup, prepare};
#[tokio::test]
async fn can_prepare_and_cleanup() {
prepare().await;
cleanup().await;
}
2023-05-27 11:50:07 +02:00
}