Spec.purs 4.92 KB
Newer Older
1 2
module Gargantext.Utils.Spec where

3
import Data.Argonaut as Argonaut
4
import Data.Argonaut.Decode.Error (JsonDecodeError)
5 6 7
import Data.Either (Either(..), isLeft)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
8 9 10 11 12
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)

import Gargantext.Prelude

13
import Gargantext.Utils as GU
14
import Gargantext.Utils.Argonaut (genericEnumDecodeJson, genericEnumEncodeJson, genericSumDecodeJson, genericSumEncodeJson)
15
import Gargantext.Utils.Crypto as Crypto
16
import Gargantext.Utils.Math as GUM
17 18 19 20 21 22 23 24 25 26 27 28 29 30

data Fruit
  = Boat { hi :: Int }
  | Gravy String
  | Pork Int

derive instance eqFruit :: Eq Fruit
derive instance genericFruit :: Generic Fruit _
instance showFruit :: Show Fruit where
  show = genericShow
instance decodeJsonFruit :: Argonaut.DecodeJson Fruit where
  decodeJson = genericSumDecodeJson
instance encodeJsonFruit :: Argonaut.EncodeJson Fruit where
  encodeJson = genericSumEncodeJson
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45
data EnumTest
  = Member1
  | Member2
  | Member3

derive instance eqEnumTest :: Eq EnumTest
derive instance genericEnumTest :: Generic EnumTest _
instance showEnumTest :: Show EnumTest where
  show = genericShow
instance decodeJsonEnumTest :: Argonaut.DecodeJson EnumTest where
  decodeJson = genericEnumDecodeJson
instance encodeJsonEnumTest :: Argonaut.EncodeJson EnumTest where
  encodeJson = genericEnumEncodeJson

46 47 48 49
spec :: Spec Unit
spec =
  describe "G.Utils" do
    it "zeroPad 1 works" do
50 51 52
      GU.zeroPad 1 0 `shouldEqual` "0"
      GU.zeroPad 1 1 `shouldEqual` "1"
      GU.zeroPad 1 10 `shouldEqual` "10"
53
    it "zeroPad 2 works" do
54 55 56 57
      GU.zeroPad 2 0 `shouldEqual` "00"
      GU.zeroPad 2 1 `shouldEqual` "01"
      GU.zeroPad 2 10 `shouldEqual` "10"
      GU.zeroPad 2 100 `shouldEqual` "100"
58
    it "zeroPad 3 works" do
59 60 61 62 63 64 65
      GU.zeroPad 3 0 `shouldEqual` "000"
      GU.zeroPad 3 1 `shouldEqual` "001"
      GU.zeroPad 3 10 `shouldEqual` "010"
      GU.zeroPad 3 99 `shouldEqual` "099"
      GU.zeroPad 3 100 `shouldEqual` "100"
      GU.zeroPad 3 101 `shouldEqual` "101"
      GU.zeroPad 3 1000 `shouldEqual` "1000"
66
    it "log10 10" do
67
      GUM.log10 10.0 `shouldEqual` 1.0
68 69

    it "genericSumDecodeJson works" do
70
      let result1 = Argonaut.decodeJson =<< Argonaut.parseJson """{"Boat":{"hi":1}}"""
71 72
      result1 `shouldEqual` Right (Boat { hi: 1 })

73
      let result2 = Argonaut.decodeJson =<< Argonaut.parseJson """{"Gravy":"hi"}"""
74 75
      result2 `shouldEqual` Right (Gravy "hi")

76 77
      let result3 = Argonaut.decodeJson =<< Argonaut.parseJson """{"Boat":123}"""
      isLeft (result3 :: Either JsonDecodeError Fruit) `shouldEqual` true
78 79 80 81 82 83 84 85 86 87 88 89 90

    it "genericSumEncodeJson works and loops back with decode" do
      let input1 = Boat { hi: 1 }
      let result1 = Argonaut.encodeJson input1
      let result1' = Argonaut.decodeJson result1
      Argonaut.stringify result1 `shouldEqual` """{"Boat":{"hi":1}}"""
      result1' `shouldEqual` Right input1

      let input2 = Gravy "hi"
      let result2 = Argonaut.encodeJson input2
      let result2' = Argonaut.decodeJson result2
      Argonaut.stringify result2 `shouldEqual` """{"Gravy":"hi"}"""
      result2' `shouldEqual` Right input2
91 92

    it "genericEnumDecodeJson works" do
93
      let result1 = Argonaut.decodeJson =<< Argonaut.parseJson "\"Member1\""
94 95
      result1 `shouldEqual` Right Member1

96
      let result2 = Argonaut.decodeJson =<< Argonaut.parseJson "\"Member2\""
97 98
      result2 `shouldEqual` Right Member2

99 100
      let result3 = Argonaut.decodeJson =<< Argonaut.parseJson "\"Failure\""
      isLeft (result3 :: Either JsonDecodeError EnumTest) `shouldEqual` true
101 102 103 104 105 106 107 108 109 110 111 112 113

    it "genericSumEncodeJson works and loops back with decode" do
      let input1 = Member1
      let result1 = Argonaut.encodeJson input1
      let result1' = Argonaut.decodeJson result1
      Argonaut.stringify result1 `shouldEqual` "\"Member1\""
      result1' `shouldEqual` Right input1

      let input2 = Member2
      let result2 = Argonaut.encodeJson input2
      let result2' = Argonaut.decodeJson result2
      Argonaut.stringify result2 `shouldEqual` "\"Member2\""
      result2' `shouldEqual` Right input2
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

------------------------------------------------------------------------
-- | Crypto Hash tests
    it "Hash String with backend works" do
      let text = "To hash with backend"
      let hashed = "8a69a94d164279af2b7d1443ce08da6184b3d7e815406076e148159c284b53c3"
                   -- ^ hash from backend with text above
      Crypto.hash text `shouldEqual` hashed

    it "Hash List with backend works" do
      let list = ["a","b"]
      let hashed = "ab19ec537f09499b26f0f62eed7aefad46ab9f498e06a7328ce8e8ef90da6d86"
                   -- ^ hash from backend with text above
      Crypto.hash list `shouldEqual` hashed

------------------------------------------------------------------------
-- | TODO property based tests
    it "Hash works with any order of list" do
      let hash1 = Crypto.hash ["a","b"]
      let hash2 = Crypto.hash ["b","a"]
      hash1 `shouldEqual` hash2