Skip to content

Commit

Permalink
Add tests for sqlite_demo.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav-rama committed May 15, 2024
1 parent 781e052 commit d1dfbbf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bin/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod image_download_test;
mod k256_message_sign_test;
mod read_json_test;
mod secp256r1_test;
mod sqlite_test;
mod traits_in_action_test;
mod write_json_tests;
mod zk_fibo_verify_test;
64 changes: 64 additions & 0 deletions src/bin/tests/sqlite_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#[cfg(test)]
mod tests {

use std::result::Result;
use sqlx::{sqlite::SqliteQueryResult, SqlitePool, };

async fn create_schema(db_url:&str) -> Result<SqliteQueryResult, sqlx::Error> {
let pool = SqlitePool::connect(&db_url).await?;
let qry =
"PRAGMA foreign_keys = ON ;
CREATE TABLE IF NOT EXISTS settings
(
settings_id INTEGER PRIMARY KEY NOT NULL,
description TEXT NOT NULL,
created_on DATETIME DEFAULT (datetime('now','localtime')),
updated_on DATETIME DEFAULT (datetime('now','localtime')),
done BOOLEAN NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS project
(
project_id INTEGER PRIMARY KEY AUTOINCREMENT,
product_name TEXT ,
created_on DATETIME DEFAULT (datetime('now','localtime')),
updated_on DATETIME DEFAULT (datetime('now','localtime')),
img_directory TEXT NOT NULL,
out_directory TEXT NOT NULL,
status TEXT NOT NULL,
settings_id INTEGER NOT NULL DEFAULT 1,
FOREIGN KEY (settings_id) REFERENCES settings (settings_id) ON UPDATE SET NULL ON DELETE SET NULL
);";
let result = sqlx::query(&qry).execute(&pool).await;
pool.close().await;
return result;
}

#[async_std::test]
async fn test_create_schema() {
let db_url = "sqlite://sqlite.db";
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";
let pool = SqlitePool::connect(db_url).await.unwrap();
let result = create_schema(db_url).await;
assert!(result.is_ok());

let qry = "INSERT INTO settings (description) VALUES($1)";
let result = sqlx::query(&qry).bind("testing").execute(&pool).await;
match result {
Ok(_) => println!("Inserted into settings"),
Err(e) => panic!("{}",e),
}
assert!(result.is_ok());

let row: (String,) = sqlx::query_as("SELECT description FROM settings")
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(row.0, "testing");
}
}

0 comments on commit d1dfbbf

Please sign in to comment.