The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Nicola/glossary creator api (#65961)
Nicola Mometto35c649f88b30
diff --git a/src/metabase/glossary/api.clj b/src/metabase/glossary/api.cljindex 985d4cd4337..0a77f31156e 100644--- a/src/metabase/glossary/api.clj+++ b/src/metabase/glossary/api.clj@@ -15,8 +15,9 @@[:or[:like [:lower :term] [:lower (str "%" search "%")]][:like [:lower :definition] [:lower (str "%" search "%")]]])]- {:data (t2/select :model/Glossary (cond-> {:order-by [[:term :asc]]}- where (assoc :where where)))}))+ {:data (t2/hydrate (t2/select :model/Glossary (cond-> {:order-by [[:term :asc]]}+ where (assoc :where where)))+ :creator)}))(api.macros/defendpoint :post "/""Create a new glossary entry."@@ -25,11 +26,14 @@{:keys [term definition]} :- [:map[:term ms/NonBlankString][:definition ms/NonBlankString]]]- (let [glossary (t2/insert-returning-instance! :model/Glossary {:term term :definition definition})]+ (let [glossary (t2/insert-returning-instance! :model/Glossary+ {:term term+ :definition definition+ :creator_id api/*current-user-id*})](events/publish-event! :event/glossary-create{:object glossary:user-id api/*current-user-id*})- glossary))+ (t2/hydrate glossary :creator)))(api.macros/defendpoint :put "/:id""Update an existing glossary entry."@@ -45,7 +49,7 @@{:object glossary:previous-object previous-glossary:user-id api/*current-user-id*})- glossary)))+ (t2/hydrate glossary :creator))))(api.macros/defendpoint :delete "/:id""Delete a glossary entry."diff --git a/test/metabase/glossary/api_test.clj b/test/metabase/glossary/api_test.cljindex f4ea17df32e..8b3e0978c77 100644--- a/test/metabase/glossary/api_test.clj+++ b/test/metabase/glossary/api_test.clj@@ -12,10 +12,20 @@(testing "can create a new glossary entry as superuser"(let [response (mt/user-http-request :crowberto :post 200 "glossary"{:term "API"- :definition "Application Programming Interface"})]+ :definition "Application Programming Interface"})+ crowberto-id (mt/user->id :crowberto)+ glossary-id (:id response)+ creator-id (t2/select-one-fn :creator_id :model/Glossary glossary-id)](is (= "API" (:term response)))(is (= "Application Programming Interface" (:definition response)))- (is (pos-int? (:id response)))))+ (is (pos-int? (:id response)))+ (testing "Response includes creator_id"+ (is (= crowberto-id (:creator_id response))))+ (testing "Database record has creator_id set correctly"+ (is (= crowberto-id creator-id)))+ (testing "Response hydrates creator"+ (is (map? (:creator response)))+ (is (= crowberto-id (get-in response [:creator :id]))))))(testing "cannot create glossary entry with missing fields"(is (=? {:errors {:term "value must be a non-blank string."}}@@ -26,47 +36,60 @@{:term "Missing definition field"}))))))(testing "GET /api/glossary"- (mt/with-temp [:model/Glossary _g1 {:term "Database" :definition "A structured collection of data"}- :model/Glossary _g2 {:term "Query" :definition "A request for data from a database"}]- (testing "can list all glossary entries"- (let [response (mt/user-http-request :rasta :get 200 "glossary")- data (:data response)]- (is (<= 2 (count data)))- (is (set/subset? #{"Database" "Query"} (set (map :term data))))))+ (let [crowberto-id (mt/user->id :crowberto)]+ (mt/with-temp [:model/Glossary _g1 {:term "Database"+ :definition "A structured collection of data"+ :creator_id crowberto-id}+ :model/Glossary _g2 {:term "Query"+ :definition "A request for data from a database"+ :creator_id crowberto-id}]+ (testing "can list all glossary entries"+ (let [response (mt/user-http-request :rasta :get 200 "glossary")+ data (:data response)]+ (is (<= 2 (count data)))+ (is (set/subset? #{"Database" "Query"} (set (map :term data))))+ (testing "Response hydrates creator"+ (is (every? #(map? (:creator %)) data)))))- (testing "can search glossary entries by term"- (let [response (mt/user-http-request :rasta :get 200 "glossary" :search "data")- data (:data response)]- (is (= 2 (count data)))- (is (every? #(or (re-find #"(?i)data" (:term %))- (re-find #"(?i)data" (:definition %))) data))))+ (testing "can search glossary entries by term"+ (let [response (mt/user-http-request :rasta :get 200 "glossary" :search "data")+ data (:data response)]+ (is (= 2 (count data)))+ (is (every? #(or (re-find #"(?i)data" (:term %))+ (re-find #"(?i)data" (:definition %))) data))))- (testing "search is case insensitive"- (let [response (mt/user-http-request :rasta :get 200 "glossary" :search "DATABASE")- data (:data response)]- (is (<= 1 (count data)))- (is (set/subset? #{"Database"} (set (map :term data))))))+ (testing "search is case insensitive"+ (let [response (mt/user-http-request :rasta :get 200 "glossary" :search "DATABASE")+ data (:data response)]+ (is (<= 1 (count data)))+ (is (set/subset? #{"Database"} (set (map :term data))))))- (testing "search returns empty when no matches"- (let [response (mt/user-http-request :rasta :get 200 "glossary" :search (str "nonexistent-" (random-uuid)))- data (:data response)]- (is (= 0 (count data)))))))+ (testing "search returns empty when no matches"+ (let [response (mt/user-http-request :rasta :get 200 "glossary" :search (str "nonexistent-" (random-uuid)))+ data (:data response)]+ (is (= 0 (count data))))))))(testing "PUT /api/glossary/:id"- (mt/with-temp [:model/Glossary {gid :id} {:term "Old Term" :definition "Old definition"}]- (testing "can update glossary entry as superuser"- (let [response (mt/user-http-request :crowberto :put 200 (str "glossary/" gid)- {:term "Updated Term"- :definition "Updated definition"})]- (is (= "Updated Term" (:term response)))- (is (= "Updated definition" (:definition response)))- (is (= gid (:id response)))))+ (let [crowberto-id (mt/user->id :crowberto)]+ (mt/with-temp [:model/Glossary {gid :id} {:term "Old Term"+ :definition "Old definition"+ :creator_id crowberto-id}]+ (testing "can update glossary entry as superuser"+ (let [response (mt/user-http-request :crowberto :put 200 (str "glossary/" gid)+ {:term "Updated Term"+ :definition "Updated definition"})]+ (is (= "Updated Term" (:term response)))+ (is (= "Updated definition" (:definition response)))+ (is (= gid (:id response)))+ (testing "Response hydrates creator"+ (is (map? (:creator response)))+ (is (= crowberto-id (get-in response [:creator :id]))))))- (testing "returns 404 when updating non-existent entry"- (is (= "Not found."- (mt/user-http-request :crowberto :put 404 "glossary/99999"- {:term "Does not exist"- :definition "Does not exist"}))))))+ (testing "returns 404 when updating non-existent entry"+ (is (= "Not found."+ (mt/user-http-request :crowberto :put 404 "glossary/99999"+ {:term "Does not exist"+ :definition "Does not exist"})))))))(testing "DELETE /api/glossary/:id"(mt/with-temp [:model/Glossary {gid :id} {:term "To Delete" :definition "Will be deleted"}]