From ab5049fd1fedbaadb052c2378c09a5be81da5814 Mon Sep 17 00:00:00 2001 From: Artur Taranchiev Date: Sun, 28 Jul 2024 18:26:01 +0600 Subject: [PATCH 1/4] Add some tests --- url-shortener/project.clj | 33 +++++------ .../test/url_shortener/core_test.clj | 58 +++++++++++++++++++ url-shortener/test/url_shortener/web_test.clj | 49 ++++++++++++++++ 3 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 url-shortener/test/url_shortener/core_test.clj create mode 100644 url-shortener/test/url_shortener/web_test.clj diff --git a/url-shortener/project.clj b/url-shortener/project.clj index ffd0baa..7029980 100644 --- a/url-shortener/project.clj +++ b/url-shortener/project.clj @@ -1,34 +1,29 @@ (defproject url-shortener "0.1.0-SNAPSHOT" :description "URL shortener app" - :source-paths ["src" "resources"] :resource-paths ["resources"] - - :dependencies [ - ;; Backend + :dependencies [;; Backend [org.clojure/clojure "1.11.1"] [ring/ring-jetty-adapter "1.11.0"] [ring/ring-json "0.5.1"] [compojure "1.7.1"] [org.slf4j/slf4j-simple "2.0.10"] - ;; Frontend [reagent "1.2.0"] [org.clojure/core.async "1.6.681"] - [cljs-http "0.1.48" - :exclusions [org.clojure/core.async - com.cognitect/transit-cljs - com.cognitect/transit-js]] - [thheller/shadow-cljs "2.27.2"] ; Keep it synced with npm version! - ] - + [cljs-http + "0.1.48" + :exclusions + [org.clojure/core.async + com.cognitect/transit-cljs + com.cognitect/transit-js]] + [thheller/shadow-cljs "2.27.2"] ; Keep it synced with npm + ; version! + ;; Testing + [nubank/matcher-combinators "3.8.5"]] :main ^:skip-aot url-shortener.core - :uberjar-name "url-shortener.jar" - - :profiles {:dev {:source-paths ["dev"] - :dependencies [[ring/ring-devel "1.11.0"]]} - - :repl {:repl-options {:init-ns user}} - + :profiles {:dev {:source-paths ["dev"], + :dependencies [[ring/ring-devel "1.11.0"]]}, + :repl {:repl-options {:init-ns user}}, :uberjar {:aot :all}}) diff --git a/url-shortener/test/url_shortener/core_test.clj b/url-shortener/test/url_shortener/core_test.clj new file mode 100644 index 0000000..286d66e --- /dev/null +++ b/url-shortener/test/url_shortener/core_test.clj @@ -0,0 +1,58 @@ +(ns url-shortener.core-test + (:require [clojure.test :refer + [are deftest is run-tests testing use-fixtures]] + [matcher-combinators.test] + [url-shortener.core :refer + [alphabet-size id->int int->id list-all shorten! url-for]])) + +(defn reset-state + [t] + (with-redefs [url-shortener.core/*counter (atom 0) + url-shortener.core/*mapping (ref {})] + (t))) + +(use-fixtures :once reset-state) + +(deftest test-int->id + (are [input expected] (= (int->id input) expected) + 0 "0" + alphabet-size "10" + 9999999999999 "2q3Rktod" + 9223372036854775807 "AzL8n0Y58W7") + (is (thrown-with-msg? Exception + #"cannot be cast to class java.lang.Number" + (int->id "asdf")))) + +(deftest test-id->int + (are [input expected] (= (id->int input) expected) + "0" 0 + "z" 61 + "clj" 149031 + "Clojure" 725410830262)) + +(deftest core + (testing "Empty list" (is (= (list-all) []))) + (testing "Shorten positive" + (are [args result] (= (apply shorten! args) result) + ["http://clojurebook.com"] "1" + ["http://clojurebook.com"] "2" + ["https://clojure.org" "clj"] "clj" + ["https://clojure.org" "clj"] nil + ["http://id-already-exists.com" "clj"] nil + ["https://clojurescript.org" "cljs"] "cljs")) + (testing "Getting urls" + (are [id result] (= (url-for id) result) + "1" "http://clojurebook.com" + "2" "http://clojurebook.com" + "cljs" "https://clojurescript.org" + "clj" "https://clojure.org")) + (testing "List all" + (is (match? (list-all) + [{:id "1", :url "http://clojurebook.com"} + {:id "2", :url "http://clojurebook.com"} + {:id "clj", :url "https://clojure.org"} + {:id "cljs", :url "https://clojurescript.org"}])))) + + +;; Run the tests +(run-tests) diff --git a/url-shortener/test/url_shortener/web_test.clj b/url-shortener/test/url_shortener/web_test.clj new file mode 100644 index 0000000..7208c16 --- /dev/null +++ b/url-shortener/test/url_shortener/web_test.clj @@ -0,0 +1,49 @@ +(ns url-shortener.web-test + (:require [clojure.test :refer [are deftest testing use-fixtures]] + [matcher-combinators.test] + [url-shortener.core-test :refer [reset-state]] + [url-shortener.web :refer [handler router]])) + +(use-fixtures :once reset-state) + +(deftest ^:integration test-handler + (testing "Handlers" + (are [request response] (match? (handler request) response) + ; Post + {:uri "/", + :request-method :post, + :params + {:url + "https://github.com/Clojure-Developer/Clojure-Developer-2022-10"}} + {:status 201, + :headers {"Location" "1", + "Content-Type" "application/json; charset=utf-8"}, + :body "{\"id\":\"1\"}"} + ; Put clj + {:uri "/clj", :request-method :put, :params {:url "https://clojure.org"}} + {:status 201, + :headers {"Location" "clj", + "Content-Type" "application/json; charset=utf-8"}, + :body "{\"id\":\"clj\"}"} + ; Negative, duplicate + {:uri "/clj", :request-method :put, :params {:url "https://clojure.org"}} + {:status 409, + :headers {"Content-Type" "application/json; charset=utf-8"}, + :body "{\"error\":\"Short URL clj is already taken\"}"} + ; Get result + {:uri "/clj", :request-method :get} {:status 302, + :headers {"Location" + "https://clojure.org"}, + :body ""})) + (testing "Routers" + (are [request response] (match? (router request) response) + ; List + {:uri "/list/", :request-method :get} + {:status 200, + :headers {}, + :body + {:urls + [{:id "1", + :url + "https://github.com/Clojure-Developer/Clojure-Developer-2022-10"} + {:id "clj", :url "https://clojure.org"}]}}))) From 9c84aa0ada6aad669a5448a2a23bc09f1fa33f8a Mon Sep 17 00:00:00 2001 From: Artur Taranchiev Date: Sun, 28 Jul 2024 21:36:48 +0600 Subject: [PATCH 2/4] some more tests --- .../test/url_shortener/core_test.clj | 25 ++++++++++--------- .../test/url_shortener/frontend_test.cljs | 7 ++++++ 2 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 url-shortener/test/url_shortener/frontend_test.cljs diff --git a/url-shortener/test/url_shortener/core_test.clj b/url-shortener/test/url_shortener/core_test.clj index 286d66e..eed7f37 100644 --- a/url-shortener/test/url_shortener/core_test.clj +++ b/url-shortener/test/url_shortener/core_test.clj @@ -18,7 +18,8 @@ 0 "0" alphabet-size "10" 9999999999999 "2q3Rktod" - 9223372036854775807 "AzL8n0Y58W7") + 9223372036854775807 "AzL8n0Y58W7" + -1 "") (is (thrown-with-msg? Exception #"cannot be cast to class java.lang.Number" (int->id "asdf")))) @@ -28,22 +29,25 @@ "0" 0 "z" 61 "clj" 149031 - "Clojure" 725410830262)) + "Clojure" 725410830262 + "" 0)) (deftest core (testing "Empty list" (is (= (list-all) []))) (testing "Shorten positive" (are [args result] (= (apply shorten! args) result) - ["http://clojurebook.com"] "1" - ["http://clojurebook.com"] "2" - ["https://clojure.org" "clj"] "clj" - ["https://clojure.org" "clj"] nil + ["http://clojurebook.com"] "1" + ["http://clojurebook.com"] "2" + ["https://clojure.org" "clj"] "clj" + ["https://clojure.org" "clj"] nil ["http://id-already-exists.com" "clj"] nil - ["https://clojurescript.org" "cljs"] "cljs")) + ["https://clojurescript.org" "cljs"] "cljs" + [""] "3")) (testing "Getting urls" (are [id result] (= (url-for id) result) "1" "http://clojurebook.com" "2" "http://clojurebook.com" + "3" "" "cljs" "https://clojurescript.org" "clj" "https://clojure.org")) (testing "List all" @@ -51,8 +55,5 @@ [{:id "1", :url "http://clojurebook.com"} {:id "2", :url "http://clojurebook.com"} {:id "clj", :url "https://clojure.org"} - {:id "cljs", :url "https://clojurescript.org"}])))) - - -;; Run the tests -(run-tests) + {:id "cljs", :url "https://clojurescript.org"} + {:id "3", :url ""}])))) diff --git a/url-shortener/test/url_shortener/frontend_test.cljs b/url-shortener/test/url_shortener/frontend_test.cljs new file mode 100644 index 0000000..a36898e --- /dev/null +++ b/url-shortener/test/url_shortener/frontend_test.cljs @@ -0,0 +1,7 @@ +(ns url-shortener.frontend-test + (:require [clojure.test :refer [deftest is]] + [url-shortener.frontend :refer [header]])) + +; pretty useless I think +(deftest test-header + (is (= (header "Test") [:h2 {:style {:margin "8px 4px"}} "Test"]))) From 7b471452fece6dfcc39e33226ebe341a5bc5e6b2 Mon Sep 17 00:00:00 2001 From: Artur Taranchiev Date: Sun, 28 Jul 2024 21:45:37 +0600 Subject: [PATCH 3/4] some more --- url-shortener/test/url_shortener/core_test.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/url-shortener/test/url_shortener/core_test.clj b/url-shortener/test/url_shortener/core_test.clj index eed7f37..31483dc 100644 --- a/url-shortener/test/url_shortener/core_test.clj +++ b/url-shortener/test/url_shortener/core_test.clj @@ -22,7 +22,8 @@ -1 "") (is (thrown-with-msg? Exception #"cannot be cast to class java.lang.Number" - (int->id "asdf")))) + (int->id "asdf"))) + (is (thrown-with-msg? Exception #"because [\"]x[\"] is null" (int->id nil)))) (deftest test-id->int (are [input expected] (= (id->int input) expected) From 33471c37b105c96886e3bac8bdf113ca62798713 Mon Sep 17 00:00:00 2001 From: Artur Taranchiev Date: Sun, 28 Jul 2024 22:32:25 +0600 Subject: [PATCH 4/4] cleanup --- url-shortener/test/url_shortener/core_test.clj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/url-shortener/test/url_shortener/core_test.clj b/url-shortener/test/url_shortener/core_test.clj index 31483dc..97c37a0 100644 --- a/url-shortener/test/url_shortener/core_test.clj +++ b/url-shortener/test/url_shortener/core_test.clj @@ -1,6 +1,5 @@ (ns url-shortener.core-test - (:require [clojure.test :refer - [are deftest is run-tests testing use-fixtures]] + (:require [clojure.test :refer [are deftest is testing use-fixtures]] [matcher-combinators.test] [url-shortener.core :refer [alphabet-size id->int int->id list-all shorten! url-for]]))