Skip to content

Commit

Permalink
fix(tests): create db if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav-rama committed May 17, 2024
1 parent d1dfbbf commit ca59516
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/bin/tests/sqlite_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
mod tests {

use std::result::Result;
use sqlx::{sqlite::SqliteQueryResult, SqlitePool, };
use sqlx::{sqlite::SqliteQueryResult, SqlitePool, Sqlite,migrate::MigrateDatabase};
use async_std::task;

async fn create_db(db_url:&str) {
if !Sqlite::database_exists(&db_url).await.unwrap_or(false) {
Sqlite::create_database(&db_url).await.unwrap();
match create_schema(&db_url).await {
Ok(_) => println!("Database created Sucessfully"),
Err(e) => panic!("{}",e),
}
}
}

async fn create_schema(db_url:&str) -> Result<SqliteQueryResult, sqlx::Error> {
task::block_on(create_db(db_url));
let pool = SqlitePool::connect(&db_url).await?;
let qry =
"PRAGMA foreign_keys = ON ;
Expand Down Expand Up @@ -33,16 +45,20 @@ mod tests {
return result;
}



#[async_std::test]
async fn test_create_schema() {
let db_url = "sqlite://sqlite.db";
task::block_on(create_db(db_url));
let result = create_schema(db_url).await;
assert!(result.is_ok());
}

#[async_std::test]
async fn test_insert_into_settings() {
let db_url = "sqlite://sqlite.db";
task::block_on(create_db(db_url));
let pool = SqlitePool::connect(db_url).await.unwrap();
let result = create_schema(db_url).await;
assert!(result.is_ok());
Expand Down

0 comments on commit ca59516

Please sign in to comment.