forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeSpec.hs
More file actions
44 lines (37 loc) · 1.29 KB
/
TimeSpec.hs
File metadata and controls
44 lines (37 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-- | Time-related properties we care about.
module Data.TimeSpec (spec) where
import Data.Aeson
import Data.Time
import Data.Time.Clock.Units
import Test.Hspec
import Prelude
spec :: Spec
spec = do
timeUnitsSpec
diffTimeSpec
timeUnitsSpec :: Spec
timeUnitsSpec =
describe "time units" $ do
it "converts correctly" $ do
seconds 123 `shouldBe` 123
milliseconds 123 `shouldBe` 0.123
microseconds 123 `shouldBe` 0.000123
nanoseconds 123 `shouldBe` 0.000000123
it "has a correct Read instance" $ do
seconds (read "123") `shouldBe` 123
milliseconds (read "123") `shouldBe` 0.123
microseconds (read "123") `shouldBe` 0.000123
nanoseconds (read "123") `shouldBe` 0.000000123
it "JSON serializes as proper units" $ do
toJSON (1 :: Seconds) `shouldBe` Number 1
decode "1.0" `shouldBe` Just (1 :: Seconds)
it "converts with convertDuration" $ do
convertDuration (2 :: Minutes) `shouldBe` (120 :: NominalDiffTime)
convertDuration (60 :: Seconds) `shouldBe` (1 :: Minutes)
diffTimeSpec :: Spec
diffTimeSpec =
describe "DiffTime" $ do
it "JSON serializes as seconds" $ do
-- although we would prefer to use Seconds instead...
toJSON (1 :: DiffTime) `shouldBe` Number 1
decode "1.0" `shouldBe` Just (1 :: DiffTime)