Skip to content

Commit

Permalink
Get tests running on Postgres too. Fixes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
chr15m committed Mar 15, 2024
1 parent 8401dad commit af35f14
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,12 @@ jobs:

- name: Run the e2e tests
run: npm run test-e2e

- name: Set up PostgreSQL
run: |
sudo apt install postgresql libpq-dev
sudo service postgresql start
sudo -u postgres createuser --superuser "$USER"
- name: Run the tests on Postgres
run: npm run test-postgres
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,15 @@ See the full [documentation for the db module](https://chr15m.github.io/sitefox/

By default a local sqlite database is used and you can start persisting data on the server immediately without any configuration.
Once you move to production you can configure another database using the environment variable `DATABASE_URL`.
For example, to use a postgres database called "somedatabase":
For example, to use a postgres database called "DBNAME" you can access it as follows (depending on your network/local setup):

```
DATABASE_URL=postgres://someuser:somepassword@somehost:5432/somedatabase
DATABASE_URL="postgres://%2Fvar%2Frun%2Fpostgresql/DBNAME"
DATABASE_URL=postgres://someuser:somepassword@somehost:5432/DBNAME
DATABASE_URL=postgres:///somedatabase
```

Or simply `DATABASE_URL=postgres:///somedatabase` if your user has local access on the deploy server.
Note that you will also need to `npm install @keyv/postgres` if you want to use the Postgres backend.

To use the database and key-value interface first require the database module:

Expand Down
4 changes: 4 additions & 0 deletions bin/test-on-postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

dropdb sitefoxtest 2>/dev/null || echo "Not dropping database."
createdb sitefoxtest && SECRET=testing TESTING=1 DATABASE_URL="postgres://%2Fvar%2Frun%2Fpostgresql/sitefoxtest" npx shadow-cljs compile test
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"tmp": "0.2.1"
},
"devDependencies": {
"@keyv/postgres": "^1.4.10",
"nbb": "^1.2.182",
"playwright": "1.30.0",
"shadow-cljs": "2.19.0",
Expand All @@ -37,6 +38,7 @@
"docs": "nbb bin/generate-docs.cljs",
"pre-publish": "npm run sync-deps; nbb bin/update-readme-versions.cljs; npm run docs; echo Changes:; git log --oneline `git rev-list --tags --max-count=1`..; echo; echo 'Now commit changes and run `git tag vX.Y.Z`.'",
"test": "rm -f ./tests.sqlite; SECRET=testing TESTING=1 DATABASE_URL=sqlite://./tests.sqlite npx shadow-cljs compile test",
"test-postgres": "bin/test-on-postgres",
"test-e2e": "NODE_OPTIONS='--experimental-fetch --no-warnings' nbb --classpath src src/sitefoxtest/e2etests.cljs",
"watch": "SECRET=watching TESTING=1 shadow-cljs watch test"
}
Expand Down
15 changes: 12 additions & 3 deletions src/sitefox/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
`DATABASE_URL` for Postgres: `postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]`"
(:require
[clojure.test :refer-macros [is async]]
[clojure.test :refer-macros [is async use-fixtures]]
[promesa.core :as p]
[sitefox.util :refer [env]]
[sitefox.deps :refer [Keyv]]))
Expand Down Expand Up @@ -37,9 +37,13 @@
{:test (fn []
(async done
(p/let [c (client)
v (.query c "SELECT sqlite_version()")]
dialect (aget c "opts" "dialect")
version-fn (case dialect
"sqlite" "sqlite_version()"
"version()")
v (.query c (str "SELECT " version-fn " AS v"))]
(is (aget c "query"))
(is (-> v (aget 0) (aget "sqlite_version()")))
(is (-> v (aget 0) (aget "v")))
(done))))}
[]
(->
Expand Down Expand Up @@ -114,3 +118,8 @@
query (aget db "opts" "store" "query")
version-query (query "SELECT version();")]
version-query)))))

; make sure postgres is set up to run the tests
(use-fixtures
:once {:before #(async done (p/do! (ensure-local-postgres-db-exists) (done)))
#_#_ :after #(async done (done))})

0 comments on commit af35f14

Please sign in to comment.