git.iko.soy/typeable/postgresql-simple
on master help sign in
Test case for reworked time printers/parsers
commitc0fad6baaa6bda2cae2284cb4da1f048dd6e08b6 authorLeon P Smith <leon@melding-monads.com> dateSun 10 Jun 2012, 03:27 (14 years ago) parent68023a1
3 files changed+71
postgresql-simple.cabal+2 test/Main.hs+2 test/Time.hs+67
postgresql-simple.cabal+2
@@ -76,6 +76,7 @@ test-suite test
7676 Common
7777 Bytea
7878 Notify
79+ Time
7980
8081 ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-unused-do-bind
8182
@@ -91,3 +92,4 @@ test-suite test
9192 , HUnit
9293 , postgresql-simple
9394 , text
95+ , time
test/Main.hs+2
@@ -6,11 +6,13 @@ import System.IO
66
77import Bytea
88import Notify
9+import Time
910
1011tests :: [TestEnv -> Test]
1112tests =
1213 [ TestLabel "Bytea" . testBytea
1314 , TestLabel "Notify" . testNotify
15+ , TestLabel "Time" . testTime
1416 ]
1517
1618-- | Action for connecting to the database that will be used for testing.
test/Time.hsnew file+67
@@ -0,0 +1,67 @@
1+{-# LANGUAGE QuasiQuotes, ScopedTypeVariables #-}
2+
3+{-
4+
5+Testing strategies:
6+
7+fromString . toString == id ** Todo?
8+
9+toString . fromString == almost id ** Todo?
10+
11+postgresql -> haskell -> postgresql * Done
12+
13+haskell -> postgresql -> haskell ** Todo?
14+
15+But still, what we really want to establish is that the two values
16+correspond; for example, a conversion that consistently added hour
17+when printed to a string and subtracted an hour when parsed from string
18+would still pass these tests.
19+
20+-}
21+
22+module Time (testTime) where
23+
24+import Common
25+import Control.Monad(forM_, replicateM_)
26+import Data.Time
27+import Data.ByteString(ByteString)
28+import Database.PostgreSQL.Simple.SqlQQ
29+
30+numTests :: Int
31+numTests = 200
32+
33+testTime :: TestEnv -> Test
34+testTime env@TestEnv{..} = TestCase $ do
35+ initializeTable env
36+ execute_ conn "SET timezone TO 'UTC'"
37+ checkRoundTrips env
38+ execute_ conn "SET timezone TO 'America/Chicago'"
39+ checkRoundTrips env
40+ execute_ conn "SET timezone TO 'Asia/Tokyo'"
41+ checkRoundTrips env
42+
43+initializeTable :: TestEnv -> IO ()
44+initializeTable TestEnv{..} = withTransaction conn $ do
45+ execute_ conn
46+ [sql| CREATE TEMPORARY TABLE testtime
47+ ( x serial, y timestamptz, PRIMARY KEY(x) ) |]
48+ let pop :: ByteString -> Double -> IO () = \x y ->
49+ replicateM_ numTests $ execute conn
50+ [sql| INSERT INTO testtime (y) VALUES
51+ ('1900-01-01 00:00:00+00'::timestamptz
52+ + ?::interval * ROUND(RANDOM() * ?)) |] (x,y)
53+ pop "1 microsecond" 6.3113904e15
54+ pop "10 microseconds" 6.3113904e14
55+ pop "100 microseconds" 6.3113904e13
56+ pop "1 millisecond" 6.3113904e12
57+ pop "10 milliseconds" 6.3113904e11
58+ pop "100 milliseconds" 6.3113904e10
59+ pop "1 second" 6.3113904e9
60+
61+checkRoundTrips :: TestEnv -> IO ()
62+checkRoundTrips TestEnv{..} = do
63+ yxs :: [(UTCTime, Int)] <- query_ conn [sql| SELECT y, x FROM testtime |]
64+ forM_ yxs $ \yx -> do
65+ res <- query conn [sql| SELECT y=? FROM testtime WHERE x=? |] yx
66+ assertBool "UTCTime did not round-trip from SQL to Haskell and back" $
67+ res == [Only True]