refactored code.

This commit is contained in:
didier amyot committed 2020-09-10 14:39:26 -04:00
1 parent ed7f4ef668
commit cfb63cb473
3 files changed
+39 -233

No files matched your search

+32 -31
View File
@@ -1,6 +1,7 @@
(ns skirmish.ledger.test-core
(:require [midje.sweet :refer :all]
[skirmish.ledger.data-layer :as data-layer]
[skirmish.ledger.controllers :as controllers]
[skirmish.ledger.accounts :refer [create-connection schema]]
[skirmish.rest.util :refer [build-response]]
[skirmish.ledger.core :refer :all]
@@ -45,7 +46,7 @@
;;
(defn create-some-securities [conn]
(let [cs (create-security conn)
(let [cs (controllers/create-security conn)
create-security-1 {:body (edn->stream (get mock-securities 0)) :headers {"accept" "application/edn"}}
create-security-2 {:body (edn->stream (get mock-securities 1)) :headers {"accept" "application/edn"}}
create-security-3 {:body (edn->stream (get mock-securities 2)) :headers {"accept" "application/edn"}}
@@ -58,7 +59,7 @@
(defn create-some-accounts [conn]
(let [es (emit-securities conn)
(let [es (controllers/emit-securities conn)
emit-sec-req-1 {:body (edn->stream [100 "1-sec-1"]) :route-params {:sec-id "sec-1"}}
emit-sec-req-2 {:body (edn->stream [200 "2-sec-1"]) :route-params {:sec-id "sec-1"}}
emit-sec-req-3 {:body (edn->stream [1000 "1-sec-2"]) :route-params {:sec-id "sec-2"}}
@@ -78,7 +79,7 @@
(let [conn (create-conn)
security [:new-currency 100 :currency "currency for test" "rot13" "NC"]
request {:body (edn->stream security) :headers {"accept" "application/edn"}}
resp ((create-security conn) request)]
resp ((controllers/create-security conn) request)]
resp => (spec-check ::ok)
resp => (location? "/api/1/ledger/securities/new-currency")))
@@ -86,8 +87,8 @@
(let [conn (create-conn)
security [:new-currency 100 :currency "currency for test" "rot13" "NC"]
request {:body (edn->stream security) :headers {"accept" "application/edn"}}
resp-1 ((create-security conn) request)
resp-2 ((create-security conn) request)]
resp-1 ((controllers/create-security conn) request)
resp-2 ((controllers/create-security conn) request)]
resp-1 => (spec-check ::ok)
resp-2 => (spec-check ::status-400)))
@@ -98,9 +99,9 @@
(let [conn (create-conn)
security [:new-currency 100 :currency "currency for test" "rot13" "NC"]
create-request {:body (edn->stream security) :headers {"accept" "application/edn"}}
_ ((create-security conn) create-request)
_ ((controllers/create-security conn) create-request)
request {:route-params {:sec-id :new-currency} :headers {"accept" "application/edn"}}
resp ((get-security conn) request)
resp ((controllers/get-security conn) request)
expt {:sec-id :new-currency
:interest-rate 100
:name nil
@@ -115,18 +116,18 @@
(fact "we should not be able to get-security if it does not exists"
(let [conn (create-conn)
request {:route-params {:sec-id :new-currency} :headers {"accept" "application/edn"}}
resp ((get-security conn) request)]
resp ((controllers/get-security conn) request)]
resp => (spec-check ::status-404)
resp => #(-> % :sec-id nil?)))
(fact "we should be able to get a list of all securities"
(let [conn (create-conn)
request {:route-params {} :headers {"accept" "application/edn"}}
empty-res ((get-available-securities conn) request)
empty-res ((controllers/get-available-securities conn) request)
new-currency [:new-currency 100 :currency "currency for test" "rot13" "NC"]
create-security-1 {:body (edn->stream new-currency) :headers {"accept" "application/edn"}}
_ ((create-security conn) create-security-1)
one-res ((get-available-securities conn) request)]
_ ((controllers/create-security conn) create-security-1)
one-res ((controllers/get-available-securities conn) request)]
empty-res => (spec-check ::ok)
one-res => (spec-check ::ok)
(body empty-res) => {:securities []}
@@ -137,7 +138,7 @@
_ (d/delete-database mem-url)
conn (create-connection mem-url true)
request {:route-params {} :headers {"accept" "application/edn"}}
resp ((get-available-securities conn) request)]
resp ((controllers/get-available-securities conn) request)]
(->> resp body :securities (map :sec-id) sort) => [:apac :apax :apbm :apdr :apmh :dapm])))
@@ -147,27 +148,27 @@
req-1 {:body (edn->stream [10 :not-a-string]) :route-params {:sec-id "sec-1"} :headers {"accept" "application/edn"}}
req-2 {:body (edn->stream [-10 "foo"]) :route-params {:sec-id "sec-1"} :headers {"accept" "application/edn"}}
req-3 {:body (edn->stream [90 "account-id"]) :route-params {:sec-id "invalid-sec-id"} :headers {"accept" "application/edn"}}]
((emit-securities conn) req-1) => (spec-check ::status-400)
((emit-securities conn) req-2) => (spec-check ::status-400)
((emit-securities conn) req-3) => (spec-check ::status-404)))
((controllers/emit-securities conn) req-1) => (spec-check ::status-400)
((controllers/emit-securities conn) req-2) => (spec-check ::status-400)
((controllers/emit-securities conn) req-3) => (spec-check ::status-404)))
(fact "should create new account if targeted account does not exists"
(let [conn (create-some-securities (create-conn))
get-account-req {:route-params {:account-id "new-account-id"} :headers {"accept" "application/edn"}}
emit-sec-req {:body (edn->stream [90 "new-account-id"]) :route-params {:sec-id "sec-1"} :headers {"accept" "application/edn"}}]
((get-account conn) get-account-req) => (spec-check ::status-404)
((emit-securities conn) emit-sec-req) => (spec-check ::ok)
(body ((get-account conn) get-account-req)) => #(-> % :account :amount (= 90))))
((controllers/get-account conn) get-account-req) => (spec-check ::status-404)
((controllers/emit-securities conn) emit-sec-req) => (spec-check ::ok)
(body ((controllers/get-account conn) get-account-req)) => #(-> % :account :amount (= 90))))
(fact "should add to account if targeted account exists"
(let [conn (create-some-securities (create-conn))
get-account-req {:route-params {:account-id "new-account-id"} :headers {"accept" "application/edn"}}
emit-sec-req-1 {:body (edn->stream [90 "new-account-id"]) :route-params {:sec-id "sec-1"} :headers {"accept" "application/edn"}}
emit-sec-req-2 {:body (edn->stream [100 "new-account-id"]) :route-params {:sec-id "sec-1"} :headers {"accept" "application/edn"}}]
((get-account conn) get-account-req) => (spec-check ::status-404)
((emit-securities conn) emit-sec-req-1) => (spec-check ::ok)
((emit-securities conn) emit-sec-req-2) => (spec-check ::ok)
(body ((get-account conn) get-account-req)) => #(-> % :account :amount (= 190)))))
((controllers/get-account conn) get-account-req) => (spec-check ::status-404)
((controllers/emit-securities conn) emit-sec-req-1) => (spec-check ::ok)
((controllers/emit-securities conn) emit-sec-req-2) => (spec-check ::ok)
(body ((controllers/get-account conn) get-account-req)) => #(-> % :account :amount (= 190)))))
(facts "about accounts"
@@ -178,28 +179,28 @@
{:account-public-key "1-sec-2" :url "http://localhost:1604/api/1/ledger/accounts/1-sec-2"}
{:account-public-key "2-sec-1" :url "http://localhost:1604/api/1/ledger/accounts/2-sec-1"}
{:account-public-key "2-sec-2" :url "http://localhost:1604/api/1/ledger/accounts/2-sec-2"}]]
((get-available-accounts conn) req) => (spec-check ::ok)
(-> ((get-available-accounts conn) req) body :accounts sec->hash-set) => (sec->hash-set expt)))
((controllers/get-available-accounts conn) req) => (spec-check ::ok)
(-> ((controllers/get-available-accounts conn) req) body :accounts sec->hash-set) => (sec->hash-set expt)))
(fact "we should be able to get account"
(let [conn (create-some-securities-and-accounts (create-conn))
req {:route-params {:account-id "2-sec-1"} :headers {"accept" "application/edn"}}]
((get-account conn) req) => (spec-check ::ok)
(body ((get-account conn) req)) => {:account {:account-public-key "2-sec-1" :amount 200 :sec-id :sec-1}
:sec-url "/api/1/ledger/securities/sec-1"}))
((controllers/get-account conn) req) => (spec-check ::ok)
(body ((controllers/get-account conn) req)) => {:account {:account-public-key "2-sec-1" :amount 200 :sec-id :sec-1}
:sec-url "/api/1/ledger/securities/sec-1"}))
(fact "the interest should be payed when getting the account"
(let [conn (create-some-securities-and-accounts (create-conn))
req {:route-params {:account-id "2-sec-1"} :headers {"accept" "application/edn"}}]
((get-account conn) req) => (spec-check ::ok)
(body ((get-account conn) req)) => {:account {:account-public-key "2-sec-1" :amount 200 :sec-id :sec-1}
:sec-url "/api/1/ledger/securities/sec-1"}))
((controllers/get-account conn) req) => (spec-check ::ok)
(body ((controllers/get-account conn) req)) => {:account {:account-public-key "2-sec-1" :amount 200 :sec-id :sec-1}
:sec-url "/api/1/ledger/securities/sec-1"}))
(fact "we should not get an account if it does not exists"
(let [conn (create-some-securities-and-accounts (create-conn))
req {:route-params {:account-id "2-sec-10"} :headers {"accept" "application/edn"}}]
((get-account conn) req) => (spec-check ::status-404))))
((controllers/get-account conn) req) => (spec-check ::status-404))))
(facts "about interest computation"