summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/cli/testcases.py
blob: 3052c1814b3c1fdffe8078957ad109b60025c5be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json

from testapiclient.utils import command
from testapiclient.utils import urlparse
from testapiclient.models import testcase


def testcases_url(name):
    return urlparse.resource_join('projects', name, 'cases')


def testcase_url(parsed_args):
    return urlparse.path_join(
        testcases_url(parsed_args.project_name), parsed_args.name)


class TestcaseGet(command.Lister):

    def get_parser(self, prog_name):
        parser = super(TestcaseGet, self).get_parser(prog_name)
        parser.add_argument('--project-name',
                            required=True,
                            help='Search testcases by project name')
        return parser

    def take_action(self, parsed_args):
        columns = (
            'name',
            '_id',
            'creator',
            'creation_date'
        )
        data = self.app.client_manager.get(
            testcases_url(parsed_args.project_name))
        return self.format_output(columns, data.get('testcases', []))


class TestcaseGetOne(command.ShowOne):

    def get_parser(self, prog_name):
        parser = super(TestcaseGetOne, self).get_parser(prog_name)
        parser.add_argument('--project-name',
                            required=True,
                            help='Search testcase by project name')
        parser.add_argument('name',
                            help='Search testcase by name')
        return parser

    def take_action(self, parsed_args):
        return self.format_output(
            self.app.client_manager.get(testcase_url(parsed_args)))


class TestcaseCreate(command.ShowOne):

    def get_parser(self, prog_name):
        parser = super(TestcaseCreate, self).get_parser(prog_name)
        parser.add_argument('--project-name',
                            required=True,
                            help='Create testcase under project name')
        parser.add_argument('testcase',
                            type=json.loads,
                            help='Testcase create request format:\n'
                                 '\'{}\''.format(json.dumps(
                                     testcase.TestCaseCreateRequest().__dict__
                                     )))
        return parser

    def take_action(self, parsed_args):
        return self.format_output(
            self.app.client_manager.post(
                testcases_url(parsed_args.project_name), parsed_args.testcase))


class TestcaseDelete(command.Command):

    def get_parser(self, prog_name):
        parser = super(TestcaseDelete, self).get_parser(prog_name)
        parser.add_argument('--project-name',
                            required=True,
                            type=str,
                            help='Delete testcase by project name')
        parser.add_argument('name',
                            type=str,
                            help='Delete testcase by name')
        return parser

    def take_action(self, parsed_args):
        return self.app.client_manager.delete(testcase_url(parsed_args))


class TestcasePut(command.ShowOne):

    def get_parser(self, prog_name):
        parser = super(TestcasePut, self).get_parser(prog_name)
        parser.add_argument('--project-name',
                            type=str,
                            required=True,
                            help='Update testcase by project name')
        parser.add_argument('name',
                            type=str,
                            help='Update testcase by name')
        parser.add_argument('testcase',
                            type=json.loads,
                            help='Testcase Update request format:\n'
                                 '\'{}\''.format(json.dumps(
                                     testcase.TestCaseCreateRequest().__dict__
                                     )))
        return parser

    def take_action(self, parsed_args):
        return self.format_output(
            self.app.client_manager.put(
                testcase_url(parsed_args), parsed_args.testcase))