|
1 | 1 | (ns education.database.articles-test
|
2 |
| - (:require [clojure.test :refer :all] |
| 2 | + (:require [clojure.test :refer [testing is deftest]] |
3 | 3 | [education.database.articles :as sut]
|
4 | 4 | [education.test-data :refer [auth-user-deserialized]]
|
5 | 5 | [next.jdbc.sql :as sql]
|
|
32 | 32 | :articles/created_on (Instant/now)
|
33 | 33 | :articles/updated_on (Instant/now)})
|
34 | 34 |
|
| 35 | +;; TODO: rework this tests |
35 | 36 | (deftest add-article-test-article-id
|
36 | 37 | (testing "Test return new article ID"
|
37 | 38 | (with-redefs [sql/insert! (fn [_ _ _] test-db-article)]
|
|
40 | 41 |
|
41 | 42 | (deftest add-article-test-db-query
|
42 | 43 | (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)] |
44 | 45 | (sut/add-article nil test-user test-request-article)
|
45 | 46 | (let [[[_ _ query]] (spy/calls sql/insert!)]
|
46 | 47 | (is (= {:user_id (:id test-user)
|
|
53 | 54 |
|
54 | 55 | (deftest add-article-test-table-name
|
55 | 56 | (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)] |
57 | 58 | (sut/add-article nil nil nil)
|
58 | 59 | (let [[[_ table _]] (spy/calls sql/insert!)]
|
59 | 60 | (is (= :articles table))))))
|
|
62 | 63 | (testing "Test create article with provided description and is_main_featured"
|
63 | 64 | (let [description "Some custom description"
|
64 | 65 | 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)] |
66 | 67 | (sut/add-article nil
|
67 | 68 | test-user
|
68 | 69 | (-> test-request-article
|
|
80 | 81 | (deftest update-article-test-db-query
|
81 | 82 | (testing "Test update article database query"
|
82 | 83 | (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})] |
84 | 85 | (let [result (sut/update-article nil (:articles/id test-db-article) test-request-article)
|
85 | 86 | [[_ _ query _]] (spy/calls sql/update!)]
|
86 | 87 | (is (= {:title (:title test-request-article)
|
|
92 | 93 |
|
93 | 94 | (deftest update-article-test-table-name
|
94 | 95 | (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) |
97 | 98 | [[_ table _ _]] (spy/calls sql/update!)]
|
98 | 99 | (is (= :articles table))))))
|
99 | 100 |
|
100 | 101 | (deftest update-article-test-empty-body
|
101 | 102 | (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 {}) |
104 | 105 | [[_ _ query _]] (spy/calls sql/update!)]
|
105 | 106 | (is (= (list :updated_on) (keys query)))))))
|
106 | 107 |
|
107 | 108 | (deftest update-article-test-article-id
|
108 | 109 | (testing "Test update article database article ID"
|
109 | 110 | (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) |
112 | 113 | [[_ _ _ article-id-param]] (spy/calls sql/update!)]
|
113 | 114 | (is (= {:id article-id} article-id-param)))))))
|
114 | 115 |
|
|
129 | 130 |
|
130 | 131 | (deftest get-latest-full-sized-articles-query
|
131 | 132 | (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))] |
133 | 134 | (let [number-param 30
|
134 | 135 | [query number] (sut/get-latest-full-sized-articles nil number-param)]
|
135 | 136 | (is (= (str "SELECT id, user_id, title, body, featured_image, created_on, updated_on, description "
|
|
138 | 139 |
|
139 | 140 | (deftest get-user-articles-test-default-query
|
140 | 141 | (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))] |
142 | 143 | (let [user-id-param 42
|
143 | 144 | [query user-id limit] (sut/get-user-articles nil user-id-param)]
|
144 | 145 | (is (= (str "SELECT id, user_id, title, featured_image, updated_on, description "
|
|
149 | 150 |
|
150 | 151 | (deftest get-user-articles-test-query-with-limit
|
151 | 152 | (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))] |
153 | 154 | (let [limit-param 32
|
154 | 155 | [_ _ limit] (sut/get-user-articles nil 1 limit-param)]
|
155 | 156 | (is (= limit-param limit))))))
|
156 | 157 |
|
157 | 158 | (deftest get-article-by-id-test-table-name
|
158 | 159 | (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))] |
160 | 161 | (is (= :articles (sut/get-article-by-id nil nil))))))
|
161 | 162 |
|
162 | 163 | (deftest get-article-by-id-test-article-id-param
|
163 | 164 | (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))] |
165 | 166 | (let [article-id-param 42]
|
166 | 167 | (is (= article-id-param (sut/get-article-by-id nil article-id-param)))))))
|
167 | 168 |
|
|
178 | 179 |
|
179 | 180 | (deftest get-last-featured-article-test-no-results
|
180 | 181 | (testing "Get last featured article no results from database"
|
181 |
| - (with-redefs [sql/query (fn [_ _] [])] |
| 182 | + (with-redefs [sql/query (spy/stub [])] |
182 | 183 | (is (= nil (sut/get-last-featured-article nil))))))
|
183 | 184 |
|
184 | 185 | (deftest get-last-featured-article-test-first-result
|
185 | 186 | (testing "Get last featured article should take first result from database"
|
186 | 187 | (let [res [1 2 3 4 5 6]]
|
187 |
| - (with-redefs [sql/query (fn [_ _] res)] |
| 188 | + (with-redefs [sql/query (spy/stub res)] |
188 | 189 | (is (= (first res) (sut/get-last-featured-article nil)))))))
|
189 | 190 |
|
190 | 191 | (deftest get-last-featured-articles-test-query
|
|
202 | 203 |
|
203 | 204 | (deftest get-last-featured-articles-test-no-results
|
204 | 205 | (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 [])] |
206 | 207 | (let [res (sut/get-last-featured-articles nil 3)]
|
207 | 208 | (is (= [] res))))))
|
208 | 209 |
|
209 | 210 | (deftest delete-article-test-table-name
|
210 | 211 | (testing "Delete article database table name"
|
211 |
| - (with-redefs [sql/delete! (fn [_ t _] t)] |
| 212 | + (with-redefs [sql/delete! (spy/mock (fn [_ t _] t))] |
212 | 213 | (is (= :articles (sut/delete-article nil nil))))))
|
213 | 214 |
|
214 | 215 | (deftest delete-article-test-article-id-param
|
215 | 216 | (testing "Delete article article-id param"
|
216 |
| - (with-redefs [sql/delete! (fn [_ _ i] i)] |
| 217 | + (with-redefs [sql/delete! (spy/mock (fn [_ _ i] i))] |
217 | 218 | (let [article-id 42]
|
218 | 219 | (is (= {:id article-id} (sut/delete-article nil article-id)))))))
|
219 | 220 |
|
|
231 | 232 | user (->> role name vector (assoc auth-user-deserialized :roles))
|
232 | 233 | user-id (:id user)
|
233 | 234 | 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})] |
235 | 236 | (is (= true (sut/can-update? nil user article-id)))
|
236 | 237 | (is (spy/called-once-with? sql/get-by-id nil :articles article-id))))))
|
237 | 238 |
|
|
240 | 241 | (let [role :guest
|
241 | 242 | user (->> role name vector (assoc auth-user-deserialized :roles))
|
242 | 243 | 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})] |
244 | 245 | (is (= false (sut/can-update? nil user nil)))
|
245 | 246 | (is (spy/called-once-with? sql/get-by-id nil :articles nil))))))
|
0 commit comments