implemented so many things...
This commit is contained in:
1 parent
fd843a5425
commit
bce64fc8f6
24 files changed
+734
-59
No files matched your search
@@ -1,2 +1,183 @@
|
||||
(ns skirmish.ledger.test-core
|
||||
(:require [clojure.test :refer :all]))
|
||||
(:require [midje.sweet :refer :all]
|
||||
[datomic.api :as d]
|
||||
[clojure.edn]
|
||||
[skirmish.rest.util :refer [build-response]]
|
||||
[skirmish.ledger.core :refer :all]
|
||||
[skirmish.rest.util :refer [edn->stream]]
|
||||
[clojure.spec.alpha :as s]
|
||||
[skirmish.ledger.accounts :refer [create-connection schema]]))
|
||||
|
||||
(defn spec-check [spec]
|
||||
#(s/valid? spec %))
|
||||
|
||||
(defn body [resp]
|
||||
(-> resp :body clojure.edn/read-string))
|
||||
|
||||
(s/def ::ok #(<= 200 (:status %) 299))
|
||||
|
||||
(s/def ::status-404 #(= (:status %) 404))
|
||||
|
||||
(s/def ::status-400 #(= (:status %) 400))
|
||||
|
||||
|
||||
(defn create-conn []
|
||||
(let [mem-url "datomic:mem://skirmish"]
|
||||
(d/delete-database mem-url)
|
||||
(create-connection mem-url)))
|
||||
|
||||
|
||||
|
||||
(defn location? [path]
|
||||
#(-> % :headers (get "location") (clojure.string/ends-with? path)))
|
||||
|
||||
|
||||
(def sec->hash-set #(apply hash-set %))
|
||||
|
||||
|
||||
(defn create-some-securities [conn]
|
||||
(let [cs (create-security conn)
|
||||
create-security-1 {:body (edn->stream [:sec-1 100]) :headers {"accept" "application/edn"}}
|
||||
create-security-2 {:body (edn->stream [:sec-2 200]) :headers {"accept" "application/edn"}}
|
||||
create-security-3 {:body (edn->stream [:sec-3 300]) :headers {"accept" "application/edn"}}
|
||||
create-security-4 {:body (edn->stream [:zimbabwean-dollar 1000000]) :headers {"accept" "application/edn"}}]
|
||||
(->> create-security-1 cs (s/assert ::ok))
|
||||
(->> create-security-2 cs (s/assert ::ok))
|
||||
(->> create-security-3 cs (s/assert ::ok))
|
||||
(->> create-security-4 cs (s/assert ::ok))
|
||||
conn))
|
||||
|
||||
|
||||
(defn create-some-accounts [conn]
|
||||
(let [es (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"}}
|
||||
emit-sec-req-4 {:body (edn->stream [2000 "2-sec-2"]) :route-params {:sec-id "sec-2"}}]
|
||||
(->> emit-sec-req-1 es (s/assert ::ok))
|
||||
(->> emit-sec-req-2 es (s/assert ::ok))
|
||||
(->> emit-sec-req-3 es (s/assert ::ok))
|
||||
(->> emit-sec-req-4 es (s/assert ::ok))
|
||||
conn))
|
||||
|
||||
|
||||
(def create-some-securities-and-accounts (comp create-some-accounts create-some-securities))
|
||||
|
||||
|
||||
(facts "about the securities"
|
||||
(fact "We should be able to create a security"
|
||||
(let [conn (create-conn)
|
||||
security [:new-currency 100]
|
||||
request {:body (edn->stream security) :headers {"accept" "application/edn"}}
|
||||
resp ((create-security conn) request)]
|
||||
resp => (spec-check ::ok)
|
||||
resp => (location? "/api/1/ledger/securities/new-currency"))) ;;
|
||||
|
||||
(fact "We should not be able to create a security that already exists"
|
||||
(let [conn (create-conn)
|
||||
security [:new-currency 100]
|
||||
request {:body (edn->stream security) :headers {"accept" "application/edn"}}
|
||||
resp-1 ((create-security conn) request)
|
||||
resp-2 ((create-security conn) request)]
|
||||
resp-1 => (spec-check ::ok)
|
||||
resp-2 => (spec-check ::status-400)))
|
||||
|
||||
(fact "We should be able to update a security when it exists"
|
||||
:todo => :todo)
|
||||
|
||||
(fact "we should be able to get-security if it does exists"
|
||||
(let [conn (create-conn)
|
||||
security [:new-currency 100]
|
||||
create-request {:body (edn->stream security) :headers {"accept" "application/edn"}}
|
||||
_ ((create-security conn) create-request)
|
||||
request {:route-params {:sec-id :new-currency} :headers {"accept" "application/edn"}}
|
||||
resp ((get-security conn) request)]
|
||||
resp => (spec-check ::ok)
|
||||
(body resp) => {:sec-id :new-currency :interest-rate 100}))
|
||||
|
||||
(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 => (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)
|
||||
create-security-1 {:body (edn->stream [:new-currency 100]) :headers {"accept" "application/edn"}}
|
||||
_ ((create-security conn) create-security-1)
|
||||
one-res ((get-available-securities conn) request)]
|
||||
empty-res => (spec-check ::ok)
|
||||
one-res => (spec-check ::ok)
|
||||
(body empty-res) => {:securities []}
|
||||
(body one-res) => {:securities [{:sec-id :new-currency :url "/api/1/ledger/securities/new-currency"}]})))
|
||||
|
||||
|
||||
(facts "about the emitting securities"
|
||||
(fact "should not work if invalid request"
|
||||
(let [conn (create-some-securities (create-conn))
|
||||
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)))
|
||||
|
||||
(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))))
|
||||
|
||||
(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)))))
|
||||
|
||||
|
||||
(facts "about accounts"
|
||||
(fact "we should be able to get list of all accounts"
|
||||
(let [conn (create-some-securities-and-accounts (create-conn))
|
||||
req {:route-params {} :headers {"accept" "application/edn"}}
|
||||
expt [{:account-public-key "1-sec-1" :url "/api/1/ledger/accounts/1-sec-1"}
|
||||
{:account-public-key "1-sec-2" :url "/api/1/ledger/accounts/1-sec-2"}
|
||||
{:account-public-key "2-sec-1" :url "/api/1/ledger/accounts/2-sec-1"}
|
||||
{:account-public-key "2-sec-2" :url "/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)))
|
||||
|
||||
(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/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/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))))
|
||||
|
||||
|
||||
|
||||
;; TODO
|
||||
;; 4. transfer sec from one account to the other.
|
||||
;; 5. account for interest
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
(ns skirmish.test-util
|
||||
(:require [midje.sweet :refer :all]
|
||||
[skirmish.util :refer :all]))
|
||||
|
||||
|
||||
;; midje testing
|
||||
(facts "About events overlapping"
|
||||
(let [evt-1 #:calendar.event{:start 10 :end 15}
|
||||
evt-2 #:calendar.event{:start 12 :end 17}
|
||||
evt-3 #:calendar.event{:start 15 :end 25}
|
||||
|
||||
evt-4 #:calendar.event{:start 30 :end 40}
|
||||
evt-5 #:calendar.event{:start 33 :end 34}]
|
||||
|
||||
(fact "about edge cases"
|
||||
(get-overlapping-events nil) => empty?
|
||||
(get-overlapping-events []) => empty?)
|
||||
|
||||
(fact "about regular situations"
|
||||
(get-overlapping-events [evt-1 evt-2]) => [[evt-1 evt-2]]
|
||||
(get-overlapping-events [evt-1 evt-2 evt-3]) => [[evt-1 evt-2] [evt-2 evt-3]]
|
||||
(get-overlapping-events [evt-4 evt-5]) => [[evt-4 evt-5]])))
|
||||
|
||||
;;
|
||||
Reference in new issue
Block a user