Make tests pass

This commit is contained in:
Tomasz Rybarczyk 2018-12-06 20:14:08 +01:00
parent 111790af84
commit 3e074d1c0c
3 changed files with 19 additions and 18 deletions

View File

@ -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
```

View File

@ -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"
}
}

View File

@ -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"