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
+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]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4();
@@ -53,19 +72,4 @@ mod tests {
assert_eq!(response.id, id);
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)]
mod tests {
use chrono::{TimeDelta, TimeZone, Utc};
use chrono::{TimeZone, Utc};
use uuid::Uuid;
use super::{
@@ -219,15 +219,22 @@ mod tests {
ScheduleRequest, ScheduledTransactionRequest, ScheduledTransactionResponse,
WeekendAdjustment,
};
use crate::{
domain::{self, ScheduleError},
routes::dtos::direction::Direction,
};
use crate::{domain, 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 {
name: String::new(),
amount_cents,
name: "Rent".to_owned(),
amount_cents: 1234,
direction: Direction::Out,
source_account: None,
destination_account: None,
@@ -244,24 +251,18 @@ mod tests {
}
#[test]
fn serializes_scheduled_transaction_with_nested_schedule() {
let transaction = request(1234).into_domain(Uuid::nil()).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();
fn serializes_recurring_transaction_response_with_nested_schedule() {
let response = serde_json::to_value(scheduled_response()).unwrap();
assert_eq!(response["type"], "recurring");
assert_eq!(response["id"], Uuid::nil().to_string());
assert_eq!(response["name"], "Rent");
assert_eq!(response["amount_cents"], 1234);
assert_eq!(response["direction"], "out");
assert!(response["source_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"]["weekend_adjustment"],
@@ -270,20 +271,17 @@ mod tests {
assert!(response["schedule"]["next"].is_null());
assert!(response.get("next").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]
fn serializes_source_and_destination_accounts() {
fn serializes_recurring_transaction_response_with_source_and_destination_accounts() {
let source_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.destination_account = Some(super::AccountRequest { id: destination_id });
let transaction = transaction.into_domain(Uuid::nil()).unwrap();
let source_account = Some(domain::Account {
id: source_id,
@@ -312,9 +310,33 @@ mod tests {
}
#[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 transaction = request(1234).into_domain(id).unwrap();
let transaction = request().into_domain(id).unwrap();
let recurrence = transaction.schedule.recurrence();
assert_eq!(transaction.id, id);
@@ -327,28 +349,4 @@ mod tests {
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)]
mod tests {
use chrono::TimeZone;
use super::*;
fn request(amount_cents: u64) -> TransactionRequest {
fn request() -> TransactionRequest {
TransactionRequest {
description: "Groceries".to_string(),
amount_cents,
amount_cents: 4250,
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]
fn converts_between_dto_and_domain() {
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.amount.get(), 4250);
assert_eq!(transaction.direction, domain::Direction::Out);
assert_eq!(transaction.date, date);
let response = TransactionResponse::from(transaction);
assert_eq!(response.id, id);
assert_eq!(response.description, "Groceries");
assert_eq!(response.amount_cents, 4250);
}
#[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);
assert!(matches!(response.direction, Direction::Out));
assert_eq!(response.date, date);
}
}