Skip to content

Commit 6228c64

Browse files
committed
Add roundtrip tests for all common types
1 parent 47cbe47 commit 6228c64

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

oracle-simple.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ executable tests
2323
main-is:
2424
Main.hs
2525
build-depends:
26-
base < 5, oracle-simple, text, time, hspec, QuickCheck, quickcheck-instances
26+
base < 5, ieee754, oracle-simple, text, time, hspec, QuickCheck, quickcheck-instances
2727
hs-source-dirs:
2828
test
2929
default-language:

test/Main.hs

+68-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE DeriveAnyClass #-}
4+
{-# LANGUAGE DerivingStrategies #-}
15
{-# LANGUAGE RecordWildCards #-}
26
{-# LANGUAGE ScopedTypeVariables #-}
37
module Main where
48

5-
import Foreign.C.Types
9+
import Control.Monad.IO.Class (liftIO)
10+
import Data.AEq
611
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)
1115
import Data.Time
16+
import Foreign.C.Types
17+
import GHC.Generics
1218
import Test.Hspec
19+
import Test.Hspec.QuickCheck
20+
import Test.QuickCheck hiding ((===))
21+
import Test.QuickCheck.Instances ()
1322

1423
import Database.Oracle.Simple
1524

@@ -73,6 +82,58 @@ spec = do
7382
dpiTimeStampToUTCDPITimeStamp dpi `shouldBe` expected
7483

7584
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
7886
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

Comments
 (0)