-
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.
- Loading branch information
Showing
10 changed files
with
219 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 +1,4 @@ | ||
pub mod count_devs; | ||
pub mod devs; | ||
pub mod health_check; | ||
pub mod transaction; |
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,20 @@ | ||
use axum::extract::{Path, State}; | ||
use axum::{http::StatusCode, response::IntoResponse, Json}; | ||
use mongodb::Database; | ||
|
||
use crate::structs::api; | ||
|
||
#[tracing::instrument(name = "Creating a new transaction", skip(_client, _body))] | ||
pub async fn create_transaction( | ||
State(_client): State<Database>, | ||
Path(id): Path<u64>, | ||
Json(_body): Json<api::Transaction>, | ||
) -> impl IntoResponse { | ||
( | ||
StatusCode::OK, | ||
Json(api::Balance { | ||
balance: 123, | ||
limit: 456, | ||
}), | ||
) | ||
} |
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,2 +1,3 @@ | ||
pub mod api; | ||
pub mod person; | ||
pub mod transaction; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] | ||
pub enum Action { | ||
#[serde(rename = "c")] | ||
Credit, | ||
#[serde(rename = "d")] | ||
Debt, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] | ||
pub struct Transaction { | ||
pub action: Action, | ||
pub description: String, | ||
pub value: i64, | ||
} |
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,7 @@ | ||
mod count_devs; | ||
mod get_dev_by_id; | ||
mod get_devs_by_search_term; | ||
mod health_check; | ||
pub mod helpers; | ||
mod post_devs; | ||
|
||
mod count_devs; | ||
|
||
mod get_devs_by_search_term; | ||
mod post_transaction; |
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,62 @@ | ||
use reqwest::StatusCode; | ||
|
||
#[tokio::test] | ||
async fn returns_200_with_balance_body_given_a_valid_request() { | ||
let test_app = crate::helpers::spawn_app().await; | ||
|
||
let response = reqwest::Client::new() | ||
.post(format!("{}/clientes/1/transacoes", test_app.address)) | ||
.json(&serde_json::json!({ | ||
"valor": 1000, | ||
"tipo" : "d", | ||
"descricao" : "descricao" | ||
})) | ||
.send() | ||
.await | ||
.expect("failed request"); | ||
|
||
assert_eq!(response.status(), StatusCode::OK); | ||
let response_body = response | ||
.json::<std::collections::HashMap<String, serde_json::Value>>() | ||
.await | ||
.unwrap(); | ||
assert_eq!(response_body["saldo"], 123); | ||
assert_eq!(response_body["limite"], 456); | ||
} | ||
|
||
#[tokio::test] | ||
async fn returns_422_with_error_body_given_a_too_long_description() { | ||
let test_app = crate::helpers::spawn_app().await; | ||
|
||
let response = reqwest::Client::new() | ||
.post(format!("{}/clientes/1/transacoes", test_app.address)) | ||
.json(&serde_json::json!({ | ||
"valor": 1000, | ||
"tipo" : "d", | ||
"descricao" : "some important money that I must send you!" | ||
})) | ||
.send() | ||
.await | ||
.expect("failed request"); | ||
|
||
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); | ||
} | ||
|
||
#[tokio::test] | ||
async fn returns_422_with_error_body_given_a_too_short_description() { | ||
let test_app = crate::helpers::spawn_app().await; | ||
|
||
let response = reqwest::Client::new() | ||
.post(format!("{}/clientes/1/transacoes", test_app.address)) | ||
.json(&serde_json::json!({ | ||
"valor": 1000, | ||
"tipo" : "d", | ||
"descricao" : "" | ||
})) | ||
.send() | ||
.await | ||
.expect("failed request"); | ||
|
||
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); | ||
} | ||
|