Refactored src and dst account to just account in preparation for a "counter party", instead of a full account.
This commit is contained in:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user