1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE ScopedTypeVariables #-}
4module Test.JSON (spec) where
8import qualified Data.ByteString.Lazy.Char8 as BL8
9import qualified Data.List as L
10import Data.List.NonEmpty (NonEmpty(..))
11import qualified Data.List.NonEmpty as NE
12import qualified Data.HashMap.Strict as HM
13import qualified Data.Text as T
14import qualified Data.Vector as V
28propJSON _ = prop testName $ \(a :: a) ->
29 let jsonStr = "via " <> BL8.unpack (encode a)
30 in counterexample jsonStr (parseEither parseJSON (toJSON a)
32 where testName = show ty <> " FromJSON/ToJSON roundtrips"
33 ty = typeOf (undefined :: a)
35propApproxJSON :: forall a
44propApproxJSON _ = prop testName $ \(a :: a) ->
45 let jsonStr = "via " <> BL8.unpack (encode a)
46 in counterexample jsonStr (parseEither parseJSON (toJSON a)
48 where testName = show ty <> " FromJSON/ToJSON roundtrips"
49 ty = typeOf (undefined :: a)
53 describe "ToJSON RegexpFlags" $ do
54 it "generates the correct JSON for AllRegexpFlags" $
55 toJSON AllRegexpFlags `shouldBe` String "ALL"
57 it "generates the correct JSON for NoRegexpFlags" $
58 toJSON NoRegexpFlags `shouldBe` String "NONE"
60 it "generates the correct JSON for SomeRegexpFlags" $
61 let flags = AnyString :| [ Automaton
66 in toJSON (SomeRegexpFlags flags) `shouldBe` String "ANYSTRING|AUTOMATON|COMPLEMENT|EMPTY|INTERSECTION|INTERVAL"
68 prop "removes duplicates from flags" $ \(flags :: RegexpFlags) ->
69 let String str = toJSON flags
70 flagStrs = T.splitOn "|" str
71 in noDuplicates flagStrs
73 describe "omitNulls" $ do
74 it "checks that omitNulls drops list elements when it should" $
75 let dropped = omitNulls $ [ "test1" .= (toJSON ([] :: [Int]))
76 , "test2" .= (toJSON ("some value" :: Text))]
77 in dropped `shouldBe` Object (HM.fromList [("test2", String "some value")])
79 it "checks that omitNulls doesn't drop list elements when it shouldn't" $
80 let notDropped = omitNulls $ [ "test1" .= (toJSON ([1] :: [Int]))
81 , "test2" .= (toJSON ("some value" :: Text))]
82 in notDropped `shouldBe` Object (HM.fromList [ ("test1", Array (V.fromList [Number 1.0]))
83 , ("test2", String "some value")])
84 it "checks that omitNulls drops non list elements when it should" $
85 let dropped = omitNulls $ [ "test1" .= (toJSON Null)
86 , "test2" .= (toJSON ("some value" :: Text))]
87 in dropped `shouldBe` Object (HM.fromList [("test2", String "some value")])
88 it "checks that omitNulls doesn't drop non list elements when it shouldn't" $
89 let notDropped = omitNulls $ [ "test1" .= (toJSON (1 :: Int))
90 , "test2" .= (toJSON ("some value" :: Text))]
91 in notDropped `shouldBe` Object (HM.fromList [ ("test1", Number 1.0)
92 , ("test2", String "some value")])
94 describe "Exact isomorphism JSON instances" $ do
95 propJSON (Proxy :: Proxy Version)
96 propJSON (Proxy :: Proxy IndexName)
97 propJSON (Proxy :: Proxy MappingName)
98 propJSON (Proxy :: Proxy DocId)
99 propJSON (Proxy :: Proxy IndexAliasRouting)
100 propJSON (Proxy :: Proxy RoutingValue)
101 propJSON (Proxy :: Proxy ShardCount)
102 propJSON (Proxy :: Proxy ReplicaCount)
103 propJSON (Proxy :: Proxy TemplateName)
104 propJSON (Proxy :: Proxy TemplatePattern)
105 propJSON (Proxy :: Proxy QueryString)
106 propJSON (Proxy :: Proxy FieldName)
107 propJSON (Proxy :: Proxy Script)
108 propJSON (Proxy :: Proxy ScriptLanguage)
109 propJSON (Proxy :: Proxy ScriptInline)
110 propJSON (Proxy :: Proxy ScriptId)
111 propJSON (Proxy :: Proxy ScriptParams)
112 propJSON (Proxy :: Proxy CacheName)
113 propJSON (Proxy :: Proxy CacheKey)
114 propJSON (Proxy :: Proxy Existence)
115 propJSON (Proxy :: Proxy CutoffFrequency)
116 propJSON (Proxy :: Proxy Analyzer)
117 propJSON (Proxy :: Proxy MaxExpansions)
118 propJSON (Proxy :: Proxy Lenient)
119 propJSON (Proxy :: Proxy Tiebreaker)
120 propJSON (Proxy :: Proxy Boost)
121 propJSON (Proxy :: Proxy BoostTerms)
122 propJSON (Proxy :: Proxy MinimumMatch)
123 propJSON (Proxy :: Proxy DisableCoord)
124 propJSON (Proxy :: Proxy IgnoreTermFrequency)
125 propJSON (Proxy :: Proxy MinimumTermFrequency)
126 propJSON (Proxy :: Proxy MaxQueryTerms)
127 propJSON (Proxy :: Proxy Fuzziness)
128 propJSON (Proxy :: Proxy PrefixLength)
129 propJSON (Proxy :: Proxy TypeName)
130 propJSON (Proxy :: Proxy PercentMatch)
131 propJSON (Proxy :: Proxy StopWord)
132 propJSON (Proxy :: Proxy QueryPath)
133 propJSON (Proxy :: Proxy AllowLeadingWildcard)
134 propJSON (Proxy :: Proxy LowercaseExpanded)
135 propJSON (Proxy :: Proxy EnablePositionIncrements)
136 propJSON (Proxy :: Proxy AnalyzeWildcard)
137 propJSON (Proxy :: Proxy GeneratePhraseQueries)
138 propJSON (Proxy :: Proxy Locale)
139 propJSON (Proxy :: Proxy MaxWordLength)
140 propJSON (Proxy :: Proxy MinWordLength)
141 propJSON (Proxy :: Proxy PhraseSlop)
142 propJSON (Proxy :: Proxy MinDocFrequency)
143 propJSON (Proxy :: Proxy MaxDocFrequency)
144 propJSON (Proxy :: Proxy Filter)
145 propJSON (Proxy :: Proxy Query)
146 propJSON (Proxy :: Proxy SimpleQueryStringQuery)
147 propJSON (Proxy :: Proxy FieldOrFields)
148 propJSON (Proxy :: Proxy SimpleQueryFlag)
149 propJSON (Proxy :: Proxy RegexpQuery)
150 propJSON (Proxy :: Proxy QueryStringQuery)
151 propJSON (Proxy :: Proxy RangeQuery)
152 propJSON (Proxy :: Proxy PrefixQuery)
153 propJSON (Proxy :: Proxy NestedQuery)
154 propJSON (Proxy :: Proxy MoreLikeThisFieldQuery)
155 propJSON (Proxy :: Proxy MoreLikeThisQuery)
156 propJSON (Proxy :: Proxy IndicesQuery)
157 propJSON (Proxy :: Proxy HasParentQuery)
158 propJSON (Proxy :: Proxy HasChildQuery)
159 propJSON (Proxy :: Proxy FuzzyQuery)
160 propJSON (Proxy :: Proxy FuzzyLikeFieldQuery)
161 propJSON (Proxy :: Proxy FuzzyLikeThisQuery)
162 propJSON (Proxy :: Proxy FunctionScoreQuery)
163 propJSON (Proxy :: Proxy BoostMode)
164 propJSON (Proxy :: Proxy ScoreMode)
165 propJSON (Proxy :: Proxy ComponentFunctionScoreFunction)
166 propJSON (Proxy :: Proxy FieldValueFactor)
167 propJSON (Proxy :: Proxy FactorModifier)
168 propJSON (Proxy :: Proxy DisMaxQuery)
169 propJSON (Proxy :: Proxy CommonTermsQuery)
170 propJSON (Proxy :: Proxy CommonMinimumMatch)
171 propJSON (Proxy :: Proxy BoostingQuery)
172 propJSON (Proxy :: Proxy BoolQuery)
173 propJSON (Proxy :: Proxy MatchQuery)
174 propJSON (Proxy :: Proxy MultiMatchQueryType)
175 propJSON (Proxy :: Proxy BooleanOperator)
176 propJSON (Proxy :: Proxy ZeroTermsQuery)
177 propJSON (Proxy :: Proxy MatchQueryType)
178 propJSON (Proxy :: Proxy AliasRouting)
179 propJSON (Proxy :: Proxy IndexAliasCreate)
180 propJSON (Proxy :: Proxy SearchAliasRouting)
181 propJSON (Proxy :: Proxy ScoreType)
182 propJSON (Proxy :: Proxy Distance)
183 propJSON (Proxy :: Proxy DistanceUnit)
184 propJSON (Proxy :: Proxy DistanceType)
185 propJSON (Proxy :: Proxy OptimizeBbox)
186 propJSON (Proxy :: Proxy GeoBoundingBoxConstraint)
187 propJSON (Proxy :: Proxy GeoFilterType)
188 propJSON (Proxy :: Proxy GeoBoundingBox)
189 propJSON (Proxy :: Proxy LatLon)
190 propJSON (Proxy :: Proxy RangeExecution)
191 prop "RegexpFlags FromJSON/ToJSON roundtrips, removing dups " $ \rfs ->
192 let expected = case rfs of
193 SomeRegexpFlags fs -> SomeRegexpFlags (NE.fromList (L.nub (NE.toList fs)))
195 in parseEither parseJSON (toJSON rfs) === Right expected
196 propJSON (Proxy :: Proxy BoolMatch)
197 propJSON (Proxy :: Proxy Term)
198 propJSON (Proxy :: Proxy MultiMatchQuery)
199 propJSON (Proxy :: Proxy IndexSettings)
200 propJSON (Proxy :: Proxy CompoundFormat)
201 propJSON (Proxy :: Proxy TemplateQueryInline)
202 propJSON (Proxy :: Proxy Suggest)
203 propJSON (Proxy :: Proxy DirectGenerators)
204 propJSON (Proxy :: Proxy DirectGeneratorSuggestModeTypes)
206 describe "Approximate isomorphism JSON instances" $ do
207 propApproxJSON (Proxy :: Proxy UpdatableIndexSetting')
208 propApproxJSON (Proxy :: Proxy ReplicaCount)
209 propApproxJSON (Proxy :: Proxy ReplicaBounds)
210 propApproxJSON (Proxy :: Proxy Bytes)
211 propApproxJSON (Proxy :: Proxy AllocationPolicy)
212 propApproxJSON (Proxy :: Proxy InitialShardCount)
213 propApproxJSON (Proxy :: Proxy FSType)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list