Skip to content

Commit

Permalink
WIP test: contact and metadata db integration test
Browse files Browse the repository at this point in the history
before adding this functionality to the main function, I have to make sure it works in a test env

I already found a bug, plus, it looks like I'll need to do some Rust magic because I can't have 2 in-memory connections at the same time.
I wanted to do this anyway; as I believe it'll be more efficient.
  • Loading branch information
jasonribble committed Sep 29, 2024
1 parent 9bfee54 commit 2a48813
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/db/metadata_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl MetadataRepo for Connection {
Ok(result.last_insert_rowid())
}
async fn get_by_id(&self, contact_id: i64) -> anyhow::Result<models::Metadata> {
let query_get_by_id = "SELECT * FROM metadata WHERE contact_id=$1";
let query_get_by_id = "SELECT * FROM contact_metadata WHERE contact_id=$1";

let metadata: models::Metadata = sqlx::query_as::<_, models::Metadata>(query_get_by_id)
.bind(contact_id)
Expand Down
6 changes: 4 additions & 2 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod contact_repo;
mod metadata_repo;
#[cfg(test)]
mod tests;

pub use contact_repo::ContactRepo;
pub use contact_repo::Connection as ContactConnection;
pub use contact_repo::ContactRepo;

pub use metadata_repo::Connection as MetadataConnection;
pub use metadata_repo::MetadataRepo;
pub use metadata_repo::Connection as MetadataConnection;
70 changes: 70 additions & 0 deletions src/db/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use sqlx::{sqlite::SqlitePoolOptions, SqlitePool};

use crate::{
db::{ContactConnection, ContactRepo, MetadataConnection, MetadataRepo},
models::{Contact, Metadata},
};

async fn setup_test_db() -> SqlitePool {
let pool = SqlitePoolOptions::new()
.connect("sqlite::memory:")
.await
.expect("Failed to create in-memory SQLite database");

sqlx::query(
"CREATE TABLE IF NOT EXISTS contact_metadata (
contact_id INTEGER NOT NULL,
starred BOOLEAN NOT NULL,
is_archived BOOLEAN NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
last_seen_at TEXT,
next_reminder_at TEXT,
frequency INTEGER,
last_reminder_at TEXT
)",
)
.execute(&pool)
.await
.expect("Failed to create contact_metadata table");

sqlx::query(
"CREATE TABLE IF NOT EXISTS contacts
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
display_name TEXT NOT NULL,
email TEXT NOT NULL,
phone_number TEXT NOT NULL
);",
)
.execute(&pool)
.await
.expect("Failed to create contact table");

pool
}

#[tokio::test]
async fn test_create_contact_get_metadata() {
let pool = setup_test_db().await;

let contact_repo = ContactConnection::new(pool.clone());
let metadata_repo = MetadataConnection::new(pool.clone());

let example_contact =
Contact::new("Lewis", "Carroll", "[email protected]", "777-777-7777").unwrap();

let result_contact_id = contact_repo.create(example_contact).await;
let contact_id = result_contact_id.unwrap();

assert_eq!(contact_id, 1);

let default_metadata = Metadata::default();

let result_expected_metadata = metadata_repo.get_by_id(contact_id).await;
let expected_metadata = result_expected_metadata.unwrap();

assert_eq!(default_metadata, expected_metadata);
}

0 comments on commit 2a48813

Please sign in to comment.