From c15543d43dca07ca27fa280f8e1dcc9746279c5e Mon Sep 17 00:00:00 2001 From: Panu Kalliokoski Date: Fri, 19 Jul 2024 17:58:17 +0300 Subject: [PATCH] proper error reporting for common error conditions in ddb/sync-item! --- src/oph/ehoks/db/dynamodb.clj | 9 ++++++++- test/oph/ehoks/db/dynamodb_test.clj | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/oph/ehoks/db/dynamodb.clj b/src/oph/ehoks/db/dynamodb.clj index 7c4e91422..7f5e4be08 100644 --- a/src/oph/ehoks/db/dynamodb.clj +++ b/src/oph/ehoks/db/dynamodb.clj @@ -70,7 +70,11 @@ [table item] (let [table-name @(tables table) key-names (table-keys table) - item-key (zipmap key-names (map item key-names)) + item-key (zipmap key-names + (map #(or (item %) + (throw (ex-info "item key missing" + {:key-name % :item item}))) + key-names)) rest-item (apply dissoc item key-names) attr-names (zipmap (map (partial str "#") (range)) (map name (keys rest-item))) @@ -97,6 +101,9 @@ (-> {:hoks-id hoks-id :kyselytyypit [kyselytyyppi]} (->> (get-for-heratepalvelu-by-hoks-id-and-kyselytyypit! db/spec)) (first) + (not-empty) + (or (throw (ex-info "palaute not found" + {:hoks-id hoks-id :kyselytyyppi kyselytyyppi}))) (remove-nils) (update-keys map-keys-to-ddb) (dissoc :internal-kyselytyyppi) diff --git a/test/oph/ehoks/db/dynamodb_test.clj b/test/oph/ehoks/db/dynamodb_test.clj index 86f9ae685..9f37ef90c 100644 --- a/test/oph/ehoks/db/dynamodb_test.clj +++ b/test/oph/ehoks/db/dynamodb_test.clj @@ -16,6 +16,21 @@ (use-fixtures :once test-utils/migrate-database) (use-fixtures :each test-utils/empty-database-after-test) +(deftest amis-field-mapping + (testing "map-keys-to-ddb maps correctly" + (is (= (ddb/map-keys-to-ddb :foo) :foo)) + (is (= (ddb/map-keys-to-ddb :toimija-oppija) :toimija_oppija)) + (is (= (ddb/map-keys-to-ddb :sahkoposti) :sahkoposti)))) + +(deftest missing-sync-test + (testing "sync-item! fails when not enough information is available + for identifying the item" + (is (thrown-with-msg? clojure.lang.ExceptionInfo #"item key missing" + (ddb/sync-item! :amis {})))) + (testing "sync-amis-herate! fails when there is no information in db" + (is (thrown-with-msg? clojure.lang.ExceptionInfo #"palaute not found" + (ddb/sync-amis-herate! 54343 "aloittaneet"))))) + (def hoks-data {:opiskeluoikeus-oid "1.2.246.562.15.10000000009" :oppija-oid "1.2.246.562.24.12312312319" :ensikertainen-hyvaksyminen (LocalDate/now)