@@ -3,10 +3,19 @@
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+
613import Common
714import Database.PostgreSQL.Simple.Copy
15+import Database.PostgreSQL.Simple.ToField (ToField)
816import Database.PostgreSQL.Simple.FromField (FromField)
917import Database.PostgreSQL.Simple.HStore
18+import Database.PostgreSQL.Simple.Newtypes
1019import Database.PostgreSQL.Simple.Internal (breakOnSingleQuestionMark)
1120import Database.PostgreSQL.Simple.Types(Query(..),Values(..), PGArray(..))
1221import qualified Database.PostgreSQL.Simple.Transaction as ST
@@ -59,6 +68,8 @@ tests env = testGroup "tests"
5968 , testCase "HStore" . testHStore
6069 , testCase "citext" . testCIText
6170 , testCase "JSON" . testJSON
71+ , testCase "Aeson newtype" . testAeson
72+ , testCase "DerivingVia" . testDerivingVia
6273 , testCase "Question mark escape" . testQM
6374 , testCase "Savepoint" . testSavepoint
6475 , testCase "Unicode" . testUnicode
@@ -240,6 +251,40 @@ testJSON TestEnv{..} = do
240251 js' <- query conn "SELECT ?::json" js
241252 [js] @?= js'
242253
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)
262+ where
263+ roundTrip :: (Eq a, Show a, Typeable a, ToJSON a, FromJSON a)=> a -> Assertion
264+ roundTrip x = do
265+ y <- query conn "SELECT ?::json" (Only (Aeson x))
266+ [Only (Aeson x)] @?= y
267+
268+testDerivingVia :: TestEnv -> Assertion
269+testDerivingVia TestEnv{..} = do
270+#if __GLASGOW_HASKELL__ <806
271+ return ()
272+#else
273+ roundTrip $ DerivingVia1 42 "Meaning of Life"
274+ where
275+ roundTrip :: (Eq a, Show a, Typeable a, ToField a, FromField a)=> a -> Assertion
276+ roundTrip x = do
277+ y <- query conn "SELECT ?::json" (Only x)
278+ [Only x] @?= y
279+
280+data DerivingVia1 = DerivingVia1 Int String
281+ deriving stock (Eq, Show, Generic)
282+ deriving anyclass (FromJSON, ToJSON)
283+ deriving (ToField, FromField) via Aeson DerivingVia1
284+
285+#endif
286+
287+
243288testQM :: TestEnv -> Assertion
244289testQM TestEnv{..} = do
245290 -- Just test on a single string