@@ -3,10 +3,19 @@
3{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE DeriveDataTypeable #-}
4{-# LANGUAGE DoAndIfThenElse #-}
4{-# LANGUAGE DoAndIfThenElse #-}
5{-# LANGUAGE ScopedTypeVariables #-}
5{-# LANGUAGE ScopedTypeVariables #-}
6+#if __GLASGOW_HASKELL__ >= 806
7+{-# LANGUAGE DerivingStrategies #-}
8+{-# LANGUAGE DerivingVia #-}
9+{-# LANGUAGE DeriveAnyClass #-}
11+module Main (main) where
6import Common
13import Common
7import Database.PostgreSQL.Simple.Copy
14import Database.PostgreSQL.Simple.Copy
15+import Database.PostgreSQL.Simple.ToField (ToField)
8import Database.PostgreSQL.Simple.FromField (FromField)
16import Database.PostgreSQL.Simple.FromField (FromField)
9import Database.PostgreSQL.Simple.HStore
17import Database.PostgreSQL.Simple.HStore
18+import Database.PostgreSQL.Simple.Newtypes
10import Database.PostgreSQL.Simple.Internal (breakOnSingleQuestionMark)
19import Database.PostgreSQL.Simple.Internal (breakOnSingleQuestionMark)
11import Database.PostgreSQL.Simple.Types(Query(..),Values(..), PGArray(..))
20import Database.PostgreSQL.Simple.Types(Query(..),Values(..), PGArray(..))
12import qualified Database.PostgreSQL.Simple.Transaction as ST
21import qualified Database.PostgreSQL.Simple.Transaction as ST
@@ -59,6 +68,8 @@ tests env = testGroup "tests"
59 , testCase "HStore" . testHStore
68 , testCase "HStore" . testHStore
60 , testCase "citext" . testCIText
69 , testCase "citext" . testCIText
61 , testCase "JSON" . testJSON
70 , testCase "JSON" . testJSON
71+ , testCase "Aeson newtype" . testAeson
72+ , testCase "DerivingVia" . testDerivingVia
62 , testCase "Question mark escape" . testQM
73 , testCase "Question mark escape" . testQM
63 , testCase "Savepoint" . testSavepoint
74 , testCase "Savepoint" . testSavepoint
64 , testCase "Unicode" . testUnicode
75 , testCase "Unicode" . testUnicode
@@ -240,6 +251,40 @@ testJSON TestEnv{..} = do
240 js' <- query conn "SELECT ?::json" js
251 js' <- query conn "SELECT ?::json" js
241 [js] @?= js'
252 [js] @?= js'
254+testAeson :: TestEnv -> Assertion
255+testAeson TestEnv{..} = do
256+ roundTrip (Map.fromList [] :: Map Text Text)
257+ roundTrip (Map.fromList [("foo","bar"),("bar","baz"),("baz","hello")] :: Map Text Text)
258+ roundTrip (Map.fromList [("fo\"o","bar"),("b\\ar","baz"),("baz","\"value\\with\"escapes")] :: Map Text Text)
259+ roundTrip (V.fromList [1,2,3,4,5::Int])
260+ roundTrip ("foo" :: Text)
261+ roundTrip (42 :: Int)
263+ roundTrip :: (Eq a, Show a, Typeable a, ToJSON a, FromJSON a)=> a -> Assertion
265+ y <- query conn "SELECT ?::json" (Only (Aeson x))
266+ [Only (Aeson x)] @?= y
268+testDerivingVia :: TestEnv -> Assertion
269+testDerivingVia TestEnv{..} = do
270+#if __GLASGOW_HASKELL__ <806
273+ roundTrip $ DerivingVia1 42 "Meaning of Life"
275+ roundTrip :: (Eq a, Show a, Typeable a, ToField a, FromField a)=> a -> Assertion
277+ y <- query conn "SELECT ?::json" (Only x)
280+data DerivingVia1 = DerivingVia1 Int String
281+ deriving stock (Eq, Show, Generic)
282+ deriving anyclass (FromJSON, ToJSON)
283+ deriving (ToField, FromField) via Aeson DerivingVia1
243testQM :: TestEnv -> Assertion
288testQM :: TestEnv -> Assertion
244testQM TestEnv{..} = do
289testQM TestEnv{..} = do
245 -- Just test on a single string
290 -- Just test on a single string