4 # Copyright 2017 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
28 from rift
.mano
.utils
import project
29 gi
.require_version('RwKeyspec', '1.0')
30 from gi
.repository
.RwKeyspec
import quoted_key
34 XPATH
= "/rw-project:project[rw-project:name={}]".format(quoted_key(NAME
))
36 class TestCase(unittest
.TestCase
):
40 def set_logger(cls
, log
):
45 log
= logging
.getLogger()
46 log
.setLevel( logging
.ERROR
)
48 def test_create_from_xpath(self
):
51 1. Instance of project from xpath
52 2. project name in instance is correct
54 proj
= project
.ManoProject
.create_from_xpath(XPATH
, TestCase
.log
)
56 assert NAME
== proj
.name
57 assert XPATH
== proj
.prefix
59 obj
= project
.ManoProject
.create_from_xpath(proj
.prefix
, TestCase
.log
)
61 assert NAME
== obj
.name
62 assert XPATH
== obj
.prefix
64 def test_create(self
):
67 1. Instance of project
68 2. project name in instance is correct
70 proj
= project
.ManoProject(TestCase
.log
, name
=NAME
)
72 assert NAME
== proj
.name
73 assert XPATH
== proj
.prefix
75 obj
= project
.ManoProject
.create_from_xpath(proj
.prefix
, TestCase
.log
)
77 assert NAME
== obj
.name
78 assert XPATH
== obj
.prefix
80 def test_create_update(self
):
83 1. Instance of project
84 2. Set project name later
85 3. project name in instance is correct
87 proj
= project
.ManoProject(TestCase
.log
)
89 assert None == proj
.name
92 assert NAME
== proj
.name
93 assert XPATH
== proj
.prefix
97 except project
.ManoProjNameSetErr
as e
:
98 TestCase
.log
.debug("Expected exception: {}".format(e
))
102 obj
= project
.ManoProject
.create_from_xpath(proj
.prefix
, TestCase
.log
)
104 assert NAME
== obj
.name
105 assert XPATH
== obj
.prefix
107 def test_update_from_xpath(self
):
110 1. Instance of project
112 2. project name in instance is correct
114 proj
= project
.ManoProject(TestCase
.log
)
116 assert proj
.name
is None
118 proj
.set_from_xpath(XPATH
)
119 assert NAME
== proj
.name
120 assert XPATH
== proj
.prefix
123 proj
.set_from_xpath(XPATH
)
124 except project
.ManoProjNameSetErr
as e
:
125 TestCase
.log
.debug("Expected exception: {}".format(e
))
129 obj
= project
.ManoProject
.create_from_xpath(proj
.prefix
, TestCase
.log
)
131 assert NAME
== obj
.name
132 assert XPATH
== obj
.prefix
134 def test_create_from_xpath1(self
):
137 1. Instance of project from xpath
138 2. project name in instance is correct
140 xpath
= XPATH
+ '/rw:project/rw-project:project/rw-project:project/rw-project:project/rw-project:project/project-nsd:nsd-catalog/project-nsd:nsd[id=\'1232334\']'
141 proj
= project
.ManoProject
.create_from_xpath(xpath
, TestCase
.log
)
143 assert NAME
== proj
.name
144 assert XPATH
== proj
.prefix
146 def test_create_from_xpath2(self
):
149 1. Instance of project from xpath
150 2. project name in instance is correct
152 xpath
= '/rw-project:project[name={}]'.format(quoted_key(NAME
))
153 proj
= project
.ManoProject
.create_from_xpath(xpath
, TestCase
.log
)
155 assert NAME
== proj
.name
156 assert XPATH
== proj
.prefix
158 def test_create_from_xpath_invalid(self
):
161 1. Exception due to invalid XPATH format for extracting project
165 proj
= project
.ManoProject
.create_from_xpath(xpath
, TestCase
.log
)
166 except project
.ManoProjXpathNoProjErr
as e
:
167 TestCase
.log
.debug("Expected exception: {}".format(e
))
171 def test_create_from_xpath_invalid1(self
):
174 1. Exception due to invalid XPATH format for extracting project
176 xpath
= '/rw-project:project/{}'.format(NAME
)
178 proj
= project
.ManoProject
.create_from_xpath(xpath
, TestCase
.log
)
179 except project
.ManoProjXpathKeyErr
as e
:
180 TestCase
.log
.debug("Expected exception: {}".format(e
))
184 def test_create_from_xpath_invalid2(self
):
187 1. Exception due to invalid XPATH format for extracting project
189 xpath
= '/rw-project:project[id=test]'
191 proj
= project
.ManoProject
.create_from_xpath(xpath
, TestCase
.log
)
192 except project
.ManoProjXpathKeyErr
as e
:
193 TestCase
.log
.debug("Expected exception: {}".format(e
))
201 def main(argv
=sys
.argv
[1:]):
202 logging
.basicConfig(format
='TEST %(message)s')
204 runner
= xmlrunner
.XMLTestRunner(output
=os
.environ
["RIFT_MODULE_TEST"])
205 parser
= argparse
.ArgumentParser()
206 parser
.add_argument('-v', '--verbose', action
='store_true')
207 parser
.add_argument('-n', '--no-runner', action
='store_true')
209 args
, unknown
= parser
.parse_known_args(argv
)
213 # Set the global logging level
214 log
= logging
.getLogger()
215 log
.setLevel(logging
.DEBUG
if args
.verbose
else logging
.ERROR
)
216 TestCase
.set_logger(log
)
218 # The unittest framework requires a program name, so use the name of this
219 # file instead (we do not want to have to pass a fake program name to main
220 # when this is called from the interpreter).
221 unittest
.main(argv
=[__file__
] + unknown
+ ["-v"], testRunner
=runner
)
223 if __name__
== '__main__':