갈루아의 반서재

Luminus 를 이용한 Clojure 방명록 만들기 (5)


Adding some tests

이제 어플리케이션이 작동할 준비를 마쳤다. 몇 가지 테스트를 하기 위해 test/clj/guestbook/test/db/core.clj 파일을 열어 다음과 같이 업데이트해보자. 

/home/fukaerii/guestbook/test/clj/guestbook/test/db/core.clj

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(ns guestbook.test.db.core
  (:require [guestbook.db.core :refer [*db*] :as db]
            [luminus-migrations.core :as migrations]
            [clojure.test :refer :all]
            [clojure.java.jdbc :as jdbc]
            [guestbook.config :refer [env]]
            [mount.core :as mount]))
 
(use-fixtures
  :once
  (fn [f]
    (mount/start
      #'guestbook.config/env
      #'guestbook.db.core/*db*)
    (migrations/migrate ["migrate"] (select-keys env [:database-url]))
    (f)))
 
(deftest test-message
  (jdbc/with-db-transaction [t-conn *db*]
    (jdbc/db-set-rollback-only! t-conn)
    (let [message {:name "test"
                   :message "test"
                   :timestamp (java.util.Date.)}]
      (is (= 1 (db/save-message! t-conn message)))
      (let [result (db/get-messages t-conn {})]
        (is (= 1 (count result)))
        (is (= message (dissoc (first result) :id))))))
  (is (empty? (db/get-messages))))
cs

이제 작동시켜보자.

터미널 상에서 lein test 명령을 실행시켜 데이터베이스와의 연동작업이 제대로 이루어지고 있는지 확인할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
~/guestbook$ lein test
2018-08-23 09:25:16,120 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider
 
lein test guestbook.test.db.core
2018-08-23 09:25:18,323 [main] INFO  migratus.core - Starting migrations
2018-08-23 09:25:18,387 [main] DEBUG migratus.migrations - Looking for migrations in #object[java.io.File 0xc9a022f /home/fukaerii/guestbook/resources/migrations]
2018-08-23 09:25:18,397 [main] WARN  migratus.migrations - skipping: '20180821033932-add-users-table.up.sqles' migrations must match pattern: ^(\d+)-([^\.]+)((?:\.[^\.]+)+)$
2018-08-23 09:25:18,399 [main] INFO  migratus.core - Ending migrations
 
lein test guestbook.test.handler
 
Ran 2 tests containing 4 assertions.
0 failures, 0 errors.
cs

이 과정에서 에러가 발생하는 경우 다음 링크를 참조한다. 

Test failed in clojure luminus guestbook tutorial

출처: http://www.antilibrary.org/1818 [Alex’s Antilibrary : My collection of unread books]


Luminus 는 기본적으로 lein-test-refresh 명령을 사용할 수 있게 되어있다. 이 플러그인은 네임스페이스상의 변화를 자동으로 감지하여 지속적으로 테스트를 진행한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
*********************************************
*************** Running tests ***************
:reloading (guestbook.test.db.core)
 
Testing guestbook.test.db.core
2018-08-23 08:32:20,405 [main] INFO  migratus.core - Starting migrations
2018-08-23 08:32:20,427 [main] DEBUG migratus.migrations - Looking for migrations in #object[java.io.File 0x4798dff0 /home/fukaerii/guestbook/resources/migrations]
2018-08-23 08:32:20,429 [main] WARN  migratus.migrations - skipping: '20180821033932-add-users-table.up.sqles' migrations must match pattern: ^(\d+)-([^\.]+)((?:\.[^\.]+)+)$
2018-08-23 08:32:20,433 [main] INFO  migratus.core - Ending migrations
 
Testing guestbook.test.handler
 
Ran 2 tests containing 4 assertions.
0 failures, 0 errors.
 
Passed all tests
Finished at 08:32:20.448 (run time: 0.110s)
cs


Packaging the application

어플리케이션은 개별배포를 위해 다음의 명령을 통해 패키징이 가능하다. 

1
lein uberjar
cs

이를 통해 다음과 같이 실행할 수 있는 jar 를 만들 수 있다.

1
2
3
4
5
export DATABASE_URL="jdbc:h2:./guestbook_dev.db"
 
java -jar target/uberjar/guestbook.jar
 
 
cs

다만 jar 로 실행할 때는 DATABASE_URL 환경변수를 제공해야한다는 점을 기억해야 한다. 


최종완성본을 구동해보자. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~/guestbook$ lein run
2018-08-23 09:29:30,413 [main] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Slf4jLoggerProvider
2018-08-23 09:29:32,126 [main] INFO  guestbook.env -
-=[guestbook started successfully using the development profile]=-
2018-08-23 09:29:32,306 [main] INFO  luminus.http-server - starting HTTP server on port 3000
2018-08-23 09:29:32,366 [main] DEBUG io.undertow - starting undertow server io.undertow.Undertow@2c542a2a
2018-08-23 09:29:32,385 [main] INFO  org.xnio - XNIO version 3.3.6.Final
2018-08-23 09:29:32,639 [main] DEBUG io.undertow - Configuring listener with protocol HTTP for interface 0.0.0.0 and port 3000
2018-08-23 09:29:32,707 [main] INFO  org.projectodd.wunderboss.web.Web - Registered web context /
2018-08-23 09:29:32,708 [main] INFO  guestbook.nrepl - starting nREPL server on port 7000
2018-08-23 09:29:32,738 [main] INFO  guestbook.core - #'guestbook.db.core/*db* started
2018-08-23 09:29:32,739 [main] INFO  guestbook.core - #'guestbook.handler/init-app started
2018-08-23 09:29:32,739 [main] INFO  guestbook.core - #'guestbook.handler/app started
2018-08-23 09:29:32,739 [main] INFO  guestbook.core - #'guestbook.core/http-server started
2018-08-23 09:29:32,739 [main] INFO  guestbook.core - #'guestbook.core/repl-server started
 
 
cs