|
| 1 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE DeriveAnyClass #-} |
| 4 | +{-# LANGUAGE DerivingStrategies #-} |
1 | 5 | {-# LANGUAGE RecordWildCards #-}
|
2 | 6 | {-# LANGUAGE ScopedTypeVariables #-}
|
3 | 7 | module Main where
|
4 | 8 |
|
5 |
| -import Foreign.C.Types |
| 9 | +import Control.Monad.IO.Class (liftIO) |
| 10 | +import Data.AEq |
6 | 11 | import Data.Fixed
|
7 |
| -import Control.Monad.IO.Class (liftIO) |
8 |
| -import Test.Hspec.QuickCheck |
9 |
| -import Test.QuickCheck |
10 |
| -import Test.QuickCheck.Instances () |
| 12 | +import Data.Function |
| 13 | +import Data.Int |
| 14 | +import Data.Text (Text) |
11 | 15 | import Data.Time
|
| 16 | +import Foreign.C.Types |
| 17 | +import GHC.Generics |
12 | 18 | import Test.Hspec
|
| 19 | +import Test.Hspec.QuickCheck |
| 20 | +import Test.QuickCheck hiding ((===)) |
| 21 | +import Test.QuickCheck.Instances () |
13 | 22 |
|
14 | 23 | import Database.Oracle.Simple
|
15 | 24 |
|
@@ -73,6 +82,58 @@ spec = do
|
73 | 82 | dpiTimeStampToUTCDPITimeStamp dpi `shouldBe` expected
|
74 | 83 |
|
75 | 84 | it "Should roundtrip UTCTime through DPITimestamp (w/ nanos -- not picos) " $ \_ -> do
|
76 |
| - property $ \tod day (nanos :: Nano) -> do |
77 |
| - let utc = UTCTime day $ timeOfDayToTime tod { todSec = realToFrac nanos } |
| 85 | + property $ \(UTCTimeNanos utc) -> do |
78 | 86 | utc `shouldBe` dpiTimeStampToUTCTime (utcTimeToDPITimestamp utc)
|
| 87 | + |
| 88 | + describe "Roundtrip tests" $ do |
| 89 | + it "Should round trip random values from a table" $ \conn -> do |
| 90 | + property $ \x@TestTable{..} -> do |
| 91 | + execute_ conn "create table test (a varchar(300), b number (12,0), c number (12,0), d number (12,0) null, e timestamp, f number (38,28))" |
| 92 | + execute conn "insert into test values (:1,:2,:3,:4,:5,:6)" x |
| 93 | + y <- query_ conn "select * from test" |
| 94 | + execute_ conn "drop table test" |
| 95 | + [x] `shouldBe` y |
| 96 | + |
| 97 | +data TestTable |
| 98 | + = TestTable |
| 99 | + { fieldText :: Text |
| 100 | + , fieldInt :: Int |
| 101 | + , fieldInt64 :: Int64 |
| 102 | + , fieldMaybeInt :: Maybe Int |
| 103 | + , fieldUTCTime :: UTCTimeNanos |
| 104 | + , fieldDouble :: Double |
| 105 | + } deriving stock (Generic, Show) |
| 106 | + deriving anyclass (ToRow, FromRow) |
| 107 | + |
| 108 | +instance Eq TestTable where |
| 109 | + x == y = |
| 110 | + and |
| 111 | + [ ((==) `on` fieldText) x y |
| 112 | + , ((==) `on` fieldInt) x y |
| 113 | + , ((==) `on` fieldInt64) x y |
| 114 | + , ((==) `on` fieldMaybeInt) x y |
| 115 | + , ((==) `on` fieldUTCTime) x y |
| 116 | + , ((~==) `on` fieldDouble) x y |
| 117 | + ] |
| 118 | + |
| 119 | +instance Arbitrary TestTable where |
| 120 | + arbitrary = |
| 121 | + TestTable |
| 122 | + <$> arbitrary |
| 123 | + <*> arbitrary -- choose (- 2^12, 2 ^ 12) |
| 124 | + <*> arbitrary -- choose (- 2^12, 2 ^ 12) |
| 125 | + <*> arbitrary -- oneof [ Just <$> choose (- 2^12, 2 ^ 12), pure Nothing ] |
| 126 | + <*> arbitrary |
| 127 | + <*> arbitrary |
| 128 | + |
| 129 | +newtype UTCTimeNanos = UTCTimeNanos UTCTime |
| 130 | + deriving stock (Eq) |
| 131 | + deriving newtype (Show, FromField, ToField, HasDPINativeType) |
| 132 | + |
| 133 | +instance Arbitrary UTCTimeNanos where |
| 134 | + arbitrary = do |
| 135 | + tod <- arbitrary |
| 136 | + day <- arbitrary |
| 137 | + nanos :: Nano <- arbitrary |
| 138 | + let utcTime = UTCTime day $ timeOfDayToTime tod { todSec = realToFrac nanos } |
| 139 | + pure (UTCTimeNanos utcTime) |
0 commit comments