packages feed

tmp-proc-postgres (empty) → 0.5.0.0

raw patch · 7 files changed

+287/−0 lines, 7 filesdep +basedep +bytestringdep +hspecsetup-changed

Dependencies added: base, bytestring, hspec, hspec-tmp-proc, postgresql-simple, text, tmp-proc, tmp-proc-postgres

Files

+ ChangeLog.md view
@@ -0,0 +1,6 @@+# Revision history for tmp-proc-postgres++## 0.5.0.0 -- 2021-09-28++* First version. Extracted from an unreleased version of the tmp-proc library+* Initial upload to hackage
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2021, Tim Emiola++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Tim Emiola nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/System/TmpProc/Docker/Postgres.hs view
@@ -0,0 +1,120 @@+{-# LANGUAGE DataKinds             #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE NamedFieldPuns        #-}+{-# LANGUAGE OverloadedStrings     #-}+{-# LANGUAGE ScopedTypeVariables   #-}+{-# LANGUAGE TypeFamilies          #-}+{-# OPTIONS_HADDOCK prune not-home #-}+{-|+Copyright   : (c) 2020-2021 Tim Emiola+SPDX-License-Identifier: BSD3+Maintainer  : Tim Emiola <adetokunbo@users.noreply.github.com >++Provides an instance of 'Proc' that launches @postgres@ as a @tmp proc@.++The instance this module provides can be used in integration tests as is.++It's also possible to write other instances that launch @postgres@ in different+ways; for those, this instance can be used as a reference example.++-}+module System.TmpProc.Docker.Postgres+  ( -- * 'Proc' instance+    TmpPostgres(..)++    -- * Useful definitions+  , aProc+  , aHandle++    -- * Re-exports+  , module System.TmpProc+  )+where++import           Control.Exception          (catch)+import qualified Data.ByteString.Char8      as C8+import           Data.String                (fromString)+import           Data.Text                  (Text)+import qualified Data.Text                  as Text++import           Database.PostgreSQL.Simple (Connection, SqlError, close,+                                             connectPostgreSQL, execute_)++import           System.TmpProc             (Connectable (..), HList (..),+                                             HandlesOf, HostIpAddress,+                                             Pinged (..), Proc (..),+                                             ProcHandle (..), SvcURI,+                                             startupAll, withTmpConn)+++{-| A singleton 'HList' containing a 'TmpPostgres'. -}+aProc :: HList '[TmpPostgres]+aProc = TmpPostgres [] `HCons` HNil+++{-| An 'HList' that contains the handle created from 'aProc'. -}+aHandle :: IO (HandlesOf '[TmpPostgres])+aHandle = startupAll aProc+++{-| Provides the capability to launch a Postgres database as a @tmp proc@.++The constructor receives the names of the tables to be dropped on 'reset'.++-}+data TmpPostgres = TmpPostgres [Text]+++{-| Specifies how to run @postgres@ as a @tmp proc@. -}+instance Proc TmpPostgres where+  type Image TmpPostgres = "postgres:10.6"+  type Name TmpPostgres = "a-postgres-db"++  uriOf = mkUri'+  runArgs = runArgs'+  ping = toPinged . connectPostgreSQL . hUri+  reset = reset'++{-| Specifies how to connect to a tmp @postgres@ db. -}+instance Connectable TmpPostgres where+  type Conn TmpPostgres = Connection++  openConn = connectPostgreSQL . hUri+  closeConn = close+++{-| Makes a uri whose password matches the one specified in 'runArgs''. -}+mkUri' :: HostIpAddress -> SvcURI+mkUri' ip = "host="+             <> C8.pack (Text.unpack ip)+             <> " dbname=postgres user=postgres password="+             <> dbPassword+             <> " port=5432"+++dbPassword :: C8.ByteString+dbPassword = "mysecretpassword"+++{-| Match the password used in 'mkUri''. -}+runArgs' :: [Text]+runArgs' =+  [ "-e"+  , "POSTGRES_PASSWORD=" <> Text.pack (C8.unpack dbPassword)+  ]+++toPinged :: IO a -> IO Pinged+toPinged action = ((action >> pure OK)+                    `catch` (\(_ :: SqlError) -> pure NotOK))+                  `catch` (\(_ :: IOError) -> pure NotOK)++++{-| Empty all rows in the tables, if any are specified. -}+reset' :: ProcHandle TmpPostgres -> IO ()+reset' handle@(ProcHandle {hProc}) =+  let go (TmpPostgres []) = pure ()+      go (TmpPostgres tables) = withTmpConn handle $ \c ->+        mapM_ (execute_ c . (fromString . (++) "DELETE FROM ") . Text.unpack) tables+  in go hProc
+ test/Spec.hs view
@@ -0,0 +1,12 @@+module Main (main) where++import           Test.Hspec++import           System.IO+import qualified Test.TmpProc.Docker.PostgresSpec as PG++main :: IO ()+main = do+  hSetBuffering stdin NoBuffering+  hSetBuffering stdout NoBuffering+  hspec PG.spec
+ test/Test/TmpProc/Docker/PostgresSpec.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE DataKinds         #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeApplications  #-}+module Test.TmpProc.Docker.PostgresSpec where++import           Test.Hspec+import           Test.Hspec.TmpProc++import           Control.Exception              (onException)+import           Data.Proxy                     (Proxy (..))+import           Data.Text                      (Text)+import qualified Data.Text                      as Text+import           Database.PostgreSQL.Simple     (execute_)++import           System.TmpProc.Docker.Postgres+++spec :: Spec+spec = tdescribe desc $ do+  beforeAll setupHandles $ afterAll terminateAll $ do+    context "when accessing from a list of other procs" $ do++      context "ixPing" $ do++        it "should succeed" $ \hs+          -> ixPing @TmpPostgres Proxy hs `shouldReturn` OK++      context "ixReset" $ do++        it "should succeed when accessed by Name" $ \hs+          -> ixReset @"a-postgres-db" Proxy hs `shouldReturn` ()++        it "should succeed when accessed by Type" $ \hs+          -> ixReset @TmpPostgres Proxy hs `shouldReturn` ()+++setupHandles :: IO (HList '[ProcHandle TmpPostgres])+setupHandles = do+  hs <- startupAll $ testProc `HCons` HNil+  initTable hs `onException` terminateAll hs+  pure hs+++testProc :: TmpPostgres+testProc = TmpPostgres [testTable]+++testTable :: Text+testTable = "to_be_reset"+++initTable :: HList '[ProcHandle TmpPostgres] -> IO ()+initTable xs = withConnOf @TmpPostgres Proxy xs $ \c -> do+  let commands =+        [ "CREATE TABLE to_be_reset (an_id INTEGER)"+        , "INSERT INTO to_be_reset VALUES (347)"+        ]+  mapM_ (execute_ c) commands+++desc :: String+desc = "Tmp.Proc:Postgres:" ++ Text.unpack (nameOf testProc)
+ tmp-proc-postgres.cabal view
@@ -0,0 +1,55 @@+cabal-version:      3.0+name:               tmp-proc-postgres+version:            0.5.0.0+synopsis:           Shows how to run a PostgreSQL database as a tmp proc+description:+  An example of using tmp-proc to launch dockerized PostgreSQL in integration tests.++license:            BSD-3-Clause+license-file:       LICENSE+copyright:          2021 Tim Emiola+author:             Tim Emiola+maintainer:         adetokunbo@users.noreply.github.com+category:           testing+bug-reports:        https://github.com/adetokunbo/tmp-proc/issues+build-type:         Simple+extra-source-files:+  ChangeLog.md+tested-with:+  GHC == 8.10.5++source-repository head+  type:     git+  location: https://github.com/adetokunbo/tmp-proc.git++library+  exposed-modules:  System.TmpProc.Docker.Postgres+  hs-source-dirs:   src+  build-depends:+    , base               >=4.11     && <4.16+    , bytestring         >=0.10.8.2 && <0.12+    , postgresql-simple  >=0.5.4.0  && <0.6.5+    , text               ^>=1.2.3+    , tmp-proc           >=0.5.0.0  && <0.6.0.0++  default-language: Haskell2010+  ghc-options:      -fno-ignore-asserts -Wall++test-suite integration-test+  type:             exitcode-stdio-1.0+  main-is:          Spec.hs+  other-modules:    Test.TmpProc.Docker.PostgresSpec+  hs-source-dirs:   test+  build-depends:+    , base+    , bytestring+    , hspec+    , hspec-tmp-proc+    , postgresql-simple+    , text+    , tmp-proc+    , tmp-proc-postgres++  default-language: Haskell2010+  ghc-options:+    -threaded -rtsopts -with-rtsopts=-N -fno-ignore-asserts -Wall