Skip to content

Commit

Permalink
example test simplification through submission
Browse files Browse the repository at this point in the history
  • Loading branch information
Nothing4You committed Oct 17, 2024
1 parent 859dfb3 commit 3ed0879
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
30 changes: 11 additions & 19 deletions crates/api/src/site/registration_applications/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use lemmy_db_schema::{
local_site::{LocalSite, LocalSiteInsertForm},
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitInsertForm},
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
person::{Person, PersonInsertForm},
person::PersonInsertForm,
registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
site::{Site, SiteInsertForm},
},
Expand All @@ -39,11 +39,9 @@ async fn create_test_site(context: &Data<LemmyContext>) -> LemmyResult<(Instance

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;

let admin_person = Person::create(
pool,
&PersonInsertForm::test_form(inserted_instance.id, "admin"),
)
.await?;
let admin_person = PersonInsertForm::simple_test_form(inserted_instance.id, None)
.submit(pool)
.await?;
LocalUser::create(
pool,
&LocalUserInsertForm::test_form_admin(admin_person.id),
Expand Down Expand Up @@ -78,11 +76,11 @@ async fn create_test_site(context: &Data<LemmyContext>) -> LemmyResult<(Instance
async fn signup(
pool: &mut DbPool<'_>,
instance_id: InstanceId,
name: &str,
email: Option<&str>,
) -> LemmyResult<(LocalUser, RegistrationApplication)> {
let person_insert_form = PersonInsertForm::test_form(instance_id, name);
let person = Person::create(pool, &person_insert_form).await?;
let person = PersonInsertForm::simple_test_form(instance_id, None)
.submit(pool)
.await?;

let local_user_insert_form = match email {
Some(email) => LocalUserInsertForm {
Expand Down Expand Up @@ -146,7 +144,7 @@ async fn test_application_approval() -> LemmyResult<()> {
let mut expected_unread_applications = 0u8;

let (local_user_with_email, app_with_email) =
signup(pool, instance.id, "user_w_email", Some("lemmy@localhost")).await?;
signup(pool, instance.id, Some("lemmy@localhost")).await?;

let (application_count, unread_applications, all_applications) =
get_application_statuses(&context, admin_local_user_view.clone()).await?;
Expand Down Expand Up @@ -239,13 +237,7 @@ async fn test_application_approval() -> LemmyResult<()> {
.accepted_application
);

let (_local_user, app_with_email_2) = signup(
pool,
instance.id,
"user_w_email_2",
Some("lemmy2@localhost"),
)
.await?;
let (_local_user, app_with_email_2) = signup(pool, instance.id, Some("lemmy2@localhost")).await?;
let (application_count, unread_applications, all_applications) =
get_application_statuses(&context, admin_local_user_view.clone()).await?;

Expand Down Expand Up @@ -326,7 +318,7 @@ async fn test_application_approval() -> LemmyResult<()> {
expected_total_applications,
);

signup(pool, instance.id, "user_wo_email", None).await?;
signup(pool, instance.id, None).await?;

expected_total_applications += 1;
expected_unread_applications += 1;
Expand All @@ -348,7 +340,7 @@ async fn test_application_approval() -> LemmyResult<()> {
expected_total_applications,
);

signup(pool, instance.id, "user_w_email_3", None).await?;
signup(pool, instance.id, None).await?;

expected_total_applications += 1;
expected_unread_applications += 1;
Expand Down
13 changes: 6 additions & 7 deletions crates/db_schema/src/impls/moderator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,12 @@ mod tests {

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;

let new_mod = PersonInsertForm::test_form(inserted_instance.id, "the mod");

let inserted_mod = Person::create(pool, &new_mod).await?;

let new_person = PersonInsertForm::test_form(inserted_instance.id, "jim2");

let inserted_person = Person::create(pool, &new_person).await?;
let inserted_mod = PersonInsertForm::simple_test_form(inserted_instance.id, None)
.submit(pool)
.await?;
let inserted_person = PersonInsertForm::simple_test_form(inserted_instance.id, None)
.submit(pool)
.await?;

let new_community = CommunityInsertForm::new(
inserted_instance.id,
Expand Down
25 changes: 18 additions & 7 deletions crates/db_schema/src/impls/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ impl PersonInsertForm {
pub fn test_form(instance_id: InstanceId, name: &str) -> Self {
Self::new(name.to_owned(), "pubkey".to_string(), instance_id)
}

pub fn simple_test_form(instance_id: InstanceId, name: Option<&str>) -> Self {
let name = name.map_or_else(|| uuid::Uuid::new_v4().to_string(), ToString::to_string);
Self::new(name.to_string(), "pubkey".to_string(), instance_id)
}

pub async fn submit(&self, pool: &mut DbPool<'_>) -> Result<Person, Error> {
Person::create(pool, self).await
}
}

#[async_trait]
Expand Down Expand Up @@ -257,9 +266,9 @@ mod tests {

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;

let new_person = PersonInsertForm::test_form(inserted_instance.id, "holly");

let inserted_person = Person::create(pool, &new_person).await?;
let inserted_person = PersonInsertForm::simple_test_form(inserted_instance.id, Some("holly"))
.submit(pool)
.await?;

let expected_person = Person {
id: inserted_person.id,
Expand Down Expand Up @@ -311,10 +320,12 @@ mod tests {
let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;

let person_form_1 = PersonInsertForm::test_form(inserted_instance.id, "erich");
let person_1 = Person::create(pool, &person_form_1).await?;
let person_form_2 = PersonInsertForm::test_form(inserted_instance.id, "michele");
let person_2 = Person::create(pool, &person_form_2).await?;
let person_1 = PersonInsertForm::simple_test_form(inserted_instance.id, None)
.submit(pool)
.await?;
let person_2 = PersonInsertForm::simple_test_form(inserted_instance.id, None)
.submit(pool)
.await?;

let follow_form = PersonFollowerForm {
person_id: person_1.id,
Expand Down
16 changes: 7 additions & 9 deletions crates/db_views/src/vote_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ impl VoteView {

#[cfg(test)]
mod tests {

use crate::structs::VoteView;
use lemmy_db_schema::{
source::{
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
community::{Community, CommunityInsertForm, CommunityPersonBan, CommunityPersonBanForm},
instance::Instance,
person::{Person, PersonInsertForm},
person::PersonInsertForm,
post::{Post, PostInsertForm, PostLike, PostLikeForm},
},
traits::{Bannable, Crud, Likeable},
Expand All @@ -109,13 +108,12 @@ mod tests {

let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;

let new_person = PersonInsertForm::test_form(inserted_instance.id, "timmy_vv");

let inserted_timmy = Person::create(pool, &new_person).await?;

let new_person_2 = PersonInsertForm::test_form(inserted_instance.id, "sara_vv");

let inserted_sara = Person::create(pool, &new_person_2).await?;
let inserted_timmy = PersonInsertForm::simple_test_form(inserted_instance.id, None)
.submit(pool)
.await?;
let inserted_sara = PersonInsertForm::simple_test_form(inserted_instance.id, None)
.submit(pool)
.await?;

let new_community = CommunityInsertForm::new(
inserted_instance.id,
Expand Down

0 comments on commit 3ed0879

Please sign in to comment.