Debian Codegraph @

test/hs/Test/Ganeti/Query/Language.hs

raw
1 {-# LANGUAGE TemplateHaskell, FlexibleInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for ganeti-htools.
5
6 -}
7
8 {-
9
10 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.Query.Language
39 ( testQuery_Language
40 , genFilter
41 , genJSValue
42 ) where
43
44 import Test.HUnit (Assertion, assertEqual)
45 import Test.QuickCheck
46
47 import Control.Arrow (second)
48 import Text.JSON
49
50 import Test.Ganeti.TestHelper
51 import Test.Ganeti.TestCommon
52
53 import Ganeti.JSON
54 import Ganeti.Query.Language
55
56 {-# ANN module "HLint: ignore Use camelCase" #-}
57
58
59 instance Arbitrary (Filter FilterField) where
60 arbitrary = genFilter
61
62 instance Arbitrary FilterRegex where
63 arbitrary = genName >>= mkRegex -- a name should be a good regex
64
65 -- | Custom 'Filter' generator (top-level), which enforces a
66 -- (sane) limit on the depth of the generated filters.
67 genFilter :: Gen (Filter FilterField)
68 genFilter = choose (0, 10) >>= genFilter'
69
70 -- | Custom generator for filters that correctly halves the state of
71 -- the generators at each recursive step, per the QuickCheck
72 -- documentation, in order not to run out of memory.
73 genFilter' :: Int -> Gen (Filter FilterField)
74 genFilter' 0 =
75 oneof [ pure EmptyFilter
76 , TrueFilter <$> genName
77 , EQFilter <$> genName <*> value
78 , LTFilter <$> genName <*> value
79 , GTFilter <$> genName <*> value
80 , LEFilter <$> genName <*> value
81 , GEFilter <$> genName <*> value
82 , RegexpFilter <$> genName <*> arbitrary
83 , ContainsFilter <$> genName <*> value
84 ]
85 where value = oneof [ QuotedString <$> genName
86 , NumericValue <$> arbitrary
87 ]
88 genFilter' n =
89 oneof [ AndFilter <$> vectorOf n'' (genFilter' n')
90 , OrFilter <$> vectorOf n'' (genFilter' n')
91 , NotFilter <$> genFilter' n'
92 ]
93 where n' = n `div` 2 -- sub-filter generator size
94 n'' = max n' 2 -- but we don't want empty or 1-element lists,
95 -- so use this for and/or filter list length
96
97 $(genArbitrary ''QueryTypeOp)
98
99 $(genArbitrary ''QueryTypeLuxi)
100
101 $(genArbitrary ''ItemType)
102
103 $(genArbitrary ''ResultStatus)
104
105 $(genArbitrary ''FieldType)
106
107 $(genArbitrary ''FieldDefinition)
108
109 -- | Generates an arbitrary JSValue. We do this via a function a not
110 -- via arbitrary instance since that would require us to define an
111 -- arbitrary for JSValue, which can be recursive, entering the usual
112 -- problems with that; so we only generate the base types, not the
113 -- recursive ones, and not 'JSNull', which we can't use in a
114 -- 'RSNormal' 'ResultEntry'.
115 genJSValue :: Gen JSValue
116 genJSValue =
117 oneof [ JSBool <$> arbitrary
118 , JSRational <$> pure False <*> arbitrary
119 , JSString <$> (toJSString <$> arbitrary)
120 , (JSArray . map showJSON) <$> (arbitrary::Gen [Int])
121 , JSObject . toJSObject . map (second showJSON) <$>
122 (arbitrary::Gen [(String, Int)])
123 ]
124
125 -- | Generates a 'ResultEntry' value.
126 genResultEntry :: Gen ResultEntry
127 genResultEntry = do
128 rs <- arbitrary
129 rv <- case rs of
130 RSNormal -> Just <$> genJSValue
131 _ -> pure Nothing
132 return $ ResultEntry rs rv
133
134 $(genArbitrary ''QueryFieldsResult)
135
136 -- | Tests that serialisation/deserialisation of filters is
137 -- idempotent.
138 prop_filter_serialisation :: Property
139 prop_filter_serialisation = forAll genFilter testSerialisation
140
141 -- | Tests that filter regexes are serialised correctly.
142 prop_filterregex_instances :: FilterRegex -> Property
143 prop_filterregex_instances rex =
144 counterexample "failed JSON encoding" (testSerialisation rex)
145
146 -- | Tests 'ResultStatus' serialisation.
147 prop_resultstatus_serialisation :: ResultStatus -> Property
148 prop_resultstatus_serialisation = testSerialisation
149
150 -- | Tests 'FieldType' serialisation.
151 prop_fieldtype_serialisation :: FieldType -> Property
152 prop_fieldtype_serialisation = testSerialisation
153
154 -- | Tests 'FieldDef' serialisation.
155 prop_fielddef_serialisation :: FieldDefinition -> Property
156 prop_fielddef_serialisation = testSerialisation
157
158 -- | Tests 'ResultEntry' serialisation. Needed especially as this is
159 -- done manually, and not via buildObject (different serialisation
160 -- format).
161 prop_resultentry_serialisation :: Property
162 prop_resultentry_serialisation = forAll genResultEntry testSerialisation
163
164 -- | Tests 'FieldDef' serialisation. We use a made-up maximum limit of
165 -- 20 for the generator, since otherwise the lists become too long and
166 -- we don't care so much about list length but rather structure.
167 prop_fieldsresult_serialisation :: Property
168 prop_fieldsresult_serialisation =
169 forAll (resize 20 arbitrary::Gen QueryFieldsResult) testSerialisation
170
171 -- | Tests 'ItemType' serialisation.
172 prop_itemtype_serialisation :: ItemType -> Property
173 prop_itemtype_serialisation = testSerialisation
174
175 -- | Tests basic cases of filter parsing, including legacy ones.
176 case_filterParsing :: Assertion
177 case_filterParsing = do
178 let check :: String -> Filter String -> Assertion
179 check str expected = do
180 jsval <- fromJResult "could not parse filter" $ decode str
181 assertEqual str expected jsval
182
183 val = QuotedString "val"
184
185 valRegex <- mkRegex "val"
186
187 check "null" EmptyFilter
188 check "[\"&\", null, null]" $ AndFilter [EmptyFilter, EmptyFilter]
189 check "[\"|\", null, null]" $ OrFilter [EmptyFilter, EmptyFilter]
190 check "[\"!\", null]" $ NotFilter EmptyFilter
191 check "[\"?\", \"field\"]" $ TrueFilter "field"
192 check "[\"==\", \"field\", \"val\"]" $ EQFilter "field" val
193 check "[\"<\", \"field\", \"val\"]" $ LTFilter "field" val
194 check "[\">\", \"field\", \"val\"]" $ GTFilter "field" val
195 check "[\"<=\", \"field\", \"val\"]" $ LEFilter "field" val
196 check "[\">=\", \"field\", \"val\"]" $ GEFilter "field" val
197 check "[\"=~\", \"field\", \"val\"]" $ RegexpFilter "field" valRegex
198 check "[\"=[]\", \"field\", \"val\"]" $ ContainsFilter "field" val
199 -- Legacy filters
200 check "[\"=\", \"field\", \"val\"]" $ EQFilter "field" val
201 check "[\"!=\", \"field\", \"val\"]" $ NotFilter (EQFilter "field" val)
202
203 testSuite "Query/Language"
204 [ 'prop_filter_serialisation
205 , 'prop_filterregex_instances
206 , 'prop_resultstatus_serialisation
207 , 'prop_fieldtype_serialisation
208 , 'prop_fielddef_serialisation
209 , 'prop_resultentry_serialisation
210 , 'prop_fieldsresult_serialisation
211 , 'prop_itemtype_serialisation
212 , 'case_filterParsing
213 ]