-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nilenso/feature/add_components_in_test
Update test fixtures
- Loading branch information
Showing
6 changed files
with
92 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |