Test updates.

This commit is contained in:
2026-09-29 22:35:03 +02:00
parent 3858dd7216
commit f7e3fc5342
5 changed files with 153 additions and 109 deletions
+7 -3
View File
@@ -9,10 +9,14 @@ pub mod scheduled_transactions;
pub mod transactions; pub mod transactions;
pub fn create_router(pool: sqlx::PgPool) -> axum::Router { pub fn create_router(pool: sqlx::PgPool) -> axum::Router {
let accounts = accounts::create_router();
let scheduled_transactions = scheduled_transactions::create_router();
let transactions = transactions::create_router();
let api = axum::Router::new() let api = axum::Router::new()
.nest("/accounts", accounts::create_router()) .nest("/accounts", accounts)
.nest("/scheduled-transactions", scheduled_transactions::create_router()) .nest("/scheduled-transactions", scheduled_transactions)
.nest("/transactions", transactions::create_router()); .nest("/transactions", transactions);
axum::Router::new() axum::Router::new()
.nest("/api", api) .nest("/api", api)
+19 -15
View File
@@ -42,6 +42,25 @@ mod tests {
} }
} }
#[test]
fn serializes_account_response() {
let id = Uuid::nil();
let account = request().into_domain(id).unwrap();
let response = serde_json::to_value(AccountResponse::from(account)).unwrap();
assert_eq!(response["id"], id.to_string());
assert_eq!(response["name"], "Savings");
}
#[test]
fn deserializes_account_response_as_request() {
let account = request().into_domain(Uuid::nil()).unwrap();
let response = serde_json::to_value(AccountResponse::from(account)).unwrap();
let request: AccountRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.name, "Savings");
}
#[test] #[test]
fn converts_between_dto_and_domain() { fn converts_between_dto_and_domain() {
let id = Uuid::new_v4(); let id = Uuid::new_v4();
@@ -53,19 +72,4 @@ mod tests {
assert_eq!(response.id, id); assert_eq!(response.id, id);
assert_eq!(response.name, "Savings"); assert_eq!(response.name, "Savings");
} }
#[test]
fn keeps_account_json_flat() {
let response = AccountResponse {
id: Uuid::nil(),
name: "Savings".to_owned(),
};
let response = serde_json::to_value(response).unwrap();
assert_eq!(response["id"], Uuid::nil().to_string());
assert_eq!(response["name"], "Savings");
let request: AccountRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.name, "Savings");
}
} }
+36
View File
@@ -26,3 +26,39 @@ impl From<domain::Direction> for Direction {
} }
} }
} }
#[cfg(test)]
mod tests {
use super::Direction;
use crate::domain;
#[test]
fn serializes_direction() {
assert_eq!(serde_json::to_value(Direction::In).unwrap(), "in");
assert_eq!(serde_json::to_value(Direction::Out).unwrap(), "out");
}
#[test]
fn deserializes_direction() {
let incoming: Direction = serde_json::from_value("in".into()).unwrap();
assert!(matches!(incoming, Direction::In));
let outgoing: Direction = serde_json::from_value("out".into()).unwrap();
assert!(matches!(outgoing, Direction::Out));
}
#[test]
fn converts_between_dto_and_domain() {
let domain_in: domain::Direction = Direction::In.into();
assert_eq!(domain_in, domain::Direction::In);
let domain_out: domain::Direction = Direction::Out.into();
assert_eq!(domain_out, domain::Direction::Out);
let dto_in: Direction = domain::Direction::In.into();
assert!(matches!(dto_in, Direction::In));
let dto_out: Direction = domain::Direction::Out.into();
assert!(matches!(dto_out, Direction::Out));
}
}
+52 -54
View File
@@ -211,7 +211,7 @@ impl From<domain::WeekendAdjustment> for WeekendAdjustment {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use chrono::{TimeDelta, TimeZone, Utc}; use chrono::{TimeZone, Utc};
use uuid::Uuid; use uuid::Uuid;
use super::{ use super::{
@@ -219,15 +219,22 @@ mod tests {
ScheduleRequest, ScheduledTransactionRequest, ScheduledTransactionResponse, ScheduleRequest, ScheduledTransactionRequest, ScheduledTransactionResponse,
WeekendAdjustment, WeekendAdjustment,
}; };
use crate::{ use crate::{domain, routes::dtos::direction::Direction};
domain::{self, ScheduleError},
routes::dtos::direction::Direction,
};
fn request(amount_cents: u64) -> RecurringTransactionRequest { fn scheduled_response() -> ScheduledTransactionResponse {
let transaction = request().into_domain(Uuid::nil()).unwrap();
ScheduledTransactionResponse::from(RecurringTransactionResponse::new(
transaction,
None,
None,
None,
))
}
fn request() -> RecurringTransactionRequest {
RecurringTransactionRequest { RecurringTransactionRequest {
name: String::new(), name: "Rent".to_owned(),
amount_cents, amount_cents: 1234,
direction: Direction::Out, direction: Direction::Out,
source_account: None, source_account: None,
destination_account: None, destination_account: None,
@@ -244,24 +251,18 @@ mod tests {
} }
#[test] #[test]
fn serializes_scheduled_transaction_with_nested_schedule() { fn serializes_recurring_transaction_response_with_nested_schedule() {
let transaction = request(1234).into_domain(Uuid::nil()).unwrap(); let response = serde_json::to_value(scheduled_response()).unwrap();
let source_account = None;
let destination_account = None;
let schedule_next = None;
let response = ScheduledTransactionResponse::from(RecurringTransactionResponse::new(
transaction,
source_account,
destination_account,
schedule_next,
));
let response = serde_json::to_value(response).unwrap();
assert_eq!(response["type"], "recurring"); assert_eq!(response["type"], "recurring");
assert_eq!(response["id"], Uuid::nil().to_string()); assert_eq!(response["id"], Uuid::nil().to_string());
assert_eq!(response["name"], "Rent");
assert_eq!(response["amount_cents"], 1234); assert_eq!(response["amount_cents"], 1234);
assert_eq!(response["direction"], "out");
assert!(response["source_account"].is_null()); assert!(response["source_account"].is_null());
assert!(response["destination_account"].is_null()); assert!(response["destination_account"].is_null());
assert!(response["schedule"]["start"].is_string());
assert!(response["schedule"]["end"].is_null());
assert_eq!(response["schedule"]["recurrence"]["every"], 2);
assert_eq!(response["schedule"]["recurrence"]["unit"], "month"); assert_eq!(response["schedule"]["recurrence"]["unit"], "month");
assert_eq!( assert_eq!(
response["schedule"]["weekend_adjustment"], response["schedule"]["weekend_adjustment"],
@@ -270,20 +271,17 @@ mod tests {
assert!(response["schedule"]["next"].is_null()); assert!(response["schedule"]["next"].is_null());
assert!(response.get("next").is_none()); assert!(response.get("next").is_none());
assert!(response.get("data").is_none()); assert!(response.get("data").is_none());
let request: ScheduledTransactionRequest = serde_json::from_value(response).unwrap();
let ScheduledTransactionRequest::Recurring(request) = request;
assert_eq!(request.amount_cents, 1234);
assert_eq!(request.schedule.recurrence.every, 2);
} }
#[test] #[test]
fn serializes_source_and_destination_accounts() { fn serializes_recurring_transaction_response_with_source_and_destination_accounts() {
let source_id = Uuid::new_v4(); let source_id = Uuid::new_v4();
let destination_id = Uuid::new_v4(); let destination_id = Uuid::new_v4();
let mut transaction = request(1234);
let mut transaction = request();
transaction.source_account = Some(super::AccountRequest { id: source_id }); transaction.source_account = Some(super::AccountRequest { id: source_id });
transaction.destination_account = Some(super::AccountRequest { id: destination_id }); transaction.destination_account = Some(super::AccountRequest { id: destination_id });
let transaction = transaction.into_domain(Uuid::nil()).unwrap(); let transaction = transaction.into_domain(Uuid::nil()).unwrap();
let source_account = Some(domain::Account { let source_account = Some(domain::Account {
id: source_id, id: source_id,
@@ -312,9 +310,33 @@ mod tests {
} }
#[test] #[test]
fn converts_to_domain() { fn deserializes_scheduled_transaction_response_as_request() {
let response = serde_json::to_value(scheduled_response()).unwrap();
let request: ScheduledTransactionRequest = serde_json::from_value(response).unwrap();
let ScheduledTransactionRequest::Recurring(request) = request;
assert_eq!(request.name, "Rent");
assert_eq!(request.amount_cents, 1234);
assert!(matches!(request.direction, Direction::Out));
assert_eq!(
request.schedule.start,
Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap()
);
assert_eq!(request.schedule.end, None);
assert_eq!(request.schedule.recurrence.every, 2);
assert!(matches!(
request.schedule.recurrence.unit,
RecurrenceUnit::Month
));
assert!(matches!(
request.schedule.weekend_adjustment,
Some(WeekendAdjustment::NextBusinessDay)
));
}
#[test]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4(); let id = Uuid::new_v4();
let transaction = request(1234).into_domain(id).unwrap(); let transaction = request().into_domain(id).unwrap();
let recurrence = transaction.schedule.recurrence(); let recurrence = transaction.schedule.recurrence();
assert_eq!(transaction.id, id); assert_eq!(transaction.id, id);
@@ -327,28 +349,4 @@ mod tests {
Some(domain::WeekendAdjustment::NextBusinessDay) Some(domain::WeekendAdjustment::NextBusinessDay)
); );
} }
#[test]
fn rejects_invalid_requests() {
assert!(matches!(
request(i64::MAX as u64 + 1).into_domain(Uuid::nil()),
Err(domain::DomainError::Amount(_))
));
let mut every = request(100);
every.schedule.recurrence.every = 0;
assert!(matches!(
every.into_domain(Uuid::nil()),
Err(domain::DomainError::Schedule(
ScheduleError::RecurrenceInterval(_)
))
));
let mut end = request(100);
end.schedule.end = Some(end.schedule.start - TimeDelta::seconds(1));
assert!(matches!(
end.into_domain(Uuid::nil()),
Err(domain::DomainError::Schedule(ScheduleError::EndBeforeStart))
));
}
} }
+39 -37
View File
@@ -48,61 +48,63 @@ impl From<domain::Transaction> for TransactionResponse {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use chrono::TimeZone;
use super::*; use super::*;
fn request(amount_cents: u64) -> TransactionRequest { fn request() -> TransactionRequest {
TransactionRequest { TransactionRequest {
description: "Groceries".to_string(), description: "Groceries".to_string(),
amount_cents, amount_cents: 4250,
direction: Direction::Out, direction: Direction::Out,
date: Utc::now(), date: Utc.with_ymd_and_hms(2026, 9, 29, 12, 0, 0).unwrap(),
} }
} }
#[test]
fn serializes_transaction_response() {
let id = Uuid::nil();
let date = request().date;
let transaction = request().into_domain(id).unwrap();
let response = serde_json::to_value(TransactionResponse::from(transaction)).unwrap();
assert_eq!(response["id"], id.to_string());
assert_eq!(response["description"], "Groceries");
assert_eq!(response["amount_cents"], 4250);
assert_eq!(response["direction"], "out");
assert_eq!(response["date"], serde_json::to_value(date).unwrap());
}
#[test]
fn deserializes_transaction_response_as_request() {
let date = request().date;
let transaction = request().into_domain(Uuid::nil()).unwrap();
let response = serde_json::to_value(TransactionResponse::from(transaction)).unwrap();
let request: TransactionRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.description, "Groceries");
assert_eq!(request.amount_cents, 4250);
assert!(matches!(request.direction, Direction::Out));
assert_eq!(request.date, date);
}
#[test] #[test]
fn converts_between_dto_and_domain() { fn converts_between_dto_and_domain() {
let id = Uuid::new_v4(); let id = Uuid::new_v4();
let transaction = request(4250).into_domain(id).unwrap(); let request = request();
let date = request.date;
let transaction = request.into_domain(id).unwrap();
assert_eq!(transaction.id, id); assert_eq!(transaction.id, id);
assert_eq!(transaction.amount.get(), 4250); assert_eq!(transaction.amount.get(), 4250);
assert_eq!(transaction.direction, domain::Direction::Out); assert_eq!(transaction.direction, domain::Direction::Out);
assert_eq!(transaction.date, date);
let response = TransactionResponse::from(transaction); let response = TransactionResponse::from(transaction);
assert_eq!(response.id, id); assert_eq!(response.id, id);
assert_eq!(response.description, "Groceries"); assert_eq!(response.description, "Groceries");
assert_eq!(response.amount_cents, 4250); assert_eq!(response.amount_cents, 4250);
} assert!(matches!(response.direction, Direction::Out));
assert_eq!(response.date, date);
#[test]
fn rejects_amounts_that_do_not_fit_in_database() {
let error = request(i64::MAX as u64 + 1)
.into_domain(Uuid::nil())
.unwrap_err();
assert!(matches!(
error,
domain::DomainError::Amount(domain::CentsError(value))
if value == i64::MAX as i128 + 1
));
}
#[test]
fn keeps_transaction_json_flat() {
let response = TransactionResponse {
id: Uuid::nil(),
description: "Groceries".to_owned(),
amount_cents: 4250,
direction: Direction::Out,
date: Utc::now(),
};
let response = serde_json::to_value(response).unwrap();
assert_eq!(response["id"], Uuid::nil().to_string());
assert_eq!(response["amount_cents"], 4250);
assert_eq!(response["direction"], "out");
assert_eq!(response["description"], "Groceries");
let request: TransactionRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.description, "Groceries");
assert_eq!(request.amount_cents, 4250);
} }
} }