Skip to content

Commit cdfa969

Browse files
author
Roman Rudakov
committed
Fix some clj-kondo issues
1 parent a422b01 commit cdfa969

File tree

9 files changed

+172
-204
lines changed

9 files changed

+172
-204
lines changed

src/education/http/endpoints/articles.clj

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
(ns education.http.endpoints.articles
22
(:require [compojure.api.sweet :refer [context DELETE GET PATCH POST]]
33
[education.database.articles :as articlesdb]
4-
[education.http.constants
5-
:refer
6-
[no-access-error-message not-found-error-message server-error-message]]
4+
[education.http.constants :as const]
75
[education.http.restructure :refer [require-roles]]
86
[education.specs.articles :as specs]
97
[education.specs.error :as err]
10-
[ring.util.http-response
11-
:refer
12-
[created forbidden internal-server-error no-content not-found ok]]))
8+
[ring.util.http-response :as status]))
139

1410
;; Converters
1511
(defn to-short-article-response
@@ -41,18 +37,18 @@
4137
(fn [{:keys [identity]}]
4238
(let [user (:user identity)
4339
new-id (str (articlesdb/add-article db user article))]
44-
(created (str "/articles/" new-id) {:id new-id}))))
40+
(status/created (str "/articles/" new-id) {:id new-id}))))
4541

4642
(defn- update-article-handler
4743
"Update existing article handler."
4844
[db article-id article]
4945
(fn [{:keys [identity]}]
5046
(if (articlesdb/can-update? db (:user identity) article-id)
5147
(case (articlesdb/update-article db article-id article)
52-
1 (no-content)
53-
0 (not-found {:message not-found-error-message})
54-
(internal-server-error {:message server-error-message}))
55-
(forbidden {:message no-access-error-message}))))
48+
1 (status/no-content)
49+
0 (status/not-found {:message const/not-found-error-message})
50+
(status/internal-server-error {:message const/server-error-message}))
51+
(status/forbidden {:message const/no-access-error-message}))))
5652

5753
(defn- get-all-articles-handler
5854
"Get all recent articles handler."
@@ -62,49 +58,49 @@
6258
(articlesdb/get-user-articles db user-id limit))]
6359
(->> articles
6460
(map to-short-article-response)
65-
ok)))
61+
status/ok)))
6662

6763
(defn- get-latest-full-articles-handler
6864
"Get latest full articles handler."
6965
[db number]
7066
(let [articles (articlesdb/get-latest-full-sized-articles db number)]
7167
(->> articles
7268
(map to-full-article-response)
73-
ok)))
69+
status/ok)))
7470

7571
(defn- get-article-by-id-handler
7672
"Get article by `article-id` handler."
7773
[db article-id]
7874
(let [article (articlesdb/get-article-by-id db article-id)]
7975
(if (nil? article)
80-
(not-found {:message not-found-error-message})
81-
(ok (to-full-article-response article)))))
76+
(status/not-found {:message const/not-found-error-message})
77+
(status/ok (to-full-article-response article)))))
8278

8379
(defn- get-last-main-featured-article-handler
8480
"Get last main featured article handler."
8581
[db]
8682
(let [mf-article (articlesdb/get-last-featured-article db)]
8783
(if (nil? mf-article)
88-
(not-found {:message not-found-error-message})
89-
(ok (to-short-article-response mf-article)))))
84+
(status/not-found {:message const/not-found-error-message})
85+
(status/ok (to-short-article-response mf-article)))))
9086

9187
(defn- get-last-featured-articles-handler
9288
"Get last featured articles handler."
9389
[db limit]
9490
(->> (articlesdb/get-last-featured-articles db limit)
9591
(map to-short-article-response)
96-
ok))
92+
status/ok))
9793

9894
(defn- delete-article-handler
9995
"Delete article by `article-id` handler."
10096
[db article-id]
10197
(fn [{:keys [identity]}]
10298
(if (articlesdb/can-update? db (:user identity) article-id)
10399
(case (articlesdb/delete-article db article-id)
104-
1 (no-content)
105-
0 (not-found {:message not-found-error-message})
106-
(internal-server-error {:message server-error-message}))
107-
(forbidden {:message no-access-error-message}))))
100+
1 (status/no-content)
101+
0 (status/not-found {:message const/not-found-error-message})
102+
(status/internal-server-error {:message const/server-error-message}))
103+
(status/forbidden {:message const/no-access-error-message}))))
108104

109105
;; Define routes
110106
(defn articles-routes

src/education/http/endpoints/users.clj

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
[education.http.restructure :refer [require-roles]]
88
[education.specs.error :as err]
99
[education.specs.users :as specs]
10-
[ring.util.http-response
11-
:refer
12-
[created no-content not-found ok unauthorized]]))
10+
[ring.util.http-response :as status]))
1311

1412
;; Converters
1513
(defn to-user-response
@@ -41,8 +39,8 @@
4139
[db config credentials]
4240
(let [[ok? res] (create-auth-token db config credentials)]
4341
(if ok?
44-
(ok res)
45-
(unauthorized res))))
42+
(status/ok res)
43+
(status/unauthorized res))))
4644

4745
(defn- all-users-handler
4846
"Return all users list."
@@ -51,32 +49,32 @@
5149
usersdb/get-all-users
5250
(map to-user-response)
5351
vec
54-
ok))
52+
status/ok))
5553

5654
(defn- get-user-handler
5755
"Get user by ID handler."
5856
[db id]
5957
(let [user (usersdb/get-user db id)]
6058
(if (nil? user)
61-
(not-found {:message not-found-error-message})
62-
(ok (to-user-response user)))))
59+
(status/not-found {:message not-found-error-message})
60+
(status/ok (to-user-response user)))))
6361

6462
(defn- add-user-handler
6563
"Create new user handler."
6664
[db user]
67-
(created (str (usersdb/add-user db user))))
65+
(status/created (str (usersdb/add-user db user))))
6866

6967
(defn- update-user-handler
7068
"Update existing user by `user-id`."
7169
[db user-id user]
7270
(usersdb/update-user db user-id user)
73-
(no-content))
71+
(status/no-content))
7472

7573
(defn- delete-user-handler
7674
"Delete existing user handler."
7775
[db user-id]
7876
(usersdb/delete-user db user-id)
79-
(no-content))
77+
(status/no-content))
8078

8179
;; Define routes
8280
(defn users-routes

src/education/http/routes.clj

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
(ns education.http.routes
22
(:require [compojure.api.exception :as ex]
33
[compojure.api.sweet :refer [api context]]
4-
[education.http.constants
5-
:refer
6-
[bad-request-error-message
7-
conflict-error-message
8-
not-found-error-message
9-
server-error-message]]
4+
[education.http.constants :as const]
105
[education.http.endpoints.articles :refer [articles-routes]]
116
[education.http.endpoints.roles :refer [roles-routes]]
127
[education.http.endpoints.users :refer [users-routes]]
13-
[ring.util.http-response
14-
:refer
15-
[bad-request conflict internal-server-error not-found]])
8+
[ring.util.http-response :as status])
169
(:import java.sql.SQLException))
1710

1811
(defn sql-exception-handler
1912
"Database exception mapper."
2013
[]
2114
(fn [^SQLException e _ _]
2215
(case (.getSQLState e)
23-
"23505" (conflict {:message conflict-error-message})
24-
"23503" (not-found {:message not-found-error-message})
25-
"23502" (bad-request {:message bad-request-error-message})
26-
(internal-server-error {:message (.getServerErrorMessage e)
27-
:error_code (.getSQLState e)}))))
16+
"23505" (status/conflict {:message const/conflict-error-message})
17+
"23503" (status/not-found {:message const/not-found-error-message})
18+
"23502" (status/bad-request {:message const/bad-request-error-message})
19+
(status/internal-server-error {:message (.getServerErrorMessage e)
20+
:error_code (.getSQLState e)}))))
2821

2922
(defn request-validation-handler
3023
"Verify request body and raise error."
3124
[]
3225
(fn [^Exception e _ _]
33-
(bad-request {:message bad-request-error-message
34-
:details (.getMessage e)})))
26+
(status/bad-request {:message const/bad-request-error-message
27+
:details (.getMessage e)})))
3528

3629
(defn response-validation-handler
3730
"Return error in case of invalid response."
3831
[]
3932
(fn [^Exception e _ _]
40-
(internal-server-error
41-
{:message server-error-message
33+
(status/internal-server-error
34+
{:message const/server-error-message
4235
:details (.getMessage e)})))
4336

4437
(defn api-routes

test/education/database/articles_test.clj

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns education.database.articles-test
2-
(:require [clojure.test :refer :all]
2+
(:require [clojure.test :refer [testing is deftest]]
33
[education.database.articles :as sut]
44
[education.test-data :refer [auth-user-deserialized]]
55
[next.jdbc.sql :as sql]
@@ -32,6 +32,7 @@
3232
:articles/created_on (Instant/now)
3333
:articles/updated_on (Instant/now)})
3434

35+
;; TODO: rework this tests
3536
(deftest add-article-test-article-id
3637
(testing "Test return new article ID"
3738
(with-redefs [sql/insert! (fn [_ _ _] test-db-article)]
@@ -40,7 +41,7 @@
4041

4142
(deftest add-article-test-db-query
4243
(testing "Test create article database query"
43-
(with-redefs [sql/insert! (spy/mock (fn [_ _ q] test-db-article))]
44+
(with-redefs [sql/insert! (spy/stub test-db-article)]
4445
(sut/add-article nil test-user test-request-article)
4546
(let [[[_ _ query]] (spy/calls sql/insert!)]
4647
(is (= {:user_id (:id test-user)
@@ -53,7 +54,7 @@
5354

5455
(deftest add-article-test-table-name
5556
(testing "test create article database name"
56-
(with-redefs [sql/insert! (spy/mock (fn [_ t _] test-db-article))]
57+
(with-redefs [sql/insert! (spy/stub test-db-article)]
5758
(sut/add-article nil nil nil)
5859
(let [[[_ table _]] (spy/calls sql/insert!)]
5960
(is (= :articles table))))))
@@ -62,7 +63,7 @@
6263
(testing "Test create article with provided description and is_main_featured"
6364
(let [description "Some custom description"
6465
is-main-featured true]
65-
(with-redefs [sql/insert! (spy/mock (fn [_ _ q] test-db-article))]
66+
(with-redefs [sql/insert! (spy/stub test-db-article)]
6667
(sut/add-article nil
6768
test-user
6869
(-> test-request-article
@@ -80,7 +81,7 @@
8081
(deftest update-article-test-db-query
8182
(testing "Test update article database query"
8283
(let [now (Instant/now)]
83-
(with-redefs [sql/update! (spy/mock (fn [_ _ q _] {:next.jdbc/update-count 1}))]
84+
(with-redefs [sql/update! (spy/stub {:next.jdbc/update-count 1})]
8485
(let [result (sut/update-article nil (:articles/id test-db-article) test-request-article)
8586
[[_ _ query _]] (spy/calls sql/update!)]
8687
(is (= {:title (:title test-request-article)
@@ -92,23 +93,23 @@
9293

9394
(deftest update-article-test-table-name
9495
(testing "Test update article database table name"
95-
(with-redefs [sql/update! (spy/mock (fn [_ t _ _] {:next.jdbc/update-count 1}))]
96-
(let [result (sut/update-article nil nil nil)
96+
(with-redefs [sql/update! (spy/stub {:next.jdbc/update-count 1})]
97+
(let [_ (sut/update-article nil nil nil)
9798
[[_ table _ _]] (spy/calls sql/update!)]
9899
(is (= :articles table))))))
99100

100101
(deftest update-article-test-empty-body
101102
(testing "Test update article with empty body"
102-
(with-redefs [sql/update! (spy/mock (fn [_ _ q _] {:next.jdbc/update-count 1}))]
103-
(let [result (sut/update-article nil nil {})
103+
(with-redefs [sql/update! (spy/stub {:next.jdbc/update-count 1})]
104+
(let [_ (sut/update-article nil nil {})
104105
[[_ _ query _]] (spy/calls sql/update!)]
105106
(is (= (list :updated_on) (keys query)))))))
106107

107108
(deftest update-article-test-article-id
108109
(testing "Test update article database article ID"
109110
(let [article-id 432]
110-
(with-redefs [sql/update! (spy/mock (fn [_ _ _ i] {:next.jdbc/update-count 1}))]
111-
(let [result (sut/update-article nil article-id nil)
111+
(with-redefs [sql/update! (spy/stub {:next.jdbc/update-count 1})]
112+
(let [_ (sut/update-article nil article-id nil)
112113
[[_ _ _ article-id-param]] (spy/calls sql/update!)]
113114
(is (= {:id article-id} article-id-param)))))))
114115

@@ -129,7 +130,7 @@
129130

130131
(deftest get-latest-full-sized-articles-query
131132
(testing "Test get latest full sized articles database query"
132-
(with-redefs [sql/query (fn [_ q] q)]
133+
(with-redefs [sql/query (spy/mock (fn [_ q] q))]
133134
(let [number-param 30
134135
[query number] (sut/get-latest-full-sized-articles nil number-param)]
135136
(is (= (str "SELECT id, user_id, title, body, featured_image, created_on, updated_on, description "
@@ -138,7 +139,7 @@
138139

139140
(deftest get-user-articles-test-default-query
140141
(testing "Test get user articles default database query"
141-
(with-redefs [sql/query (fn [_ q] q)]
142+
(with-redefs [sql/query (spy/mock (fn [_ q] q))]
142143
(let [user-id-param 42
143144
[query user-id limit] (sut/get-user-articles nil user-id-param)]
144145
(is (= (str "SELECT id, user_id, title, featured_image, updated_on, description "
@@ -149,19 +150,19 @@
149150

150151
(deftest get-user-articles-test-query-with-limit
151152
(testing "Test get user articles with custom limit param"
152-
(with-redefs [sql/query (fn [_ q] q)]
153+
(with-redefs [sql/query (spy/mock (fn [_ q] q))]
153154
(let [limit-param 32
154155
[_ _ limit] (sut/get-user-articles nil 1 limit-param)]
155156
(is (= limit-param limit))))))
156157

157158
(deftest get-article-by-id-test-table-name
158159
(testing "Test get article by ID database table name"
159-
(with-redefs [sql/get-by-id (fn [_ t _] t)]
160+
(with-redefs [sql/get-by-id (spy/mock (fn [_ t _] t))]
160161
(is (= :articles (sut/get-article-by-id nil nil))))))
161162

162163
(deftest get-article-by-id-test-article-id-param
163164
(testing "Test get article by ID article-id param"
164-
(with-redefs [sql/get-by-id (fn [_ _ i] i)]
165+
(with-redefs [sql/get-by-id (spy/mock (fn [_ _ i] i))]
165166
(let [article-id-param 42]
166167
(is (= article-id-param (sut/get-article-by-id nil article-id-param)))))))
167168

@@ -178,13 +179,13 @@
178179

179180
(deftest get-last-featured-article-test-no-results
180181
(testing "Get last featured article no results from database"
181-
(with-redefs [sql/query (fn [_ _] [])]
182+
(with-redefs [sql/query (spy/stub [])]
182183
(is (= nil (sut/get-last-featured-article nil))))))
183184

184185
(deftest get-last-featured-article-test-first-result
185186
(testing "Get last featured article should take first result from database"
186187
(let [res [1 2 3 4 5 6]]
187-
(with-redefs [sql/query (fn [_ _] res)]
188+
(with-redefs [sql/query (spy/stub res)]
188189
(is (= (first res) (sut/get-last-featured-article nil)))))))
189190

190191
(deftest get-last-featured-articles-test-query
@@ -202,18 +203,18 @@
202203

203204
(deftest get-last-featured-articles-test-no-results
204205
(testing "Get latest featured article list no results from database"
205-
(with-redefs [sql/query (spy/mock (fn [_ _] []))]
206+
(with-redefs [sql/query (spy/stub [])]
206207
(let [res (sut/get-last-featured-articles nil 3)]
207208
(is (= [] res))))))
208209

209210
(deftest delete-article-test-table-name
210211
(testing "Delete article database table name"
211-
(with-redefs [sql/delete! (fn [_ t _] t)]
212+
(with-redefs [sql/delete! (spy/mock (fn [_ t _] t))]
212213
(is (= :articles (sut/delete-article nil nil))))))
213214

214215
(deftest delete-article-test-article-id-param
215216
(testing "Delete article article-id param"
216-
(with-redefs [sql/delete! (fn [_ _ i] i)]
217+
(with-redefs [sql/delete! (spy/mock (fn [_ _ i] i))]
217218
(let [article-id 42]
218219
(is (= {:id article-id} (sut/delete-article nil article-id)))))))
219220

@@ -231,7 +232,7 @@
231232
user (->> role name vector (assoc auth-user-deserialized :roles))
232233
user-id (:id user)
233234
article-id 44]
234-
(with-redefs [sql/get-by-id (spy/mock (fn [_ _ _] {:articles/user_id user-id}))]
235+
(with-redefs [sql/get-by-id (spy/stub {:articles/user_id user-id})]
235236
(is (= true (sut/can-update? nil user article-id)))
236237
(is (spy/called-once-with? sql/get-by-id nil :articles article-id))))))
237238

@@ -240,6 +241,6 @@
240241
(let [role :guest
241242
user (->> role name vector (assoc auth-user-deserialized :roles))
242243
article-user-id (inc (:id user))]
243-
(with-redefs [sql/get-by-id (spy/mock (fn [_ _ _] {:articles/user_id article-user-id}))]
244+
(with-redefs [sql/get-by-id (spy/stub {:articles/user_id article-user-id})]
244245
(is (= false (sut/can-update? nil user nil)))
245246
(is (spy/called-once-with? sql/get-by-id nil :articles nil))))))

0 commit comments

Comments
 (0)