-
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.
I found this https://github.com/thebracket/webinar_axumcrud and I'm very happy about it
- Loading branch information
1 parent
55aebb3
commit 031463b
Showing
2 changed files
with
42 additions
and
0 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ pub trait ContactRepo { | |
async fn save_contact(&self, contact: models::Contact) -> anyhow::Result<i64>; | ||
async fn get_all(&self) -> anyhow::Result<Vec<models::IndexedContact>>; | ||
async fn update_contact(&self, update: models::ContactBuilder) -> anyhow::Result<()>; | ||
async fn get_by_id(&self, id: i64) -> anyhow::Result<models::IndexedContact>; | ||
} | ||
|
||
pub struct PostgresContactRepo { | ||
|
@@ -82,6 +83,18 @@ impl ContactRepo for PostgresContactRepo { | |
|
||
Ok(()) | ||
} | ||
|
||
async fn get_by_id(&self, id: i64) -> anyhow::Result<models::IndexedContact> { | ||
let query_get_by_id = "SELECT * FROM contacts WHERE id=$1"; | ||
|
||
let contact: models::IndexedContact = | ||
sqlx::query_as::<_, models::IndexedContact>(query_get_by_id) | ||
.bind(id) | ||
.fetch_one(&*self.pg_pool) | ||
.await?; | ||
|
||
Ok(contact) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
@@ -144,4 +157,29 @@ mod tests { | |
|
||
assert!(result.is_ok()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_contact_by_id() { | ||
let mut mock_contact_repo = MockContactRepo::new(); | ||
|
||
let contact = models::IndexedContact { | ||
id: 1, | ||
contact: models::Contact::new("John", "Doe", "[email protected]", "1234567890") | ||
.unwrap(), | ||
}; | ||
|
||
mock_contact_repo | ||
.expect_get_by_id() | ||
.times(1) | ||
.with(eq(contact.id)) | ||
.return_once(|_| Ok(contact)); | ||
|
||
let result = mock_contact_repo.get_by_id(1).await; | ||
|
||
assert!(result.is_ok()); | ||
|
||
let actual_contact = result.unwrap(); | ||
|
||
assert_eq!(actual_contact.id, 1); | ||
} | ||
} |
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