fix(db): unit test
This commit is contained in:
parent
8c8ce903db
commit
fae85e71d7
28
src/db.rs
28
src/db.rs
|
@ -41,41 +41,23 @@ pub mod db_tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DBTestComponents {
|
pub struct DBTestComponents {
|
||||||
d: Option<OAuthDatabase>,
|
d: OAuthDatabase,
|
||||||
dropped: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DBTestComponents {
|
impl DBTestComponents {
|
||||||
fn default() -> Self {
|
|
||||||
Self { d: None, dropped: false }
|
|
||||||
}
|
|
||||||
pub async fn new(mut d: OAuthDatabase) -> Result<Self, tokio_postgres::Error> {
|
pub async fn new(mut d: OAuthDatabase) -> Result<Self, tokio_postgres::Error> {
|
||||||
d.psql_execute("create table rust_test(name text, age int)", &[]).await?;
|
d.psql_execute("create table rust_test(name text, age int)", &[]).await?;
|
||||||
Ok(Self { d: Some(d), dropped: false })
|
Ok(Self { d })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(&mut self, item: DBTestEntryType) -> Result<(), tokio_postgres::Error> {
|
pub async fn create(&mut self, item: DBTestEntryType) -> Result<(), tokio_postgres::Error> {
|
||||||
self.d.as_mut().unwrap().psql_execute("insert into rust_test (name, age) values ($1, $2)", &[&item.name, &item.age]).await?;
|
self.d.psql_execute("insert into rust_test (name, age) values ($1, $2)", &[&item.name, &item.age]).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn destroy(&mut self) -> Result<(), tokio_postgres::Error> {
|
pub async fn destroy(&mut self) -> Result<(), tokio_postgres::Error> {
|
||||||
eprintln!("destroy() called.");
|
self.d.psql_execute("drop table rust_test", &[]).await?;
|
||||||
self.d.as_mut().unwrap().psql_execute("drop table rust_test", &[]).await?;
|
|
||||||
eprintln!("destroy() finished.");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for DBTestComponents {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
eprintln!("drop() called.");
|
|
||||||
if !self.dropped {
|
|
||||||
let mut this = DBTestComponents::default();
|
|
||||||
std::mem::swap(&mut this, self);
|
|
||||||
this.dropped = true;
|
|
||||||
tokio::spawn(async move {this.destroy().await});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,9 +33,7 @@ async fn main() {
|
||||||
#[tokio::test(flavor = "multi_thread")]
|
#[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;
|
||||||
eprintln!("1");
|
|
||||||
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();
|
||||||
eprintln!("2");
|
|
||||||
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(())));
|
||||||
eprintln!("3");
|
let _ = b.destroy().await;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue