-
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.
This seems like the right idea so that I don't have multiple pools being cloned. Still learning, not attached to the solution but now I get to satisfy the test
- Loading branch information
1 parent
2a48813
commit b6011e2
Showing
6 changed files
with
65 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use sqlx::SqlitePool; | ||
use std::sync::Arc; | ||
pub struct Connection { | ||
pub sqlite_pool: Arc<SqlitePool>, | ||
} | ||
|
||
impl Connection { | ||
pub fn new(pool: SqlitePool) -> Self { | ||
Self { | ||
sqlite_pool: Arc::new(pool), | ||
} | ||
} | ||
} |
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,33 +1,20 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::models; | ||
use async_trait::async_trait; | ||
use sqlx::SqlitePool; | ||
|
||
use super::connection::Connection; | ||
|
||
#[cfg_attr(test, mockall::automock)] | ||
#[async_trait] | ||
pub trait ContactRepo { | ||
async fn create(&self, contact: models::Contact) -> anyhow::Result<i64>; | ||
async fn get_all(&self) -> anyhow::Result<Vec<models::IndexedContact>>; | ||
async fn update(&self, update: models::ContactBuilder) -> anyhow::Result<()>; | ||
async fn get_by_id(&self, id: i64) -> anyhow::Result<models::IndexedContact>; | ||
} | ||
|
||
pub struct Connection { | ||
sqlite_pool: Arc<SqlitePool>, | ||
} | ||
|
||
impl Connection { | ||
pub fn new(pool: SqlitePool) -> Self { | ||
Self { | ||
sqlite_pool: Arc::new(pool), | ||
} | ||
} | ||
async fn create_contact(&self, contact: models::Contact) -> anyhow::Result<i64>; | ||
async fn get_all_contacts(&self) -> anyhow::Result<Vec<models::IndexedContact>>; | ||
async fn update_contact(&self, update: models::ContactBuilder) -> anyhow::Result<()>; | ||
async fn get_contact_by_id(&self, id: i64) -> anyhow::Result<models::IndexedContact>; | ||
} | ||
|
||
#[async_trait] | ||
impl ContactRepo for Connection { | ||
async fn create(&self, contact: models::Contact) -> anyhow::Result<i64> { | ||
async fn create_contact(&self, contact: models::Contact) -> anyhow::Result<i64> { | ||
let query = "INSERT INTO contacts | ||
(first_name, last_name, display_name, email, phone_number) | ||
VALUES (?, ?, ?, ?, ?)"; | ||
|
@@ -43,7 +30,7 @@ impl ContactRepo for Connection { | |
Ok(result.last_insert_rowid()) | ||
} | ||
|
||
async fn get_all(&self) -> anyhow::Result<Vec<models::IndexedContact>> { | ||
async fn get_all_contacts(&self) -> anyhow::Result<Vec<models::IndexedContact>> { | ||
let get_contacts_query = | ||
"SELECT id, first_name, last_name, display_name, email, phone_number | ||
FROM contacts | ||
|
@@ -57,7 +44,7 @@ impl ContactRepo for Connection { | |
Ok(contacts_with_id) | ||
} | ||
|
||
async fn update(&self, contact: models::ContactBuilder) -> anyhow::Result<()> { | ||
async fn update_contact(&self, contact: models::ContactBuilder) -> anyhow::Result<()> { | ||
sqlx::query!( | ||
r#" | ||
UPDATE contacts | ||
|
@@ -84,7 +71,7 @@ impl ContactRepo for Connection { | |
Ok(()) | ||
} | ||
|
||
async fn get_by_id(&self, id: i64) -> anyhow::Result<models::IndexedContact> { | ||
async fn get_contact_by_id(&self, id: i64) -> anyhow::Result<models::IndexedContact> { | ||
let query_get_by_id = "SELECT * FROM contacts WHERE id=$1"; | ||
|
||
let contact: models::IndexedContact = | ||
|
@@ -110,12 +97,12 @@ mod tests { | |
models::Contact::new("John", "Smith", "[email protected]", "123-456-7890").unwrap(); | ||
|
||
mock_contact_repo | ||
.expect_create() | ||
.expect_create_contact() | ||
.times(1) | ||
.with(eq(test_contact.clone())) | ||
.returning(|_| Ok(1)); | ||
|
||
let result = mock_contact_repo.create(test_contact).await; | ||
let result = mock_contact_repo.create_contact(test_contact).await; | ||
|
||
let result = result.unwrap(); | ||
|
||
|
@@ -133,11 +120,11 @@ mod tests { | |
}]; | ||
|
||
mock_contact_repo | ||
.expect_get_all() | ||
.expect_get_all_contacts() | ||
.times(1) | ||
.return_once(move || Ok(contacts)); | ||
|
||
let result = mock_contact_repo.get_all().await; | ||
let result = mock_contact_repo.get_all_contacts().await; | ||
|
||
assert!(result.is_ok()); | ||
} | ||
|
@@ -147,7 +134,7 @@ mod tests { | |
let mut mock_contact_repo = MockContactRepo::new(); | ||
|
||
mock_contact_repo | ||
.expect_update() | ||
.expect_update_contact() | ||
.times(1) | ||
.return_once(|_| Ok(())); | ||
|
||
|
@@ -161,7 +148,7 @@ mod tests { | |
) | ||
.unwrap(); | ||
|
||
let result = mock_contact_repo.update(edits).await; | ||
let result = mock_contact_repo.update_contact(edits).await; | ||
|
||
assert!(result.is_ok()); | ||
} | ||
|
@@ -177,12 +164,12 @@ mod tests { | |
}; | ||
|
||
mock_contact_repo | ||
.expect_get_by_id() | ||
.expect_get_contact_by_id() | ||
.times(1) | ||
.with(eq(contact.id)) | ||
.return_once(|_| Ok(contact)); | ||
|
||
let result = mock_contact_repo.get_by_id(1).await; | ||
let result = mock_contact_repo.get_contact_by_id(1).await; | ||
|
||
assert!(result.is_ok()); | ||
|
||
|
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,10 +1,10 @@ | ||
mod connection; | ||
mod contact_repo; | ||
mod metadata_repo; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub use contact_repo::Connection as ContactConnection; | ||
pub use contact_repo::ContactRepo; | ||
pub use connection::Connection; | ||
|
||
pub use metadata_repo::Connection as MetadataConnection; | ||
pub use contact_repo::ContactRepo; | ||
pub use metadata_repo::MetadataRepo; |
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,7 +1,7 @@ | ||
use sqlx::{sqlite::SqlitePoolOptions, SqlitePool}; | ||
|
||
use crate::{ | ||
db::{ContactConnection, ContactRepo, MetadataConnection, MetadataRepo}, | ||
db::{Connection, ContactRepo, MetadataRepo}, | ||
models::{Contact, Metadata}, | ||
}; | ||
|
||
|
@@ -50,20 +50,19 @@ async fn setup_test_db() -> SqlitePool { | |
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 data_repo = Connection::new(pool); | ||
|
||
let example_contact = | ||
Contact::new("Lewis", "Carroll", "[email protected]", "777-777-7777").unwrap(); | ||
|
||
let result_contact_id = contact_repo.create(example_contact).await; | ||
let result_contact_id = data_repo.create_contact(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 result_expected_metadata = data_repo.get_metadata_by_id(contact_id).await; | ||
let expected_metadata = result_expected_metadata.unwrap(); | ||
|
||
assert_eq!(default_metadata, expected_metadata); | ||
|
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