fix tests

This commit is contained in:
Felix Ableitner 2023-03-27 22:14:47 +02:00
parent a9f8e4489b
commit cbe41b17ba
3 changed files with 47 additions and 42 deletions

View file

@ -25,7 +25,11 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use diesel_async::{
pooled_connection::deadpool::Object as PooledConnection,
AsyncPgConnection,
RunQueryDsl,
};
use lemmy_utils::error::LemmyError;
use tokio::sync::OnceCell;
@ -115,15 +119,22 @@ impl SiteLanguage {
.await
}
pub async fn read(pool: &DbPool, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
let conn = &mut get_conn(pool).await?;
let langs = site_language::table
async fn read_internal(
conn: &mut PooledConnection<AsyncPgConnection>,
for_site_id: SiteId,
) -> Result<Vec<LanguageId>, Error> {
site_language::table
.filter(site_language::site_id.eq(for_site_id))
.order(site_language::language_id)
.select(site_language::language_id)
.load(conn)
.await?;
.await
}
pub async fn read(pool: &DbPool, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
let conn = &mut get_conn(pool).await?;
let langs = Self::read_internal(conn, for_site_id).await?;
convert_read_languages(conn, langs).await
}
@ -233,19 +244,25 @@ impl CommunityLanguage {
Ok(())
}
pub async fn read(
pool: &DbPool,
async fn read_internal(
conn: &mut PooledConnection<AsyncPgConnection>,
for_community_id: CommunityId,
) -> Result<Vec<LanguageId>, Error> {
use crate::schema::community_language::dsl::{community_id, community_language, language_id};
let conn = &mut get_conn(pool).await?;
let langs = community_language
community_language
.filter(community_id.eq(for_community_id))
.order(language_id)
.select(language_id)
.get_results(conn)
.await?;
.await
}
pub async fn read(
pool: &DbPool,
for_community_id: CommunityId,
) -> Result<Vec<LanguageId>, Error> {
let conn = &mut get_conn(pool).await?;
let langs = Self::read_internal(conn, for_community_id).await?;
convert_read_languages(conn, langs).await
}
@ -258,10 +275,11 @@ impl CommunityLanguage {
if language_ids.is_empty() {
language_ids = SiteLanguage::read_local(pool).await?;
}
let lang_ids = convert_update_languages(conn, language_ids).await?;
// No need to update if languages are unchanged
let current = CommunityLanguage::read(pool, for_community_id).await?;
if current == language_ids {
let current = CommunityLanguage::read_internal(conn, for_community_id).await?;
if current == lang_ids {
return Ok(());
}
@ -275,7 +293,7 @@ impl CommunityLanguage {
.execute(conn)
.await?;
for l in language_ids {
for l in lang_ids {
let form = CommunityLanguageForm {
community_id: for_community_id,
language_id: l,

View file

@ -2,7 +2,7 @@ use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
schema::community::dsl::{actor_id, community, deleted, local, name, removed},
source::{
actor_language::{CommunityLanguage, SiteLanguage},
actor_language::CommunityLanguage,
community::{
Community,
CommunityFollower,
@ -21,7 +21,6 @@ use crate::{
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
use diesel_async::RunQueryDsl;
use futures::future::OptionFuture;
#[async_trait]
impl Crud for Community {
@ -42,13 +41,11 @@ impl Crud for Community {
async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let existing = OptionFuture::from(
form
.actor_id
.as_ref()
.map(|id| Community::read_from_apub_id(pool, id)),
)
.await;
let is_new_community = match &form.actor_id {
Some(id) => Community::read_from_apub_id(pool, id).await?.is_none(),
None => true,
};
// Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
let community_ = insert_into(community)
.values(form)
@ -59,15 +56,8 @@ impl Crud for Community {
.await?;
// Initialize languages for new community
if existing.is_none() {
let site_languages = SiteLanguage::read_local(pool).await;
if let Ok(langs) = site_languages {
// if site exists, init community with site languages
CommunityLanguage::update(pool, langs, community_.id).await?;
} else {
// otherwise, init with all languages (this only happens during tests)
CommunityLanguage::update(pool, vec![], community_.id).await?;
}
if is_new_community {
CommunityLanguage::update(pool, vec![], community_.id).await?;
}
Ok(community_)

View file

@ -10,7 +10,6 @@ use crate::{
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use futures::future::OptionFuture;
use url::Url;
#[async_trait]
@ -26,13 +25,11 @@ impl Crud for Site {
async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let existing = OptionFuture::from(
form
.actor_id
.as_ref()
.map(|id_| Site::read_from_apub_id(pool, id_)),
)
.await;
let is_new_site = match &form.actor_id {
Some(id_) => Site::read_from_apub_id(pool, id_).await?.is_none(),
None => true,
};
// Can't do separate insert/update commands because InsertForm/UpdateForm aren't convertible
let site_ = insert_into(site)
.values(form)
@ -43,7 +40,7 @@ impl Crud for Site {
.await?;
// initialize languages if site is newly created
if existing.is_none() {
if !is_new_site {
// initialize with all languages
SiteLanguage::update(pool, vec![], &site_).await?;
}