Cleaned up repository layer.
This commit is contained in:
@@ -4,6 +4,13 @@ pub struct Upsert<T> {
|
||||
}
|
||||
|
||||
impl<T> Upsert<T> {
|
||||
pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Upsert<U> {
|
||||
Upsert {
|
||||
current: f(self.current),
|
||||
previous: self.previous.map(f),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_map<U, E>(self, mut f: impl FnMut(T) -> Result<U, E>) -> Result<Upsert<U>, E> {
|
||||
Ok(Upsert {
|
||||
current: f(self.current)?,
|
||||
|
||||
@@ -14,8 +14,7 @@ where
|
||||
{
|
||||
let token = database::access_tokens::get_by_hash(executor, secret_hash.hash())
|
||||
.await?
|
||||
.map(decode_access_token)
|
||||
.transpose()?;
|
||||
.map(decode_access_token);
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
@@ -29,8 +28,7 @@ where
|
||||
{
|
||||
let token = database::access_tokens::get_by_refresh_secret_hash(executor, secret_hash.hash())
|
||||
.await?
|
||||
.map(decode_access_token)
|
||||
.transpose()?;
|
||||
.map(decode_access_token);
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
@@ -45,9 +43,7 @@ where
|
||||
let token = database::access_tokens::upsert(executor, encode_access_token(access_token))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.map(|upserted| upserted.try_map(decode_access_token))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
.map(|upserted| upserted.map(decode_access_token).into());
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
@@ -61,8 +57,7 @@ where
|
||||
{
|
||||
let token = database::access_tokens::delete_by_hash(executor, secret_hash.hash())
|
||||
.await?
|
||||
.map(decode_access_token)
|
||||
.transpose()?;
|
||||
.map(decode_access_token);
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
@@ -87,15 +82,15 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_access_token(row: AccessTokenRow) -> Result<HashedAccessToken, RepositoryError> {
|
||||
Ok(HashedAccessToken {
|
||||
fn decode_access_token(row: AccessTokenRow) -> HashedAccessToken {
|
||||
HashedAccessToken {
|
||||
id: row.id,
|
||||
user_id: row.user_id,
|
||||
user_app_id: row.user_app_id,
|
||||
secret_hash: HashedSecret::from(row.secret_hash),
|
||||
expires_at: row.expires_at,
|
||||
issued_at: row.issued_at,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
|
||||
@@ -111,33 +106,14 @@ fn encode_access_token(token: HashedAccessToken) -> AccessTokenRow {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{Duration, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn row() -> AccessTokenRow {
|
||||
let issued_at = Utc::now();
|
||||
AccessTokenRow {
|
||||
id: Uuid::new_v4(),
|
||||
user_id: Uuid::new_v4(),
|
||||
user_app_id: Some(Uuid::new_v4()),
|
||||
secret_hash: vec![0; 32],
|
||||
expires_at: issued_at + Duration::days(1),
|
||||
issued_at,
|
||||
}
|
||||
}
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_access_token_user_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::AccessTokenUser));
|
||||
fn classifies_upsert_error_user_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::AccessTokenUser,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -149,8 +125,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_access_token_user_app_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::AccessTokenUserApp));
|
||||
fn classifies_upsert_error_user_app_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::AccessTokenUserApp,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -162,31 +141,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
fn classifies_upsert_error_unknown_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, user_id, user_app_id, secret_hash) = (
|
||||
row.id,
|
||||
row.user_id,
|
||||
row.user_app_id,
|
||||
row.secret_hash.clone(),
|
||||
);
|
||||
let token = decode_access_token(row).unwrap();
|
||||
assert_eq!(token.id, id);
|
||||
assert_eq!(token.user_id, user_id);
|
||||
assert_eq!(token.user_app_id, user_app_id);
|
||||
assert_eq!(token.secret_hash.hash(), secret_hash.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let token = decode_access_token(row()).unwrap();
|
||||
let round_trip = decode_access_token(encode_access_token(token.clone())).unwrap();
|
||||
assert!(round_trip == token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, DuplicateEntity, QueryConversionError, ReferenceRelation, ReferenceViolationKind,
|
||||
DuplicateEntity, QueryConversionError, ReferenceRelation, ReferenceViolationKind,
|
||||
RepositoryError, dtos::Upsert,
|
||||
};
|
||||
use crate::{
|
||||
@@ -38,7 +38,7 @@ where
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
.collect();
|
||||
|
||||
Ok(members)
|
||||
}
|
||||
@@ -54,8 +54,7 @@ where
|
||||
{
|
||||
let member = database::account_members::get_by_id(executor, user_id, account_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
.map(decode);
|
||||
|
||||
Ok(member)
|
||||
}
|
||||
@@ -71,9 +70,7 @@ where
|
||||
let member = database::account_members::upsert(executor, user_id, encode(member))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.map(|upserted| upserted.try_map(decode))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
.map(|upserted| upserted.map(decode).into());
|
||||
|
||||
Ok(member)
|
||||
}
|
||||
@@ -92,7 +89,7 @@ where
|
||||
.await
|
||||
.map_err(classify_upsert_error)?;
|
||||
|
||||
Ok(decode(row)?)
|
||||
Ok(decode(row))
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
@@ -107,8 +104,7 @@ where
|
||||
let member = database::account_members::delete_by_id(executor, user_id, account_id, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
.map(decode);
|
||||
|
||||
Ok(member)
|
||||
}
|
||||
@@ -156,12 +152,12 @@ fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(row: AccountMemberRow) -> Result<AccountMember, DecodeError> {
|
||||
Ok(AccountMember {
|
||||
fn decode(row: AccountMemberRow) -> AccountMember {
|
||||
AccountMember {
|
||||
id: row.id,
|
||||
account_id: row.account_id,
|
||||
user_id: row.user_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(member: AccountMember) -> AccountMemberRow {
|
||||
@@ -176,16 +172,12 @@ fn encode(member: AccountMember) -> AccountMemberRow {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_account_member_upsert_account_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::AccountMemberAccount));
|
||||
fn classifies_upsert_error_account_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::AccountMemberAccount,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -197,8 +189,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_account_member_upsert_user_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::AccountMemberUser));
|
||||
fn classifies_upsert_error_user_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::AccountMemberUser,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -210,21 +205,16 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
fn classifies_upsert_error_unknown_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> AccountMemberRow {
|
||||
AccountMemberRow {
|
||||
id: Uuid::new_v4(),
|
||||
account_id: Uuid::new_v4(),
|
||||
user_id: Uuid::new_v4(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_account_member_upsert_unique_violation() {
|
||||
fn classifies_upsert_error_account_member_unique_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::UniqueViolation {
|
||||
constraint: Constraint::AccountMemberAccountUser,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
@@ -239,7 +229,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_unique_violations_generic() {
|
||||
fn classifies_upsert_error_unknown_unique_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::UniqueViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
@@ -248,7 +238,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_account_member_delete_last_member_check_violation() {
|
||||
fn classifies_delete_error_last_member_check_violation() {
|
||||
let error = classify_delete_error(DatabaseError::CheckViolation {
|
||||
constraint: Constraint::AccountMemberLastMember,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
@@ -264,28 +254,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_delete_check_violations_generic() {
|
||||
fn classifies_delete_error_unknown_check_violation() {
|
||||
let error = classify_delete_error(DatabaseError::CheckViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, account_id, user_id) = (row.id, row.account_id, row.user_id);
|
||||
let member = decode(row).unwrap();
|
||||
assert_eq!(member.id, id);
|
||||
assert_eq!(member.account_id, account_id);
|
||||
assert_eq!(member.user_id, user_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let member = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(member.clone())).unwrap();
|
||||
assert_eq!(round_trip, member);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@ use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::Upsert,
|
||||
QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError, dtos::Upsert,
|
||||
};
|
||||
use crate::{
|
||||
database::{self, Constraint, DatabaseError, dtos::AccountRow},
|
||||
@@ -41,7 +40,7 @@ where
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<_, _>>()?;
|
||||
.collect();
|
||||
|
||||
Ok(accounts)
|
||||
}
|
||||
@@ -56,8 +55,7 @@ where
|
||||
{
|
||||
let account = database::accounts::get_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
.map(decode);
|
||||
|
||||
Ok(account)
|
||||
}
|
||||
@@ -78,7 +76,7 @@ where
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let account_map = accounts
|
||||
.into_iter()
|
||||
@@ -98,9 +96,7 @@ where
|
||||
{
|
||||
let upserted = database::accounts::upsert(executor, user_id, encode(account))
|
||||
.await?
|
||||
.map(|upserted| upserted.try_map(decode))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
.map(|upserted| upserted.map(decode).into());
|
||||
|
||||
Ok(upserted)
|
||||
}
|
||||
@@ -116,8 +112,7 @@ where
|
||||
let account = database::accounts::delete_by_id(executor, user_id, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
.map(decode);
|
||||
|
||||
Ok(account)
|
||||
}
|
||||
@@ -144,11 +139,11 @@ fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(row: AccountRow) -> Result<Account, DecodeError> {
|
||||
Ok(Account {
|
||||
fn decode(row: AccountRow) -> Account {
|
||||
Account {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(account: Account) -> AccountRow {
|
||||
@@ -160,22 +155,14 @@ fn encode(account: Account) -> AccountRow {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_account_delete_foreign_key_constraint() {
|
||||
let error = classify_delete_error(foreign_key_violation(
|
||||
Constraint::RecurringTransactionAccount,
|
||||
));
|
||||
fn classifies_delete_error_recurring_transaction_account_foreign_key_violation() {
|
||||
let error = classify_delete_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RecurringTransactionAccount,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
&error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -187,8 +174,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_transaction_account_delete_foreign_key_constraint() {
|
||||
let error = classify_delete_error(foreign_key_violation(Constraint::TransactionAccount));
|
||||
fn classifies_delete_error_transaction_account_foreign_key_violation() {
|
||||
let error = classify_delete_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::TransactionAccount,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -200,31 +190,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_account_delete_foreign_keys_generic() {
|
||||
let error = classify_delete_error(foreign_key_violation(Constraint::Unnamed));
|
||||
fn classifies_delete_error_unknown_foreign_key_violation() {
|
||||
let error = classify_delete_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> AccountRow {
|
||||
AccountRow {
|
||||
id: Uuid::new_v4(),
|
||||
name: "Savings".to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let id = row.id;
|
||||
let account = decode(row).unwrap();
|
||||
assert_eq!(account.id, id);
|
||||
assert_eq!(account.name, "Savings");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let account = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(account.clone())).unwrap();
|
||||
assert_eq!(round_trip, account);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,7 @@ fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeEr
|
||||
every: RecurrenceInterval::try_from(row.recurrence_every)?,
|
||||
unit: decode_recurrence_unit(row.recurrence_unit)?,
|
||||
};
|
||||
|
||||
let weekend_adjustment = row
|
||||
.weekend_adjustment
|
||||
.map(decode_weekend_adjustment)
|
||||
@@ -157,24 +158,14 @@ fn encode(transaction: RecurringTransaction) -> RecurringTransactionRow {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{TimeDelta, TimeZone, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::*;
|
||||
use crate::domain::{Direction, RecurrenceUnit, ScheduleError, WeekendAdjustment};
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_upsert_foreign_key_constraints() {
|
||||
let account = classify_upsert_error(foreign_key_violation(
|
||||
Constraint::RecurringTransactionAccount,
|
||||
));
|
||||
fn classifies_upsert_error_account_foreign_key_violation() {
|
||||
let account = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RecurringTransactionAccount,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
&account,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -186,73 +177,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
fn classifies_upsert_error_unknown_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> RecurringTransactionRow {
|
||||
RecurringTransactionRow {
|
||||
id: Uuid::new_v4(),
|
||||
name: "Rent".to_owned(),
|
||||
account_id: Uuid::new_v4(),
|
||||
amount_cents: 120_000,
|
||||
direction: "out".to_owned(),
|
||||
starts_at: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
|
||||
ends_at: None,
|
||||
recurrence_every: 1,
|
||||
recurrence_unit: "month".to_owned(),
|
||||
weekend_adjustment: Some("next_business_day".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, start, account_id) = (row.id, row.starts_at, row.account_id);
|
||||
let transaction = decode(row).unwrap();
|
||||
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.account_id, account_id);
|
||||
assert_eq!(transaction.schedule.start(), start);
|
||||
assert_eq!(transaction.schedule.end(), None);
|
||||
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]
|
||||
fn rejects_corrupt_rows() {
|
||||
let mut every = row();
|
||||
every.recurrence_every = 0;
|
||||
assert!(matches!(
|
||||
decode(every),
|
||||
Err(DecodeError::Schedule(ScheduleError::RecurrenceInterval(_)))
|
||||
));
|
||||
|
||||
let mut unit = row();
|
||||
unit.recurrence_unit = "hour".to_owned();
|
||||
assert!(matches!(decode(unit), Err(DecodeError::RecurrenceUnit(_))));
|
||||
|
||||
let mut end = row();
|
||||
end.ends_at = Some(end.starts_at - TimeDelta::seconds(1));
|
||||
assert!(matches!(
|
||||
decode(end),
|
||||
Err(DecodeError::Schedule(ScheduleError::EndBeforeStart))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let transaction = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(transaction.clone())).unwrap();
|
||||
assert_eq!(round_trip, transaction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,7 @@ where
|
||||
{
|
||||
let token = database::refresh_tokens::get_by_hash(executor, secret_hash.hash())
|
||||
.await?
|
||||
.map(decode_refresh_token)
|
||||
.transpose()?;
|
||||
.map(decode_refresh_token);
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
@@ -30,9 +29,7 @@ where
|
||||
let token = database::refresh_tokens::upsert(executor, encode_refresh_token(refresh_token))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.map(|upserted| upserted.try_map(decode_refresh_token))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
.map(|upserted| upserted.map(decode_refresh_token).into());
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
@@ -46,36 +43,33 @@ where
|
||||
{
|
||||
let token = database::refresh_tokens::delete_by_hash(executor, secret_hash.hash())
|
||||
.await?
|
||||
.map(decode_refresh_token)
|
||||
.transpose()?;
|
||||
.map(decode_refresh_token);
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
let relation = match &error {
|
||||
match &error {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RefreshTokenAccessToken,
|
||||
..
|
||||
} => ReferenceRelation::AccessToken,
|
||||
_ => return RepositoryError::Database(error),
|
||||
};
|
||||
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation,
|
||||
} => RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::AccessToken,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
source: error,
|
||||
},
|
||||
_ => RepositoryError::Database(error),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_refresh_token(row: RefreshTokenRow) -> Result<HashedRefreshToken, RepositoryError> {
|
||||
Ok(HashedRefreshToken {
|
||||
fn decode_refresh_token(row: RefreshTokenRow) -> HashedRefreshToken {
|
||||
HashedRefreshToken {
|
||||
id: row.id,
|
||||
token_id: row.access_token_id,
|
||||
secret_hash: HashedSecret::from(row.secret_hash),
|
||||
expires_at: row.expires_at,
|
||||
issued_at: row.issued_at,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
|
||||
@@ -90,33 +84,14 @@ fn encode_refresh_token(token: HashedRefreshToken) -> RefreshTokenRow {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{Duration, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn row() -> RefreshTokenRow {
|
||||
let issued_at = Utc::now();
|
||||
RefreshTokenRow {
|
||||
id: Uuid::new_v4(),
|
||||
access_token_id: Uuid::new_v4(),
|
||||
secret_hash: vec![0; 32],
|
||||
expires_at: issued_at + Duration::days(1),
|
||||
issued_at,
|
||||
}
|
||||
}
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_refresh_token_upsert_foreign_key_violation() {
|
||||
let error =
|
||||
classify_upsert_error(foreign_key_violation(Constraint::RefreshTokenAccessToken));
|
||||
fn classifies_upsert_error_access_token_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RefreshTokenAccessToken,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -128,26 +103,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
fn classifies_upsert_error_unknown_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, access_token_id, secret_hash) =
|
||||
(row.id, row.access_token_id, row.secret_hash.clone());
|
||||
let token = decode_refresh_token(row).unwrap();
|
||||
assert_eq!(token.id, id);
|
||||
assert_eq!(token.token_id, access_token_id);
|
||||
assert_eq!(token.secret_hash.hash(), secret_hash.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let token = decode_refresh_token(row()).unwrap();
|
||||
let round_trip = decode_refresh_token(encode_refresh_token(token.clone())).unwrap();
|
||||
assert!(round_trip == token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,22 +131,14 @@ fn encode(transaction: Transaction) -> TransactionRow {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::Utc;
|
||||
use uuid::Uuid;
|
||||
|
||||
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));
|
||||
fn classifies_upsert_error_account_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::TransactionAccount,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -158,51 +150,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
fn classifies_upsert_error_unknown_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> TransactionRow {
|
||||
TransactionRow {
|
||||
id: Uuid::new_v4(),
|
||||
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, account_id, date) = (row.id, row.account_id, row.date);
|
||||
let transaction = decode(row).unwrap();
|
||||
|
||||
assert_eq!(transaction.id, id);
|
||||
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]
|
||||
fn rejects_corrupt_rows() {
|
||||
let mut negative = row();
|
||||
negative.amount_cents = -1;
|
||||
assert!(matches!(decode(negative), Err(DecodeError::Amount(_))));
|
||||
|
||||
let mut direction = row();
|
||||
direction.direction = "sideways".to_owned();
|
||||
assert!(matches!(decode(direction), Err(DecodeError::Direction(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let transaction = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(transaction.clone())).unwrap();
|
||||
assert_eq!(round_trip, transaction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,32 +144,14 @@ fn encode(secret: UserAppSecret) -> UserAppSecretRow {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::{Duration, Utc};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn row() -> UserAppSecretRow {
|
||||
let created_at = Utc::now();
|
||||
|
||||
UserAppSecretRow {
|
||||
id: Uuid::new_v4(),
|
||||
app_id: Uuid::new_v4(),
|
||||
secret_hash: vec![0; 32],
|
||||
expires_at: created_at + Duration::days(1),
|
||||
created_at,
|
||||
}
|
||||
}
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_user_app_secret_upsert_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::UserAppSecretApp));
|
||||
fn classifies_upsert_error_app_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::UserAppSecretApp,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -181,15 +163,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
fn classifies_upsert_error_unknown_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let secret = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(secret.clone())).unwrap();
|
||||
assert_eq!(round_trip, secret);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
||||
use crate::{
|
||||
database::{self, Constraint, DatabaseError, dtos::UserAppRow},
|
||||
domain::UserApp,
|
||||
repository::DecodeError,
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -35,7 +34,7 @@ where
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
.collect();
|
||||
|
||||
Ok(apps)
|
||||
}
|
||||
@@ -50,8 +49,7 @@ where
|
||||
{
|
||||
let app = database::user_apps::get_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
.map(decode);
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
@@ -65,8 +63,7 @@ where
|
||||
{
|
||||
let app = database::user_apps::get_by_id_unscoped(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
.map(decode);
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
@@ -81,9 +78,7 @@ where
|
||||
let app = database::user_apps::upsert(executor, encode(app))
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.map(|upserted| upserted.try_map(decode))
|
||||
.transpose()?
|
||||
.map(Into::into);
|
||||
.map(|upserted| upserted.map(decode).into());
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
@@ -98,8 +93,7 @@ where
|
||||
{
|
||||
let app = database::user_apps::delete_by_id(executor, user_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
.map(decode);
|
||||
|
||||
Ok(app)
|
||||
}
|
||||
@@ -118,11 +112,11 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(row: UserAppRow) -> Result<UserApp, DecodeError> {
|
||||
Ok(UserApp {
|
||||
fn decode(row: UserAppRow) -> UserApp {
|
||||
UserApp {
|
||||
id: row.id,
|
||||
user_id: row.user_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(app: UserApp) -> UserAppRow {
|
||||
@@ -136,16 +130,12 @@ fn encode(app: UserApp) -> UserAppRow {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_user_app_upsert_foreign_key_violation() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::UserAppUser));
|
||||
fn classifies_upsert_error_user_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::UserAppUser,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -157,32 +147,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unnamed));
|
||||
fn classifies_upsert_error_unknown_foreign_key_violation() {
|
||||
let error = classify_upsert_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> UserAppRow {
|
||||
UserAppRow {
|
||||
id: Uuid::new_v4(),
|
||||
user_id: Uuid::new_v4(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let (id, user_id) = (row.id, row.user_id);
|
||||
let app = decode(row).unwrap();
|
||||
|
||||
assert_eq!(app.id, id);
|
||||
assert_eq!(app.user_id, user_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let app = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(app.clone())).unwrap();
|
||||
assert_eq!(round_trip, app);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@ use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::Upsert,
|
||||
QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError, dtos::Upsert,
|
||||
};
|
||||
use crate::{
|
||||
database::{self, Constraint, DatabaseError, dtos::UserRow},
|
||||
@@ -35,7 +34,7 @@ where
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<_, _>>()?;
|
||||
.collect();
|
||||
|
||||
Ok(users)
|
||||
}
|
||||
@@ -44,10 +43,7 @@ pub async fn get_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<User>, Rep
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let user = database::users::get_by_id(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
let user = database::users::get_by_id(executor, id).await?.map(decode);
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
@@ -67,7 +63,7 @@ where
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(decode)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let user_map = users.into_iter().map(|user| (user.id, user)).collect();
|
||||
|
||||
@@ -80,7 +76,7 @@ where
|
||||
{
|
||||
let user = database::users::upsert(executor, encode(user))
|
||||
.await?
|
||||
.try_map(decode)?
|
||||
.map(decode)
|
||||
.into();
|
||||
|
||||
Ok(user)
|
||||
@@ -93,8 +89,7 @@ where
|
||||
let user = database::users::delete_by_id(executor, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
.map(decode);
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
@@ -113,11 +108,11 @@ fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(row: UserRow) -> Result<User, DecodeError> {
|
||||
Ok(User {
|
||||
fn decode(row: UserRow) -> User {
|
||||
User {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(user: User) -> UserRow {
|
||||
@@ -129,21 +124,14 @@ fn encode(user: User) -> UserRow {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_account_member_user_delete_foreign_key_constraint() {
|
||||
let error = classify_delete_error(foreign_key_violation(Constraint::AccountMemberUser));
|
||||
|
||||
fn classifies_delete_account_member_foreign_key_violation() {
|
||||
let error = classify_delete_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::AccountMemberUser,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(
|
||||
error,
|
||||
RepositoryError::ReferenceViolation {
|
||||
@@ -155,33 +143,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_user_delete_foreign_keys_generic() {
|
||||
let error = classify_delete_error(foreign_key_violation(Constraint::Unnamed));
|
||||
|
||||
fn classifies_delete_error_unknown_foreign_key_violation() {
|
||||
let error = classify_delete_error(DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::Unnamed,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
});
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> UserRow {
|
||||
UserRow {
|
||||
id: Uuid::new_v4(),
|
||||
name: Some("Test User".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_valid_rows() {
|
||||
let row = row();
|
||||
let id = row.id;
|
||||
let user = decode(row).unwrap();
|
||||
|
||||
assert_eq!(user.id, id);
|
||||
assert_eq!(user.name, Some("Test User".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_through_rows() {
|
||||
let user = decode(row()).unwrap();
|
||||
let round_trip = decode(encode(user.clone())).unwrap();
|
||||
assert_eq!(round_trip, user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user