Skip to content

Commit

Permalink
Merge branch 'circuithub:master' into verify
Browse files Browse the repository at this point in the history
  • Loading branch information
abigailalice authored Aug 12, 2024
2 parents 81a52b0 + 9580b6f commit 72e4408
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Rel8/Expr/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ infixr 6 ++.

-- | Matches regular expression, case sensitive
--
-- Corresponds to the @~.@ operator.
-- Corresponds to the @~@ operator.
(~.) :: Expr Text -> Expr Text -> Expr Bool
(~.) = binaryOperator "~."
(~.) = binaryOperator "~"
infix 2 ~.


Expand Down
22 changes: 17 additions & 5 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import Test.Tasty.Hedgehog ( testProperty )

-- text
import Data.Text ( Text, pack, unpack )
import qualified Data.Text as T
import qualified Data.Text.Lazy
import Data.Text.Encoding ( decodeUtf8 )

Expand Down Expand Up @@ -542,14 +543,18 @@ instance Rel8.DBComposite Composite where
compositeTypeName = "composite"
compositeFields = Rel8.namesFromLabels

-- | Postgres doesn't support the NULL character (not to be confused with a NULL value) inside strings.
removeNull :: Text -> Text
removeNull = T.filter (/='\0')


testDBType :: IO TmpPostgres.DB -> TestTree
testDBType getTestDatabase = testGroup "DBType instances"
[ dbTypeTest "Bool" Gen.bool
, dbTypeTest "ByteString" $ Gen.bytes (Range.linear 0 128)
, dbTypeTest "CalendarDiffTime" genCalendarDiffTime
, dbTypeTest "CI Lazy Text" $ mk . Data.Text.Lazy.fromStrict <$> Gen.text (Range.linear 0 10) Gen.unicode
, dbTypeTest "CI Text" $ mk <$> Gen.text (Range.linear 0 10) Gen.unicode
, dbTypeTest "CI Lazy Text" $ mk . Data.Text.Lazy.fromStrict . removeNull <$> Gen.text (Range.linear 0 10) Gen.unicode
, dbTypeTest "CI Text" $ mk .removeNull <$> Gen.text (Range.linear 0 10) Gen.unicode
, dbTypeTest "Composite" genComposite
, dbTypeTest "Day" genDay
, dbTypeTest "Double" $ (/ 10) . fromIntegral @Int @Double <$> Gen.integral (Range.linear (-100) 100)
Expand All @@ -558,10 +563,10 @@ testDBType getTestDatabase = testGroup "DBType instances"
, dbTypeTest "Int32" $ Gen.integral @_ @Int32 Range.linearBounded
, dbTypeTest "Int64" $ Gen.integral @_ @Int64 Range.linearBounded
, dbTypeTest "Lazy ByteString" $ Data.ByteString.Lazy.fromStrict <$> Gen.bytes (Range.linear 0 128)
, dbTypeTest "Lazy Text" $ Data.Text.Lazy.fromStrict <$> Gen.text (Range.linear 0 10) Gen.unicode
, dbTypeTest "Lazy Text" $ Data.Text.Lazy.fromStrict . removeNull <$> Gen.text (Range.linear 0 10) Gen.unicode
, dbTypeTest "LocalTime" genLocalTime
, dbTypeTest "Scientific" $ (/ 10) . fromIntegral @Int @Scientific <$> Gen.integral (Range.linear (-100) 100)
, dbTypeTest "Text" $ Gen.text (Range.linear 0 10) Gen.unicode
, dbTypeTest "Text" $ removeNull <$> Gen.text (Range.linear 0 10) Gen.unicode
, dbTypeTest "TimeOfDay" genTimeOfDay
, dbTypeTest "UTCTime" $ UTCTime <$> genDay <*> genDiffTime
, dbTypeTest "UUID" $ Data.UUID.fromWords <$> genWord32 <*> genWord32 <*> genWord32 <*> genWord32
Expand Down Expand Up @@ -737,7 +742,14 @@ testFromRational = databasePropertyTest "fromRational" \transaction -> do
pure $ fromRational rational
diff result (~=) double
where
a ~= b = abs (a - b) < 1e-15
wholeDigits x = fromIntegral $ length $ show $ round x
-- A Double gives us between 15-17 decimal digits of precision.
-- It's tempting to say that two numbers are equal if they differ by less than 1e15.
-- But this doesn't hold.
-- The precision is split between the whole numer part and the decimal part of the number.
-- For instance, a number between 10 and 99 only has around 13 digits of precision in its decimal part.
-- Postgres and Haskell show differing amounts of digits in these cases,
a ~= b = abs (a - b) < 10**(-15 + wholeDigits a)
infix 4 ~=


Expand Down

0 comments on commit 72e4408

Please sign in to comment.