Update unit tests to not use core async

This commit is contained in:
Nicholas Kariniemi 2014-10-10 22:02:51 +03:00
parent e581f34afc
commit 87b88f9340

View file

@ -1,8 +1,8 @@
(ns grub.test.unit.state
(:require [grub.state :as state]
[grub.sync :as sync]
[midje.sweet :refer :all]
[hasch.core :as hasch]
[clojure.core.async :as a :refer [<!! >!! chan go]]))
[hasch.core :as hasch]))
(defn hashed-states [& states]
(->> states
@ -10,110 +10,89 @@
:state s}))
(into [])))
(defn states-atom [& states]
(atom (apply hashed-states states)))
(defn <!!? [c]
(let [[v p] (a/alts!! [c (a/timeout 100)])]
v))
(fact "Applies diff and returns empty diff when no server changes"
(let [states (states-atom
(let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}})
msg {:type :diff
:diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
:hash (:hash (first @states))}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states)
(>!! in msg)
(let [response (<!!? out)]
@states => (hashed-states
{:grubs {"1" {:completed false, :text "2 apples"}}, :recipes {}}
{:grubs {"1" {:completed true, :text "2 apples"}}, :recipes {}})
response => {:type :diff
:diff {:grubs {:deleted #{}, :updated nil}
:recipes {:deleted #{}, :updated nil}}
:hash (:hash (last @states))})))
event {:type :diff
:diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
:hash (:hash (first states))
:states states
:shadow (:state (last states))
:client? false}
{:keys [new-states new-shadow out-event]} (state/handle-event event)]
new-states => (hashed-states
{:grubs {"1" {:completed false, :text "2 apples"}}, :recipes {}}
{:grubs {"1" {:completed true, :text "2 apples"}}, :recipes {}})
out-event => {:type :diff
:diff {:grubs {:deleted #{}, :updated nil}
:recipes {:deleted #{}, :updated nil}}
:hash (:hash (last new-states))}))
(fact "Applies diff and returns changes when server has changed"
(let [states (states-atom
(let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}})
msg {:type :diff
:diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
:hash (:hash (first @states))}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states)
(>!! in msg)
(let [response (<!!? out)]
@states => (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}
{:grubs {"1" {:text "2 apples" :completed true}
"2" {:text "3 onions" :completed false}}
:recipes {}})
response => {:type :diff
:diff {:grubs {:deleted #{}
:updated {"2" {:completed false, :text "3 onions"}}}
:recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid {:grubs {"1" {:text "2 apples" :completed true}}
:recipes {}})})))
event {:type :diff
:diff {:grubs {:updated {"1" {:completed true}} :deleted #{}}}
:hash (:hash (first states))
:states states
:shadow sync/empty-state
:client? false}
{:keys [new-states new-shadow out-event]} (state/handle-event event)]
new-states => (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}
{:grubs {"1" {:text "2 apples" :completed true}
"2" {:text "3 onions" :completed false}}
:recipes {}})
out-event => {:type :diff
:diff {:grubs {:deleted #{}
:updated {"2" {:completed false, :text "3 onions"}}}
:recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid {:grubs {"1" {:text "2 apples" :completed true}}
:recipes {}})}))
(fact "Force full sync if client is out of sync"
(let [states (states-atom
(let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}})
msg {:type :diff
:diff {:grubs {:updated {"0" {:completed true}} :deleted #{}}}
:hash (:hash {:grubs {"0" {:text "milk" :completed false}}
:recipes {}})}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states)
(>!! in msg)
(let [response (<!!? out)]
@states => (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}})
response => {:type :full-sync
:state {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}})))
event {:type :diff
:diff {:grubs {:updated {"0" {:completed true}} :deleted #{}}}
:hash (:hash {:grubs {"0" {:text "milk" :completed false}}
:recipes {}})
:states states
:shadow sync/empty-state
:client? false}
{:keys [new-states new-shadow out-event]} (state/handle-event event)]
new-states => nil
out-event => {:type :full-sync
:state {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}}))
(fact "Full sync if client requests it"
(let [states (states-atom
(let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}})
msg {:type :full-sync-request}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states)
(>!! in msg)
(let [response (<!!? out)]
@states => (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}})
response => {:type :full-sync
:state {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}})))
event {:type :full-sync-request
:states states}
{:keys [new-states new-shadow out-event]} (state/handle-event event)]
new-states => nil
out-event => {:type :full-sync
:state {:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}}))
(fact "Passes diffs of new states to client"
(let [states (states-atom
(fact "New state - server passes diff to client, does not update shadow"
(let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
@ -123,26 +102,59 @@
"3" {:text "milk" :completed false}}
:recipes {}})
client-state {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
msg {:type :new-state
:state (:state (last @states))}
in (chan 1)
out (chan 1)]
(state/make-server-agent in out states client-state)
(>!! in msg)
(let [response (<!!? out)]
@states => (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}
:recipes {}})
response => {:type :diff
:diff {:grubs {:deleted #{}
:updated {"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}}
:recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid client-state)})))
event {:type :new-state
:state (:state (last states))
:client? false
:states states
:shadow client-state}
{:keys [new-states new-shadow out-event]} (state/handle-event event)]
new-states => (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}
:recipes {}})
new-shadow => nil
out-event => {:type :diff
:diff {:grubs {:deleted #{}
:updated {"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}}
:recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid client-state)}))
(fact "New state - client passes diff to server, does not update shadow"
(let [states (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}
:recipes {}})
shadow {:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
event {:type :new-state
:state (:state (last states))
:client? true
:states states
:shadow shadow}
{:keys [new-states new-shadow out-event]} (state/handle-event event)]
new-states => (hashed-states
{:grubs {"1" {:text "2 apples" :completed false}} :recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}}
:recipes {}}
{:grubs {"1" {:text "2 apples" :completed false}
"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}
:recipes {}})
new-shadow => nil
out-event => {:type :diff
:diff {:grubs {:deleted #{}
:updated {"2" {:text "3 onions" :completed false}
"3" {:text "milk" :completed false}}}
:recipes {:deleted #{}, :updated nil}}
:hash (hasch/uuid shadow)}))