feat: db pg adapter: define username_exists adapter

This commit is contained in:
Aravinth Manivannan 2024-05-17 23:23:44 +05:30
parent 4e4287c418
commit 2e081e3803
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View file

@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use super::DBOutPostgresAdapter;
use crate::identity::application::port::output::db::{
errors::*, username_exists::UsernameExistsOutDBPort,
};
#[async_trait::async_trait]
impl UsernameExistsOutDBPort for DBOutPostgresAdapter {
async fn username_exists(&self, username: &str) -> OutDBPortResult<bool> {
let res = sqlx::query!(
"SELECT EXISTS (
SELECT 1
FROM user_query
WHERE
username = $1
);",
username
)
.fetch_one(&self.pool)
.await?;
if let Some(x) = res.exists {
Ok(x)
} else {
Ok(false)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[actix_rt::test]
async fn test_postgres_username_exists() {
let username = "foo@exmaple.com";
let settings = crate::settings::tests::get_settings().await;
let db = super::DBOutPostgresAdapter::new(
sqlx::postgres::PgPool::connect(&settings.database.url)
.await
.unwrap(),
);
// state doesn't exist
assert!(!db.username_exists(username).await.unwrap());
sqlx::query!(
"INSERT INTO user_query
(view_id, version, username, email, hashed_password)
VALUES ($1, $2, $3, $4, $5);",
"1",
1,
username,
"foo",
"passwd"
)
.execute(&db.pool)
.await
.unwrap();
// state exists
assert!(db.username_exists(username).await.unwrap());
settings.drop_db().await;
}
}