@@ -5,12 +5,14 @@ module Main where
55
66import Control.Applicative
77import Data.Aeson
8+import Data.HashMap.Strict (fromList)
89import Data.List (nub)
910import Data.List.NonEmpty (NonEmpty (..))
1011import Data.Text (Text)
1112import qualified Data.Text as T
1213import Data.Time.Calendar (Day (..))
1314import Data.Time.Clock (UTCTime (..), secondsToDiffTime)
15+import qualified Data.Vector as V
1416import Database.Bloodhound
1517import GHC.Generics (Generic)
1618import Network.HTTP.Client
@@ -382,3 +384,25 @@ main = hspec $ do
382384 let String str = toJSON flags
383385 flagStrs = T.splitOn "|" str
384386 in noDuplicates flagStrs
387+
388+
389+ describe "omitNulls" $ do
390+ it "checks that omitNulls drops list elements when it should" $
391+ let dropped = omitNulls $ [ "test1" .= (toJSON ([] :: [Int]))
392+ , "test2" .= (toJSON ("some value" :: Text))]
393+ in dropped `shouldBe` Object (fromList [("test2", String "some value")])
394+
395+ it "checks that omitNulls doesn't drop list elements when it shouldn't" $
396+ let notDropped = omitNulls $ [ "test1" .= (toJSON ([1] :: [Int]))
397+ , "test2" .= (toJSON ("some value" :: Text))]
398+ in notDropped `shouldBe` Object (fromList [ ("test1", Array (V.fromList [Number 1.0]))
399+ , ("test2", String "some value")])
400+ it "checks that omitNulls drops non list elements when it should" $
401+ let dropped = omitNulls $ [ "test1" .= (toJSON Null)
402+ , "test2" .= (toJSON ("some value" :: Text))]
403+ in dropped `shouldBe` Object (fromList [("test2", String "some value")])
404+ it "checks that omitNulls doesn't drop non list elements when it shouldn't" $
405+ let notDropped = omitNulls $ [ "test1" .= (toJSON (1 :: Int))
406+ , "test2" .= (toJSON ("some value" :: Text))]
407+ in notDropped `shouldBe` Object (fromList [ ("test1", Number 1.0)
408+ , ("test2", String "some value")])