module Signet.Unstable.Type.ToleranceTest where import qualified Data.Time as Time import qualified Signet.Unstable.Exception.ToleranceException as ToleranceException import qualified Signet.Unstable.Extra.Tasty as Tasty import qualified Signet.Unstable.Type.Timestamp as Timestamp import qualified Signet.Unstable.Type.Tolerance as Tolerance import Test.Tasty.HUnit ((@?=)) spec :: Tasty.Spec spec :: Spec spec = TestName -> Spec -> Spec Tasty.describe TestName "Signet.Unstable.Type.Tolerance" (Spec -> Spec) -> Spec -> Spec forall a b. (a -> b) -> a -> b $ do TestName -> Spec -> Spec Tasty.describe TestName "check" (Spec -> Spec) -> Spec -> Spec forall a b. (a -> b) -> a -> b $ do TestName -> Assertion -> Spec Tasty.it TestName "succeeds when timestamp is within tolerance" (Assertion -> Spec) -> Assertion -> Spec forall a b. (a -> b) -> a -> b $ do let tolerance :: Tolerance tolerance = NominalDiffTime -> Tolerance Tolerance.MkTolerance NominalDiffTime 1 let utcTime :: UTCTime utcTime = Day -> DiffTime -> UTCTime Time.UTCTime (Year -> MonthOfYear -> MonthOfYear -> Day Time.fromGregorian Year 2001 MonthOfYear 1 MonthOfYear 1) DiffTime 60 let timestamp :: Timestamp timestamp = UTCTime -> Timestamp Timestamp.MkTimestamp UTCTime utcTime let result :: Either ToleranceException () result = Tolerance -> UTCTime -> Timestamp -> Either ToleranceException () Tolerance.check Tolerance tolerance UTCTime utcTime Timestamp timestamp Either ToleranceException () result Either ToleranceException () -> Either ToleranceException () -> Assertion forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion @?= () -> Either ToleranceException () forall a b. b -> Either a b Right () TestName -> Assertion -> Spec Tasty.it TestName "fails when timestamp is too old" (Assertion -> Spec) -> Assertion -> Spec forall a b. (a -> b) -> a -> b $ do let tolerance :: Tolerance tolerance = NominalDiffTime -> Tolerance Tolerance.MkTolerance NominalDiffTime 1 let utcTime :: UTCTime utcTime = Day -> DiffTime -> UTCTime Time.UTCTime (Year -> MonthOfYear -> MonthOfYear -> Day Time.fromGregorian Year 2001 MonthOfYear 1 MonthOfYear 1) DiffTime 60 let timestamp :: Timestamp timestamp = UTCTime -> Timestamp Timestamp.MkTimestamp UTCTime utcTime let now :: UTCTime now = NominalDiffTime -> UTCTime -> UTCTime Time.addUTCTime NominalDiffTime 2 UTCTime utcTime let result :: Either ToleranceException () result = Tolerance -> UTCTime -> Timestamp -> Either ToleranceException () Tolerance.check Tolerance tolerance UTCTime now Timestamp timestamp Either ToleranceException () result Either ToleranceException () -> Either ToleranceException () -> Assertion forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion @?= ToleranceException -> Either ToleranceException () forall a b. a -> Either a b Left (Timestamp -> ToleranceException ToleranceException.MkToleranceException Timestamp timestamp) TestName -> Assertion -> Spec Tasty.it TestName "fails when timestamp is in the future" (Assertion -> Spec) -> Assertion -> Spec forall a b. (a -> b) -> a -> b $ do let tolerance :: Tolerance tolerance = NominalDiffTime -> Tolerance Tolerance.MkTolerance NominalDiffTime 1 let utcTime :: UTCTime utcTime = Day -> DiffTime -> UTCTime Time.UTCTime (Year -> MonthOfYear -> MonthOfYear -> Day Time.fromGregorian Year 2001 MonthOfYear 1 MonthOfYear 1) DiffTime 60 let timestamp :: Timestamp timestamp = UTCTime -> Timestamp Timestamp.MkTimestamp UTCTime utcTime let now :: UTCTime now = NominalDiffTime -> UTCTime -> UTCTime Time.addUTCTime (-NominalDiffTime 2) UTCTime utcTime let result :: Either ToleranceException () result = Tolerance -> UTCTime -> Timestamp -> Either ToleranceException () Tolerance.check Tolerance tolerance UTCTime now Timestamp timestamp Either ToleranceException () result Either ToleranceException () -> Either ToleranceException () -> Assertion forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion @?= ToleranceException -> Either ToleranceException () forall a b. a -> Either a b Left (Timestamp -> ToleranceException ToleranceException.MkToleranceException Timestamp timestamp)