Skip to content

Commit

Permalink
[Fix #21] Remove the ctx parameter to execute
Browse files Browse the repository at this point in the history
It proved itself to be unnecessary given we have :initial-ctx. This is a
breaking change.
  • Loading branch information
arichiardi committed Jan 18, 2019
1 parent 717d892 commit a8d8bdc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 53 deletions.
6 changes: 2 additions & 4 deletions src/fonda/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
(s/fdef execute
:args (s/cat :config ::config
:steps ::steps
:context ::r/ctx
:on-success ::r/on-success
:on-anomaly ::r/on-anomaly
:on-exception ::r/on-exception))
Expand All @@ -46,11 +45,10 @@
- path: Path where to assoc the result of the processor
- name: The name of the step
- `ctx` The runtime context, merged to the initial context. Must be a map.
- `on-success` Callback that gets called with the context if all the steps succeeded.
- `on-anomaly` Callback that gets called with an anomaly when any step returns one.
- `on-exception` Callback that gets called with an exception when any step triggers one."
([config steps ctx on-success on-anomaly on-exception]
([config steps on-success on-anomaly on-exception]

(let [{:keys [anomaly?
log-exception
Expand All @@ -62,7 +60,7 @@
:log-exception log-exception
:log-anomaly log-anomaly
:log-success log-success
:ctx (merge (:initial-ctx config) ctx)
:ctx (or (:initial-ctx config) {})
:on-success on-success
:on-anomaly on-anomaly
:on-exception on-exception
Expand Down
1 change: 0 additions & 1 deletion src/fonda/runtime.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
(s/def ::log-anomaly (s/nilable fn?))
(s/def ::log-success (s/nilable fn?))
(s/def ::initial-ctx map?) ;; part of the config
(s/def ::ctx map?)
(s/def ::exception (s/nilable #(instance? js/Error %)))
(s/def ::anomaly (s/nilable any?))
(s/def ::on-success fn?)
Expand Down
71 changes: 38 additions & 33 deletions test/fonda/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@
(deftest execute-empty-chain-test-1
(testing "Passing empty configuration with empty steps should call on-success with a nil value."
(async done
(fonda/execute {} [] {}
(fonda/execute {} []
(fn [res]
(is (= {} res)) (done))
anomaly-cb-throw
exception-cb-throw))))

(deftest execute-empty-chain-test-2
(testing "Passing a context on the configuration with empty steps should call on-success with that context."
(let [ctx {:initial "value"}]
(let [initial {:foo :bar}]
(async done
(fonda/execute {} [] ctx
(fn [res] (is (= ctx res)) (done))
(fonda/execute {:initial-ctx initial}
[]
(fn [res] (is (= initial res)) (done))
anomaly-cb-throw
exception-cb-throw)))))

Expand All @@ -49,31 +50,33 @@
processor {:path processor-path
:name "processor name"
:processor (fn [_] processor-res)}]
(fonda/execute {} [processor] {}
(fonda/execute {}
[processor]
(fn [res] (is (= processor-res (get-in res processor-path))) (done))
anomaly-cb-throw
exception-cb-throw)))))

(deftest one-successful-sync-tap-doesnt-augment-context-test
(testing "Passing one synchronous tap should call on-success with the initial context"
(async done
(let [initial-context {:initial "context"}
(let [initial {:foo :bar}
tap {:name "tap1"
:tap (fn [_] :whatever-value)}]
(fonda/execute {} [tap] initial-context
(fn [res] (is (= initial-context res)) (done))
(fonda/execute {:initial-ctx initial} [tap]
(fn [res] (is (= initial res)) (done))
anomaly-cb-throw
exception-cb-throw)))))

(deftest one-successful-sync-tap-is-passed-the-context
(testing "Passing one synchronous tap should call on-success with the initial context"
(async done
(let [initial-context {:initial "context"}
(let [initial {:foo :bar}
tap {:name "tap1"
:tap (fn [ctx]
(is (= initial-context ctx)) (done)
(is (= initial ctx)) (done)
:whatever-value)}]
(fonda/execute {} [tap] initial-context
(fonda/execute {:initial-ctx initial}
[tap]
(fn [_])
anomaly-cb-throw
exception-cb-throw)))))
Expand All @@ -87,7 +90,8 @@
processor {:path processor-path
:name "processor name"
:processor (fn [_] (js/Promise.resolve processor-res))}]
(fonda/execute {} [processor] {}
(fonda/execute {}
[processor]
(fn [res] (is (= processor-res (get-in res processor-path))) (done))
anomaly-cb-throw
exception-cb-throw)))))
Expand All @@ -99,7 +103,8 @@
processor {:path [:processor-path]
:name "processor name"
:processor (fn [_] processor-res)}]
(fonda/execute {} [processor] {}
(fonda/execute {}
[processor]
(fn [_])
(fn [anomaly] (is (= processor-res anomaly)) (done))
exception-cb-throw)))))
Expand All @@ -111,7 +116,8 @@
tap {:path [:processor-path]
:name "processor name"
:tap (fn [_] tap-res)}]
(fonda/execute {} [tap] {}
(fonda/execute {}
[tap]
success-cb-throw
(fn [anomaly] (is (= tap-res anomaly)) (done))
exception-cb-throw)))))
Expand All @@ -126,7 +132,7 @@
log-anomaly (fn [{:keys [anomaly]}]
(is (= processor-res anomaly)) (done))]
(fonda/execute {:log-anomaly log-anomaly}
[processor] {}
[processor]
success-cb-throw
(fn [_])
exception-cb-throw)))))
Expand All @@ -138,7 +144,8 @@
processor {:path [:processor-path]
:name "processor name"
:processor (fn [_] (js/Promise.resolve processor-res))}]
(fonda/execute {} [processor] {}
(fonda/execute {}
[processor]
success-cb-throw
(fn [anomaly] (is (= processor-res anomaly)) (done))
exception-cb-throw)))))
Expand All @@ -153,7 +160,7 @@
log-anomaly (fn [{:keys [anomaly]}]
(is (= processor-res anomaly)) (done))]
(fonda/execute {:log-anomaly log-anomaly}
[processor] {}
[processor]
success-cb-throw
(fn [_])
exception-cb-throw)))))
Expand All @@ -165,7 +172,8 @@
processor {:path [:processor-path]
:name "processor name"
:processor (fn [_] (throw processor-res))}]
(fonda/execute {} [processor] {}
(fonda/execute {}
[processor]
success-cb-throw
anomaly-cb-throw
(fn [err] (is (= processor-res err)) (done)))))))
Expand All @@ -176,7 +184,8 @@
(let [tap-res (js/Error "Bad exception")
tap {:name "processor name"
:tap (fn [_] (throw tap-res))}]
(fonda/execute {} [tap] {}
(fonda/execute {}
[tap]
success-cb-throw
anomaly-cb-throw
(fn [err] (is (= tap-res err)) (done)))))))
Expand All @@ -191,7 +200,7 @@
log-exception (fn [{:keys [exception]}]
(is (= processor-res exception)) (done))]
(fonda/execute {:log-exception log-exception}
[processor] {}
[processor]
success-cb-throw
anomaly-cb-throw
(fn [_]))))))
Expand All @@ -204,7 +213,8 @@
processor {:path [:processor-path]
:name "processor name"
:processor (fn [_] (js/Promise.reject processor-res))}]
(fonda/execute {} [processor] {}
(fonda/execute {}
[processor]
success-cb-throw
anomaly-cb-throw
(fn [err] (is (= processor-res err)) (done)))))))
Expand All @@ -219,7 +229,7 @@
log-exception (fn [{:keys [exception]}]
(is (= processor-res exception)) (done))]
(fonda/execute {:log-exception log-exception}
[processor] {}
[processor]
success-cb-throw
anomaly-cb-throw
(fn [_]))))))
Expand All @@ -231,7 +241,7 @@
step2-fn inc]
(fonda/execute {}
[{:path [:step1] :name "step1" :processor (fn [_] step1-val)}
{:path [:step2] :name "step2" :processor (fn [{:keys [step1]}] (step2-fn step1))}] {}
{:path [:step2] :name "step2" :processor (fn [{:keys [step1]}] (step2-fn step1))}]
(fn [res]
(is (= res {:step1 step1-val :step2 (step2-fn step1-val)})) (done))
anomaly-cb-throw
Expand All @@ -249,7 +259,7 @@
{:path [:step2]
:name "step2"
:processor (fn [{:keys [step1]}]
(js/Promise.resolve (step2-fn step1)))}] {}
(js/Promise.resolve (step2-fn step1)))}]
(fn [res]
(is (= res {:step1 step1-val :step2 (step2-fn step1-val)})) (done))
anomaly-cb-throw
Expand All @@ -273,11 +283,11 @@
{:path [:step3]
:name "step3"
:processor (fn [{:keys [step2]}]
(step3-fn step2))}] {}
(step3-fn step2))}]
(fn [res]
(is (= res {:step1 step1-val
:step2 (step2-fn step1-val)
:step3 (-> step1-val (step2-fn) (step3-fn))})) (done))
:step2 (step2-fn step1-val)
:step3 (-> step1-val (step2-fn) (step3-fn))})) (done))
anomaly-cb-throw
exception-cb-throw)))))

Expand All @@ -299,7 +309,6 @@
{:path [:step3]
:name "step3"
:processor (fn [_] 1)}]
{}
success-cb-throw
(fn [anomaly] (is (= unsuccessful-res anomaly)) (done))
exception-cb-throw)))))
Expand All @@ -323,7 +332,6 @@
{:path [:step3]
:name "step3"
:processor (fn [_] (swap! step3-counter inc))}]
{}
success-cb-throw
(fn [anomaly] (is (and (= 1 @step1-counter)
(= 0 @step3-counter))) (done))
Expand All @@ -346,7 +354,6 @@
{:path [:step3]
:name "step3"
:processor (fn [_] 1)}]
{}
success-cb-throw
anomaly-cb-throw
(fn [err] (is (= exception err)) (done)))))))
Expand All @@ -370,7 +377,6 @@
{:path [:step3]
:name "step1"
:processor (fn [_] (swap! step3-counter inc))}]
{}
success-cb-throw
anomaly-cb-throw
(fn [err] (is (and (= 1 @step1-counter)
Expand All @@ -394,8 +400,7 @@
{:path [:step3]
:name "step1"
:processor (fn [_] (swap! step3-counter inc))}]
{}
success-cb-throw
exception-cb-throw
(fn [err] (is (and (= 1 @step1-counter)
(= 0 @step3-counter))) (done)))))))
(= 0 @step3-counter))) (done)))))))
30 changes: 15 additions & 15 deletions test/fonda/loggers_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
log-anomaly (fn [{:keys [stack]}]
(is (= (:name processor) (:name (last stack)))) (done))]
(fonda/execute {:log-anomaly log-anomaly}
[processor] {}
[processor]
success-cb-throw
(fn [_]) ;; Anomaly callbacks are tested elsewhere
(fn [_]) ;; Anomaly callbacks are tested elsewhere
exception-cb-throw))))

(deftest log-exception-gets-steps-stack-test
Expand All @@ -48,10 +48,10 @@
log-exception (fn [{:keys [stack]}]
(is (= (:name processor) (:name (last stack)))) (done))]
(fonda/execute {:log-exception log-exception}
[processor] {}
[processor]
success-cb-throw
anomaly-cb-throw
(fn [_]))))) ;; Exception callbacks are tested elsewhere
(fn [_]))))) ;; Exception callbacks are tested elsewhere

(deftest log-success-gets-steps-stack-test
(async done
Expand All @@ -61,8 +61,8 @@
log-success (fn [{:keys [stack]}]
(is (= (:name processor) (:name (last stack)))) (done))]
(fonda/execute {:log-success log-success}
[processor] {}
(fn [_]) ;; Success callbacks are tested elsewhere
[processor]
(fn [_]) ;; Success callbacks are tested elsewhere
anomaly-cb-throw
exception-cb-throw))))

Expand All @@ -82,7 +82,7 @@
(is (true? @log-success-run?))
(done))]
(fonda/execute {:log-success log-success}
[processor] {}
[processor]
success-cb
anomaly-cb-throw
exception-cb-throw))))
Expand All @@ -103,7 +103,7 @@
(is (true? @log-anomaly-run?))
(done))]
(fonda/execute {:log-anomaly log-anomaly}
[processor] {}
[processor]
success-cb-throw
anomaly-cb
exception-cb-throw))))
Expand All @@ -124,7 +124,7 @@
(is (true? @log-exception-run?))
(done))]
(fonda/execute {:log-exception log-exception}
[processor] {}
[processor]
success-cb-throw
anomaly-cb-throw
exception-cb))))
Expand All @@ -139,8 +139,8 @@
(is (= processor (select-keys (last stack) [:path :name :processor])))
(done))]
(fonda/execute {:log-success log-success}
[processor] {}
(fn [_]) ;; ;; Success callbacks are tested elsewhere
[processor]
(fn [_]) ;; Success callbacks are tested elsewhere
anomaly-cb-throw
exception-cb-throw))))

Expand All @@ -155,9 +155,9 @@
(is (= anomaly returned-anomaly))
(done))]
(fonda/execute {:log-anomaly log-anomaly}
[processor] {}
[processor]
success-cb-throw
(fn [_]) ;; Anomaly callbacks are tested elsewhere
(fn [_]) ;; Anomaly callbacks are tested elsewhere
exception-cb-throw))))

(deftest log-exception-gets-stack-and-exception-test
Expand All @@ -171,7 +171,7 @@
(is (= exception thrown-exception))
(done))]
(fonda/execute {:log-exception log-exception}
[processor] {}
[processor]
success-cb-throw
anomaly-cb-throw
(fn [_]))))) ;; Exception callbacks are tested elsewhere
(fn [_]))))) ;; Exception callbacks are tested elsewhere

0 comments on commit a8d8bdc

Please sign in to comment.