aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2018-02-17 23:14:17 -0300
committerGabriel A. Giovanini <mail@gabrielgio.me>2018-02-17 23:14:17 -0300
commit65357035e6fee399993da80d6d26aa833979cfcf (patch)
tree86ebc6de8d6c4ff70674403deba887a5983e47e4
parent64773838cb5c177994a7d86b557ea464fa06dcd2 (diff)
downloadqueue-api-65357035e6fee399993da80d6d26aa833979cfcf.tar.gz
queue-api-65357035e6fee399993da80d6d26aa833979cfcf.tar.bz2
queue-api-65357035e6fee399993da80d6d26aa833979cfcf.zip
Adds test for db.core
-rw-r--r--src/clj/queue_api/db/core.clj15
-rw-r--r--src/clj/queue_api/routes/services.clj2
-rw-r--r--test/clj/queue_api/test/db/core_test.clj84
3 files changed, 90 insertions, 11 deletions
diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj
index fa3db41..dc04187 100644
--- a/src/clj/queue_api/db/core.clj
+++ b/src/clj/queue_api/db/core.clj
@@ -1,11 +1,12 @@
(ns queue-api.db.core
(:require [datascript.core :as d]
- [mount.core :as mount]))
+ [mount.core :as mount]
+ [clj-time.core :as time]))
(def schema {:agent/id {:db/unique :db.unique/identity}
:agent/primary-skillset {:db/cardinality :db.cardinality/many}
:agent/secondary-skillset {:db/cardinality :db.cardinality/many}
- :agent/jobs {:db.valueType :db.type/ref}
+ :agent/job {:db.valueType :db.type/ref}
:job/id {:db/unique :db.unique/identity}
:job/agent {:db.valueType :db.type/ref}})
@@ -22,6 +23,7 @@
(d/transact! queue-api.db.core/conn [{:job/id id
:job/type type
:job/urgent urgent
+ :job/date (time/now)
:job/status :unassigned}]))
(defn request-job [id]
@@ -31,13 +33,6 @@
[{:type "Dummy"
:jobs -2}])
-(defn q-agent [id]
- (d/q '[:find ?type :in $ ?id
- :where
- [?e :agent/id ?id]
- [?x :job/type ?type]
- [?x :job/agent ?e]]))
-
(defn q-status [s]
(d/q '[:find ?id :in $ ?status
:where
@@ -45,7 +40,7 @@
[?e :job/id ?id]]
@conn s))
-(defn get-queue []
+(defn sum-queue []
{:completed (map #(first %) (q-status :completed))
:processing (map #(first %) (q-status :processing))
:unassigned (map #(first %) (q-status :unassigned))}) \ No newline at end of file
diff --git a/src/clj/queue_api/routes/services.clj b/src/clj/queue_api/routes/services.clj
index 0e94c18..5d6d6cb 100644
--- a/src/clj/queue_api/routes/services.clj
+++ b/src/clj/queue_api/routes/services.clj
@@ -47,6 +47,6 @@
:processing[String]
:unassigned [String]}
:summary "Get a summary of an agent"
- (ok (db/get-queue)))))
+ (ok (db/sum-queue)))))
diff --git a/test/clj/queue_api/test/db/core_test.clj b/test/clj/queue_api/test/db/core_test.clj
new file mode 100644
index 0000000..4c30ed3
--- /dev/null
+++ b/test/clj/queue_api/test/db/core_test.clj
@@ -0,0 +1,84 @@
+(ns queue-api.test.db.core-test
+ (:require [clojure.test :refer :all]
+ [ring.mock.request :refer :all]
+ [queue-api.db.core :refer :all]
+ [datascript.core :as d]
+ [clj-time.core :as time]
+ [mount.core :as mount])
+ (:import (java.util UUID)))
+
+(def simple-schema [;;Job that will be kept :unassigned
+ {:job/id "51ab0771-f1e4-4268-868f-9029a58f6612"
+ :type "rewards-question"
+ :job/status :unassigned
+ :job/date (time/now)
+ :job/urgent false}
+ ;;Job that will be changed to processing
+ {:job/id "b201d085-91b5-4a13-9a74-7861426e9996"
+ :type "rewards-question"
+ :job/status :unassigned
+ :job/date (time/now)
+ :job/urgent true}
+ ;;Job that will be changed to completed
+ {:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"
+ :type "rewards-question"
+ :job/status :unassigned
+ :job/date (time/now)
+ :job/urgent true}
+ ;;Agent that will be linked to jobs
+ {:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"
+ :agent/name "Dummy Derp"
+ :primary-skillset ["rewards-question" "bills-question"]
+ :secondary-skillset []}
+ ;;Change job status to :completed and link agent
+ {:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"
+ :job/status :completed
+ :job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
+ ;;Change job status to processing and link agent
+ {:job/id "b201d085-91b5-4a13-9a74-7861426e9996"
+ :job/status :processing
+ :job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
+ ;;Link jog with job that it is processing
+ {:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"
+ :agent/job [:job/id "b201d085-91b5-4a13-9a74-7861426e9996"]}])
+
+(use-fixtures
+ :each
+ (fn [f]
+ (mount/stop #'queue-api.db.core/conn)
+ (mount/start #'queue-api.db.core/conn)
+ (d/transact! conn simple-schema)
+ (f)))
+
+(deftest add-agent-test
+ (testing "Test adding agent"
+ (let [tx (add-agent {:id "1d7158f8-f7a0-4ff9-9758-25efcc5aae65"
+ :name "Derpinson"
+ :primary-skillset ["rewards-question" "bills-question"]
+ :secondary-skillset []})
+ tx-data (:tx-data tx)]
+ (is (not (empty? tx-data))))))
+
+(deftest add-job-test
+ (testing "test adding job"
+ (let [tx (add-job {:id "2dc043a4-6708-4b1e-885d-e59082733e4d"
+ :type "rewards-question"
+ :urgent false})
+ tx-data (:tx-data tx)]
+ (is (not (empty? tx-data))))))
+
+(deftest q-status-test
+ (testing "test query job by status"
+ (are [left right]
+ (= left right)
+ #{["51ab0771-f1e4-4268-868f-9029a58f6612"]} (q-status :unassigned)
+ #{["1e0d939d-494b-48d2-9247-b5ae207a519a"]} (q-status :completed)
+ #{["b201d085-91b5-4a13-9a74-7861426e9996"]} (q-status :processing)
+ #{} (q-status :nil))))
+
+(deftest sum-queue-test
+ (testing "test get summary of current state of the queue"
+ (is (= {:completed ["1e0d939d-494b-48d2-9247-b5ae207a519a"]
+ :processing ["b201d085-91b5-4a13-9a74-7861426e9996"]
+ :unassigned ["51ab0771-f1e4-4268-868f-9029a58f6612"]}
+ (sum-queue))))) \ No newline at end of file