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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
module QuerySpec
( spec )
where
import qualified Data.Vector as V
import qualified Data.Aeson as JSON
import Data.Aeson.QQ.Simple (aesonQQ)
import Data.Aeson (Value)
import Data.Vector (Vector)
import Test.Hspec
import Data.Aeson.JSONPath (queryQQ, jsonPath)
import Prelude
getVector :: Value -> Vector Value
getVector (JSON.Array arr) = arr
getVector _ = V.empty
-- taken from https://serdejsonpath.live/
rootDoc :: Value
rootDoc = [aesonQQ|{
"store": {
"books": [
{
"title": "Guns, Germs, and Steel",
"author": "Jared Diamond",
"category": "reference",
"price": 24.99
},
{
"title": "David Copperfield",
"author": "Charles Dickens",
"category": "fiction",
"price": 12.99
},
{
"title": "Moby Dick",
"author": "Herman Melville",
"category": "fiction",
"price": 8.99
},
{
"title": "Crime and Punishment",
"author": "Fyodor Dostoevsky",
"category": "fiction",
"price": 19.99
}
]
}
}|]
storeDoc :: Value
storeDoc = [aesonQQ|
{
"books": [
{
"title": "Guns, Germs, and Steel",
"author": "Jared Diamond",
"category": "reference",
"price": 24.99
},
{
"title": "David Copperfield",
"author": "Charles Dickens",
"category": "fiction",
"price": 12.99
},
{
"title": "Moby Dick",
"author": "Herman Melville",
"category": "fiction",
"price": 8.99
},
{
"title": "Crime and Punishment",
"author": "Fyodor Dostoevsky",
"category": "fiction",
"price": 19.99
}
]
}|]
booksDoc :: Value
booksDoc = [aesonQQ| [
{
"title": "Guns, Germs, and Steel",
"author": "Jared Diamond",
"category": "reference",
"price": 24.99
},
{
"title": "David Copperfield",
"author": "Charles Dickens",
"category": "fiction",
"price": 12.99
},
{
"title": "Moby Dick",
"author": "Herman Melville",
"category": "fiction",
"price": 8.99
},
{
"title": "Crime and Punishment",
"author": "Fyodor Dostoevsky",
"category": "fiction",
"price": 19.99
}
]|]
books0Doc :: Value
books0Doc = [aesonQQ|[
{
"title": "Guns, Germs, and Steel",
"author": "Jared Diamond",
"category": "reference",
"price": 24.99
}
]|]
books0And2Doc :: Value
books0And2Doc = [aesonQQ|[
{
"title": "Guns, Germs, and Steel",
"author": "Jared Diamond",
"category": "reference",
"price": 24.99
},
{
"title": "Moby Dick",
"author": "Herman Melville",
"category": "fiction",
"price": 8.99
}
]|]
books1To3Doc :: Value
books1To3Doc = [aesonQQ|[
{
"title": "David Copperfield",
"author": "Charles Dickens",
"category": "fiction",
"price": 12.99
},
{
"title": "Moby Dick",
"author": "Herman Melville",
"category": "fiction",
"price": 8.99
}
]|]
books1To3And0And1Doc :: Value
books1To3And0And1Doc = [aesonQQ|[
{
"title": "David Copperfield",
"author": "Charles Dickens",
"category": "fiction",
"price": 12.99
},
{
"title": "Moby Dick",
"author": "Herman Melville",
"category": "fiction",
"price": 8.99
},
{
"title": "Guns, Germs, and Steel",
"author": "Jared Diamond",
"category": "reference",
"price": 24.99
},
{
"title": "David Copperfield",
"author": "Charles Dickens",
"category": "fiction",
"price": 12.99
}
]|]
lessThanPrice20Books :: Value
lessThanPrice20Books = [aesonQQ| [
{
"title": "David Copperfield",
"author": "Charles Dickens",
"category": "fiction",
"price": 12.99
},
{
"title": "Moby Dick",
"author": "Herman Melville",
"category": "fiction",
"price": 8.99
},
{
"title": "Crime and Punishment",
"author": "Fyodor Dostoevsky",
"category": "fiction",
"price": 19.99
}
]|]
alphaArr :: Value
alphaArr = [aesonQQ| ["a","b","c","d","e","f","g"] |]
fgArr :: Value
fgArr = [aesonQQ| ["f","g"] |]
bdArr :: Value
bdArr = [aesonQQ| ["b","d"] |]
fdArr :: Value
fdArr = [aesonQQ| ["f","d"] |]
gfedcbaArr :: Value
gfedcbaArr = [aesonQQ| ["g","f","e","d","c","b","a"] |]
rfcExample1 :: Value
rfcExample1 = [aesonQQ| {
"o": {"j": 1, "k": 2},
"a": [5, 3, [{"j": 4}, {"k": 6}]]
} |]
rfcExample1Desc :: Value
rfcExample1Desc = [aesonQQ| [
[5, 3, [{"j": 4}, {"k": 6}]],
{"j": 1, "k": 2},
5,
3,
[{"j": 4}, {"k": 6}],
{"j": 4},
{"k": 6},
4,
6,
1,
2
]|]
boolsAndNulls :: Value
boolsAndNulls = [aesonQQ| [
{ "a": true },
{ "a": false },
{ "a": null }
]|]
boolsAndNullsA :: Value
boolsAndNullsA = [aesonQQ| [{ "a": true }] |]
boolsAndNullsB :: Value
boolsAndNullsB = [aesonQQ| [{ "a": false }] |]
boolsAndNullsC :: Value
boolsAndNullsC = [aesonQQ| [{ "a": null }] |]
spec :: Spec
spec = do
describe "test query" $ do
it "returns root document when query is $" $
queryQQ [jsonPath|$|] rootDoc `shouldBe` V.singleton rootDoc
it "returns store object when queryQQ is $.store" $
queryQQ [jsonPath|$.store|] rootDoc `shouldBe` V.singleton storeDoc
it "returns store object when queryQQ is $['store']" $
queryQQ [jsonPath|$['store']|] rootDoc `shouldBe` V.singleton storeDoc
it "returns books array when queryQQ is $.store.books" $
queryQQ [jsonPath|$.store.books|] rootDoc `shouldBe` V.singleton booksDoc
it "returns 0-index book item when queryQQ is $.store.books[0]" $
queryQQ [jsonPath|$.store.books[0]|] rootDoc `shouldBe` getVector books0Doc
it "returns 0-index book item when queryQQ is $.store.books[-4]" $
queryQQ [jsonPath|$.store.books[-4]|] rootDoc `shouldBe` getVector books0Doc
it "returns 0,2-index item when queryQQ is $.store.books[0,2]" $
queryQQ [jsonPath|$.store.books[0,2]|] rootDoc `shouldBe` getVector books0And2Doc
it "returns 1To3-index when queryQQ is $.store.books[1:3]" $
queryQQ [jsonPath|$.store.books[1:3]|] rootDoc `shouldBe` getVector books1To3Doc
it "returns 1To3-index and 0,1-index when queryQQ is $.store.books[1:3,0,1]" $
queryQQ [jsonPath|$.store.books[1:3,0,1]|] rootDoc `shouldBe` getVector books1To3And0And1Doc
it "returns slice with queryQQ $[5:]" $
queryQQ [jsonPath|$[5:]|] alphaArr `shouldBe` getVector fgArr
it "returns slice with queryQQ $[1:5:2]" $
queryQQ [jsonPath|$[1:5:2]|] alphaArr `shouldBe` getVector bdArr
it "returns slice with queryQQ $[5:1:-2]" $
queryQQ [jsonPath|$[5:1:-2]|] alphaArr `shouldBe` getVector fdArr
it "returns slice with queryQQ $[::-1]" $
queryQQ [jsonPath|$[::-1]|] alphaArr `shouldBe` getVector gfedcbaArr
it "returns root with queryQQ $.*" $
queryQQ [jsonPath|$.*|] rootDoc `shouldBe` V.singleton storeDoc
it "returns root with queryQQ $[*]" $
queryQQ [jsonPath|$[*]|] rootDoc `shouldBe` V.singleton storeDoc
it "returns descendants with queryQQ $..*" $
queryQQ [jsonPath|$..*|] rfcExample1 `shouldBe` getVector rfcExample1Desc
it "returns descendants with queryQQ $..[*]" $
queryQQ [jsonPath|$..[*]|] rfcExample1 `shouldBe` getVector rfcExample1Desc
it "returns with filtering: number comparison" $
queryQQ [jsonPath|$.store.books[?@.price < 20]|] rootDoc
`shouldBe` getVector lessThanPrice20Books
it "returns with filtering: not operator" $
queryQQ [jsonPath|$.store.books[?!(@.price < 20)]|] rootDoc
`shouldBe` getVector books0Doc
it "returns with filtering: true value" $
queryQQ [jsonPath|$[?@.a == true]|] boolsAndNulls
`shouldBe` getVector boolsAndNullsA
it "returns with filtering: false value" $
queryQQ [jsonPath|$[?@.a == false]|] boolsAndNulls
`shouldBe` getVector boolsAndNullsB
it "returns with filtering: null value" $
queryQQ [jsonPath|$[?@.a == null]|] boolsAndNulls
`shouldBe` getVector boolsAndNullsC
it "returns with filtering: string comparison" $
queryQQ [jsonPath|$.store.books[?@.author == 'Jared Diamond']|] rootDoc
`shouldBe` getVector books0Doc
it "returns with filtering: test expression" $
queryQQ [jsonPath|$.store.books[?@.author]|] rootDoc
`shouldBe` getVector booksDoc
it "returns with filtering with spaced out segments: test expression" $
queryQQ [jsonPath|$ .store .books[?@ .author]|] rootDoc
`shouldBe` getVector booksDoc
it "returns with filtering: test expr gives empty with non-existent key" $
queryQQ [jsonPath|$.store.books[?@.not_here]|] rootDoc
`shouldBe` V.empty
|