2019-08-31 19:17:27 +02:00
|
|
|
{-# LANGUAGE ApplicativeDo #-}
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
module Xanthous.Data.EntityMapSpec where
|
|
|
|
--------------------------------------------------------------------------------
|
2019-11-29 20:33:52 +01:00
|
|
|
import Test.Prelude
|
2019-08-31 19:17:27 +02:00
|
|
|
--------------------------------------------------------------------------------
|
2019-11-29 20:33:52 +01:00
|
|
|
import qualified Data.Aeson as JSON
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import Xanthous.Data.EntityMap
|
2019-08-31 19:17:27 +02:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = defaultMain test
|
|
|
|
|
|
|
|
test :: TestTree
|
2019-09-15 19:00:28 +02:00
|
|
|
test = localOption (QuickCheckTests 20)
|
|
|
|
$ testGroup "Xanthous.Data.EntityMap"
|
2019-08-31 19:17:27 +02:00
|
|
|
[ testBatch $ monoid @(EntityMap Int) mempty
|
2019-09-15 19:00:28 +02:00
|
|
|
, testGroup "Deduplicate"
|
2019-09-20 18:03:30 +02:00
|
|
|
[ testGroup "Semigroup laws"
|
|
|
|
[ testProperty "associative" $ \(a :: Deduplicate (EntityMap Int)) b c ->
|
|
|
|
a <> (b <> c) === (a <> b) <> c
|
|
|
|
]
|
2019-09-15 19:00:28 +02:00
|
|
|
]
|
2019-08-31 19:17:27 +02:00
|
|
|
, testGroup "Eq laws"
|
|
|
|
[ testProperty "reflexivity" $ \(em :: EntityMap Int) ->
|
|
|
|
em == em
|
|
|
|
, testProperty "symmetric" $ \(em₁ :: EntityMap Int) em₂ ->
|
|
|
|
(em₁ == em₂) == (em₂ == em₁)
|
|
|
|
, testProperty "transitive" $ \(em₁ :: EntityMap Int) em₂ em₃ ->
|
|
|
|
if (em₁ == em₂ && em₂ == em₃)
|
|
|
|
then (em₁ == em₃)
|
|
|
|
else True
|
|
|
|
]
|
2019-11-29 20:33:52 +01:00
|
|
|
, testGroup "JSON encoding/decoding"
|
2019-11-30 21:00:39 +01:00
|
|
|
[ testProperty "round-trips" $ \(em :: EntityMap Int) ->
|
|
|
|
let em' = JSON.decode (JSON.encode em)
|
|
|
|
in counterexample (show (em' ^? _Just . lastID, em ^. lastID
|
|
|
|
, em' ^? _Just . byID == em ^. byID . re _Just
|
|
|
|
, em' ^? _Just . byPosition == em ^. byPosition . re _Just
|
|
|
|
, em' ^? _Just . _EntityMap == em ^. _EntityMap . re _Just
|
|
|
|
))
|
|
|
|
$ em' === Just em
|
|
|
|
, testProperty "Preserves IDs" $ \(em :: EntityMap Int) ->
|
2019-11-29 20:33:52 +01:00
|
|
|
let Just em' = JSON.decode $ JSON.encode em
|
|
|
|
in toEIDsAndPositioned em' === toEIDsAndPositioned em
|
|
|
|
]
|
2019-08-31 19:17:27 +02:00
|
|
|
]
|