aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2018-02-20 21:34:01 -0300
committerGabriel A. Giovanini <mail@gabrielgio.me>2018-02-20 21:34:01 -0300
commitfd953613e3259c63f54d49e042e16e50d47dd8fa (patch)
treefcfbbc4286650fc51a28b2a075705f5a19b14049
parentc70778a52e1e654f89970aad21b60e1f939818dd (diff)
parentb8f65dd00187023edf33ea462b92f50b01e32b00 (diff)
downloadqueue-api-fd953613e3259c63f54d49e042e16e50d47dd8fa.tar.gz
queue-api-fd953613e3259c63f54d49e042e16e50d47dd8fa.tar.bz2
queue-api-fd953613e3259c63f54d49e042e16e50d47dd8fa.zip
Merge branch 'master' of https://gitlab.com/gabrielgio/queue-api
-rw-r--r--src/clj/queue_api/db/core.clj39
-rw-r--r--test/clj/queue_api/test/db/core_test.clj208
2 files changed, 133 insertions, 114 deletions
diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj
index 0381e1e..00af5cb 100644
--- a/src/clj/queue_api/db/core.clj
+++ b/src/clj/queue_api/db/core.clj
@@ -36,18 +36,18 @@
(defn agent-jobs
"Get a job that has a agent bounded with given `id` and a status of `s`"
[id s]
- (let [q (d/q '[:find ?jid
- :in $ ?id ?s
- :where
- [?e :agent/id ?id]
- [?x :job/id ?jid]
- [?x :job/status ?s]
- [?x :job/agent ?e]]
- @conn id s)]
- (map #(d/entity @conn [:job/id (first %)]) q)))
+ (map #(d/entity @conn [:job/id (first %)])
+ (d/q '[:find ?jid
+ :in $ ?id ?s
+ :where
+ [?e :agent/id ?id]
+ [?x :job/id ?jid]
+ [?x :job/status ?s]
+ [?x :job/agent ?e]]
+ @conn id s)))
(defn q-job
- "Fetch job and sort by date
+ "Query jobs and sort them by date
`u`: urgent flag
`s`: status of the job
`ts`: types of the job"
@@ -66,7 +66,7 @@
(map #(d/entity @conn [:job/id (last %)]))))
(defn q-status
- "Query job filtering only by `s`"
+ "Query job by status `s`"
[s]
(d/q '[:find ?id :in $ ?status
:where
@@ -75,7 +75,7 @@
@conn s))
(defn sum-agent
- "Get how many jobs a agent (`id`) has performed aggregated by type"
+ "Get how many jobs a agent `id` has performed aggregated by type"
[id]
(let [jobs (agent-jobs id :completed)]
(reduce (fn [l r]
@@ -85,7 +85,7 @@
(conj l {t (inc (get l t))})))) {} jobs)))
(defn sum-queue
- "Count all job aggregated by type"
+ "Count all jobs aggregated by type"
[]
{:completed (map first (q-status :completed))
:processing (map first (q-status :processing))
@@ -96,10 +96,10 @@
if nothing was found query for not urgent ones."
[s]
(let [ju (q-job true :unassigned s)]
- (if (not (empty? ju))
+ (if (seq ju)
(first ju)
(let [jn (q-job false :unassigned s)]
- (if (not (empty? jn))
+ (if (seq jn)
(first jn))))))
(defn request-job
@@ -108,7 +108,7 @@
(let [a (d/entity @conn [:agent/id id])]
(if (not (nil? a))
(let [p (q-skillset (:agent/primary-skillset a))]
- (if (not (nil? p))
+ (if-not (nil? p)
p
(q-skillset (:agent/secondary-skillset a)))))))
@@ -139,17 +139,14 @@
"Change status of a job `j` to :processing
and bind it to an agent `a`"
[^String a ^String j]
- (if (not (nil? j))
- (do
- (t-job j :processing a)
- (bind-agent a j))))
+ (when-not (nil? j) (t-job j :processing a) (bind-agent a j)))
(defn dequeue-job
"Dequeue a job to a agent `id`"
[^String id]
(let [a (d/entity @conn [:agent/id id])]
(if (not (nil? a))
- (let [jid (-> (request-job id) :job/id)]
+ (let [jid (:job/id (request-job id))]
(end-job a)
(start-job id jid)
jid)))) \ No newline at end of file
diff --git a/test/clj/queue_api/test/db/core_test.clj b/test/clj/queue_api/test/db/core_test.clj
index 3909c31..08c8a2a 100644
--- a/test/clj/queue_api/test/db/core_test.clj
+++ b/test/clj/queue_api/test/db/core_test.clj
@@ -6,92 +6,96 @@
[clj-time.core :as time]
[mount.core :as mount]))
-(def base-schema [;;Job that will be kept :unassigned
- {:job/id "dfa7744f-8d61-4d37-885f-dbef2d4df13b"
- :job/type "rewards-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 19 1 0 1)
- :job/urgent false}
- {:job/id "c471f9e0-fbbd-461d-adb0-7466c2d9830f"
- :job/type "purchases-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 19 1 0 0)
- :job/urgent false}
- {:job/id "51ab0771-f1e4-4268-868f-9029a58f6612"
- :job/type "rewards-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 18 1 0 2)
- :job/urgent true}
- {:job/id "96cf6f11-591d-4cde-9ab0-56e371acb6d2"
- :job/type "rewards-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 18 1 0 1)
- :job/urgent false}
- {:job/id "6e90db58-7a6d-46e2-a02a-e33dc82e6e33"
- :job/type "purchases-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 18 1 0 0)
- :job/urgent true}
- {:job/id "f90e149e-fa51-4212-a6bf-8cf81c78d28c"
- :job/type "purchases-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 17 1 0 0)
- :job/urgent false}
- ;;Job that will be changed to processing
- {:job/id "b201d085-91b5-4a13-9a74-7861426e9996"
- :job/type "purchases-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 18 1 0 4)
- :job/urgent true}
- ;;Job that will be changed to completed
- {:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"
- :job/type "bills-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 18 1 0 3)
- :job/urgent false}
- {:job/id "aa327540-4e24-47f4-9e9c-81cdd5195934"
- :job/type "bills-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 18 1 0 6)
- :job/urgent true}
- {:job/id "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"
- :job/type "purchases-question"
- :job/status :unassigned
- :job/date (time/date-time 2018 2 18 1 0 8)
- :job/urgent true}
- ;;Agents
- {:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"
- :agent/name "Dummy Derp"
- :agent/primary-skillset ["rewards-question" "bills-question"]
- :agent/secondary-skillset []}
- {:agent/id "ff02347a-1b2d-440c-a2a1-7a593dba564e"
- :agent/name "Derpinson"
- :agent/primary-skillset ["nil-question"]
- :agent/secondary-skillset ["purchases-question"]}
- ;;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"]}
- {:job/id "aa327540-4e24-47f4-9e9c-81cdd5195934"
- :job/status :completed
- :job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
- {:job/id "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"
- :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 agent with job that it is being processed
- {:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"
- :agent/job [:job/id "b201d085-91b5-4a13-9a74-7861426e9996"]}])
+(def base-schema
+ "Scenario base for tests, it has most of possible combinations to simulate a real setup"
+ [;;Jobs that will be kept :unassigned
+ {:job/id "dfa7744f-8d61-4d37-885f-dbef2d4df13b"
+ :job/type "rewards-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 19 1 0 1)
+ :job/urgent false}
+ {:job/id "c471f9e0-fbbd-461d-adb0-7466c2d9830f"
+ :job/type "purchases-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 19 1 0 0)
+ :job/urgent false}
+ {:job/id "51ab0771-f1e4-4268-868f-9029a58f6612"
+ :job/type "rewards-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 2)
+ :job/urgent true}
+ {:job/id "96cf6f11-591d-4cde-9ab0-56e371acb6d2"
+ :job/type "rewards-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 1)
+ :job/urgent false}
+ {:job/id "6e90db58-7a6d-46e2-a02a-e33dc82e6e33"
+ :job/type "purchases-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 0)
+ :job/urgent true}
+ {:job/id "f90e149e-fa51-4212-a6bf-8cf81c78d28c"
+ :job/type "purchases-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 17 1 0 0)
+ :job/urgent false}
+ ;;Job that will be changed to processing
+ {:job/id "b201d085-91b5-4a13-9a74-7861426e9996"
+ :job/type "purchases-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 4)
+ :job/urgent true}
+ ;;Jobs that will be changed to completed
+ {:job/id "1e0d939d-494b-48d2-9247-b5ae207a519a"
+ :job/type "bills-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 3)
+ :job/urgent false}
+ {:job/id "aa327540-4e24-47f4-9e9c-81cdd5195934"
+ :job/type "bills-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 6)
+ :job/urgent true}
+ {:job/id "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"
+ :job/type "purchases-question"
+ :job/status :unassigned
+ :job/date (time/date-time 2018 2 18 1 0 8)
+ :job/urgent true}
+ ;;Agents
+ {:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"
+ :agent/name "Dummy Derp"
+ :agent/primary-skillset ["rewards-question" "bills-question"]
+ :agent/secondary-skillset []}
+ {:agent/id "ff02347a-1b2d-440c-a2a1-7a593dba564e"
+ :agent/name "Derpinson"
+ :agent/primary-skillset ["nil-question"]
+ :agent/secondary-skillset ["purchases-question"]}
+ ;;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"]}
+ {:job/id "aa327540-4e24-47f4-9e9c-81cdd5195934"
+ :job/status :completed
+ :job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
+ {:job/id "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"
+ :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 agent with job that it is being processed
+ {:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"
+ :agent/job [:job/id "b201d085-91b5-4a13-9a74-7861426e9996"]}])
-(def complete-schema [{:job/id "51ab0771-f1e4-4268-868f-9029a58f6612"
- :job/status :completed
- :job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
- {:job/id "6e90db58-7a6d-46e2-a02a-e33dc82e6e33"
- :job/status :completed
- :job/agent [:agent/id "ff02347a-1b2d-440c-a2a1-7a593dba564e"]}])
+(def complete-schema
+ "It completes some jobs and links them to an agent"
+ [{:job/id "51ab0771-f1e4-4268-868f-9029a58f6612"
+ :job/status :completed
+ :job/agent [:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d"]}
+ {:job/id "6e90db58-7a6d-46e2-a02a-e33dc82e6e33"
+ :job/status :completed
+ :job/agent [:agent/id "ff02347a-1b2d-440c-a2a1-7a593dba564e"]}])
(use-fixtures
:each
@@ -117,7 +121,7 @@
(is (not (empty? tx-data))))))
(deftest add-job-test
- (testing "test adding job"
+ (testing "Test adding job"
(let [tx (add-job {:id "2dc043a4-6708-4b1e-885d-e59082733e4d"
:type "rewards-question"
:urgent false})
@@ -125,7 +129,7 @@
(is (not (empty? tx-data))))))
(deftest q-status-test
- (testing "test query job by status"
+ (testing "Test query job by status"
(are [left right]
(= left right)
#{["96cf6f11-591d-4cde-9ab0-56e371acb6d2"]
@@ -141,7 +145,7 @@
#{} (q-status :nil))))
(deftest sum-queue-test
- (testing "test get summary of current state of the queue"
+ (testing "test get summary of the current state of the queue"
(is (= {:completed ["1e0d939d-494b-48d2-9247-b5ae207a519a"
"aa327540-4e24-47f4-9e9c-81cdd5195934"
"3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"],
@@ -155,7 +159,7 @@
(sum-queue)))))
(deftest q-job-test
- (testing "test query job"
+ (testing "Test query job"
(is (= [(job "96cf6f11-591d-4cde-9ab0-56e371acb6d2")
(job "dfa7744f-8d61-4d37-885f-dbef2d4df13b")]
(q-job false :unassigned "rewards-question")))
@@ -180,7 +184,7 @@
(is (= [] (agent-jobs "644be0ce-035d-48cb-867e-8e6de2714a8d" :unassigned)))))
(deftest sum-agent-test
- (testing "Test sum-agent"
+ (testing "Test get summary of an agent"
(is (= {"bills-question" 2
"purchases-question" 1}
(sum-agent "644be0ce-035d-48cb-867e-8e6de2714a8d")))
@@ -258,6 +262,24 @@
(job "dfa7744f-8d61-4d37-885f-dbef2d4df13b") (:agent/job (agent "644be0ce-035d-48cb-867e-8e6de2714a8d"))
;; last empty iteration
nil (dequeue-job "644be0ce-035d-48cb-867e-8e6de2714a8d")
- :completed (:job/status (job "dfa7744f-8d61-4d37-885f-dbef2d4df13b")))))
-
-
+ :completed (:job/status (job "dfa7744f-8d61-4d37-885f-dbef2d4df13b"))))
+ (testing "Testing Derpinson dequeuing process"
+ (are [left right]
+ (= left right)
+ ;;first iteration
+ "6e90db58-7a6d-46e2-a02a-e33dc82e6e33" (dequeue-job "ff02347a-1b2d-440c-a2a1-7a593dba564e")
+ :processing (:job/status (job "6e90db58-7a6d-46e2-a02a-e33dc82e6e33"))
+ (job "6e90db58-7a6d-46e2-a02a-e33dc82e6e33") (:agent/job (agent "ff02347a-1b2d-440c-a2a1-7a593dba564e"))
+ ;;second iteration
+ "f90e149e-fa51-4212-a6bf-8cf81c78d28c" (dequeue-job "ff02347a-1b2d-440c-a2a1-7a593dba564e")
+ :completed (:job/status (job "6e90db58-7a6d-46e2-a02a-e33dc82e6e33"))
+ :processing (:job/status (job "f90e149e-fa51-4212-a6bf-8cf81c78d28c"))
+ (job "f90e149e-fa51-4212-a6bf-8cf81c78d28c") (:agent/job (agent "ff02347a-1b2d-440c-a2a1-7a593dba564e"))
+ ;;third iteration
+ "c471f9e0-fbbd-461d-adb0-7466c2d9830f" (dequeue-job "ff02347a-1b2d-440c-a2a1-7a593dba564e")
+ :completed (:job/status (job "f90e149e-fa51-4212-a6bf-8cf81c78d28c"))
+ :processing (:job/status (job "c471f9e0-fbbd-461d-adb0-7466c2d9830f"))
+ (job "c471f9e0-fbbd-461d-adb0-7466c2d9830f") (:agent/job (agent "ff02347a-1b2d-440c-a2a1-7a593dba564e"))
+ ;;last empty iteration
+ nil (dequeue-job "ff02347a-1b2d-440c-a2a1-7a593dba564e")
+ :completed (:job/status (job "c471f9e0-fbbd-461d-adb0-7466c2d9830f")))))