9c0eb6363cbc350d8ee64688a291d1f927f0378f
[releng.git] / utils / test / result_collection_api / resources / testcase_handlers.py
1 from common.constants import HTTP_FORBIDDEN
2 from resources.handlers import GenericApiHandler
3 from resources.testcase_models import Testcase
4 from tornado_swagger_ui.tornado_swagger import swagger
5
6
7 class GenericTestcaseHandler(GenericApiHandler):
8     def __init__(self, application, request, **kwargs):
9         super(GenericTestcaseHandler, self).__init__(application,
10                                                      request,
11                                                      **kwargs)
12         self.table = self.db_testcases
13         self.table_cls = Testcase
14
15
16 class TestcaseCLHandler(GenericTestcaseHandler):
17     @swagger.operation(nickname="list-all")
18     def get(self, project_name):
19         """
20             @description: list all testcases of a project by project_name
21             @return 200: return all testcases of this project,
22                          empty list is no testcase exist in this project
23             @rtype: L{TestCases}
24         """
25         query = dict()
26         query['project_name'] = project_name
27         self._list(query)
28
29     @swagger.operation(nickname="create")
30     def post(self, project_name):
31         """
32             @description: create a testcase of a project by project_name
33             @param body: testcase to be created
34             @type body: L{TestcaseCreateRequest}
35             @in body: body
36             @rtype: L{Testcase}
37             @return 200: testcase is created in this project.
38             @raise 403: project not exist
39                         or testcase already exists in this project
40             @raise 400: post without body
41         """
42         def p_query(data):
43             return {'name': data.project_name}
44
45         def tc_query(data):
46             return {
47                 'project_name': data.project_name,
48                 'name': data.name
49             }
50
51         def p_error(data):
52             message = 'Could not find project [{}]'.format(data.project_name)
53             return HTTP_FORBIDDEN, message
54
55         def tc_error(data):
56             message = '{} already exists as a testcase in project {}'\
57                 .format(data.name, data.project_name)
58             return HTTP_FORBIDDEN, message
59
60         db_checks = [(self.db_projects, True, p_query, p_error),
61                      (self.db_testcases, False, tc_query, tc_error)]
62         self._create(db_checks, project_name=project_name)
63
64
65 class TestcaseGURHandler(GenericTestcaseHandler):
66     @swagger.operation(nickname='get-one')
67     def get(self, project_name, case_name):
68         """
69             @description: get a single testcase
70                             by case_name and project_name
71             @rtype: L{Testcase}
72             @return 200: testcase exist
73             @raise 404: testcase not exist
74         """
75         query = dict()
76         query['project_name'] = project_name
77         query["name"] = case_name
78         self._get_one(query)
79
80     @swagger.operation(nickname="update")
81     def put(self, project_name, case_name):
82         """
83             @description: update a single testcase
84                             by project_name and case_name
85             @param body: testcase to be updated
86             @type body: L{TestcaseUpdateRequest}
87             @in body: body
88             @rtype: L{Project}
89             @return 200: update success
90             @raise 404: testcase or project not exist
91             @raise 403: new testcase name already exist in project
92                         or nothing to update
93         """
94         query = {'project_name': project_name, 'name': case_name}
95         db_keys = ['name', 'project_name']
96         self._update(query, db_keys)
97
98     @swagger.operation(nickname='delete')
99     def delete(self, project_name, case_name):
100         """
101             @description: delete a testcase by project_name and case_name
102             @return 200: delete success
103             @raise 404: testcase not exist
104         """
105         query = {'project_name': project_name, 'name': case_name}
106         self._delete(query)