1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use super::{AccountID, AccountStatus, AccountingError, ProgramState};
use chrono::NaiveDate;
use commodity::exchange_rate::ExchangeRate;
use commodity::Commodity;
use rust_decimal::{prelude::Zero, Decimal};
use std::fmt;
use std::rc::Rc;
use std::slice;

#[cfg(feature = "serde-support")]
use serde::{Deserialize, Serialize};

/// A representation of what type of [Action](Action) is being performed.
#[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Clone)]
pub enum ActionType {
    /// An [Action](Action) to edit the status of an [Account](crate::Account).
    /// Represented by the [EditAccountStatus](EditAccountStatus) struct.
    ///
    /// This action has the highest priority when being sorted, because
    /// other actions on the same day may depend on this already having
    /// been executed.
    EditAccountStatus,
    /// An [Action](Action) to assert the current balance of an account while
    /// a [Program](super::Program) is being executed. Represented by a
    /// [BalanceAssertion](BalanceAssertion) struct.
    BalanceAssertion,
    /// A [Action](Action) to perform a transaction between [Account](crate::Account)s.
    /// Represented by the [Transaction](Transaction) struct.
    Transaction,
}

impl ActionType {
    /// Return an iterator over all available [ActionType](ActionType) variants.
    pub fn iterator() -> slice::Iter<'static, ActionType> {
        static ACTION_TYPES: [ActionType; 3] = [
            ActionType::EditAccountStatus,
            ActionType::BalanceAssertion,
            ActionType::Transaction,
        ];
        ACTION_TYPES.iter()
    }
}

/// A trait which represents an enum/sized data structure which is
/// capable of storing every possible concrete implementation of
/// [Action](Action) for your [Program](crate::Program).
///
/// If you have some custom actions, you need to implement this trait
/// yourself and use it to store your actions that you provide to
/// [Program](crate::Program).
pub trait ActionTypeValueEnum {
    fn as_action(&self) -> &dyn Action;
}

/// An enum to store every possible concrete implementation of
/// [Action](Action) in a `Sized` element.
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-support", serde(tag = "type"))]
#[derive(Debug, Clone, PartialEq)]
pub enum ActionTypeValue {
    EditAccountStatus(EditAccountStatus),
    BalanceAssertion(BalanceAssertion),
    Transaction(Transaction),
}

impl ActionTypeValueEnum for ActionTypeValue {
    fn as_action(&self) -> &dyn Action {
        match self {
            ActionTypeValue::EditAccountStatus(action) => action,
            ActionTypeValue::BalanceAssertion(action) => action,
            ActionTypeValue::Transaction(action) => action,
        }
    }
}

impl From<EditAccountStatus> for ActionTypeValue {
    fn from(action: EditAccountStatus) -> Self {
        ActionTypeValue::EditAccountStatus(action)
    }
}

impl From<BalanceAssertion> for ActionTypeValue {
    fn from(action: BalanceAssertion) -> Self {
        ActionTypeValue::BalanceAssertion(action)
    }
}

impl From<Transaction> for ActionTypeValue {
    fn from(action: Transaction) -> Self {
        ActionTypeValue::Transaction(action)
    }
}

/// Represents an action which can modify [ProgramState](ProgramState).
pub trait Action: fmt::Display + fmt::Debug {
    /// The date/time (in the account history) that the action was performed.
    fn date(&self) -> NaiveDate;

    /// Perform the action to mutate the [ProgramState](ProgramState).
    fn perform(&self, program_state: &mut ProgramState) -> Result<(), AccountingError>;

    /// What type of action is being performed.
    fn action_type(&self) -> ActionType;
}

/// A way to sort [Action](Action)s by their date, and then by the
/// priority of their [ActionType](ActionType).
///
/// # Example
/// ```
/// use doublecount::{ActionTypeValue, ActionOrder};
/// use std::rc::Rc;
///
/// let mut actions: Vec<Rc<ActionTypeValue>> = Vec::new();
///
/// // let's pretend we created and added
/// // some actions to the actions vector
///
/// // sort the actions using this order
/// actions.sort_by_key(|a| ActionOrder(a.clone()));
/// ```
pub struct ActionOrder<A>(pub Rc<A>);

impl<A> PartialEq for ActionOrder<A>
where
    A: ActionTypeValueEnum,
{
    fn eq(&self, other: &ActionOrder<A>) -> bool {
        let self_action = self.0.as_action();
        let other_action = other.0.as_action();
        self_action.action_type() == other_action.action_type()
            && self_action.date() == other_action.date()
    }
}

impl<A> Eq for ActionOrder<A> where A: ActionTypeValueEnum {}

impl<A> PartialOrd for ActionOrder<A>
where
    A: ActionTypeValueEnum,
{
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        let self_action = self.0.as_action();
        let other_action = other.0.as_action();
        self_action
            .date()
            .partial_cmp(&other_action.date())
            .map(|date_order| {
                date_order.then(self_action.action_type().cmp(&other_action.action_type()))
            })
    }
}

impl<A> Ord for ActionOrder<A>
where
    A: ActionTypeValueEnum,
{
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let self_action = self.0.as_action();
        let other_action = other.0.as_action();
        self_action
            .date()
            .cmp(&other_action.date())
            .then(self_action.action_type().cmp(&other_action.action_type()))
    }
}

/// A movement of [Commodity](Commodity) between two or more accounts
/// on a given `date`. Implements [Action](Action) so it can be
/// applied to change [AccountState](super::AccountState)s.
///
/// The sum of the [Commodity](Commodity) `amount`s contained within a
/// transaction's [TransactionElement](TransactionElement)s needs to
/// be equal to zero, or one of the elements needs to have a `None`
/// value `amount`.
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Transaction {
    /// Description of this transaction.
    pub description: Option<String>,
    /// The date that the transaction occurred.
    pub date: NaiveDate,
    /// Elements which compose this transaction.
    ///
    /// See [Transaction](Transaction) for more information about the
    /// constraints which apply to this field.
    pub elements: Vec<TransactionElement>,
}

impl Transaction {
    /// Create a new [Transaction](Transaction).
    pub fn new<S: Into<String>>(
        description: Option<S>,
        date: NaiveDate,
        elements: Vec<TransactionElement>,
    ) -> Transaction {
        Transaction {
            description: description.map(|s| s.into()),
            date,
            elements,
        }
    }

    /// Create a new simple [Transaction](Transaction), containing
    /// only two elements, transfering an `amount` from `from_account`
    /// to `to_account` on the given `date`, with the given
    /// `exchange_rate` (required if the currencies of the accounts
    /// are different).
    ///
    /// # Example
    /// ```
    /// # use doublecount::Transaction;
    /// # use std::rc::Rc;
    /// use doublecount::Account;
    /// use commodity::{CommodityType, Commodity};
    /// use chrono::Local;
    /// use std::str::FromStr;
    ///
    /// let aud = Rc::from(CommodityType::from_currency_alpha3("AUD").unwrap());
    ///
    /// let account1 = Rc::from(Account::new_with_id(Some("Account 1"), aud.id, None));
    /// let account2 = Rc::from(Account::new_with_id(Some("Account 2"), aud.id, None));
    ///
    /// let transaction = Transaction::new_simple(
    ///    Some("balancing"),
    ///    Local::today().naive_local(),
    ///    account1.id,
    ///    account2.id,
    ///    Commodity::from_str("100.0 AUD").unwrap(),
    ///    None,
    /// );
    ///
    /// assert_eq!(2, transaction.elements.len());
    /// let element0 = transaction.elements.get(0).unwrap();
    /// let element1 = transaction.elements.get(1).unwrap();
    /// assert_eq!(Some(Commodity::from_str("-100.0 AUD").unwrap()), element0.amount);
    /// assert_eq!(account1.id, element0.account_id);
    /// assert_eq!(account2.id, element1.account_id);
    /// assert_eq!(None, element1.amount);
    /// ```
    pub fn new_simple<S: Into<String>>(
        description: Option<S>,
        date: NaiveDate,
        from_account: AccountID,
        to_account: AccountID,
        amount: Commodity,
        exchange_rate: Option<ExchangeRate>,
    ) -> Transaction {
        Transaction::new(
            description,
            date,
            vec![
                TransactionElement::new(from_account, Some(amount.neg()), exchange_rate.clone()),
                TransactionElement::new(to_account, None, exchange_rate),
            ],
        )
    }

    /// Get the [TransactionElement](TransactionElement) associated
    /// with the given [Account](crate::Account)'s id.
    pub fn get_element(&self, account_id: &AccountID) -> Option<&TransactionElement> {
        self.elements.iter().find(|e| &e.account_id == account_id)
    }
}

impl fmt::Display for Transaction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Transaction")
    }
}

impl Action for Transaction {
    fn date(&self) -> NaiveDate {
        self.date
    }

    fn perform(&self, program_state: &mut ProgramState) -> Result<(), AccountingError> {
        // check that the transaction has at least 2 elements
        if self.elements.len() < 2 {
            return Err(AccountingError::InvalidTransaction(
                self.clone(),
                String::from("a transaction cannot have less than 2 elements"),
            ));
        }

        //TODO: add check to ensure that transaction doesn't have duplicate account references?

        // first process the elements to automatically calculate amounts

        let mut empty_amount_element: Option<usize> = None;
        for (i, element) in self.elements.iter().enumerate() {
            if element.amount.is_none() {
                if empty_amount_element.is_none() {
                    empty_amount_element = Some(i)
                } else {
                    return Err(AccountingError::InvalidTransaction(
                        self.clone(),
                        String::from("multiple elements with no amount specified"),
                    ));
                }
            }
        }

        let sum_commodity_type_id = match empty_amount_element {
            Some(empty_i) => {
                let empty_element = self.elements.get(empty_i).unwrap();

                match program_state.get_account(&empty_element.account_id) {
                    Some(account) => account.commodity_type_id,
                    None => {
                        return Err(AccountingError::MissingAccountState(
                            empty_element.account_id,
                        ))
                    }
                }
            }
            None => {
                let account_id = self
                    .elements
                    .get(0)
                    .expect("there should be at least 2 elements in the transaction")
                    .account_id;

                match program_state.get_account(&account_id) {
                    Some(account) => account.commodity_type_id,
                    None => return Err(AccountingError::MissingAccountState(account_id)),
                }
            }
        };

        let mut sum = Commodity::new(Decimal::zero(), sum_commodity_type_id);

        let mut modified_elements = self.elements.clone();

        // Calculate the sum of elements (not including the empty element if there is one)
        for (i, element) in self.elements.iter().enumerate() {
            match empty_amount_element {
                Some(empty_i) => {
                    if i != empty_i {
                        //TODO: perform commodity type conversion here if required
                        sum = match sum.add(&element.amount.as_ref().unwrap()) {
                            Ok(value) => value,
                            Err(error) => return Err(AccountingError::Commodity(error)),
                        }
                    }
                }
                None => {}
            }
        }

        // Calculate the value to use for the empty element (negate the sum of the other elements)
        match empty_amount_element {
            Some(empty_i) => {
                let modified_emtpy_element: &mut TransactionElement =
                    modified_elements.get_mut(empty_i).unwrap();
                let negated_sum = sum.neg();
                modified_emtpy_element.amount = Some(negated_sum.clone());

                sum = match sum.add(&negated_sum) {
                    Ok(value) => value,
                    Err(error) => return Err(AccountingError::Commodity(error)),
                }
            }
            None => {}
        };

        if sum.value != Decimal::zero() {
            return Err(AccountingError::InvalidTransaction(
                self.clone(),
                String::from("sum of transaction elements does not equal zero"),
            ));
        }

        for transaction in &modified_elements {
            let mut account_state = program_state
                .get_account_state_mut(&transaction.account_id)
                .expect(
                    format!(
                        "unable to find state for account with id: {} please ensure this account was added to the program state before execution.",
                        transaction.account_id
                    )
                    .as_ref(),
                );

            match account_state.status {
                AccountStatus::Closed => Err(AccountingError::InvalidAccountStatus {
                    account_id: transaction.account_id,
                    status: account_state.status,
                }),
                _ => Ok(()),
            }?;

            // TODO: perform the commodity type conversion using the exchange rate (if present)

            let transaction_amount = match &transaction.amount {
                Some(amount) => amount,
                None => {
                    return Err(AccountingError::InvalidTransaction(
                        self.clone(),
                        String::from(
                            "unable to calculate all required amounts for this transaction",
                        ),
                    ))
                }
            };

            account_state.amount = match account_state.amount.add(transaction_amount) {
                Ok(commodity) => commodity,
                Err(err) => {
                    return Err(AccountingError::Commodity(err));
                }
            }
        }

        return Ok(());
    }

    fn action_type(&self) -> ActionType {
        ActionType::Transaction
    }
}

/// An element of a [Transaction](Transaction).
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct TransactionElement {
    /// The account to perform the transaction to
    pub account_id: AccountID,

    /// The amount of [Commodity](Commodity) to add to the account.
    ///
    /// This may be `None`, if it is the only element within a
    /// [Transaction](Transaction), which is None. If it is `None`,
    /// it's amount will be automatically calculated from the amounts
    /// in the other elements present in the transaction.
    pub amount: Option<Commodity>,

    /// The exchange rate to use for converting the amount in this element
    /// to a different [CommodityType](commodity::CommodityType).
    pub exchange_rate: Option<ExchangeRate>,
}

impl TransactionElement {
    /// Create a new [TransactionElement](TransactionElement).
    pub fn new(
        account_id: AccountID,
        amount: Option<Commodity>,
        exchange_rate: Option<ExchangeRate>,
    ) -> TransactionElement {
        TransactionElement {
            account_id,
            amount,
            exchange_rate,
        }
    }
}

/// A type of [Action](Action) to edit the
/// [AccountStatus](AccountStatus) of a given [Account](crate::Account)'s
/// [AccountState](super::AccountState).
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct EditAccountStatus {
    account_id: AccountID,
    newstatus: AccountStatus,
    date: NaiveDate,
}

impl EditAccountStatus {
    /// Create a new [EditAccountStatus](EditAccountStatus).
    pub fn new(
        account_id: AccountID,
        newstatus: AccountStatus,
        date: NaiveDate,
    ) -> EditAccountStatus {
        EditAccountStatus {
            account_id,
            newstatus,
            date,
        }
    }
}

impl fmt::Display for EditAccountStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Edit Account Status")
    }
}

impl Action for EditAccountStatus {
    fn date(&self) -> NaiveDate {
        self.date
    }

    fn perform(&self, program_state: &mut ProgramState) -> Result<(), AccountingError> {
        let mut account_state = program_state
            .get_account_state_mut(&self.account_id)
            .unwrap();
        account_state.status = self.newstatus;
        return Ok(());
    }

    fn action_type(&self) -> ActionType {
        ActionType::EditAccountStatus
    }
}

/// A type of [Action](Action) to check and assert the balance of a
/// given [Account](crate::Account) in its [AccountStatus](AccountStatus) at
/// the beginning of the given date.
///
/// When running its [perform()](Action::perform()) method, if this
/// assertion fails, a [FailedBalanceAssertion](FailedBalanceAssertion)
/// will be recorded in the [ProgramState](ProgramState).
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct BalanceAssertion {
    account_id: AccountID,
    date: NaiveDate,
    expected_balance: Commodity,
}

impl BalanceAssertion {
    /// Create a new [BalanceAssertion](BalanceAssertion). The balance
    /// will be considered at the beginning of the provided `date`.
    pub fn new(
        account_id: AccountID,
        date: NaiveDate,
        expected_balance: Commodity,
    ) -> BalanceAssertion {
        BalanceAssertion {
            account_id,
            date,
            expected_balance,
        }
    }
}

impl fmt::Display for BalanceAssertion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Assert Account Balance")
    }
}

/// Records the failure of a [BalanceAssertion](BalanceAssertion) when
/// it is evaluated using its implementation of the
/// [Action::perform()](Action::perform()) method.
#[derive(Debug, Clone)]
pub struct FailedBalanceAssertion {
    pub assertion: BalanceAssertion,
    pub actual_balance: Commodity,
}

impl FailedBalanceAssertion {
    /// Create a new [FailedBalanceAssertion](FailedBalanceAssertion).
    pub fn new(assertion: BalanceAssertion, actual_balance: Commodity) -> FailedBalanceAssertion {
        FailedBalanceAssertion {
            assertion,
            actual_balance,
        }
    }
}

impl fmt::Display for FailedBalanceAssertion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Failed Account Balance Assertion")
    }
}

// When running this action's `perform()` method implementation, if
// this assertion fails, a [FailedBalanceAssertion](FailedBalanceAssertion)
// will be recorded in the [ProgramState](ProgramState).
impl Action for BalanceAssertion {
    fn date(&self) -> NaiveDate {
        self.date
    }

    fn perform(&self, program_state: &mut ProgramState) -> Result<(), AccountingError> {
        match program_state.get_account_state(&self.account_id) {
            Some(state) => {
                if state
                    .amount
                    .eq_approx(self.expected_balance, Commodity::default_epsilon())
                {
                } else {
                }
            }
            None => {
                return Err(AccountingError::MissingAccountState(self.account_id));
            }
        }

        return Ok(());
    }

    fn action_type(&self) -> ActionType {
        ActionType::BalanceAssertion
    }
}

#[cfg(test)]
mod tests {
    use super::{ActionType, BalanceAssertion, EditAccountStatus, Transaction};
    use crate::{AccountID, AccountStatus};
    use chrono::NaiveDate;
    use commodity::Commodity;
    use std::{collections::HashSet, str::FromStr};

    #[test]
    fn action_type_order() {
        let mut tested_types: HashSet<ActionType> = HashSet::new();

        let mut action_types_unordered: Vec<ActionType> = vec![
            ActionType::Transaction,
            ActionType::EditAccountStatus,
            ActionType::BalanceAssertion,
            ActionType::EditAccountStatus,
            ActionType::Transaction,
            ActionType::BalanceAssertion,
        ];

        let num_action_types = ActionType::iterator().count();

        action_types_unordered.iter().for_each(|action_type| {
            tested_types.insert(action_type.clone());
        });

        assert_eq!(num_action_types, tested_types.len());

        action_types_unordered.sort();

        let action_types_ordered: Vec<ActionType> = vec![
            ActionType::EditAccountStatus,
            ActionType::EditAccountStatus,
            ActionType::BalanceAssertion,
            ActionType::BalanceAssertion,
            ActionType::Transaction,
            ActionType::Transaction,
        ];

        assert_eq!(action_types_ordered, action_types_unordered);
    }

    #[cfg(feature = "serde-support")]
    #[test]
    fn edit_account_status_serde() {
        use serde_json;

        let json = r#"{
    "account_id": "TestAccount",
    "newstatus": "Open",
    "date": "2020-05-10"
}"#;
        let action: EditAccountStatus = serde_json::from_str(json).unwrap();

        let reference_action = EditAccountStatus::new(
            AccountID::from("TestAccount").unwrap(),
            AccountStatus::Open,
            NaiveDate::from_ymd(2020, 05, 10),
        );

        assert_eq!(action, reference_action);

        insta::assert_json_snapshot!(action);
    }

    #[cfg(feature = "serde-support")]
    #[test]
    fn balance_assertion_serde() {
        use serde_json;

        let json = r#"{
    "account_id": "TestAccount",
    "date": "2020-05-10",
    "expected_balance": {
        "value": "1.0",
        "type_id": "AUD"
    }
}"#;
        let action: BalanceAssertion = serde_json::from_str(json).unwrap();

        let reference_action = BalanceAssertion::new(
            AccountID::from("TestAccount").unwrap(),
            NaiveDate::from_ymd(2020, 05, 10),
            Commodity::from_str("1.0 AUD").unwrap(),
        );

        assert_eq!(action, reference_action);

        insta::assert_json_snapshot!(action);
    }

    #[cfg(feature = "serde-support")]
    #[test]
    fn transaction_serde() {
        use serde_json;

        let json = r#"{
    "description": "TestTransaction",
    "date": "2020-05-10",
    "elements": [
        {
            "account_id": "TestAccount1",
            "amount": {
                "value": "-1.0",
                "type_id": "AUD"
            }
        },
        {
            "account_id": "TestAccount2"
        }
    ]  
}"#;
        let action: Transaction = serde_json::from_str(json).unwrap();

        let reference_action = Transaction::new_simple(
            Some("TestTransaction"),
            NaiveDate::from_ymd(2020, 05, 10),
            AccountID::from("TestAccount1").unwrap(),
            AccountID::from("TestAccount2").unwrap(),
            Commodity::from_str("1.0 AUD").unwrap(),
            None,
        );

        assert_eq!(action, reference_action);

        insta::assert_json_snapshot!(action);
    }
}