@@ -3,19 +3,10 @@
33{-# LANGUAGE DeriveDataTypeable #-}
44{-# LANGUAGE DoAndIfThenElse #-}
55{-# LANGUAGE ScopedTypeVariables #-}
6−#if __GLASGOW_HASKELL__ >= 806
7−{-# LANGUAGE DerivingStrategies #-}
8−{-# LANGUAGE DerivingVia #-}
9−{-# LANGUAGE DeriveAnyClass #-}
10−#endif
11−module Main (main) where
12−
136import Common
147import Database.PostgreSQL.Simple.Copy
15−import Database.PostgreSQL.Simple.ToField (ToField)
168import Database.PostgreSQL.Simple.FromField (FromField)
179import Database.PostgreSQL.Simple.HStore
18−import Database.PostgreSQL.Simple.Newtypes
1910import Database.PostgreSQL.Simple.Internal (breakOnSingleQuestionMark)
2011import Database.PostgreSQL.Simple.Types(Query(..),Values(..), PGArray(..))
2112import qualified Database.PostgreSQL.Simple.Transaction as ST
@@ -24,7 +15,6 @@ import Control.Applicative
2415import Control.Exception as E
2516import Control.Monad
2617import Data.Char
27−import Data.Foldable (toList)
2818import Data.List (concat, sort)
2919import Data.IORef
3020import Data.Monoid ((<>))
@@ -46,7 +36,7 @@ import qualified Data.Text.Encoding as T
4636import qualified Data.Vector as V
4737import System.FilePath
4838import System.Timeout(timeout)
49−import Data.Time.Compat (getCurrentTime, diffUTCTime)
39+import Data.Time(getCurrentTime, diffUTCTime)
5040import System.Environment (getEnvironment)
5141
5242import Test.Tasty
@@ -54,8 +44,6 @@ import Test.Tasty.Golden
5444import Notify
5545import Serializable
5646import Time
57−import Interval
58−import Exception (testExceptions)
5947
6048tests :: TestEnv -> TestTree
6149tests env = testGroup "tests"
@@ -66,14 +54,11 @@ tests env = testGroup "tests"
6654 , testCase "Notify" . testNotify
6755 , testCase "Serializable" . testSerializable
6856 , testCase "Time" . testTime
69− , testCase "Interval" . testInterval
7057 , testCase "Array" . testArray
7158 , testCase "Array of nullables" . testNullableArray
7259 , testCase "HStore" . testHStore
7360 , testCase "citext" . testCIText
7461 , testCase "JSON" . testJSON
75− , testCase "Aeson newtype" . testAeson
76− , testCase "DerivingVia" . testDerivingVia
7762 , testCase "Question mark escape" . testQM
7863 , testCase "Savepoint" . testSavepoint
7964 , testCase "Unicode" . testUnicode
@@ -85,7 +70,6 @@ tests env = testGroup "tests"
8570 , testCase "2-ary generic" . testGeneric2
8671 , testCase "3-ary generic" . testGeneric3
8772 , testCase "Timeout" . testTimeout
88− , testCase "Exceptions" . testExceptions
8973 ]
9074
9175testBytea :: TestEnv -> TestTree
@@ -139,7 +123,7 @@ testFold TestEnv{..} = do
139123 xs <- readIORef ref
140124 writeIORef ref $! (a,b):xs
141125 xs <- readIORef ref
142− reverse xs @?= [(a,b) | b <- [1..50], a <- [1..100]]
126+ reverse xs @?= [(a,b) | a <- [1..100], b <- [1..50]]
143127
144128 -- Make sure fold propagates our exception.
145129 ref <- newIORef []
@@ -256,40 +240,6 @@ testJSON TestEnv{..} = do
256240 js' <- query conn "SELECT ?::json" js
257241 [js] @?= js'
258242
259−testAeson :: TestEnv -> Assertion
260−testAeson TestEnv{..} = do
261− roundTrip (Map.fromList [] :: Map Text Text)
262− roundTrip (Map.fromList [("foo","bar"),("bar","baz"),("baz","hello")] :: Map Text Text)
263− roundTrip (Map.fromList [("fo\"o","bar"),("b\\ar","baz"),("baz","\"value\\with\"escapes")] :: Map Text Text)
264− roundTrip (V.fromList [1,2,3,4,5::Int])
265− roundTrip ("foo" :: Text)
266− roundTrip (42 :: Int)
267− where
268− roundTrip :: (Eq a, Show a, Typeable a, ToJSON a, FromJSON a)=> a -> Assertion
269− roundTrip x = do
270− y <- query conn "SELECT ?::json" (Only (Aeson x))
271− [Only (Aeson x)] @?= y
272−
273−testDerivingVia :: TestEnv -> Assertion
274−testDerivingVia TestEnv{..} = do
275−#if __GLASGOW_HASKELL__ <806
276− return ()
277−#else
278− roundTrip $ DerivingVia1 42 "Meaning of Life"
279− where
280− roundTrip :: (Eq a, Show a, Typeable a, ToField a, FromField a)=> a -> Assertion
281− roundTrip x = do
282− y <- query conn "SELECT ?::json" (Only x)
283− [Only x] @?= y
284−
285−data DerivingVia1 = DerivingVia1 Int String
286− deriving stock (Eq, Show, Generic)
287− deriving anyclass (FromJSON, ToJSON)
288− deriving (ToField, FromField) via Aeson DerivingVia1
289−
290−#endif
291−
292−
293243testQM :: TestEnv -> Assertion
294244testQM TestEnv{..} = do
295245 -- Just test on a single string
@@ -447,14 +397,6 @@ testCopy TestEnv{..} = do
447397 -- so that we can issue more queries:
448398 [Only (x::Int)] <- query_ conn "SELECT 2 + 2"
449399 x @?= 4
450− -- foldCopyData
451− copy_ conn "COPY copy_test TO STDOUT (FORMAT CSV)"
452− (acc, count) <- foldCopyData conn
453− (\acc row -> return (row:acc))
454− (\acc count -> return (acc, count))
455− []
456− sort acc @?= sort copyRows
457− count @?= 2
458400 where
459401 copyRows = ["1,foo\n"
460402 ,"2,bar\n"]
@@ -474,13 +416,12 @@ testCopyFailures env = testGroup "Copy failures"
474416
475417goldenTest :: TestName -> IO BL.ByteString -> TestTree
476418goldenTest testName =
477− goldenVsStringDiff testName diff (resultsDir </> fileName<.>"expected")
419+ goldenVsString testName (resultsDir </> fileName<.>"expected")
478420 where
479421 resultsDir = "test" </> "results"
480422 fileName = map normalize testName
481423 normalize c | not (isAlpha c) = '-'
482424 | otherwise = c
483− diff ref new = ["diff", "-u", ref, new]
484425
485426-- | Test that we provide a sensible error message on failure
486427testCopyUniqueConstraintError :: TestEnv -> TestTree
@@ -609,11 +550,7 @@ isFormatError i FormatError{..}
609550-- that 'testConnect' connects to the same database every time it is called.
610551withTestEnv :: ByteString -> (TestEnv -> IO a) -> IO a
611552withTestEnv connstr cb =
612− withConn $ \conn -> do
613− -- currently required for interval to work.
614− -- we also test that this doesn't interfere with anything else
615− execute_ conn "SET intervalstyle TO 'iso_8601'"
616−
553+ withConn $ \conn ->
617554 cb TestEnv
618555 { conn = conn
619556 , withConn = withConn
@@ -622,27 +559,13 @@ withTestEnv connstr cb =
622559 withConn = bracket (connectPostgreSQL connstr) close
623560
624561main :: IO ()
625−main = withConnstring $ \connstring -> do
626− withTestEnv connstring (defaultMain . tests)
627−
628−withConnstring :: (BS8.ByteString -> IO ()) -> IO ()
629−withConnstring kont = do
562+main = do
630563 env <- getEnvironment
631564 case lookup "DATABASE_CONNSTRING" env of
632− Just s -> kont (BS8.pack (special s))
633− Nothing -> case lookup "GITHUB_ACTIONS" env of
634− Just "true" -> kont (BS8.pack gha)
635− _ -> putStrLn "Set DATABASE_CONNSTRING environment variable"
636− where
565+ Nothing -> putStrLn "Set DATABASE_CONNSTRING environment variable"
566+ Just s -> withTestEnv (BS8.pack (special s)) (defaultMain . tests)
567+ where
637568 -- https://www.appveyor.com/docs/services-databases/
638569 special "appveyor" = "dbname='TestDb' user='postgres' password='Password12!'"
639570 special "travis" = ""
640571 special s = s
641−
642− gha = unwords
643− [ "dbname='postgres'"
644− , "user='postgres'"
645− , "password='postgres'"
646− , "host='postgres'"
647− , "port=5432"
648− ]