-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
40 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
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. |
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,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"]]}}) |
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 |
---|---|---|
|
@@ -32,4 +32,4 @@ | |
first | ||
:version | ||
extract-postgres-version) | ||
"14.8")))) | ||
"14.10")))) |