From b3d17f52355c978d8cf2ab94b773dfe4c737c26d Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 4 Oct 2012 02:08:56 +0100
Subject: [PATCH] Add a makeSimpleFilter function

And associated unittests. This will be needed for classic-style
queries.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 htest/Test/Ganeti/Query/Filter.hs | 14 ++++++++++++++
 htools/Ganeti/Query/Filter.hs     |  7 +++++++
 2 files changed, 21 insertions(+)

diff --git a/htest/Test/Ganeti/Query/Filter.hs b/htest/Test/Ganeti/Query/Filter.hs
index 6bb0c2ce2..8f7044b59 100644
--- a/htest/Test/Ganeti/Query/Filter.hs
+++ b/htest/Test/Ganeti/Query/Filter.hs
@@ -42,6 +42,7 @@ import Test.Ganeti.Objects (genEmptyCluster)
 import Ganeti.BasicTypes
 import Ganeti.JSON
 import Ganeti.Objects
+import Ganeti.Query.Filter
 import Ganeti.Query.Language
 import Ganeti.Query.Query
 import Ganeti.Utils (niceSort)
@@ -159,9 +160,22 @@ prop_node_bad_filter rndname rndint =
              "numeric value in non-list field"
            ]
 
+-- | Tests make simple filter.
+prop_makeSimpleFilter :: Property
+prop_makeSimpleFilter =
+  forAll (resize 10 $ listOf1 getName) $ \names ->
+  forAll getName $ \namefield ->
+  conjoin [ printTestCase "test expected names" $
+              makeSimpleFilter namefield names ==?
+              OrFilter (map (EQFilter namefield . QuotedString) names)
+          , printTestCase "test empty names" $
+              makeSimpleFilter namefield [] ==? EmptyFilter
+          ]
+
 testSuite "Query/Filter"
   [ 'prop_node_single_filter
   , 'prop_node_many_filter
   , 'prop_node_regex_filter
   , 'prop_node_bad_filter
+  , 'prop_makeSimpleFilter
   ]
diff --git a/htools/Ganeti/Query/Filter.hs b/htools/Ganeti/Query/Filter.hs
index 24ce79639..42453e167 100644
--- a/htools/Ganeti/Query/Filter.hs
+++ b/htools/Ganeti/Query/Filter.hs
@@ -48,6 +48,7 @@ module Ganeti.Query.Filter
   ( compileFilter
   , evaluateFilter
   , requestedNames
+  , makeSimpleFilter
   ) where
 
 import Control.Applicative
@@ -185,3 +186,9 @@ requestedNames namefield (EQFilter fld val) =
     then Just [val]
     else Nothing
 requestedNames _ _ = Nothing
+
+-- | Builds a simple filter from a list of names.
+makeSimpleFilter :: String -> [String] -> Filter FilterField
+makeSimpleFilter _ [] = EmptyFilter
+makeSimpleFilter namefield vals =
+  OrFilter $ map (EQFilter namefield . QuotedString) vals
-- 
GitLab