Debian Codegraph @

test/hs/Test/Ganeti/Query/Aliases.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for query aliases.
5
6 -}
7
8 {-
9
10 Copyright (C) 2013 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.Aliases
39 ( testQuery_Aliases
40 ) where
41
42 import Data.List
43
44 import Test.Ganeti.TestHelper
45 import Test.HUnit
46
47 import Ganeti.Query.Common ()
48 import qualified Ganeti.Query.Instance as I
49 import Ganeti.Query.Language
50 import Ganeti.Query.Types
51
52 {-# ANN module "HLint: ignore Use camelCase" #-}
53
54 -- | Converts field list to field name list
55 toFieldNameList :: FieldList a b -> [FieldName]
56 toFieldNameList = map (\(x,_,_) -> fdefName x)
57
58 -- | Converts alias list to alias name list
59 toAliasNameList :: [(FieldName, FieldName)] -> [FieldName]
60 toAliasNameList = map fst
61
62 -- | Converts alias list to alias target list
63 toAliasTargetList :: [(FieldName, FieldName)] -> [FieldName]
64 toAliasTargetList = map snd
65
66 -- | Checks for shadowing
67 checkShadowing :: String
68 -> FieldList a b
69 -> [(FieldName, FieldName)]
70 -> Assertion
71 checkShadowing name fields aliases =
72 assertBool (name ++ " aliases do not shadow fields") .
73 null $ toFieldNameList fields `intersect` toAliasNameList aliases
74
75 -- | Checks for target existence
76 checkTargets :: String
77 -> FieldList a b
78 -> [(FieldName, FieldName)]
79 -> Assertion
80 checkTargets name fields aliases =
81 assertBool (name ++ " alias targets exist") .
82 null $ toAliasTargetList aliases \\ toFieldNameList fields
83
84 -- | Check that instance aliases do not shadow existing fields
85 case_instanceAliasesNoShadowing :: Assertion
86 case_instanceAliasesNoShadowing =
87 checkShadowing "Instance" I.instanceFields I.instanceAliases
88
89 -- | Check that instance alias targets exist
90 case_instanceAliasesTargetsExist :: Assertion
91 case_instanceAliasesTargetsExist =
92 checkTargets "Instance" I.instanceFields I.instanceAliases
93
94 testSuite "Query/Aliases"
95 [ 'case_instanceAliasesNoShadowing,
96 'case_instanceAliasesTargetsExist
97 ]