Skip to content

Commit

Permalink
update everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Bigsy committed Nov 15, 2024
1 parent 3815f7a commit 92b8df6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 40 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ 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'

- 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
111 changes: 84 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
## 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.
14 changes: 7 additions & 7 deletions project.clj
Original file line number Diff line number Diff line change
@@ -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"]]}})
5 changes: 4 additions & 1 deletion src/pg_embedded_clj/postgres.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,4 +22,6 @@
(->pg port log-redirect))

(defmethod ig/halt-key! ::postgres [_ pg]
(halt! pg))
(halt! pg))

(->pg 9999 nil)
2 changes: 1 addition & 1 deletion test/pg_embedded_clj/custom_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"))))))
2 changes: 1 addition & 1 deletion test/pg_embedded_clj/default_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
first
:version
extract-postgres-version)
"14.8"))))
"14.10"))))

0 comments on commit 92b8df6

Please sign in to comment.