fix(db): finish unit test for db::OAuthDatabase::psql_execute and db::OAuthDatabase::psql_query

This commit is contained in:
subcrip 2024-03-30 15:30:14 +08:00
parent ff3afe2869
commit 935ea5555d
Signed by: subcrip
SSH Key Fingerprint: SHA256:dFPFi68d8C87YkFkEBU4TkcrYRySWpekRR1hbnDWUCw
2 changed files with 23 additions and 10 deletions

View File

@ -2,6 +2,7 @@ use std::time::Duration;
pub struct OAuthDatabase { pub struct OAuthDatabase {
client: tokio_postgres::Client, client: tokio_postgres::Client,
handle: tokio::task::JoinHandle<Result<(), tokio_postgres::Error>>,
} }
impl OAuthDatabase { impl OAuthDatabase {
@ -11,8 +12,8 @@ impl OAuthDatabase {
} }
pub async fn new(client: tokio_postgres::Client, connection: tokio_postgres::Connection<tokio_postgres::Socket, tokio_postgres::tls::NoTlsStream>) -> Result<Self, tokio_postgres::Error> { pub async fn new(client: tokio_postgres::Client, connection: tokio_postgres::Connection<tokio_postgres::Socket, tokio_postgres::tls::NoTlsStream>) -> Result<Self, tokio_postgres::Error> {
let obj = Self { client }; let handle = tokio::spawn(connection);
tokio::spawn(connection); let obj = Self { client, handle };
Ok(obj) Ok(obj)
} }
@ -23,6 +24,10 @@ impl OAuthDatabase {
async fn psql_query<T: ?Sized + tokio_postgres::ToStatement>(&mut self, query: &T, params: &[&(dyn tokio_postgres::types::ToSql + Sync)]) -> Result<Vec<tokio_postgres::Row>, tokio_postgres::Error> { async fn psql_query<T: ?Sized + tokio_postgres::ToStatement>(&mut self, query: &T, params: &[&(dyn tokio_postgres::types::ToSql + Sync)]) -> Result<Vec<tokio_postgres::Row>, tokio_postgres::Error> {
self.client.query(query, params).await self.client.query(query, params).await
} }
pub fn disconnect(&mut self) {
self.handle.abort();
}
} }
#[cfg(test)] #[cfg(test)]
@ -30,8 +35,8 @@ pub mod db_tests {
use super::*; use super::*;
pub struct DBTestEntryType { pub struct DBTestEntryType {
name: String, pub name: String,
age: i32, pub age: i32,
} }
impl DBTestEntryType { impl DBTestEntryType {
@ -55,8 +60,19 @@ pub mod db_tests {
Ok(()) Ok(())
} }
pub async fn read(&mut self) -> Result<Vec<DBTestEntryType>, tokio_postgres::Error> {
// TODO:
let v = self.d.psql_query("select * from rust_test", &[]).await?;
let mut res = Vec::new();
for row in v {
res.push(DBTestEntryType::new(row.get("name"), row.get("age")));
}
Ok(res)
}
pub async fn destroy(&mut self) -> Result<(), tokio_postgres::Error> { pub async fn destroy(&mut self) -> Result<(), tokio_postgres::Error> {
self.d.psql_execute("drop table rust_test", &[]).await?; self.d.psql_execute("drop table rust_test", &[]).await?;
self.d.disconnect();
Ok(()) Ok(())
} }
} }

View File

@ -25,15 +25,12 @@ async fn main() {
} }
#[tokio::test] #[tokio::test]
async fn test_table_level_operations() {
use db::db_tests;
let _b = db_tests::DBTestComponents::new(db::OAuthDatabase::connect("localhost", 5432, "postgres", "configjson", "myoauth").await.unwrap()).await.unwrap();
}
#[tokio::test(flavor = "multi_thread")]
async fn test_entry_level_operations() { async fn test_entry_level_operations() {
use db::db_tests; use db::db_tests;
let mut b = db_tests::DBTestComponents::new(db::OAuthDatabase::connect("localhost", 5432, "postgres", "configjson", "myoauth").await.unwrap()).await.unwrap(); let mut b = db_tests::DBTestComponents::new(db::OAuthDatabase::connect("localhost", 5432, "postgres", "configjson", "myoauth").await.unwrap()).await.unwrap();
assert!(matches!(b.create(db_tests::DBTestEntryType::new("subcrip".to_string(), 19)).await, Ok(()))); assert!(matches!(b.create(db_tests::DBTestEntryType::new("subcrip".to_string(), 19)).await, Ok(())));
let items = b.read().await.unwrap();
assert_eq!(items[0].name, "subcrip");
assert_eq!(items[0].age, 19);
let _ = b.destroy().await; let _ = b.destroy().await;
} }