From b5760e2ddcd2270107660bdcac86ec2718b145b8 Mon Sep 17 00:00:00 2001 From: Andrew Suzuki Date: Tue, 14 Jun 2016 11:12:06 -0400 Subject: [PATCH] utils tweaks; many more utils tests --- src/cljs/ulysses/handlers.cljs | 6 +- src/cljs/ulysses/utils.cljs | 70 +++++++++------- test/cljs/ulysses/runner.cljs | 2 +- test/cljs/ulysses/utils_test.cljs | 133 ++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 34 deletions(-) diff --git a/src/cljs/ulysses/handlers.cljs b/src/cljs/ulysses/handlers.cljs index 9712382..53b9f68 100644 --- a/src/cljs/ulysses/handlers.cljs +++ b/src/cljs/ulysses/handlers.cljs @@ -14,7 +14,7 @@ parse-int-id str->int json->clj - to-id-map + by-id find-by-id remove-by-id merge-by-id @@ -162,7 +162,7 @@ (register-handler :request-grant-ops - debounce-request-grant-ops) + debounce-request-grant-ops) (register-handler :request-grant-ops-start @@ -202,7 +202,7 @@ :faculty parse-int-id list - to-id-map))))))) + by-id))))))) ;; ---------------------------------------------------------------------------- ;; user data diff --git a/src/cljs/ulysses/utils.cljs b/src/cljs/ulysses/utils.cljs index 26d6349..19cd9cd 100644 --- a/src/cljs/ulysses/utils.cljs +++ b/src/cljs/ulysses/utils.cljs @@ -114,6 +114,26 @@ (string/trimr) (str ellip))))) +(defn into-maybe + "if target is a list or lazy sequence, return coll. + otherwise, convert coll into the same type as target" + [target coll] + (let [list-or-lzs? ((some-fn list? lazy-seq?) target) + f (if list-or-lzs? identity (partial into (empty target)))] + (f coll))) + +(defn by-id + "convert a seqable of maps with :id into a + hashmap with id keys and original map values. + if there are multiple with the same :id, + then it takes the last + ex. (by-id [{:id 1} {:id 2}]) + => {1 {:id 1}, 2 {:id 2}}" + [seqable] + (->> seqable + (group-by :id) + (remap last))) + (defn find-by-id "find a map by :id within a seqable (of maps)" [id seqable] @@ -127,31 +147,18 @@ the rightmost having the highest preference; the result taking on the type of the first" [a & more] - (let [list-or-lzs? ((some-fn list? lazy-seq?) a) - into-maybe (if list-or-lzs? identity (partial into (empty a))) - all (apply (partial concat a) more)] - (->> all - (group-by :id) - (vals) - (map last) - (into-maybe)))) + (->> (apply (partial concat a) more) + (by-id) + (vals) + (into-maybe a))) (defn remove-by-id "remove a map by :id within a seqable (of maps)" [id seqable] (let [id-int (str->int id)] - (remove - #(= id-int (:id %)) - seqable))) - -(defn to-id-map - "convert a list of maps with :id into a hashmap with :id keys" - [seqable] - (reduce - (fn [a e] - (assoc a (:id e) e)) - (hash-map) - seqable)) + (->> seqable + (remove #(= id-int (:id %))) + (into-maybe seqable)))) (defn new-id "generate a new integer id, given a list of maps @@ -169,17 +176,20 @@ [m ids] (->> ids (map (partial get m)) - (remove nil?))) + (remove nil?) + (into-maybe ids))) (defn valid-request-id? "determines if either positive integer or positive integer string" [id] - (->> id - (str) - (re-find #"^\d+$") - (nil?) - (not))) + (if (= (str id) "0") + false + (->> id + (str) + (re-find #"^\d+$") + (nil?) + (not)))) (defn throttle [ms f] (let [c (chan (sliding-buffer 1))] @@ -221,7 +231,7 @@ "when obj is a javascript object, get property" [obj prop-name] (when (object? obj) - (aget obj prop-name))) + (aget obj (name prop-name)))) ;; ---------------------------------------------------------------------------- ;; classes @@ -295,8 +305,8 @@ [] (or (when-let [d js/document] - (let [body (wop d "body") - de (wop d "documentElement") + (let [body (wop d :body) + de (wop d :documentElement) wb (partial wop body) wde (partial wop de)] (when (or body de) @@ -324,7 +334,7 @@ "mechanism to contextual (as keyword) helper ex. :nsf => :info" [mechanism] - (mechanism config/mechanism-contextuals)) + (get config/mechanism-contextuals mechanism)) (defn update-db-pagination "update db :page-current and :page-last given paginated response" diff --git a/test/cljs/ulysses/runner.cljs b/test/cljs/ulysses/runner.cljs index 3385c49..63aac91 100644 --- a/test/cljs/ulysses/runner.cljs +++ b/test/cljs/ulysses/runner.cljs @@ -5,4 +5,4 @@ [ulysses.db-test])) ;; run all tests in all namespaces matching ulysses.* -(doo-all-tests #"ulysses.*") +(doo-all-tests #"ulysses.*-test") diff --git a/test/cljs/ulysses/utils_test.cljs b/test/cljs/ulysses/utils_test.cljs index 51032a6..eccabdd 100644 --- a/test/cljs/ulysses/utils_test.cljs +++ b/test/cljs/ulysses/utils_test.cljs @@ -14,6 +14,16 @@ false "" false 0)) +(deftest lazy-seq? + (are [x y] (= x (utils/lazy-seq? y)) + true (lazy-seq) + true (map integer? [1 2 3]) + false (list) + false (vector) + false (hash-map) + false nil + false "")) + ; missing: ptr ; missing: ptr-first @@ -119,6 +129,19 @@ (testing "custom ellipsis" (is (= "foo[..]" (utils/truncate-with-ellipsis "foo bar" 3 "[..]"))))) +(deftest into-maybe + (is (= (list 1) (utils/into-maybe (list) [1]))) + (is (= (lazy-seq (list 1 2 3)) (utils/into-maybe (list) [1 2 3]))) + (is (= #{1} (utils/into-maybe #{1} [1]))) + (is (= #{1 2} (utils/into-maybe #{3} #{1 2})))) + +(deftest by-id + (are [x y] (= x (utils/by-id y)) + {} [] + {1 {:id 1}} [{:id 1}] + {1 {:id 1} 2 {:id 2}} [{:id 1} {:id 2}] + {1 {:id 1} 2 {:id 2}} [{:id 1} {:id 2 :old true} {:id 2}])) + (deftest find-by-id (let [ms [{:id 1} {:id 2} {:id 94}]] (testing "found (type int)" @@ -156,3 +179,113 @@ #{{:id 0} {:id 1} {:id 2}} [#{0} #{1 2} #{0 2 1}] #{{:id 0} {:id 1} {:id 2 :a true}} [#{0} [1 2] #{{:id 2 :a true}}] (list {:id 0} {:id 1} {:id 2 :a true} {:id 3}) [(list 0 1 2) [1 {:id 2 :a true} 3] []])))) + +(deftest remove-by-id + (are [x id y] (= x (utils/remove-by-id id y)) + [{:id 1} {:id 2} {:id 3}] 85 [{:id 1} {:id 2} {:id 3}] + #{{:id 1} {:id 2} {:id 3}} 85 #{{:id 1} {:id 2} {:id 3}} + [{:id 1} {:id 3}] 2 [{:id 1} {:id 2} {:id 3}] + [{:id 1} {:id 3}] "2" [{:id 1} {:id 2} {:id 3}])) + +(deftest new-id + (are [x y] (= x (utils/new-id y)) + 1 (list) + 2 (list {:id 1}) + 3 (list {:id 1} {:id 2}) + 40 (list {:id 1} {:id 39} {:id 4}))) + +(deftest map-lookup + (are [x m ids] (= x (utils/map-lookup m ids)) + (list) {} (list) + (vector) {} (vector) + [] {2 {:id 2}} [1] + [{:id 1}] {1 {:id 1} 2 {:id 2}} [1] + [{:id 1} {:id 2}] {1 {:id 1} 2 {:id 2}} [1 2] + [{:id 1} {:id 2}] {1 {:id 1} 2 {:id 2}} [1 2 3])) + +(deftest valid-request-id? + (testing "yes" + (are [x] (utils/valid-request-id? x) + 1 5 "1" "5" "12345")) + (testing "no" + (are [x] (not (utils/valid-request-id? x)) + 0 -5 "0" "-5" nil [] ""))) + +; missing: throttle + +; missing: debounce + +; missing: debounce-standard + +(deftest wop + (testing "is object" + (are [x y prop] (= x (utils/wop y prop)) + 1 #js {"ok" 1} "ok" + 1 #js {"ok" 1} :ok + nil #js {"ok" 1} "missing" + nil #js {"ok" 1} :missing)) + (testing "is not object" + (are [y prop] (nil? (utils/wop y prop)) + #js [1 2 3] :foo + nil :foo + {:foo 3} :foo))) + +; missing: classes-vector + +; missing: classes + +; missing: classes-attr + +; missing: btn-classes + +; missing: d-p + +; missing: window-size + +; missing: document-size + +; missing: window-scroll-position + +(deftest mechanism-to-contextual + (are [x y] (= x (utils/mechanism-to-contextual y)) + :info :nsf + :success :nih + nil :not-found)) + +; missing: update-db-pagination + +; missing: faculties-pool-params + +; missing: metric-maxes + +; missing: verify-user-poll-response + +; missing: validate-builder-filters + +(deftest parse-int-id + (are [x y] (= x (utils/parse-int-id y)) + {:id 3} {:id "3"} + {:id 123} {:id "123"} + {:id 3} {:id 3} + {:id 123} {:id 123} + {:id nil} {:id "*"})) + +; missing: clean-incoming-grant-op + +; missing: clean-incoming-grant-ops + +; missing: request + +; missing: request-grant-ops-channel + +; missing: debounce-request-grant-ops + +; missing: request-faculties-pool-channel + +; missing: debounce-request-faculties-pool + +; missing: request-workspace-save-channel + +; missing: debounce-request-workspace-save + +; missing: scroll-to