module Jerimum.Tests.Unit.PostgreSQL.Types.BinaryTest
  ( tests
  ) where

import Data.ByteString                             (ByteString, pack)
import Jerimum.PostgreSQL.Types.Binary
import Jerimum.Tests.Unit.PostgreSQL.Types.Helpers
import Test.Tasty
import Test.Tasty.QuickCheck

newtype BinaryG =
  BinaryG ByteString
  deriving (Show, Eq)

tests :: TestTree
tests = testGroup "PostgreSQL.Types.Binary" [testCborCodec]

testCborCodec :: TestTree
testCborCodec =
  testGroup
    "cbor codec"
    [ testProperty "identity" $ \(BinaryG bs) ->
        Right bs == runDecoder binaryDecoderV0 (runEncoder $ binaryEncoderV0 bs)
    ]

instance Arbitrary BinaryG where
  arbitrary = do
    size <- choose (1, 20)
    ba <- vector size
    pure $ BinaryG (pack ba)
