From 87b88f93404f5658ce57d2652e7719a45add6767 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Fri, 10 Oct 2014 22:02:51 +0300 Subject: [PATCH] Update unit tests to not use core async --- src/test/grub/test/unit/state.clj | 226 ++++++++++++++++-------------- 1 file changed, 119 insertions(+), 107 deletions(-) diff --git a/src/test/grub/test/unit/state.clj b/src/test/grub/test/unit/state.clj index d07a1af..8d9bbdd 100644 --- a/src/test/grub/test/unit/state.clj +++ b/src/test/grub/test/unit/state.clj @@ -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 !! in msg) - (let [response ( (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 ( (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 ( (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 ( (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 ( (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)}))