Refactored src and dst account to just account in preparation for a "counter party", instead of a full account.
This commit is contained in:
@@ -119,9 +119,15 @@ where
|
||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
match &error {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint:
|
||||
Constraint::RecurringTransactionSourceAccount
|
||||
| Constraint::RecurringTransactionDestinationAccount,
|
||||
constraint: Constraint::TransactionAccount,
|
||||
..
|
||||
} => RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::Transaction,
|
||||
kind: ReferenceViolationKind::StillReferenced,
|
||||
source: error,
|
||||
},
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RecurringTransactionAccount,
|
||||
..
|
||||
} => RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::RecurringTransaction,
|
||||
@@ -162,7 +168,7 @@ mod tests {
|
||||
#[test]
|
||||
fn classifies_account_delete_foreign_key_constraint() {
|
||||
let error = classify_delete_error(foreign_key_violation(
|
||||
Constraint::RecurringTransactionSourceAccount,
|
||||
Constraint::RecurringTransactionAccount,
|
||||
));
|
||||
assert!(matches!(
|
||||
&error,
|
||||
@@ -172,16 +178,24 @@ mod tests {
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
let database_error = std::error::Error::source(&error).unwrap();
|
||||
let sqlx_error = std::error::Error::source(database_error).unwrap();
|
||||
assert!(sqlx_error.to_string().contains("test database cause"));
|
||||
#[test]
|
||||
fn classifies_transaction_account_delete_foreign_key_constraint() {
|
||||
let error = classify_delete_error(foreign_key_violation(Constraint::TransactionAccount));
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::Transaction,
|
||||
kind: ReferenceViolationKind::StillReferenced,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_account_delete_foreign_keys_generic() {
|
||||
let error = classify_delete_error(foreign_key_violation(Constraint::Unnamed));
|
||||
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
@@ -197,7 +211,6 @@ mod tests {
|
||||
let row = row();
|
||||
let id = row.id;
|
||||
let account = decode(row).unwrap();
|
||||
|
||||
assert_eq!(account.id, id);
|
||||
assert_eq!(account.name, "Savings");
|
||||
}
|
||||
|
||||
@@ -93,13 +93,9 @@ where
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
let relation = match &error {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RecurringTransactionSourceAccount,
|
||||
constraint: Constraint::RecurringTransactionAccount,
|
||||
..
|
||||
} => Some(ReferenceRelation::SourceAccount),
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RecurringTransactionDestinationAccount,
|
||||
..
|
||||
} => Some(ReferenceRelation::DestinationAccount),
|
||||
} => Some(ReferenceRelation::Account),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
@@ -133,10 +129,9 @@ fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeEr
|
||||
Ok(RecurringTransaction {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
account_id: row.account_id,
|
||||
amount: Cents::try_from(row.amount_cents)?,
|
||||
direction: decode_direction(row.direction)?,
|
||||
source_account_id: row.source_account_id,
|
||||
destination_account_id: row.destination_account_id,
|
||||
schedule: Schedule::new(row.starts_at, row.ends_at, recurrence, weekend_adjustment)?,
|
||||
})
|
||||
}
|
||||
@@ -147,10 +142,9 @@ fn encode(transaction: RecurringTransaction) -> RecurringTransactionRow {
|
||||
RecurringTransactionRow {
|
||||
id: transaction.id,
|
||||
name: transaction.name,
|
||||
account_id: transaction.account_id,
|
||||
amount_cents: transaction.amount.into(),
|
||||
direction: encode_direction(transaction.direction).to_owned(),
|
||||
source_account_id: transaction.source_account_id,
|
||||
destination_account_id: transaction.destination_account_id,
|
||||
starts_at: transaction.schedule.start(),
|
||||
ends_at: transaction.schedule.end(),
|
||||
recurrence_every: recurrence.every.into(),
|
||||
@@ -179,29 +173,13 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn classifies_upsert_foreign_key_constraints() {
|
||||
let source = classify_upsert_error(foreign_key_violation(
|
||||
Constraint::RecurringTransactionSourceAccount,
|
||||
let account = classify_upsert_error(foreign_key_violation(
|
||||
Constraint::RecurringTransactionAccount,
|
||||
));
|
||||
assert!(matches!(
|
||||
&source,
|
||||
&account,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::SourceAccount,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
..
|
||||
}
|
||||
));
|
||||
|
||||
let database_error = std::error::Error::source(&source).unwrap();
|
||||
let sqlx_error = std::error::Error::source(database_error).unwrap();
|
||||
assert!(sqlx_error.to_string().contains("test database cause"));
|
||||
|
||||
let destination = classify_upsert_error(foreign_key_violation(
|
||||
Constraint::RecurringTransactionDestinationAccount,
|
||||
));
|
||||
assert!(matches!(
|
||||
&destination,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::DestinationAccount,
|
||||
relation: ReferenceRelation::Account,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
..
|
||||
}
|
||||
@@ -211,7 +189,6 @@ mod tests {
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
@@ -219,10 +196,9 @@ mod tests {
|
||||
RecurringTransactionRow {
|
||||
id: Uuid::new_v4(),
|
||||
name: "Rent".to_owned(),
|
||||
account_id: Uuid::new_v4(),
|
||||
amount_cents: 120_000,
|
||||
direction: "out".to_owned(),
|
||||
source_account_id: Some(Uuid::new_v4()),
|
||||
destination_account_id: Some(Uuid::new_v4()),
|
||||
starts_at: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
|
||||
ends_at: None,
|
||||
recurrence_every: 1,
|
||||
@@ -234,29 +210,23 @@ mod tests {
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, start, source_account_id, destination_account_id) = (
|
||||
row.id,
|
||||
row.starts_at,
|
||||
row.source_account_id,
|
||||
row.destination_account_id,
|
||||
);
|
||||
let (id, start, account_id) = (row.id, row.starts_at, row.account_id);
|
||||
let transaction = decode(row).unwrap();
|
||||
let recurrence = transaction.schedule.recurrence();
|
||||
|
||||
assert_eq!(transaction.id, id);
|
||||
assert_eq!(transaction.name, "Rent");
|
||||
assert_eq!(transaction.amount.get(), 120_000);
|
||||
assert_eq!(transaction.direction, Direction::Out);
|
||||
assert_eq!(transaction.source_account_id, source_account_id);
|
||||
assert_eq!(transaction.destination_account_id, destination_account_id);
|
||||
assert_eq!(transaction.account_id, account_id);
|
||||
assert_eq!(transaction.schedule.start(), start);
|
||||
assert_eq!(transaction.schedule.end(), None);
|
||||
assert_eq!(recurrence.every.get(), 1);
|
||||
assert_eq!(recurrence.unit, RecurrenceUnit::Month);
|
||||
assert_eq!(
|
||||
transaction.schedule.weekend_adjustment(),
|
||||
Some(WeekendAdjustment::NextBusinessDay)
|
||||
);
|
||||
|
||||
let recurrence = transaction.schedule.recurrence();
|
||||
assert_eq!(recurrence.every.get(), 1);
|
||||
assert_eq!(recurrence.unit, RecurrenceUnit::Month);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, RepositoryError,
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
encoding::{decode_direction, encode_direction},
|
||||
};
|
||||
use crate::{
|
||||
database::{dtos::TransactionRow, transactions as db},
|
||||
database::{Constraint, DatabaseError, dtos::TransactionRow, transactions as db},
|
||||
domain::{Cents, Transaction},
|
||||
};
|
||||
|
||||
@@ -72,12 +72,27 @@ where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let transaction = db::upsert(executor, encode(transaction))
|
||||
.await?
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?;
|
||||
|
||||
Ok(transaction.into())
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
match &error {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::TransactionAccount,
|
||||
..
|
||||
} => RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::Account,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
source: error,
|
||||
},
|
||||
_ => RepositoryError::Database(error),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
@@ -88,20 +103,22 @@ where
|
||||
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
|
||||
Ok(Transaction {
|
||||
id: row.id,
|
||||
description: row.description,
|
||||
account_id: row.account_id,
|
||||
amount: Cents::try_from(row.amount_cents)?,
|
||||
direction: decode_direction(row.direction)?,
|
||||
date: row.date,
|
||||
description: row.description,
|
||||
})
|
||||
}
|
||||
|
||||
fn encode(transaction: Transaction) -> TransactionRow {
|
||||
TransactionRow {
|
||||
id: transaction.id,
|
||||
description: transaction.description,
|
||||
account_id: transaction.account_id,
|
||||
amount_cents: transaction.amount.into(),
|
||||
direction: encode_direction(transaction.direction).to_owned(),
|
||||
date: transaction.date,
|
||||
description: transaction.description,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,27 +130,55 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::domain::Direction;
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_account_upsert_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::TransactionAccount));
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::Account,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> TransactionRow {
|
||||
TransactionRow {
|
||||
id: Uuid::new_v4(),
|
||||
description: "Groceries".to_owned(),
|
||||
account_id: Uuid::new_v4(),
|
||||
amount_cents: 4250,
|
||||
direction: "out".to_owned(),
|
||||
date: Utc::now(),
|
||||
description: "Groceries".to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, date) = (row.id, row.date);
|
||||
let (id, account_id, date) = (row.id, row.account_id, row.date);
|
||||
let transaction = decode(row).unwrap();
|
||||
|
||||
assert_eq!(transaction.id, id);
|
||||
assert_eq!(transaction.description, "Groceries");
|
||||
assert_eq!(transaction.account_id, account_id);
|
||||
assert_eq!(transaction.amount.get(), 4250);
|
||||
assert_eq!(transaction.direction, Direction::Out);
|
||||
assert_eq!(transaction.date, date);
|
||||
assert_eq!(transaction.description, "Groceries");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user