From 3e074d1c0cec71e7d7890b1a67475aa065c5edfb Mon Sep 17 00:00:00 2001 From: Tomasz Rybarczyk Date: Thu, 6 Dec 2018 20:14:08 +0100 Subject: [PATCH] Make tests pass --- README.md | 16 ++++++++++------ package.json | 11 ++++------- test/Main.purs | 10 +++++----- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a6d5d25..6e99862 100644 --- a/README.md +++ b/README.md @@ -14,21 +14,25 @@ This guide is a literate Purescript file which is extracted into testing module Let's start with imports. ```purescript -module Test.Example where +module Test.README where import Prelude -import Database.PostgreSQL (defaultPoolConfiguration, command, execute, newPool, query, Query(Query), withConnection, withTransaction) +import Database.PostgreSQL (defaultPoolConfiguration, command, execute, newPool, PG, query, Query(Query), withConnection, withTransaction) import Database.PostgreSQL.Row (Row0(Row0), Row3(Row3)) import Data.Decimal as Decimal import Data.Maybe (Maybe(..)) import Data.Tuple.Nested ((/\)) -import Effect.Aff (Aff) import Effect.Class (liftEffect) import Test.Assert (assert) ``` -The whole API for interaction with postgres is performed asynchronously in `Aff`. +The whole API for interaction with postgres is performed asynchronously in `Aff`. To be honest +it is `type PG a = ExceptT PGError Aff a` so you can easily catch database related errors in +a sane (read typed) manner. The only function which is done in plain `Effect` is `newPool`. + +Don't be scarred you can turn `PG a` into `Aff (Either PGError a)` just running single function +`runExceptT`. We assume here that postgres is running on a standard local port with `ident` authentication so configuration can be nearly empty (`defaultPoolConfiguration`). @@ -38,10 +42,10 @@ is run by our test suite and we want to exit after execution quickly ;-) ```purescript -run ∷ Aff Unit +run ∷ PG Unit run = do - pool <- newPool ((defaultPoolConfiguration "purspg") { idleTimeoutMillis = Just 1000 }) + pool <- liftEffect $ newPool ((defaultPoolConfiguration "purspg") { idleTimeoutMillis = Just 1000 }) withConnection pool \conn -> do ``` diff --git a/package.json b/package.json index 8678dea..a2f0885 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,15 @@ { "name": "purescript-postgresql-client", "dependencies": { - "bower-dependency-tree": "^0.1.2", "decimal.js": "^10.0.0", - "g": "^2.0.1", - "global": "^4.3.2", - "pg": "^6.1.2", - "pulp": "^12.3.0" + "pg": "^6.1.2" }, "devDependencies": { - "literate-purescript": "^0.1.2" + "paluh-litps": "^0.1.4", + "pulp": "^12.3.0" }, "scripts": { - "pretest": "litps compile --file README.md; mv README.purs test/Example.purs", + "pretest": "paluh-litps compile --file README.md; mv README.purs test/README.purs", "test": "pulp test" } } diff --git a/test/Main.purs b/test/Main.purs index 44f0d91..4a3f09b 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -29,7 +29,7 @@ import Foreign.Object (Object, fromFoldable) import Math ((%)) import Partial.Unsafe (unsafePartial) import Test.Assert (assert) -import Test.Example (run) as Example +import Test.README (run) as README import Test.Unit (TestSuite, suite) import Test.Unit as Test.Unit import Test.Unit.Assert (equal) @@ -83,10 +83,10 @@ main ∷ Effect Unit main = do void $ launchAff do -- Running guide from README - -- Example.run + void $ runExceptT $ README.run -- Actual test suite - pool <- newPool config + pool <- liftEffect $ newPool config checkPGErrors $ withConnection pool \conn -> do execute conn (Query """ CREATE TEMPORARY TABLE foods ( @@ -312,13 +312,13 @@ main = do let doNothing _ = pure unit Test.Unit.test "connection refused" do - testPool <- newPool cannotConnectConfig + testPool <- liftEffect $ newPool cannotConnectConfig runExceptT (withConnection testPool doNothing) >>= case _ of Left (ConnectionError cause) -> equal cause "ECONNREFUSED" _ -> Test.Unit.failure "foo" Test.Unit.test "no such database" do - testPool <- newPool noSuchDatabaseConfig + testPool <- liftEffect $ newPool noSuchDatabaseConfig runExceptT (withConnection testPool doNothing) >>= case _ of Left (ProgrammingError { code, message }) -> equal code "3D000" _ -> Test.Unit.failure "PostgreSQL error was expected"