Skip to content

Add roundtrip tests for all common types #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion oracle-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ executable tests
main-is:
Main.hs
build-depends:
base < 5, oracle-simple, text, time, hspec, QuickCheck, quickcheck-instances
base < 5, ieee754, oracle-simple, text, time, hspec, QuickCheck, quickcheck-instances
hs-source-dirs:
test
default-language:
Expand Down
75 changes: 68 additions & 7 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Main where

import Foreign.C.Types
import Control.Monad.IO.Class (liftIO)
import Data.AEq
import Data.Fixed
import Control.Monad.IO.Class (liftIO)
import Test.Hspec.QuickCheck
import Test.QuickCheck
import Test.QuickCheck.Instances ()
import Data.Function
import Data.Int
import Data.Text (Text)
import Data.Time
import Foreign.C.Types
import GHC.Generics
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck hiding ((===))
import Test.QuickCheck.Instances ()

import Database.Oracle.Simple

Expand Down Expand Up @@ -73,6 +82,58 @@ spec = do
dpiTimeStampToUTCDPITimeStamp dpi `shouldBe` expected

it "Should roundtrip UTCTime through DPITimestamp (w/ nanos -- not picos) " $ \_ -> do
property $ \tod day (nanos :: Nano) -> do
let utc = UTCTime day $ timeOfDayToTime tod { todSec = realToFrac nanos }
property $ \(UTCTimeNanos utc) -> do
utc `shouldBe` dpiTimeStampToUTCTime (utcTimeToDPITimestamp utc)

describe "Roundtrip tests" $ do
it "Should round trip random values from a table" $ \conn -> do
property $ \expected@TestTable{..} -> do
execute_ conn "create table test (a varchar(300), b number (12,0), c number (12,0), d number (12,0) null, e timestamp (9), f number (38,28))"
execute conn "insert into test values (:1,:2,:3,:4,:5,:6)" expected
actual <- query_ conn "select * from test"
execute_ conn "drop table test"
actual `shouldBe` [expected]

data TestTable
= TestTable
{ fieldText :: Text
, fieldInt :: Int
, fieldInt64 :: Int64
, fieldMaybeInt :: Maybe Int
, fieldUTCTime :: UTCTimeNanos
, fieldDouble :: Double
} deriving stock (Generic, Show)
deriving anyclass (ToRow, FromRow)

instance Eq TestTable where
x == y =
and
[ ((==) `on` fieldText) x y
, ((==) `on` fieldInt) x y
, ((==) `on` fieldInt64) x y
, ((==) `on` fieldMaybeInt) x y
, ((==) `on` fieldUTCTime) x y
, ((~==) `on` fieldDouble) x y
]

instance Arbitrary TestTable where
arbitrary =
TestTable
<$> arbitrary
<*> arbitrary -- choose (- 2^12, 2 ^ 12)
Copy link
Contributor

@apkhandkar apkhandkar Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number(12,0) fields above have a range of [999999999999, -999999999999], these tests fail with a "value larger than specified precision" error because Int/Int64 are wider. choose (-(10^12 - 1), 10^12 -1) could be used here.

<*> arbitrary -- choose (- 2^12, 2 ^ 12)
<*> arbitrary -- oneof [ Just <$> choose (- 2^12, 2 ^ 12), pure Nothing ]
<*> arbitrary
<*> arbitrary

newtype UTCTimeNanos = UTCTimeNanos UTCTime
deriving stock (Eq)
deriving newtype (Show, FromField, ToField, HasDPINativeType)

instance Arbitrary UTCTimeNanos where
arbitrary = do
tod <- arbitrary
day <- arbitrary
nanos :: Nano <- arbitrary
let utcTime = UTCTime day $ timeOfDayToTime tod { todSec = realToFrac nanos }
pure (UTCTimeNanos utcTime)