forked from yesodweb/persistent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgIntervalTest.hs
More file actions
74 lines (65 loc) · 2.29 KB
/
PgIntervalTest.hs
File metadata and controls
74 lines (65 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module PgIntervalTest where
import Data.Fixed (Fixed (MkFixed), Micro, Pico)
import Data.Time.Clock (secondsToNominalDiffTime)
import Database.Persist.Postgresql (PgInterval (..))
import qualified Database.PostgreSQL.Simple.Interval as Interval
import PgInit
import Test.Hspec.QuickCheck
share
[mkPersist sqlSettings, mkMigrate "pgIntervalMigrate"]
[persistLowerCase|
PgIntervalDb
interval_field PgInterval
deriving Eq
deriving Show
IntervalDb
interval_field Interval.Interval
deriving Eq Show
|]
clamp :: (Ord a) => a -> a -> a -> a
clamp lo hi = max lo . min hi
-- Before version 15, PostgreSQL can't parse all possible intervals.
-- Each component is limited to the range of Int32.
-- So anything beyond 2,147,483,647 hours will fail to parse.
microsecondLimit :: Int64
microsecondLimit = 2147483647 * 60 * 60 * 1000000
specs :: Spec
specs = do
describe "Postgres Interval Property tests" $ do
prop "Round trips" $ \int64 -> runConnAssert $ do
let
eg =
PgIntervalDb
. PgInterval
. secondsToNominalDiffTime
. (realToFrac :: Micro -> Pico)
. MkFixed
. toInteger
$ clamp (-microsecondLimit) microsecondLimit int64
rid <- insert eg
r <- getJust rid
liftIO $ r `shouldBe` eg
prop "interval round trips" $ \(m, d, u) -> runConnAssert $ do
let
expected =
IntervalDb . Interval.MkInterval m d $
clamp (-microsecondLimit) microsecondLimit u
key <- insert expected
actual <- getJust key
liftIO $ actual `shouldBe` expected