Skip to content

Commit

Permalink
test(processor): test processing
Browse files Browse the repository at this point in the history
  • Loading branch information
pbillaut committed Sep 15, 2024
1 parent 1c5c989 commit 9bb7b42
Showing 1 changed file with 102 additions and 2 deletions.
104 changes: 102 additions & 2 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::collections::HashMap;
use std::error::Error;
use tracing::{error, warn};

// TODO: This fn is public to be able to benchmark it. This should be handled with a bench feature
// instead.
// TODO: This fn is public to be able to benchmark it. This should probably be handled with a bench
// feature instead.
pub fn process_activities<I, E>(activities: I) -> Vec<Account>
where
E: Error,
Expand Down Expand Up @@ -49,3 +49,103 @@ pub trait Processor {
self.write(accounts)
}
}

#[cfg(test)]
mod tests {
use crate::account::test_utils::LockStatus;
use crate::account::Account;
use crate::account_activity::AccountActivity;
use crate::processor::process_activities;
use crate::processor::tests::DummyError::ParseError;
use crate::transaction::TransactionID;
use crate::ClientID;
use thiserror::Error;

#[derive(Error, Debug, Clone)]
pub enum DummyError {
#[error("error parsing account activity record")]
ParseError,
}

struct TestCase {
activities: Vec<Result<AccountActivity, DummyError>>,
expected: Vec<Account>,
}

fn test(test_case: TestCase) {
let output = process_activities(test_case.activities.into_iter());
for (account, expected) in output.into_iter().zip(test_case.expected) {
assert_eq!(account.client_id(), expected.client_id());
assert_eq!(account.available(), expected.available());
assert_eq!(account.held(), expected.held());
assert_eq!(account.total(), expected.total());
assert_eq!(account.is_locked(), expected.is_locked());
}
}

#[test]
fn erroneous_records_are_skipped() {
test(
TestCase {
activities: vec![
Ok(AccountActivity::deposit(
TransactionID::default(),
ClientID::default(),
10.0
)),
// The next record couldn't be parsed
Err(ParseError),
Ok(AccountActivity::withdrawal(
TransactionID::default(),
ClientID::default(),
5.0
))
],
expected: vec![
Account::with_values(
ClientID::default(),
5.0,
0.0,
5.0,
LockStatus::Unlocked
)
],
}
)
}

#[test]
fn invalid_activities_are_skipped() {
test(
TestCase {
activities: vec![
Ok(AccountActivity::deposit(
TransactionID::default(),
ClientID::default(),
10.0
)),
// The next activity should cause an insufficient funds error
Ok(AccountActivity::withdrawal(
TransactionID::default(),
ClientID::default(),
15.0
)),
Ok(AccountActivity::withdrawal(
TransactionID::default(),
ClientID::default(),
10.0
)),
],
expected: vec![
Account::with_values(
ClientID::default(),
0.0,
0.0,
0.0,
LockStatus::Unlocked
)
],
}
)
}
}

0 comments on commit 9bb7b42

Please sign in to comment.