Skip to content

Commit f083d3b

Browse files
author
Timothy Washington
committed
-- adding a create-account helper function
1 parent d9757bb commit f083d3b

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

src/bkell/domain/account.clj

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[bkell.domain.helper :as hlp]))
55

66

7-
(declare find-account-by-name)
7+
(declare find-account-by-name
8+
add-account)
89

910
(defn no-duplicate-account? [ds group-name account]
1011
(let [a (find-account-by-name ds group-name (:name account))]
@@ -18,6 +19,20 @@
1819

1920
(every? true? results)))
2021

22+
(def account-type-mappings {:expense :debit
23+
:revenue :credit
24+
:liability :credit
25+
:asset :debit
26+
:capital :credit})
27+
28+
(defn create-account [ds group-name aname atype]
29+
{:pre [(some #{atype} (keys account-type-mappings))]}
30+
31+
(let [account {:name aname
32+
:type atype
33+
:counterWeight (atype account-type-mappings)}]
34+
(add-account ds group-name account)))
35+
2136
(defn add-account [ds group-name account]
2237
{:pre [(no-duplicate-account? ds group-name account)]}
2338

@@ -67,6 +82,14 @@
6782
:group/name gname}}}))
6883

6984

85+
(defn update-account [ds id account]
86+
87+
;; can only update if no other entries point to it
88+
;; can only update :name or :type (:counterWeight is automatically changed)
89+
90+
)
91+
92+
7093
(comment
7194

7295
(acc/find-account-by-id ds "webkell" 123)

src/bkell/domain/helper.clj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
[clojure.set :as set]
44
[slingshot.slingshot :refer [try+ throw+]]))
55

6-
(defn find-by-id [ds id]
7-
(adi/select ds id :first))
6+
(defn find-core [args]
7+
(apply adi/select args))
8+
9+
(defn find-by-id
10+
([ds id] (find-by-id ds id [:first]))
11+
([ds id opts]
12+
(let [args [ds id]
13+
argsF (set/union args opts)]
14+
15+
(find-core argsF))))
816

917
(defn find-country-by-code
1018
([ds code]
@@ -14,7 +22,7 @@
1422
(let [args [ds {:country {:id code}}]
1523
argsF (set/union args opts)]
1624

17-
(apply adi/select argsF))))
25+
(find-core argsF))))
1826

1927
(defn find-currency-by-code
2028
([ds code]
@@ -24,7 +32,7 @@
2432
(let [args [ds {:currency {:id code}}]
2533
argsF (set/union args opts)]
2634

27-
(apply adi/select argsF))))
35+
(find-core argsF))))
2836

2937
(defn generate-nominal-group [ds gname uname country currency]
3038

test/bkell/domain/account_test.clj

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
(defn account-generator []
2121
(gen/hash-map :name gen/string-ascii
22-
:type (gen/elements [:asset :liability :revenue :expense])
22+
:type (gen/elements [:asset :liability :revenue :expense :capital])
2323
:counterWeight (gen/elements [:debit :credit])))
2424

25-
(defspec test-add-an-account
25+
(defspec test-add-account
2626
10
2727
(prop/for-all [account (account-generator)]
2828

@@ -36,6 +36,38 @@
3636

3737
(-> result nil? not)))))
3838

39+
(defspec test-create-account
40+
5
41+
(prop/for-all [_ gen/int]
42+
43+
(let [group-name "webkell"
44+
ds (hlp/setup-db!)
45+
46+
aname "fubar"
47+
atype :asset
48+
result (acc/create-account ds group-name aname atype)]
49+
50+
(and (set/subset? #{:counterWeight :name :type}
51+
(-> result first :book :accounts first keys set))
52+
53+
(-> result nil? not)))))
54+
55+
(defspec test-create-account-bad-type
56+
5
57+
(prop/for-all [_ gen/int]
58+
59+
(let [group-name "webkell"
60+
ds (hlp/setup-db!)
61+
62+
aname "fubar"
63+
atype :fubar
64+
result (try+ (acc/create-account ds group-name aname atype)
65+
(catch AssertionError e &throw-context))]
66+
67+
(= '(:cause :message :object :stack-trace :throwable)
68+
(sort (keys result))))))
69+
70+
3971
(defspec test-restrict-duplicate-account
4072
10
4173
(prop/for-all [account (account-generator)]

0 commit comments

Comments
 (0)