diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 2cab57e..5753f07 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -23,10 +23,10 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Prepare java - uses: actions/setup-java@v3.5.0 + uses: actions/setup-java@v4.5.0 with: distribution: 'adopt' java-version: '11' @@ -34,7 +34,7 @@ jobs: - name: Install clojure tools-deps uses: DeLaGuardo/setup-clojure@master with: - lein: 2.9.10 + lein: 2.11.2 - name: Execute clojure code run: lein test diff --git a/README.md b/README.md index 8041cc9..3ac30e4 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,108 @@ # pg-embedded-clj -Embedded postgres for clojure - based on https://github.com/zonkyio/embedded-postgres +A Clojure library that provides embedded PostgreSQL for testing and development purposes. Based on [zonkyio/embedded-postgres](https://github.com/zonkyio/embedded-postgres), this library makes it easy to spin up and tear down PostgreSQL instances in your Clojure applications. + +[![Clojars Project](https://img.shields.io/clojars/v/org.clojars.bigsy/pg-embedded-clj.svg)](https://clojars.org/org.clojars.bigsy/pg-embedded-clj) + +## Features + +- Simple API for starting and stopping PostgreSQL instances +- Configurable port and logging options +- Support for different PostgreSQL versions and architectures +- Perfect for integration testing and local development +- Automatic cleanup of resources + +## Installation + +Add the following dependency to your `project.clj`: + +```clojure +[org.clojars.bigsy/pg-embedded-clj "latest-version"] +``` + +## PostgreSQL Version Configuration + +By default, this library uses the standard Zonky PostgreSQL version. For most use cases, this default version is sufficient. + +To use a specific PostgreSQL version or architecture, include an additional dependency from [zonky-postgres-binaries](https://mvnrepository.com/artifact/io.zonky.test.postgres). For example: + +```clojure +;; For PostgreSQL 17 on Apple Silicon +[io.zonky.test.postgres/embedded-postgres-binaries-darwin-arm64v8 "17.0.0"] +``` ## Usage -[![Clojars Project](https://img.shields.io/clojars/v/org.clojars.bigsy/pg-embedded-clj.svg)](https://clojars.org/org.clojars.bigsy/pg-embedded-clj) -### Development: +### Development Mode ```clojure -(require 'pg-embedded-clj.core) +(require '[pg-embedded-clj.core :refer :all]) -;; Start an embedded pg with default port: +;; Start PostgreSQL with default settings (port 5432) (init-pg) -;; another call will halt the previous system: -(init-pg) +;; Start PostgreSQL with custom configuration +(init-pg {:port 5433 + :log-redirect "postgres.log"}) -;; When you're done: +;; Stop the PostgreSQL instance (halt-pg!) ``` -### Testing: +### Testing -**NOTE**: these will halt running pg-embedded instances +The library provides convenient fixtures for integration testing: ```clojure -(require 'clojure.test) +(ns your-test-namespace + (:require [clojure.test :refer :all] + [pg-embedded-clj.core :refer [with-pg-fn default-config]])) -(use-fixtures :once with-pg-fn) - -(defn around-all +;; Using the fixture with custom configuration +(defn test-fixture [f] (with-pg-fn (merge default-config - {:port 54321 - :log-redirect "wibble.log"}) - f)) + {:port 54321 + :log-redirect "test.log"}) + f)) + +(use-fixtures :once test-fixture) + +;; Your tests here +(deftest your-integration-test + (testing "Database operations" + ;; Your test code here + )) + +;; For ad-hoc testing blocks +(deftest another-test + (with-pg default-config + ;; Your test code here + )) +``` -(use-fixtures :once around-all) +## Configuration Options -;;; You can also wrap ad-hoc code in init/halt: -(with-pg default-config - ,,, :do-something ,,,) +The following configuration options are available: + +```clojure +{:port 5432 ; PostgreSQL port (default: 5432) + :log-redirect nil ; Log file path (default: nil for no logging) + :data-dir nil} ; Custom data directory (optional) ``` -### Other useful clojure wrapped embedded testing libs -* https://github.com/Bigsy/dynamo-embedded-clj -* https://github.com/Bigsy/redis-embedded-clj -* https://github.com/Bigsy/s3-clj -* https://github.com/Bigsy/elasticmq-clj -* https://github.com/Bigsy/sns-clj \ No newline at end of file +## Related Libraries + +Check out these other useful embedded testing libraries for Clojure: + +* [dynamo-embedded-clj](https://github.com/Bigsy/dynamo-embedded-clj) - Embedded DynamoDB +* [redis-embedded-clj](https://github.com/Bigsy/redis-embedded-clj) - Embedded Redis +* [s3-clj](https://github.com/Bigsy/s3-clj) - Embedded S3 +* [elasticmq-clj](https://github.com/Bigsy/elasticmq-clj) - Embedded ElasticMQ +* [sns-clj](https://github.com/Bigsy/sns-clj) - Embedded SNS + +## License + +Copyright © 2024 + +Distributed under the Eclipse Public License, the same as Clojure. diff --git a/project.clj b/project.clj index ffb1d17..612d92a 100644 --- a/project.clj +++ b/project.clj @@ -1,13 +1,13 @@ -(defproject org.clojars.bigsy/pg-embedded-clj "1.0.1" +(defproject org.clojars.bigsy/pg-embedded-clj "1.0.2" :description "Embedded postgres for clojure" :url "https://github.com/Bigsy/pg-embedded-clj" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} - :dependencies [[org.clojure/clojure "1.11.1"] - [integrant "0.8.0"] - [io.zonky.test/embedded-postgres "2.0.4"] - [org.clojure/tools.logging "1.2.4"] - [org.clojure/tools.namespace "1.3.0"] - [org.slf4j/slf4j-jdk14 "2.0.1"]] + :dependencies [[org.clojure/clojure "1.12.0"] + [integrant "0.13.1"] + [io.zonky.test/embedded-postgres "2.0.7"] + [org.clojure/tools.logging "1.3.0"] + [org.clojure/tools.namespace "1.5.0"]] + :profiles {:dev {:dependencies [[org.clojure/java.jdbc "0.7.12"]]}}) diff --git a/src/pg_embedded_clj/postgres.clj b/src/pg_embedded_clj/postgres.clj index 636f1d7..d3a66f1 100644 --- a/src/pg_embedded_clj/postgres.clj +++ b/src/pg_embedded_clj/postgres.clj @@ -6,6 +6,7 @@ (defn ->pg [port pg-log] (let [pg (-> (EmbeddedPostgres/builder) + (.setLocaleConfig "locale", "en_US.UTF-8") (.setPort port))] (when pg-log (let [log-redirector (ProcessBuilder$Redirect/appendTo (io/file pg-log))] (-> pg @@ -21,4 +22,6 @@ (->pg port log-redirect)) (defmethod ig/halt-key! ::postgres [_ pg] - (halt! pg)) \ No newline at end of file + (halt! pg)) + +(->pg 9999 nil) \ No newline at end of file diff --git a/test/pg_embedded_clj/custom_test.clj b/test/pg_embedded_clj/custom_test.clj index c7467cc..4bd12b6 100644 --- a/test/pg_embedded_clj/custom_test.clj +++ b/test/pg_embedded_clj/custom_test.clj @@ -36,7 +36,7 @@ first :version extract-postgres-version) - "14.8"))) + "14.10"))) (testing "using custom log redirect" (is (= true (.exists (io/as-file "wibble.log")))))) diff --git a/test/pg_embedded_clj/default_test.clj b/test/pg_embedded_clj/default_test.clj index 5353445..c3b5eba 100644 --- a/test/pg_embedded_clj/default_test.clj +++ b/test/pg_embedded_clj/default_test.clj @@ -32,4 +32,4 @@ first :version extract-postgres-version) - "14.8")))) + "14.10"))))