 ; 2016-03-23     103  Groceries Checking
 ; 2016-03-30     104  Groceries Checking
 (deftest update-a-transaction-short-circuit-updates
-  (let [context (realize short-circuit-context)
-        [checking] (:accounts context)
-        [_t1 _t2 t3] (:transactions context)
-        updated (-> t3
-                    (rename-keys {:transaction-date :original-transaction-date})
-                    (assoc :transaction-date (t/local-date 2016 3 8)))
+  (let [ctx (realize short-circuit-context)
+        checking (find-account ctx "Checking")
+        trx (find-transaction ctx (t/local-date 2016 3 16) "Kroger")
+        updated (change-date trx (t/local-date 2016 3 8))
         update-calls (atom {})]
     (binding [update-item transactions/update-item-index-and-balance]
       (with-redefs [transactions/update-item-index-and-balance (fn [item]

                                 :quantity 103}]}]})))

(deftest update-a-transaction-change-account
  (let [context (realize change-account-context)
        [rent
         groceries] (find-accounts context "Rent" "Groceries")
        t3 (find-transaction context (t/local-date 2016 3 16) "Kroger")
        _ (transactions/update (assoc-in t3 [:items 0 :account-id] (:id rent)))
        actual-groceries (map #(select-keys % [:index :quantity :balance])
                              (items-by-account (:id groceries)))
        actual-rent (map #(select-keys % [:index :quantity :balance])
                         (items-by-account (:id rent)))
        expected-groceries [{:index 1
                             :quantity 103M
                             :balance 204M}
                            {:index 0
                             :quantity 101M
                             :balance 101M}]
        expected-rent [{:index 0
                        :quantity 102M
                        :balance 102M}]]
    (testing "Accounts have the correct items"
      (is (= expected-groceries actual-groceries)
          "Groceries should have the correct items after update")
      (is (= expected-rent actual-rent)
          "Rent chould have the correct items after update"))
    (assert-account-quantities groceries 204M rent 102M)))

(def change-action-context

(deftest update-a-transaction-change-action
  (let [context (realize change-action-context)
        [checking _ groceries] (:accounts context)
        result (-> (find-transaction context (t/local-date 2016 3 16) "Kroger")
                   (update-items {(:id groceries) {:action :credit}
                                  (:id checking) {:action :debit}})
                   transactions/update)]
    (is (valid? result))
    (is (seq-of-maps-like? [{:index 2
                             :quantity 101M
                             :balance 192M}
                            {:index 1
                             :quantity 12M
                             :balance 91M}
                            {:index 0
                             :quantity 103M
                             :balance 103M}]
                           (items-by-account (:id groceries)))
        "Expected the groceries items to be updated")
    (assert-account-quantities groceries 192M checking 808M)))

(def add-remove-item-context
