diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2018-02-20 00:04:08 -0300 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2018-02-20 00:04:08 -0300 |
commit | d1c15fe5fa8d5aac6a7ca056e79ca8872971a9d9 (patch) | |
tree | 86f0b7c3d3efe68c6ec2dde770487d3fe2fe18ef | |
parent | 8e06fdb6f18e8cc63a5d501e68a175685fb808b5 (diff) | |
download | queue-api-d1c15fe5fa8d5aac6a7ca056e79ca8872971a9d9.tar.gz queue-api-d1c15fe5fa8d5aac6a7ca056e79ca8872971a9d9.tar.bz2 queue-api-d1c15fe5fa8d5aac6a7ca056e79ca8872971a9d9.zip |
Even more test
-rw-r--r-- | src/clj/queue_api/db/core.clj | 15 | ||||
-rw-r--r-- | src/clj/queue_api/routes/services.clj | 7 | ||||
-rw-r--r-- | test/clj/queue_api/test/db/core_test.clj | 224 |
3 files changed, 160 insertions, 86 deletions
diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj index f40548c..faa4d65 100644 --- a/src/clj/queue_api/db/core.clj +++ b/src/clj/queue_api/db/core.clj @@ -78,14 +78,11 @@ "Get how many jobs a agent (`id`) has performed aggregated by type" [id] (let [jobs (agent-jobs id :completed)] - (->> (reduce (fn [l r] - (let [t (:job/type r)] - (if (nil? (get l t)) - (conj l {t 1}) - (conj l {t (inc (get l t))})))) {} jobs) - (map (fn [x] - {:type (first x) - :jobs (last x)}))))) + (reduce (fn [l r] + (let [t (:job/type r)] + (if (nil? (get l t)) + (conj l {t 1}) + (conj l {t (inc (get l t))})))) {} jobs))) (defn sum-queue "Count all job aggregated by type" @@ -154,4 +151,4 @@ (let [jid (-> (request-job id) :job/id)] (end-job a) (start-job id jid) - {:job_id jid :agent_id id}))))
\ No newline at end of file + jid))))
\ 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 694f0d8..dee949a 100644 --- a/src/clj/queue_api/routes/services.clj +++ b/src/clj/queue_api/routes/services.clj @@ -26,7 +26,10 @@ :return [{:type String :jobs s/Int}] :body-params [agent_id :- String] :summary "Get summary of an agent" - (ok (db/sum-agent agent_id)))) + (let [jobs (db/sum-agent agent_id)] + (map (fn [x] + {:type (first x) + :jobs (last x)}) jobs)))) (context "/job" [] :tags ["job"] @@ -44,7 +47,7 @@ (let [j (db/dequeue-job agent_id)] (if (nil? j) (bad-request {:message "Agent does not exist"}) - (ok {:job_request j}))))) + (ok {:job_request {:job_id j :agent_id agent_id}}))))) (context "/queue" [] :tags ["queue"] diff --git a/test/clj/queue_api/test/db/core_test.clj b/test/clj/queue_api/test/db/core_test.clj index d64b550..3909c31 100644 --- a/test/clj/queue_api/test/db/core_test.clj +++ b/test/clj/queue_api/test/db/core_test.clj @@ -6,68 +6,99 @@ [clj-time.core :as time] [mount.core :as mount])) -(def simple-schema [;;Job that will be kept :unassigned - {: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 false} - {: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 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} - ;;Agent that will be linked to jobs - {:agent/id "644be0ce-035d-48cb-867e-8e6de2714a8d" - :agent/name "Dummy Derp" - :agent/primary-skillset ["rewards-question" "bills-question"] - :agent/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"]} - {: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 [;;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 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"]}]) (use-fixtures :each (fn [f] (mount/stop #'queue-api.db.core/conn) (mount/start #'queue-api.db.core/conn) - (d/transact! conn simple-schema) + (d/transact! conn base-schema) (f))) (defn agent [id] @@ -97,8 +128,12 @@ (testing "test query job by status" (are [left right] (= left right) - #{["51ab0771-f1e4-4268-868f-9029a58f6612"] - ["96cf6f11-591d-4cde-9ab0-56e371acb6d2"]} (q-status :unassigned) + #{["96cf6f11-591d-4cde-9ab0-56e371acb6d2"] + ["51ab0771-f1e4-4268-868f-9029a58f6612"] + ["f90e149e-fa51-4212-a6bf-8cf81c78d28c"] + ["dfa7744f-8d61-4d37-885f-dbef2d4df13b"] + ["c471f9e0-fbbd-461d-adb0-7466c2d9830f"] + ["6e90db58-7a6d-46e2-a02a-e33dc82e6e33"]} (q-status :unassigned) #{["1e0d939d-494b-48d2-9247-b5ae207a519a"] ["aa327540-4e24-47f4-9e9c-81cdd5195934"] ["3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"]} (q-status :completed) @@ -112,13 +147,17 @@ "3cdc52fe-b538-40a6-a9d7-92fa840c2c4b"], :processing ["b201d085-91b5-4a13-9a74-7861426e9996"], :unassigned ["96cf6f11-591d-4cde-9ab0-56e371acb6d2" - "51ab0771-f1e4-4268-868f-9029a58f6612"]} + "f90e149e-fa51-4212-a6bf-8cf81c78d28c" + "dfa7744f-8d61-4d37-885f-dbef2d4df13b" + "51ab0771-f1e4-4268-868f-9029a58f6612" + "c471f9e0-fbbd-461d-adb0-7466c2d9830f" + "6e90db58-7a6d-46e2-a02a-e33dc82e6e33"]} (sum-queue))))) (deftest q-job-test (testing "test query job" (is (= [(job "96cf6f11-591d-4cde-9ab0-56e371acb6d2") - (job "51ab0771-f1e4-4268-868f-9029a58f6612")] + (job "dfa7744f-8d61-4d37-885f-dbef2d4df13b")] (q-job false :unassigned "rewards-question"))) (is (= [(job "b201d085-91b5-4a13-9a74-7861426e9996")] (q-job true :processing "purchases-question"))) @@ -142,15 +181,10 @@ (deftest sum-agent-test (testing "Test sum-agent" - (is (= [{:type "bills-question", :jobs 2} {:type "purchases-question", :jobs 1}] + (is (= {"bills-question" 2 + "purchases-question" 1} (sum-agent "644be0ce-035d-48cb-867e-8e6de2714a8d"))) - (is (= [] (sum-agent "00000000-0000-0000-0000-000000000000"))))) - -;;TODO:adicionar mais testes -(deftest request-job-test - (testing "test query for skillset" - (is (= (job "96cf6f11-591d-4cde-9ab0-56e371acb6d2") - (request-job "644be0ce-035d-48cb-867e-8e6de2714a8d"))))) + (is (= {} (sum-agent "00000000-0000-0000-0000-000000000000"))))) (deftest bind-agent-test (testing "Test bind agent" @@ -179,11 +213,51 @@ :agent/job :job/status)))))) (deftest start-job-test - (testing "Test start a new job" - (let [_ (start-job "644be0ce-035d-48cb-867e-8e6de2714a8d" "96cf6f11-591d-4cde-9ab0-56e371acb6d2") - a (agent "644be0ce-035d-48cb-867e-8e6de2714a8d")] - (are [left right] - (= left right) - :processing (-> a :agent/job :job/status) - "96cf6f11-591d-4cde-9ab0-56e371acb6d2" (-> a :agent/job :job/id))))) + (testing "Test start a new job" + (let [_ (start-job "644be0ce-035d-48cb-867e-8e6de2714a8d" "96cf6f11-591d-4cde-9ab0-56e371acb6d2") + a (agent "644be0ce-035d-48cb-867e-8e6de2714a8d")] + (are [left right] + (= left right) + :processing (-> a :agent/job :job/status) + "96cf6f11-591d-4cde-9ab0-56e371acb6d2" (-> a :agent/job :job/id))))) + +(deftest request-job-test + (testing "test query for skillset" + ;;primary-skillset urgent + (is (= (job "51ab0771-f1e4-4268-868f-9029a58f6612") + (request-job "644be0ce-035d-48cb-867e-8e6de2714a8d"))) + ;;secondary-skillset urgent + (is (= (job "6e90db58-7a6d-46e2-a02a-e33dc82e6e33") + (request-job "ff02347a-1b2d-440c-a2a1-7a593dba564e"))) + (d/transact! conn complete-schema) + ;;primary-skillset not urgent + (is (= (job "96cf6f11-591d-4cde-9ab0-56e371acb6d2") + (request-job "644be0ce-035d-48cb-867e-8e6de2714a8d"))) + ;;secondary-skillset not urgent + (is (= (job "f90e149e-fa51-4212-a6bf-8cf81c78d28c") + (request-job "ff02347a-1b2d-440c-a2a1-7a593dba564e"))))) + +(deftest dequeue-job-test + (testing "Test Dummy Derp dequeuing process" + (are [left right] + (= left right) + ;;first iteration + "51ab0771-f1e4-4268-868f-9029a58f6612" (dequeue-job "644be0ce-035d-48cb-867e-8e6de2714a8d") + :completed (:job/status (job "b201d085-91b5-4a13-9a74-7861426e9996")) + :processing (:job/status (job "51ab0771-f1e4-4268-868f-9029a58f6612")) + (job "51ab0771-f1e4-4268-868f-9029a58f6612") (:agent/job (agent "644be0ce-035d-48cb-867e-8e6de2714a8d")) + ;;second iteration + "96cf6f11-591d-4cde-9ab0-56e371acb6d2" (dequeue-job "644be0ce-035d-48cb-867e-8e6de2714a8d") + :completed (:job/status (job "51ab0771-f1e4-4268-868f-9029a58f6612")) + :processing (:job/status (job "96cf6f11-591d-4cde-9ab0-56e371acb6d2")) + (job "96cf6f11-591d-4cde-9ab0-56e371acb6d2") (:agent/job (agent "644be0ce-035d-48cb-867e-8e6de2714a8d")) + ;; third iteration + "dfa7744f-8d61-4d37-885f-dbef2d4df13b" (dequeue-job "644be0ce-035d-48cb-867e-8e6de2714a8d") + :completed (:job/status (job "96cf6f11-591d-4cde-9ab0-56e371acb6d2")) + :processing (:job/status (job "dfa7744f-8d61-4d37-885f-dbef2d4df13b")) + (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"))))) + |