Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/175 remove testing utilities from production code #228

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Breaking changes:
- changed interceptors behaviour making them similar to pedestal and sieppari
- changed error handling
- addded deps.edn
- a bunch of other small fixes.
- a bunch of other small fixes
- removed `docker-postgres!` function.

## 0.5-rc1
Breaking change: rename almost all namespaces from `framework..` to `xiana..`).
23 changes: 23 additions & 0 deletions dev/utils.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns utils
(:require
[clj-test-containers.core :as tc]))

(defn docker-postgres!
[{pg-config :xiana/postgresql :as config}]
(let [{:keys [dbname user password image-name]} pg-config
container (-> (tc/create
{:image-name image-name
:exposed-ports [5432]
:env-vars {"POSTGRES_DB" dbname
"POSTGRES_USER" user
"POSTGRES_PASSWORD" password}})
(tc/start!))
port (get (:mapped-ports container) 5432)
pg-config (assoc
pg-config
:port port
:embedded container
:subname (str "//localhost:" port "/" dbname))]
(tc/wait {:wait-strategy :log
:message "accept connections"} (:container container))
(assoc config :xiana/postgresql pg-config)))
2 changes: 2 additions & 0 deletions examples/acl/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
:prod {:resource-paths ["config/prod"]}
:test {:resource-paths ["config/test"]
:dependencies [[clj-http "3.12.3"]
[clj-test-containers "0.6.0"]
[org.testcontainers/testcontainers "1.16.3"]
[mvxcvi/cljstyle "0.15.0"
:exclusions [org.clojure/clojure]]]}}
:aliases {"check-style" ["with-profile" "+test" "run" "-m" "cljstyle.main" "check"]
Expand Down
26 changes: 23 additions & 3 deletions examples/acl/test/acl_fixture.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
(ns acl-fixture
(:require
[acl]
[xiana.config :as config]
[xiana.db :as db]))
[clj-test-containers.core :as tc]
[xiana.config :as config]))

(defn docker-postgres!
[{pg-config :xiana/postgresql :as config}]
(let [{:keys [dbname user password image-name]} pg-config
container (-> (tc/create
{:image-name image-name
:exposed-ports [5432]
:env-vars {"POSTGRES_DB" dbname
"POSTGRES_USER" user
"POSTGRES_PASSWORD" password}})
(tc/start!))
port (get (:mapped-ports container) 5432)
pg-config (assoc
pg-config
:port port
:embedded container
:subname (str "//localhost:" port "/" dbname))]
(tc/wait {:wait-strategy :log
:message "accept connections"} (:container container))
(assoc config :xiana/postgresql pg-config)))

(defn std-system-fixture
[f]
(with-open [_ (-> (config/config)
(merge acl/app-cfg)
db/docker-postgres!
docker-postgres!
acl/->system)]
(f)))
2 changes: 2 additions & 0 deletions examples/cli-chat/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
:prod {:resource-paths ["config/prod"]}
:test {:resource-paths ["config/test"]
:dependencies [[clj-http "3.12.3"]
[clj-test-containers "0.6.0"]
[org.testcontainers/testcontainers "1.16.3"]
[http.async.client "1.3.1"]
[mvxcvi/cljstyle "0.15.0"
:exclusions [org.clojure/clojure]]]}}
Expand Down
26 changes: 23 additions & 3 deletions examples/cli-chat/test/cli_chat_fixture.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
(ns cli-chat-fixture
(:require
[cli-chat.core :refer [->system]]
[xiana.config :as config]
[xiana.db :as db]))
[clj-test-containers.core :as tc]
[xiana.config :as config]))

(defn docker-postgres!
[{pg-config :xiana/postgresql :as config}]
(let [{:keys [dbname user password image-name]} pg-config
container (-> (tc/create
{:image-name image-name
:exposed-ports [5432]
:env-vars {"POSTGRES_DB" dbname
"POSTGRES_USER" user
"POSTGRES_PASSWORD" password}})
(tc/start!))
port (get (:mapped-ports container) 5432)
pg-config (assoc
pg-config
:port port
:embedded container
:subname (str "//localhost:" port "/" dbname))]
(tc/wait {:wait-strategy :log
:message "accept connections"} (:container container))
(assoc config :xiana/postgresql pg-config)))

(defn std-system-fixture
[app-cfg f]
(with-open [_ (-> (config/config)
(merge app-cfg)
db/docker-postgres!
docker-postgres!
->system)]
(f)))
2 changes: 2 additions & 0 deletions examples/sessions/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
:prod {:resource-paths ["config/prod"]}
:test {:resource-paths ["config/test"]
:dependencies [[clj-http "3.12.3"]
[clj-test-containers "0.6.0"]
[org.testcontainers/testcontainers "1.16.3"]
[mvxcvi/cljstyle "0.15.0"
:exclusions [org.clojure/clojure]]]}}
:shadow-cljs {:nrepl {:port 8777}
Expand Down
23 changes: 22 additions & 1 deletion examples/sessions/test/integration_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[app.core :as app]
[clj-http.client :as http]
[clj-test-containers.core :as tc]
[clojure.test :refer [deftest is use-fixtures]]
[jsonista.core :as json]
[next.jdbc :as jdbc]
Expand All @@ -12,6 +13,26 @@
(java.util
UUID)))

(defn docker-postgres!
[{pg-config :xiana/postgresql :as config}]
(let [{:keys [dbname user password image-name]} pg-config
container (-> (tc/create
{:image-name image-name
:exposed-ports [5432]
:env-vars {"POSTGRES_DB" dbname
"POSTGRES_USER" user
"POSTGRES_PASSWORD" password}})
(tc/start!))
port (get (:mapped-ports container) 5432)
pg-config (assoc
pg-config
:port port
:embedded container
:subname (str "//localhost:" port "/" dbname))]
(tc/wait {:wait-strategy :log
:message "accept connections"} (:container container))
(assoc config :xiana/postgresql pg-config)))

(defn merge-connection
[config]
(assoc config :xiana/session-backend
Expand All @@ -23,7 +44,7 @@
(with-open [system (-> (config/config app/app-cfg)
(assoc :xiana/postgresql (:xiana/session-backend (config/config)))
(assoc-in [:xiana/postgresql :image-name] "postgres:14-alpine")
db/docker-postgres!
docker-postgres!
db/connect
merge-connection
app/->system)]
Expand Down
2 changes: 2 additions & 0 deletions examples/state-events/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
:description "FIXME: write description"
:min-lein-version "2.0.0"
:dependencies [[com.flexiana/framework "0.5.0-rc2"]
[clj-test-containers "0.6.0"]
[org.testcontainers/testcontainers "1.16.3"]
[tick "0.5.0-RC5"]]
:main ^:skip-aot state-events.core
:uberjar-name "state-events.jar"
Expand Down
23 changes: 22 additions & 1 deletion examples/state-events/src/backend/state_events/core.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns state-events.core
(:require
[clj-test-containers.core :as tc]
[piotr-yuxuan.closeable-map :refer [closeable-map]]
[reitit.ring :as ring]
[state-events.controller-behaviors.sse :as sseb]
Expand Down Expand Up @@ -60,10 +61,30 @@
["/events" {:get {:action event/raw}}]
["/sse" {:ws-action sse/sse-action}]])

(defn docker-postgres!
[{pg-config :xiana/postgresql :as config}]
(let [{:keys [dbname user password image-name]} pg-config
container (-> (tc/create
{:image-name image-name
:exposed-ports [5432]
:env-vars {"POSTGRES_DB" dbname
"POSTGRES_USER" user
"POSTGRES_PASSWORD" password}})
(tc/start!))
port (get (:mapped-ports container) 5432)
pg-config (assoc
pg-config
:port port
:embedded container
:subname (str "//localhost:" port "/" dbname))]
(tc/wait {:wait-strategy :log
:message "accept connections"} (:container container))
(assoc config :xiana/postgresql pg-config)))

(defn docker?
[state]
(if (get-in state [:xiana/postgresql :image-name])
(db/docker-postgres! state)
(docker-postgres! state)
state))

(defn ->system
Expand Down
7 changes: 4 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
:description "Xiana framework"
:url "https://github.com/Flexiana/framework"
:license {:name "FIXME" :url "FIXME"}
:plugins [[lein-pprint "1.1.1"]]
:dependencies [[org.clojure/clojure "1.11.1"]
[org.clojure/core.async "1.5.648"]
[org.clojure/data.json "2.4.0"] ; TODO: remove this or jsonista
Expand All @@ -18,8 +19,6 @@
[nilenso/honeysql-postgres "0.4.112"]
[org.postgresql/postgresql "42.3.3"]
[seancorfield/next.jdbc "1.2.659"]
[clj-test-containers "0.6.0"]
[org.testcontainers/testcontainers "1.16.3"]
[yogthos/config "1.2.0"]
[com.taoensso/timbre "5.2.1"]]
:source-paths ["src"]
Expand All @@ -36,7 +35,7 @@
:local {:resource-paths ["config/local"]}
:prod {:resource-paths ["config/prod"]}
:cljstyle {:dependencies []}
:test {:source-paths ["test"]
:test {:source-paths ["test" "dev"]
:resource-paths ["config/test"]
:dependencies [[lambdaisland/kaocha "1.64.1010"]
[clj-http "3.12.3"]
Expand All @@ -45,6 +44,8 @@
[mvxcvi/cljstyle "0.15.0"
:exclusions [org.clojure/clojure]]
[clj-kondo "2022.03.09"]
[clj-test-containers "0.6.0"]
[org.testcontainers/testcontainers "1.16.3"]
[nubank/matcher-combinators "3.3.1"]]}}
:aliases {"check-style" ["with-profile" "+test" "run" "-m" "cljstyle.main" "check"]
"fix-style" ["with-profile" "+test" "run" "-m" "cljstyle.main" "fix"]
Expand Down
21 changes: 0 additions & 21 deletions src/xiana/db.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns xiana.db
"Data source builder"
(:require
[clj-test-containers.core :as tc]
[honeysql-postgres.format]
[honeysql.core :as sql]
[jsonista.core :as json]
Expand Down Expand Up @@ -95,26 +94,6 @@
(get-datasource config (inc count))
(throw e)))))))

(defn docker-postgres!
[{pg-config :xiana/postgresql :as config}]
(let [{:keys [dbname user password image-name]} pg-config
container (-> (tc/create
{:image-name image-name
:exposed-ports [5432]
:env-vars {"POSTGRES_DB" dbname
"POSTGRES_USER" user
"POSTGRES_PASSWORD" password}})
(tc/start!))
port (get (:mapped-ports container) 5432)
pg-config (assoc
pg-config
:port port
:embedded container
:subname (str "//localhost:" port "/" dbname))]
(tc/wait {:wait-strategy :log
:message "accept connections"} (:container container))
(assoc config :xiana/postgresql pg-config)))

(defn migrate!
([config]
(migrate! config 0))
Expand Down
3 changes: 2 additions & 1 deletion test/xiana_fixture.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns xiana-fixture
(:require
[piotr-yuxuan.closeable-map :refer [closeable-map]]
[utils]
[xiana.commons :refer [rename-key]]
[xiana.config :as config]
[xiana.db :as db-core]
Expand All @@ -15,7 +16,7 @@
(-> (config/config)
(merge app-cfg)
(rename-key :xiana/auth :auth)
db-core/docker-postgres!
utils/docker-postgres!
db-core/connect
db-core/migrate!
session-backend/init-backend
Expand Down