3 from test_base import TestBase
4 from resources.pod_models import PodCreateRequest
5 from resources.project_models import ProjectCreateRequest
6 from resources.testcase_models import TestcaseCreateRequest
7 from resources.result_models import ResultCreateRequest, \
8 TestResult, TestResults
9 from common.constants import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND
12 __author__ = '__serena__'
15 class Details(object):
16 def __init__(self, timestart=None, duration=None, status=None):
17 self.timestart = timestart
18 self.duration = duration
23 "timestart": self.timestart,
24 "duration": self.duration,
29 def from_dict(a_dict):
35 t.timestart = a_dict.get('timestart')
36 t.duration = a_dict.get('duration')
37 t.status = a_dict.get('status')
41 class TestResultBase(TestBase):
44 self.project = 'functest'
46 self.installer = 'fuel'
48 self.build_tag = 'v3.0'
49 self.scenario = 'odl-l2'
50 self.criteria = 'passed'
51 self.trust_indicator = 0.7
52 self.start_date = "2016-05-23 07:16:09.477097"
53 self.stop_date = "2016-05-23 07:16:19.477097"
54 super(TestResultBase, self).setUp()
55 self.details = Details(timestart='0', duration='9s', status='OK')
56 self.req_d = ResultCreateRequest(pod_name=self.pod,
57 project_name=self.project,
59 installer=self.installer,
61 start_date=self.start_date,
62 stop_date=self.stop_date,
63 details=self.details.format(),
64 build_tag=self.build_tag,
65 scenario=self.scenario,
66 criteria=self.criteria,
67 trust_indicator=self.trust_indicator)
68 self.get_res = TestResult
69 self.list_res = TestResults
70 self.basePath = '/api/v1/results'
71 self.req_pod = PodCreateRequest(self.pod, 'metal', 'zte pod 1')
72 self.req_project = ProjectCreateRequest(self.project, 'vping test')
73 self.req_testcase = TestcaseCreateRequest(self.case,
76 self.create_help('/api/v1/pods', self.req_pod)
77 self.create_help('/api/v1/projects', self.req_project)
78 self.create_help('/api/v1/projects/%s/cases',
82 def assert_res(self, code, result):
83 self.assertEqual(code, HTTP_OK)
85 self.assertEqual(result.pod_name, req.pod_name)
86 self.assertEqual(result.project_name, req.project_name)
87 self.assertEqual(result.case_name, req.case_name)
88 self.assertEqual(result.installer, req.installer)
89 self.assertEqual(result.version, req.version)
90 details_req = Details.from_dict(req.details)
91 details_res = Details.from_dict(result.details)
92 self.assertEqual(details_res.duration, details_req.duration)
93 self.assertEqual(details_res.timestart, details_req.timestart)
94 self.assertEqual(details_res.status, details_req.status)
95 self.assertEqual(result.build_tag, req.build_tag)
96 self.assertEqual(result.scenario, req.scenario)
97 self.assertEqual(result.criteria, req.criteria)
98 self.assertEqual(result.trust_indicator, req.trust_indicator)
99 self.assertIsNotNone(result.start_date)
100 self.assertIsNotNone(result.stop_date)
101 self.assertIsNotNone(result._id)
104 class TestResultCreate(TestResultBase):
105 def test_nobody(self):
106 (code, body) = self.create(None)
107 self.assertEqual(code, HTTP_BAD_REQUEST)
108 self.assertIn('no payload', body)
110 def test_podNotProvided(self):
113 (code, body) = self.create(req)
114 self.assertEqual(code, HTTP_BAD_REQUEST)
115 self.assertIn('pod is not provided', body)
117 def test_projectNotProvided(self):
119 req.project_name = None
120 (code, body) = self.create(req)
121 self.assertEqual(code, HTTP_BAD_REQUEST)
122 self.assertIn('project is not provided', body)
124 def test_testcaseNotProvided(self):
127 (code, body) = self.create(req)
128 self.assertEqual(code, HTTP_BAD_REQUEST)
129 self.assertIn('testcase is not provided', body)
131 def test_noPod(self):
133 req.pod_name = 'notExistPod'
134 (code, body) = self.create(req)
135 self.assertEqual(code, HTTP_NOT_FOUND)
136 self.assertIn('Could not find POD', body)
138 def test_noProject(self):
140 req.project_name = 'notExistProject'
141 (code, body) = self.create(req)
142 self.assertEqual(code, HTTP_NOT_FOUND)
143 self.assertIn('Could not find project', body)
145 def test_noTestcase(self):
147 req.case_name = 'notExistTestcase'
148 (code, body) = self.create(req)
149 self.assertEqual(code, HTTP_NOT_FOUND)
150 self.assertIn('Could not find testcase', body)
152 def test_success(self):
153 (code, body) = self.create_d()
154 self.assertEqual(code, HTTP_OK)
155 self.assert_href(body)
158 class TestResultGet(TestResultBase):
159 def test_getOne(self):
160 _, res = self.create_d()
161 _id = res.href.split('/')[-1]
162 code, body = self.get(_id)
163 self.assert_res(code, body)
165 def test_queryPod(self):
166 self._query_and_assert(self._set_query('pod'))
168 def test_queryProject(self):
169 self._query_and_assert(self._set_query('project'))
171 def test_queryTestcase(self):
172 self._query_and_assert(self._set_query('case'))
174 def test_queryVersion(self):
175 self._query_and_assert(self._set_query('version'))
177 def test_queryInstaller(self):
178 self._query_and_assert(self._set_query('installer'))
180 def test_queryBuildTag(self):
181 self._query_and_assert(self._set_query('build_tag'))
183 def test_queryScenario(self):
184 self._query_and_assert(self._set_query('scenario'))
186 def test_queryTrustIndicator(self):
187 self._query_and_assert(self._set_query('trust_indicator'))
189 def test_queryCriteria(self):
190 self._query_and_assert(self._set_query('criteria'))
192 def test_queryPeriod(self):
193 self._query_and_assert(self._set_query('period=1'))
195 def test_combination(self):
196 self._query_and_assert(self._set_query('pod',
207 def test_notFound(self):
208 self._query_and_assert(self._set_query('pod=notExistPod',
220 def _query_and_assert(self, query, found=True):
221 _, res = self.create_d()
222 code, body = self.query(query)
224 self.assertEqual(code, HTTP_OK)
225 self.assertEqual(0, len(body.results))
227 for result in body.results:
228 self.assert_res(code, result)
230 def _set_query(self, *args):
236 uri += '{}={}&'.format(arg, eval('self.' + arg))
239 if __name__ == '__main__':