Skip to content

Commit

Permalink
Merge pull request #4 from nilenso/feature/add_components_in_test
Browse files Browse the repository at this point in the history
Update test fixtures
  • Loading branch information
zzat authored Dec 21, 2023
2 parents 3484530 + 5cd54f9 commit 8f13fb0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 28 deletions.
6 changes: 4 additions & 2 deletions swift-ticketing/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
:plugins [[lein-ring "0.12.6"]]
:ring {:handler swift-ticketing.app/swift-ticketing-app}
:aliases {"migrate" ["run" "-m" "swift-ticketing.migrations/migrate"]
"rollback" ["run" "-m" "swift-ticketing.migrations/rollback"]}
"rollback" ["run" "-m" "swift-ticketing.migrations/rollback"]
"kaocha" ["run" "-m" "kaocha.runner"]}
:profiles {:uberjar {:aot :all
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}})
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}
:dev {:dependencies [[lambdaisland/kaocha "1.87.1366"]]}})
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[ring.adapter.jetty :refer [run-jetty]]
[swift-ticketing.app :as app]))

(defrecord HTTPServer [port database worker]
(defrecord HTTPServer [port join? database worker]
component/Lifecycle

(start [component]
Expand All @@ -13,13 +13,14 @@
server (run-jetty
(app/swift-ticketing-app connection message-queue)
{:port port
:join? true})]
:join? join?})]
(assoc component :http-server server)))

(stop [component]
(println ";; Stopping API Server")
(.stop (:http-server component))
(assoc component :http-server nil)))

(defn new-http-server [port]
(map->HTTPServer {:port port}))
(defn new-http-server [port join?]
(map->HTTPServer {:port port
:join? join?}))
3 changes: 2 additions & 1 deletion swift-ticketing/src/swift_ticketing/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
(component/system-map
:database (new-database (:database config))
:app (component/using
(new-http-server (get-in config [:server :port]))
(new-http-server (get-in config [:server :port])
(get-in config [:server :join?]))
[:database :worker])
:worker (component/using
(new-worker 5 redis-opts)
Expand Down
82 changes: 71 additions & 11 deletions swift-ticketing/test/swift_ticketing/fixtures.clj
Original file line number Diff line number Diff line change
@@ -1,20 +1,80 @@
(ns swift-ticketing.fixtures
(:require
[next.jdbc :as jdbc]
[honey.sql :as sql]
[swift-ticketing.core :refer [create-connection-pool]]
[com.stuartsierra.component :as component]
[swift-ticketing.core :refer [swift-ticketing-system]]
[swift-ticketing.factory :as factory]
[swift-ticketing.config :refer [read-test-config]]
[swift-ticketing.migrations :as migrations]))
[swift-ticketing.migrations :as migrations]
[next.jdbc :as jdbc]
[honey.sql :as sql]))

(def test-env
(let [db-config (:database (read-test-config))]
{:db-spec (create-connection-pool db-config)}))
{:test-user-id nil
:db-spec nil})

(def swift-ticketing-test-system
(swift-ticketing-system (read-test-config)))

(defn start-test-system []
(alter-var-root #'swift-ticketing-test-system component/start))

(defn stop-test-system []
(alter-var-root #'swift-ticketing-test-system component/stop))

(defn run-migrations [tests]
(let [db-config (:database (read-test-config))]
(defn run-with-test-system [tests]
;; Init db connection pool, start workers
(println "start-test-system")
(start-test-system)
(println "after start-test-system")
(let [db (:database swift-ticketing-test-system)
db-config (:db-config db)
conn (:connection db)]
;; setup tables
(migrations/migrate-with db-config)
(let [test-user-id (factory/add-user-table-entry (:db-spec test-env))]
(alter-var-root #'test-env #(assoc % :test-user-id test-user-id)))
(let [test-user-id (factory/add-user-table-entry conn)]
;; add a user, update it in test env
(alter-var-root
#'test-env #(assoc %
:test-user-id test-user-id
:db-spec conn)))
;; run tests
(tests)
(migrations/rollback-with db-config)))
;; rollback db
(migrations/rollback-with db-config)
;; stop db connections, workers
(stop-test-system)))

(defn setup-test-system [test-plan]
;; Init db connection pool, start workers
(start-test-system)
(let [db (:database swift-ticketing-test-system)
db-config (:db-config db)
conn (:connection db)]
;; setup tables
(migrations/migrate-with db-config)
(let [test-user-id (factory/add-user-table-entry conn)]
;; add a user, update it in test env
(alter-var-root
#'test-env #(assoc %
:test-user-id test-user-id
:db-spec conn))))
test-plan)

(defn teardown-test-system [test-plan]
(let [db (:database swift-ticketing-test-system)
db-config (:db-config db)]
;; rollback db
(migrations/rollback-with db-config)
;; stop db connections, workers
(stop-test-system))
test-plan)

(defn truncate-tables [db-spec]
(let [tables [:ticket :ticket_type :booking :event]
truncate-fn #(sql/format {:truncate [% [:raw "cascade"]]})]
(doseq [table tables]
(jdbc/execute! db-spec (truncate-fn table)))))

(defn clear-tables [tests]
(truncate-tables (:db-spec test-env))
(tests))
14 changes: 4 additions & 10 deletions swift-ticketing/test/swift_ticketing/handlers_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
[next.jdbc :as jdbc]
[clojure.data.json :as json]
[next.jdbc.result-set :as rs]
[clojure.set :as s]
[clojure.walk :refer [keywordize-keys stringify-keys]]
[clojure.walk :refer [keywordize-keys]]
[swift-ticketing.app :refer [swift-ticketing-app]]
[swift-ticketing.fixtures :as fixtures]
[swift-ticketing.factory :as factory]
[swift-ticketing.utils :as utils]
[swift-ticketing.db.event :as db-event]
[swift-ticketing.db.ticket :as ticket]
[swift-ticketing.client :as client]
[swift-ticketing.specs :as specs]))
[swift-ticketing.client :as client]))

(use-fixtures :each fixtures/run-migrations)
(use-fixtures :each fixtures/clear-tables)

(deftest create-event-test
(let [{:keys [db-spec]} fixtures/test-env]
Expand Down Expand Up @@ -116,6 +114,7 @@
(defn create-ticket-test* [event-id ticket-request-fn]
(testing "with valid request"
(let [{:keys [request response status]} (client/create-tickets event-id (ticket-request-fn))
{:keys [db-spec test-user-id]} fixtures/test-env
ticket-type-id (get response "ticket-type-id")
created-tickets (jdbc/execute! db-spec (ticket/get-unbooked-tickets ticket-type-id) {:builder-fn rs/as-unqualified-maps})
tickets (get response "tickets")
Expand Down Expand Up @@ -150,8 +149,3 @@
(create-ticket-test* event-id factory/general-ticket-request))
(testing "Creating ticket (Seated)"
(create-ticket-test* event-id factory/seated-ticket-request))))

; (run-tests)
(run-test create-event-test)
(run-test list-events-test)
(run-test get-event-test)
6 changes: 6 additions & 0 deletions swift-ticketing/tests.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#kaocha/v1
{:fail-fast? false
:plugins [:hooks]
:kaocha.hooks/pre-run [swift-ticketing.fixtures/setup-test-system]
:kaocha.hooks/post-run [swift-ticketing.fixtures/teardown-test-system]
}

0 comments on commit 8f13fb0

Please sign in to comment.