Ditches useless tests.

This commit is contained in:
2026-10-25 22:18:19 +01:00
parent 58621b0500
commit 1d16be2137
10 changed files with 135 additions and 512 deletions
+10 -105
View File
@@ -5,7 +5,7 @@ use uuid::Uuid;
use super::direction::Direction;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct TransactionRequest {
pub account: AccountRequest,
pub amount_cents: u64,
@@ -27,7 +27,7 @@ impl TransactionRequest {
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct TransactionResponse {
pub id: Uuid,
pub account: AccountResponse,
@@ -41,10 +41,7 @@ impl TransactionResponse {
pub fn new(transaction: domain::Transaction, account: domain::Account) -> Self {
Self {
id: transaction.id,
account: AccountResponse {
id: account.id,
name: account.name,
},
account: account.into(),
amount_cents: transaction.amount.get(),
direction: transaction.direction.into(),
date: transaction.date,
@@ -53,114 +50,22 @@ impl TransactionResponse {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct AccountRequest {
pub id: Uuid,
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct AccountResponse {
pub id: Uuid,
pub name: String,
}
#[cfg(test)]
mod tests {
use chrono::TimeZone;
use super::*;
fn request() -> TransactionRequest {
TransactionRequest {
account: AccountRequest { id: Uuid::nil() },
amount_cents: 4250,
direction: Direction::Out,
date: Utc.with_ymd_and_hms(2026, 9, 29, 12, 0, 0).unwrap(),
description: "Groceries".to_string(),
impl From<domain::Account> for AccountResponse {
fn from(account: domain::Account) -> Self {
Self {
id: account.id,
name: account.name,
}
}
#[test]
fn serializes_transaction_response() {
let id = Uuid::nil();
let date = request().date;
let transaction = request().into_domain(id).unwrap();
let account = domain::Account {
id: Uuid::nil(),
name: "Checking".to_owned(),
};
let response =
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
assert_eq!(response["id"], id.to_string());
assert_eq!(response["account"]["id"], Uuid::nil().to_string());
assert_eq!(response["account"]["name"], "Checking");
assert_eq!(response["amount_cents"], 4250);
assert_eq!(response["direction"], "out");
assert_eq!(response["date"], serde_json::to_value(date).unwrap());
assert_eq!(response["description"], "Groceries");
}
#[test]
fn deserializes_transaction_response_as_request() {
let date = request().date;
let transaction = request().into_domain(Uuid::nil()).unwrap();
let account = domain::Account {
id: Uuid::nil(),
name: "Checking".to_owned(),
};
let response =
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
let request: TransactionRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.amount_cents, 4250);
assert_eq!(request.account.id, Uuid::nil());
assert!(matches!(request.direction, Direction::Out));
assert_eq!(request.date, date);
assert_eq!(request.description, "Groceries");
}
#[test]
fn requires_account_in_transaction_request() {
let transaction = request().into_domain(Uuid::nil()).unwrap();
let account = domain::Account {
id: Uuid::nil(),
name: "Checking".to_owned(),
};
let mut response =
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
response.as_object_mut().unwrap().remove("account");
assert!(serde_json::from_value::<TransactionRequest>(response).is_err());
}
#[test]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4();
let request = request();
let date = request.date;
let account_id = request.account.id;
let transaction = request.into_domain(id).unwrap();
assert_eq!(transaction.id, id);
assert_eq!(transaction.account_id, account_id);
assert_eq!(transaction.amount.get(), 4250);
assert_eq!(transaction.direction, domain::Direction::Out);
assert_eq!(transaction.date, date);
let response = TransactionResponse::new(
transaction,
domain::Account {
id: account_id,
name: "Checking".to_owned(),
},
);
assert_eq!(response.id, id);
assert_eq!(response.account.id, account_id);
assert_eq!(response.account.name, "Checking");
assert_eq!(response.amount_cents, 4250);
assert!(matches!(response.direction, Direction::Out));
assert_eq!(response.date, date);
assert_eq!(response.description, "Groceries");
}
}