-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP test: contact and metadata db integration test
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
1 parent
9bfee54
commit 2a48813
Showing
3 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |