iceshrimp-legacy/packages/backend/native-utils/tests/common.rs

217 lines
5.9 KiB
Rust
Raw Normal View History

2023-06-02 12:22:09 +02:00
#![cfg(not(feature = "napi"))]
2023-06-02 14:48:12 +02:00
mod model;
2023-05-27 11:50:07 +02:00
use chrono::Utc;
2023-06-02 14:48:12 +02:00
use native_utils::database;
use native_utils::model::entity;
use native_utils::model::entity::sea_orm_active_enums::AntennaSrcEnum;
use native_utils::util::{
id::{create_id, init_id},
random::gen_string,
};
2023-05-27 11:50:07 +02:00
use sea_orm::{
2023-06-02 09:39:52 +02:00
sea_query::TableCreateStatement, ActiveModelTrait, ConnectionTrait, DbBackend, DbConn, DbErr,
EntityTrait, IntoActiveModel, TransactionTrait,
2023-05-27 11:50:07 +02:00
};
/// Insert predefined entries in the database.
async fn prepare() {
2023-06-02 09:39:52 +02:00
database::init_database("sqlite::memory:")
2023-05-27 11:50:07 +02:00
.await
.expect("Unable to initialize database connection");
let db = database::get_database().expect("Unable to get database connection from pool");
2023-06-02 09:39:52 +02:00
setup_schema(db).await;
2023-05-27 11:50:07 +02:00
setup_model(db).await;
}
2023-06-02 09:39:52 +02:00
/// Setup schemas in the database.
async fn setup_schema(db: &DbConn) {
2023-06-01 00:12:59 +02:00
let schema = sea_orm::Schema::new(DbBackend::Sqlite);
2023-06-02 09:39:52 +02:00
let mut stmts: Vec<TableCreateStatement> = Vec::new();
macro_rules! create_table_statement {
($a:tt) => {
stmts.push(schema.create_table_from_entity(entity::$a::Entity).if_not_exists().to_owned());
};
($a:tt, $($b:tt),+) => {
create_table_statement!($a);
create_table_statement!($($b),+);
};
}
create_table_statement!(
abuse_user_report,
access_token,
ad,
announcement_read,
announcement,
antenna_note,
antenna,
app,
attestation_challenge,
auth_session,
blocking,
channel_following,
channel_note_pining,
channel,
clip_note,
clip,
drive_file,
drive_folder,
emoji,
following,
follow_request,
gallery_like,
gallery_post,
hashtag,
instance,
messaging_message,
meta,
migrations,
moderation_log,
muted_note,
muting,
note_edit,
note_favorite,
note_reaction,
note,
note_thread_muting,
note_unread,
note_watching,
notification,
page_like,
page,
password_reset_request,
poll,
poll_vote,
promo_note,
promo_read,
registration_ticket,
registry_item,
relay,
renote_muting,
signin,
sw_subscription,
used_username,
user_group_invitation,
user_group_invite,
user_group_joining,
user_group,
user_ip,
user_keypair,
user_list_joining,
user_list,
user_note_pining,
user_pending,
user_profile,
user_publickey,
user,
user_security_key,
webhook
);
db.transaction::<_, (), DbErr>(|txn| {
Box::pin(async move {
for stmt in stmts {
txn.execute(txn.get_database_backend().build(&stmt)).await?;
}
Ok(())
})
})
.await
.expect("Unable to setup schemas");
2023-06-01 00:12:59 +02:00
}
2023-05-27 11:50:07 +02:00
/// Delete all entries in the database.
async fn cleanup() {
2023-06-02 09:39:52 +02:00
let db = database::get_database().expect("Unable to get database connection from pool");
2023-05-27 11:50:07 +02:00
db.transaction::<_, (), DbErr>(|txn| {
Box::pin(async move {
2023-06-02 09:39:52 +02:00
entity::user::Entity::delete_many().exec(txn).await.unwrap();
entity::antenna::Entity::delete_many()
.exec(txn)
.await
.unwrap();
2023-05-27 11:50:07 +02:00
Ok(())
})
})
.await
.expect("Unable to delete predefined models");
}
2023-06-01 00:12:59 +02:00
async fn setup_model(db: &DbConn) {
init_id(16, "");
2023-05-27 11:50:07 +02:00
db.transaction::<_, (), DbErr>(|txn| {
Box::pin(async move {
let user_id = create_id(0).unwrap();
2023-05-27 11:50:07 +02:00
let name = "Alice";
2023-06-02 09:39:52 +02:00
let user_model = entity::user::Model {
id: user_id.to_owned(),
created_at: Utc::now().into(),
2023-07-17 00:32:32 +02:00
username: name.to_lowercase(),
username_lower: name.to_lowercase(),
2023-06-02 09:39:52 +02:00
name: Some(name.to_string()),
token: Some(gen_string(16)),
is_admin: true,
2023-05-27 11:50:07 +02:00
..Default::default()
};
2023-06-02 09:39:52 +02:00
user_model
.into_active_model()
.reset_all()
.insert(txn)
.await?;
let antenna_model = entity::antenna::Model {
id: create_id(0).unwrap(),
2023-06-02 09:39:52 +02:00
created_at: Utc::now().into(),
user_id: user_id.to_owned(),
2023-06-03 05:29:48 +02:00
name: "Alice Antenna".to_string(),
2023-06-02 09:39:52 +02:00
src: AntennaSrcEnum::All,
keywords: vec![
2023-05-31 18:09:30 +02:00
vec!["foo".to_string(), "bar".to_string()],
vec!["foobar".to_string()],
]
2023-06-02 09:39:52 +02:00
.into(),
exclude_keywords: vec![
2023-05-31 18:09:30 +02:00
vec!["abc".to_string()],
vec!["def".to_string(), "ghi".to_string()],
]
2023-06-02 09:39:52 +02:00
.into(),
notify: true,
case_sensitive: true,
2023-05-27 11:50:07 +02:00
..Default::default()
};
2023-06-02 09:39:52 +02:00
antenna_model
.into_active_model()
.reset_all()
.insert(txn)
.await?;
2023-06-03 05:29:48 +02:00
let note_model = entity::note::Model {
id: create_id(0).unwrap(),
2023-06-03 05:29:48 +02:00
created_at: Utc::now().into(),
text: Some("Testing 123".to_string()),
user_id: user_id.to_owned(),
..Default::default()
};
note_model
.into_active_model()
.reset_all()
.insert(txn)
.await?;
2023-05-27 11:50:07 +02:00
Ok(())
})
})
.await
.expect("Unable to setup predefined models");
}
2023-05-31 18:24:02 +02:00
mod int_test {
2023-06-02 09:39:52 +02:00
use super::{cleanup, prepare};
2023-05-27 12:52:15 +02:00
#[tokio::test]
async fn can_prepare_and_cleanup() {
prepare().await;
cleanup().await;
}
2023-05-27 11:50:07 +02:00
}