tried (and failed) to add cljs to my test plan
This commit is contained in:
1 parent
6867f6ffbc
commit
f5e8ffd82d
9 files changed
+12
No files matched your search
@@ -0,0 +1,30 @@
|
||||
(ns skirmish.cli.benchmark-test
|
||||
(:require [skirmish.cli.benchmark :refer :all]
|
||||
[midje.sweet :refer :all]
|
||||
[clojure.core.async :as async]))
|
||||
|
||||
(facts "creating simple orders"
|
||||
(fact "buy order"
|
||||
(let [buy-order (create-requests-payload-for-security 666 (str :equity/ibm) 1)
|
||||
expt {:benchmark/id 1 :price 2922 :quantity 2476 :security ":equity/ibm" :type :buy}]
|
||||
buy-order => expt));;
|
||||
(fact "sell order"
|
||||
(let [buy-order (create-requests-payload-for-security 667 (str :equity/ibm) 2)
|
||||
expt {:benchmark/id 2 :price 2740 :quantity 2516 :security ":equity/ibm" :type :sell}]
|
||||
buy-order => expt)))
|
||||
|
||||
(facts "the utility functions"
|
||||
(fact "no-more-then-n-simultaneously"
|
||||
(let [simultatious (atom [])
|
||||
input-chan (async/to-chan (range 10))
|
||||
sleep-time #(+ (mod (+ (* 5142 %) 8765) 200) 50)
|
||||
f (fn [i]
|
||||
(swap! simultatious #(conj % {:id i :status :start :value 1}))
|
||||
(Thread/sleep (sleep-time i))
|
||||
(swap! simultatious #(conj % {:id i :status :end :value -1}))
|
||||
i)
|
||||
res (async/<!! (async/into [] (no-more-then-n-simultaneously f input-chan 3)))
|
||||
simultatious-over-time (reductions #(+ %1 (:value %2)) 0 @simultatious)]
|
||||
(apply max simultatious-over-time) => 3
|
||||
(first simultatious-over-time) => 0
|
||||
(last simultatious-over-time) => 0)))
|
||||
@@ -0,0 +1,93 @@
|
||||
(ns skirmish.exchange.exchange-test
|
||||
(:import [skirmish.PersistentSortedList])
|
||||
(:require [midje.sweet :refer :all]
|
||||
[orchestra.spec.test :as st]
|
||||
[skirmish.exchange.exchange :refer :all]
|
||||
[skirmish.exchange.match :refer [create-buy-book
|
||||
create-sell-book
|
||||
match-order-in-book]]))
|
||||
|
||||
(st/instrument)
|
||||
|
||||
(def PersistentSortedList? (comp #(= % skirmish.PersistentSortedList) type))
|
||||
|
||||
(facts "the exchange as an whole"
|
||||
(fact "process-sell-order should work when book empty"
|
||||
(let [book empty-book
|
||||
order {:type :sell :quantity 10 :price 2 :book.order/id 1}
|
||||
expt-new-sell-book [order]
|
||||
[{new-sell-book :sell new-buy-book :buy} new-order new-trades] (process-sell-order book order)]
|
||||
new-sell-book => expt-new-sell-book
|
||||
new-buy-book => empty?
|
||||
new-order => order
|
||||
new-trades => empty?))
|
||||
|
||||
(fact "process-sell-order should work when there is a buy order that matches"
|
||||
(let [sell-book (create-sell-book)
|
||||
buy-book (create-buy-book {:type :buy :quantity 10 :price 3 :book.order/id 2})
|
||||
book {:sell sell-book :buy buy-book :id 0}
|
||||
order {:type :sell :quantity 4 :price 2 :book.order/id 3}
|
||||
expt-new-buy-book [{:price 3 :quantity 6 :type :buy :book.order/id 2}]
|
||||
expt-new-trades [#:book.order{:book-id 2 :order-id 3 :price 3 :quantity 4 :type :buy}]
|
||||
[{new-sell-book :sell new-buy-book :buy} new-order new-trades] (process-order book order)]
|
||||
new-sell-book => empty?
|
||||
new-buy-book => expt-new-buy-book
|
||||
new-order => nil?
|
||||
new-trades => expt-new-trades))
|
||||
|
||||
(fact "process-buy-order should work when book empty"
|
||||
(let [book empty-book
|
||||
order {:type :buy :quantity 10 :price 2 :book.order/id 1}
|
||||
expt-new-sell-book [order]
|
||||
[{new-sell-book :sell new-buy-book :buy} new-order new-trades] (process-order book order)]
|
||||
new-sell-book => empty?
|
||||
new-buy-book => expt-new-sell-book
|
||||
new-order => order
|
||||
new-trades => empty?))
|
||||
|
||||
(fact "process-buy-order should work when there is a buy order that matches"
|
||||
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 3 :book.order/id 2})
|
||||
buy-book (create-buy-book)
|
||||
book {:sell sell-book :buy buy-book :id 0}
|
||||
order {:type :buy :quantity 4 :price 4 :book.order/id 3}
|
||||
expt-new-sell-book [{:price 3 :quantity 6 :type :sell :book.order/id 2}]
|
||||
expt-new-trades [#:book.order{:book-id 2 :order-id 3 :price 3 :quantity 4 :type :sell}]
|
||||
[{new-sell-book :sell new-buy-book :buy} new-order new-trades] (process-order book order)]
|
||||
new-sell-book => expt-new-sell-book
|
||||
new-order => nil?
|
||||
new-trades => expt-new-trades))
|
||||
|
||||
(fact "process-order total dance"
|
||||
(let [book empty-book
|
||||
order-1 {:type :buy :quantity 10 :price 100 :book.order/id 7}
|
||||
order-2 {:type :buy :quantity 15 :price 99 :book.order/id 8}
|
||||
order-3 {:type :sell :quantity 17 :price 99 :book.order/id 9}
|
||||
order-4 {:type :sell :quantity 1 :price 102 :book.order/id 10}
|
||||
expt-book-4 {:buy [{:price 99 :quantity 8 :type :buy :book.order/id 8}]
|
||||
:sell [{:price 102 :quantity 1 :type :sell :book.order/id 10}]
|
||||
:id 4}
|
||||
[book-1 _ _] (process-order book order-1)
|
||||
[book-2 _ _] (process-order book-1 order-2)
|
||||
[book-3 _ _] (process-order book-2 order-3)
|
||||
[book-4 _ _] (process-order book-3 order-4)]
|
||||
book-4 => expt-book-4)))
|
||||
|
||||
(facts "get-processed-orders"
|
||||
(fact "bug numero uno"
|
||||
(let [sell-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 116}
|
||||
{:type :buy :quantity 8 :price 13 :book.order/id 117})
|
||||
order {:type :sell :quantity 21 :price 13 :book.order/id 118}
|
||||
[trades new-book leftover-order] (match-order-in-book sell-book order)
|
||||
processed-order (get-processed-orders order leftover-order trades sell-book)]
|
||||
processed-order => seq));;;
|
||||
(fact "bug numero dos"
|
||||
(let [order {:type :sell, :quantity 10, :price 2, :book.order/id 1}
|
||||
leftover-order nil
|
||||
trades [#:book.order{:type :buy, :quantity 10, :price 3, :order-id 1, :book-id 0}]
|
||||
book {:sell (create-sell-book)
|
||||
:buy (create-buy-book {:type :buy, :quantity 15, :price 3, :book.order/id 0})}
|
||||
ret (get-processed-orders order leftover-order trades (:buy book))]
|
||||
(count ret) => 3)))
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
(ns skirmish.exchange.match-test
|
||||
(:require [midje.sweet :refer :all]
|
||||
[skirmish.exchange.match :refer :all]))
|
||||
|
||||
|
||||
(def PersistentSortedList? (comp #(= % skirmish.PersistentSortedList) type))
|
||||
|
||||
|
||||
(facts "matching buy order in sell book"
|
||||
(fact "Simplest case I."
|
||||
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 101})
|
||||
order {:type :buy :quantity 10 :price 2 :book.order/id 102}
|
||||
[trades new-book] (match-order-in-book sell-book order)]
|
||||
new-book => empty?
|
||||
trades => [#:book.order{:quantity 10 :price 2 :book-id 101 :order-id 102 :type :sell}]))
|
||||
|
||||
(fact "Simplest case II."
|
||||
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 103})
|
||||
order {:type :buy :quantity 10 :price 3 :book.order/id 104}
|
||||
[trades new-book] (match-order-in-book sell-book order)]
|
||||
new-book => empty?
|
||||
trades => [#:book.order{:quantity 10 :price 2 :book-id 103 :order-id 104 :type :sell}]))
|
||||
(fact "Simplest case III."
|
||||
(let [sell-book (create-sell-book {:type :sell :quantity 10 :price 2 :book.order/id 105})
|
||||
order {:type :buy :quantity 8 :price 2 :book.order/id 106}
|
||||
[trades new-book] (match-order-in-book sell-book order)]
|
||||
new-book => [{:type :sell :quantity 2 :price 2 :book.order/id 105}]
|
||||
trades => [#:book.order{:quantity 8 :price 2 :book-id 105 :order-id 106 :type :sell}]))
|
||||
(fact "Simplest case IV."
|
||||
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 107} {:type :sell :quantity 8 :price 13 :book.order/id 108})
|
||||
order {:type :buy :quantity 10 :price 13 :book.order/id 109}
|
||||
[trades new-book] (match-order-in-book sell-book order)]
|
||||
new-book => empty?
|
||||
trades => [#:book.order{:quantity 2 :price 12 :book-id 107 :order-id 109 :type :sell}
|
||||
#:book.order{:quantity 8 :price 13 :book-id 108 :order-id 109 :type :sell}]))
|
||||
(fact "Simplest case V."
|
||||
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 113} {:type :sell :quantity 8 :price 13 :book.order/id 114})
|
||||
order {:type :buy :quantity 9 :price 13 :book.order/id 115}
|
||||
[trades new-book] (match-order-in-book sell-book order)]
|
||||
new-book => (create-sell-book {:type :sell :quantity 1 :price 13 :book.order/id 114})
|
||||
trades => [#:book.order{:quantity 2 :price 12 :book-id 113 :order-id 115 :type :sell}
|
||||
#:book.order{:quantity 7 :price 13 :book-id 114 :order-id 115 :type :sell}]))
|
||||
(fact "Simplest case VI."
|
||||
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 116} {:type :sell :quantity 8 :price 13 :book.order/id 117})
|
||||
order {:type :buy :quantity 21 :price 13 :book.order/id 118}
|
||||
[trades new-book leftover-order] (match-order-in-book sell-book order)]
|
||||
new-book => empty?
|
||||
trades => [#:book.order{:quantity 2 :price 12 :book-id 116 :order-id 118 :type :sell}
|
||||
#:book.order{:quantity 8 :price 13 :book-id 117 :order-id 118 :type :sell}]
|
||||
leftover-order => {:type :buy :quantity 11 :price 13 :book.order/id 118}))
|
||||
(fact "Simplest case VII."
|
||||
(let [sell-book (create-sell-book {:type :sell :quantity 2 :price 12 :book.order/id 116} {:type :sell :quantity 8 :price 13 :book.order/id 117})
|
||||
order {:type :buy :quantity 21 :price 10 :book.order/id 118}
|
||||
[trades new-book leftover-order] (match-order-in-book sell-book order)]
|
||||
new-book => sell-book
|
||||
trades => empty?
|
||||
leftover-order => order)))
|
||||
|
||||
|
||||
(facts "matching sell order in buy book"
|
||||
(fact "Simplest case I."
|
||||
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 2 :book.order/id 101})
|
||||
order {:type :sell :quantity 10 :price 2 :book.order/id 102}
|
||||
[trades new-book] (match-order-in-book buy-book order)]
|
||||
new-book => empty?
|
||||
trades => [#:book.order{:quantity 10 :price 2 :book-id 101 :order-id 102 :type :buy}]))
|
||||
|
||||
(fact "Simplest case II."
|
||||
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 4 :book.order/id 103})
|
||||
order {:type :sell :quantity 10 :price 3 :book.order/id 104}
|
||||
[trades new-book] (match-order-in-book buy-book order)]
|
||||
new-book => []
|
||||
trades => [#:book.order{:quantity 10 :price 4 :book-id 103 :order-id 104 :type :buy}]))
|
||||
|
||||
(fact "Simplest case III."
|
||||
(let [buy-book (create-buy-book {:type :buy :quantity 10 :price 2 :book.order/id 105})
|
||||
order {:type :sell :quantity 8 :price 2 :book.order/id 106}
|
||||
[trades new-book] (match-order-in-book buy-book order)]
|
||||
new-book => [{:type :buy :quantity 2 :price 2 :book.order/id 105}]
|
||||
trades => [#:book.order{:quantity 8 :price 2 :book-id 105 :order-id 106 :type :buy}]))
|
||||
|
||||
(fact "Simplest case IV."
|
||||
(let [buy-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 107}
|
||||
{:type :buy :quantity 8 :price 13 :book.order/id 108})
|
||||
order {:type :sell :quantity 10 :price 13 :book.order/id 109}
|
||||
[trades new-book] (match-order-in-book buy-book order)]
|
||||
new-book => []
|
||||
trades => [#:book.order{:quantity 2 :price 14 :book-id 107 :order-id 109 :type :buy}
|
||||
#:book.order{:quantity 8 :price 13 :book-id 108 :order-id 109 :type :buy}]))
|
||||
|
||||
(fact "Simplest case V."
|
||||
(let [buy-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 113}
|
||||
{:type :buy :quantity 8 :price 13 :book.order/id 114})
|
||||
order {:type :sell :quantity 9 :price 13 :book.order/id 115}
|
||||
[trades new-book] (match-order-in-book buy-book order)]
|
||||
new-book => (create-buy-book {:type :buy :quantity 1 :price 13 :book.order/id 114})
|
||||
trades => [#:book.order{:quantity 2 :price 14 :book-id 113 :order-id 115 :type :buy}
|
||||
#:book.order{:quantity 7 :price 13 :book-id 114 :order-id 115 :type :buy}]))
|
||||
|
||||
(fact "Simplest case VI."
|
||||
(let [sell-book (create-buy-book {:type :buy :quantity 2 :price 14 :book.order/id 116}
|
||||
{:type :buy :quantity 8 :price 13 :book.order/id 117})
|
||||
order {:type :sell :quantity 21 :price 13 :book.order/id 118}
|
||||
[trades new-book leftover-order] (match-order-in-book sell-book order)]
|
||||
new-book => empty?
|
||||
trades => [#:book.order{:quantity 2 :price 14 :book-id 116 :order-id 118 :type :buy}
|
||||
#:book.order{:quantity 8 :price 13 :book-id 117 :order-id 118 :type :buy}]
|
||||
leftover-order => {:type :sell :quantity 11 :price 13 :book.order/id 118})))
|
||||
|
||||
(facts "We should write tests for each bug found"
|
||||
(fact "book type should always be book.PersistentSortedList"
|
||||
(let [book (create-buy-book)
|
||||
order {:type :sell :quantity 21 :price 13 :book.order/id 118}
|
||||
[_ new-book _] (match-order-in-book book order)]
|
||||
(count new-book) => 0
|
||||
(type new-book) => skirmish.PersistentSortedList)))
|
||||
@@ -0,0 +1,13 @@
|
||||
(ns skirmish.middleware.test-core
|
||||
(:require [midje.sweet :refer :all]
|
||||
[skirmish.middleware.core :refer :all]))
|
||||
|
||||
(defn all? [xf]
|
||||
#(every? true? (sequence xf %)))
|
||||
|
||||
(facts "we should have a async/chan base middleware for developing."
|
||||
(fact "It should exchanges for each security in the config"
|
||||
(let [config {:securities [{:sec-id :molly} {:sec-id :pepper}]}
|
||||
exchanges (create-exchange-apis-for config)]
|
||||
(keys exchanges) => [:molly :pepper]
|
||||
(vals exchanges) => #(every? map? %))))
|
||||
@@ -0,0 +1,43 @@
|
||||
(ns skirmish.rest.util-test
|
||||
(:require [skirmish.rest.util :refer [build-response]]
|
||||
[midje.sweet :refer :all]
|
||||
[clojure.test :refer :all]))
|
||||
|
||||
(defn expt-response [body content-type status]
|
||||
{:body body
|
||||
:headers {"Content-Type" content-type}
|
||||
:status status})
|
||||
|
||||
(facts "the building the response"
|
||||
(fact "building the response in edn"
|
||||
(let [content-type "application/edn"
|
||||
request {:headers {"accept" content-type}}
|
||||
response "foo"
|
||||
expt (expt-response "foo" "application/edn" 200)]
|
||||
(build-response request response) => expt))
|
||||
(fact "building the response in edn"
|
||||
(let [content-type "application/edn"
|
||||
request {:headers {"accept" content-type}}
|
||||
response {:foo [1 2 3]}
|
||||
expt (expt-response "{:foo [1 2 3]}" "application/edn" 200)]
|
||||
(build-response request response) => expt))
|
||||
(fact "building the response in edn"
|
||||
(let [content-type "application/edn,application/json"
|
||||
request {:headers {"accept" content-type}}
|
||||
response {:foo [1 2 3]}
|
||||
expt (expt-response "{:foo [1 2 3]}" "application/edn" 200)]
|
||||
(build-response request response) => expt))
|
||||
(fact "building the response in json"
|
||||
(let [content-type "text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8"
|
||||
request {:headers {"accept" content-type}}
|
||||
response {:foo [1 2 3]}
|
||||
expt (expt-response "{\"foo\":[1,2,3]}" "application/json" 200)]
|
||||
(build-response request response) => expt))
|
||||
(fact "My browser should receive json"
|
||||
(let [content-type "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
||||
request {:headers {"accept" content-type}}
|
||||
response {:foo [1 2 3]}
|
||||
expt (expt-response "{\"foo\":[1,2,3]}" "application/json" 200)]
|
||||
(build-response request response) => expt)))
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
(ns skirmish.test-PersistentSortedList
|
||||
(:require [midje.sweet :refer :all]
|
||||
[skirmish.util :refer [sorted-list sorted-list-by]]))
|
||||
|
||||
|
||||
|
||||
(facts "test-match-buy-order-in-sell-book"
|
||||
(fact "constructing"
|
||||
(map nil? (sorted-list)) => empty?)
|
||||
;; The following are tests that fails...
|
||||
;(vec (sorted-list)) => empty?)
|
||||
(fact "Test ordering."
|
||||
(sorted-list 1 2 3) => (sorted-list 3 2 1)
|
||||
(sorted-list 1 2 2 3) => (sorted-list 3 2 1 2))
|
||||
(fact "count"
|
||||
(count (sorted-list)) => 0
|
||||
(count (sorted-list 1 2 3)) => 3
|
||||
(count (sorted-list 1 2 3 2)) => 4)
|
||||
(fact "rest"
|
||||
(rest (sorted-list 1 2 3)) => (sorted-list 2 3)
|
||||
(rest (sorted-list 1 2 3 2)) => (sorted-list 2 2 3)
|
||||
(rest (sorted-list 1)) => (sorted-list)
|
||||
(rest (sorted-list)) => empty?
|
||||
(str (type (rest (sorted-list)))) => "class skirmish.PersistentSortedList")
|
||||
(fact "next"
|
||||
(next (sorted-list 1 2 3)) => (sorted-list 2 3)
|
||||
(next (sorted-list 1 2 3 2)) => (sorted-list 2 2 3)
|
||||
(next (sorted-list 1)) => nil
|
||||
(next (sorted-list)) => nil)
|
||||
(fact "drop"
|
||||
(drop 2 (sorted-list 1 2 3)) => (sorted-list 3)
|
||||
(drop 3 (sorted-list 1 2 3)) => (list))
|
||||
(fact "empty"
|
||||
(empty (sorted-list 2 3 4)) => (sorted-list)
|
||||
(into (empty (sorted-list-by #(- %2 %1) 2 3 4)) [20 30]) => (sorted-list-by #(- %2 %1) 20 30))
|
||||
(fact "reduce"
|
||||
(reduce + (sorted-list 1 2 3)) => 6
|
||||
(reduce + 10 (sorted-list 1 2 3)) => 16)
|
||||
(fact "peek/pop"
|
||||
(peek (sorted-list 1 2 3)) => 1
|
||||
(peek (sorted-list 1 1 1 2 3)) => 1
|
||||
(peek (sorted-list)) => nil
|
||||
(pop (sorted-list 1 2 3)) => (sorted-list 2 3)
|
||||
(pop (sorted-list 1 1 2 3)) => (sorted-list 1 2 3)
|
||||
(pop (sorted-list 1 1 1 2 3)) => (sorted-list 1 1 2 3)
|
||||
(pop (sorted-list)) => (throws IllegalStateException))
|
||||
|
||||
(fact "count after change"
|
||||
(let [l (sorted-list 1 2 3)]
|
||||
(count (conj l 4)) => 4
|
||||
(count (conj l 3)) => 4
|
||||
(count (cons 3 l)) => 4
|
||||
(count (rest l)) => 2
|
||||
(count (pop l)) => 2))
|
||||
(fact "sorted-list-by"
|
||||
(sorted-list-by > 1 2 3) => [3 2 1]))
|
||||
Reference in new issue
Block a user