diff --git a/IHP/ModelSupport.hs b/IHP/ModelSupport.hs index 9ded7cbd6..d889e3f26 100644 --- a/IHP/ModelSupport.hs +++ b/IHP/ModelSupport.hs @@ -129,6 +129,9 @@ instance InputValue fieldType => InputValue (Maybe fieldType) where instance InputValue value => InputValue [value] where inputValue list = list |> map inputValue |> intercalate "," +instance InputValue Aeson.Value where + inputValue json = json |> Aeson.encode |> cs + instance Default Text where {-# INLINE def #-} def = "" diff --git a/Test/Main.hs b/Test/Main.hs index 04b30a0da..2a63e78d6 100644 --- a/Test/Main.hs +++ b/Test/Main.hs @@ -31,6 +31,7 @@ import qualified Test.View.CSSFrameworkSpec import qualified Test.Controller.ContextSpec import qualified Test.Controller.ParamSpec import qualified Test.SchemaMigrationSpec +import qualified Test.ModelSupportSpec main :: IO () main = hspec do @@ -48,4 +49,5 @@ main = hspec do Test.View.CSSFrameworkSpec.tests Test.Controller.ContextSpec.tests Test.Controller.ParamSpec.tests - Test.SchemaMigrationSpec.tests \ No newline at end of file + Test.SchemaMigrationSpec.tests + Test.ModelSupportSpec.tests \ No newline at end of file diff --git a/Test/ModelSupportSpec.hs b/Test/ModelSupportSpec.hs new file mode 100644 index 000000000..5acd0cf21 --- /dev/null +++ b/Test/ModelSupportSpec.hs @@ -0,0 +1,79 @@ +{-| +Module: Test.HaskellSupportSpec +Copyright: (c) digitally induced GmbH, 2020 +-} +module Test.ModelSupportSpec where + +import Test.Hspec +import IHP.Prelude +import IHP.ModelSupport +import qualified Data.Aeson as Aeson + +tests = do + describe "ModelSupport" do + describe "InputValue" do + describe "Text" do + it "should return the text" do + let text :: Text = "Lorem Ipsum" + (inputValue text) `shouldBe` text + + describe "Int" do + it "should return numeric representation" do + (inputValue (1 :: Int)) `shouldBe` "1" + + describe "Integer" do + it "should return numeric representation" do + (inputValue (1 :: Int)) `shouldBe` "1" + + describe "Double" do + it "should return numeric representation" do + (inputValue (1.337 :: Double)) `shouldBe` "1.337" + + describe "Float" do + it "should return numeric representation" do + (inputValue (1.337 :: Float)) `shouldBe` "1.337" + + describe "Bool" do + it "should deal with True and False" do + (inputValue True) `shouldBe` "on" + (inputValue False) `shouldBe` "off" + + describe "UUID" do + it "should return text representation" do + let uuid :: UUID = "b9a1129e-e53a-4370-b778-0d1c28dc6ecc" + (inputValue uuid) `shouldBe` "b9a1129e-e53a-4370-b778-0d1c28dc6ecc" + + describe "UTCTime" do + it "should return text representation" do + let (Just utctime) :: Maybe UTCTime = parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ" "2020-11-08T12:03:35Z" + (inputValue utctime) `shouldBe` "2020-11-08T12:03:35Z" + + describe "Day" do + it "should return text representation" do + let (Just utctime) :: Maybe UTCTime = parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ" "2020-11-08T12:03:35Z" + let day :: Day = utctDay utctime + (inputValue day) `shouldBe` "2020-11-08" + + describe "Maybe" do + it "should return empty string on Nothing" do + let value :: Maybe Int = Nothing + (inputValue value) `shouldBe` "" + + it "should return the value if Just" do + let value :: Maybe Int = Just 1 + (inputValue value) `shouldBe` "1" + + describe "[a]" do + it "should return CSV" do + let value :: [Int] = [1, 2, 3] + (inputValue value) `shouldBe` "1,2,3" + + it "should return empty string on empty list" do + let value :: [Int] = [] + (inputValue value) `shouldBe` "" + + describe "JSON" do + it "should return a json string for a Aeson.Value" do + let (Just value) :: Maybe Aeson.Value = Aeson.decode "{\"hello\":true}" + (inputValue value) `shouldBe` "{\"hello\":true}" +