Debian Codegraph @

mistralclient/tests/unit/v2/test_cli_code_sources.py

raw symbols SCIP
1 # Copyright 2020 Nokia Software.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from unittest import mock
16
17
18 from mistralclient.api.v2 import code_sources
19 from mistralclient.commands.v2 import code_sources as code_src_cmd
20 from mistralclient.tests.unit import base
21
22
23 CODE_SRC_CONTENT = """
24 from mistral_lib import actions
25
26 class HelloAction(actions.Action):
27 def __init__(self, f_name, l_name):
28 super(HelloAction, self).__init__()
29
30 self._f_name = f_name
31 self._l_name = l_name
32
33 def run(self, context):
34 return 'Hello %s %s!' % (self._f_name, self._l_name)
35 """
36
37 CODE_SRC_DICT = {
38 'id': '1-2-3-4',
39 'name': 'hello_module',
40 'namespace': '',
41 'project_id': '12345',
42 'scope': 'private',
43 'content': CODE_SRC_CONTENT,
44 'created_at': '1',
45 'updated_at': '1'
46 }
47
48 CODE_SRC = code_sources.CodeSource(mock, CODE_SRC_DICT)
49
50
51 class TestCLICodeSources(base.BaseCommandTest):
52 @mock.patch('argparse.open', create=True)
53 def test_create(self, mock_open):
54 self.client.code_sources.create.return_value = CODE_SRC
55
56 result = self.call(
57 code_src_cmd.Create,
58 app_args=['hello_module', 'hello.py']
59 )
60
61 self.assertEqual(
62 ('1-2-3-4', 'hello_module', '', '12345', 'private', '1', '1'),
63 result[1]
64 )
65
66 @mock.patch('argparse.open', create=True)
67 def test_create_public(self, mock_open):
68 code_src_dict = CODE_SRC_DICT.copy()
69 code_src_dict['scope'] = 'public'
70
71 code_src = code_sources.CodeSource(mock, code_src_dict)
72
73 self.client.code_sources.create.return_value = code_src
74
75 result = self.call(
76 code_src_cmd.Create,
77 app_args=['hello_module', '1.txt', '--public']
78 )
79
80 self.assertEqual(
81 ('1-2-3-4', 'hello_module', '', '12345', 'public', '1', '1'),
82 result[1]
83 )
84
85 self.assertEqual(
86 'public',
87 self.client.code_sources.create.call_args[1]['scope']
88 )
89
90 @mock.patch('argparse.open', create=True)
91 def test_update(self, mock_open):
92 self.client.code_sources.update.return_value = CODE_SRC
93
94 result = self.call(
95 code_src_cmd.Update,
96 app_args=['hello_module', 'hello.py']
97 )
98
99 self.assertEqual(
100 ('1-2-3-4', 'hello_module', '', '12345', 'private', '1', '1'),
101 result[1]
102 )
103
104 @mock.patch('argparse.open', create=True)
105 def test_update_public(self, mock_open):
106 code_src_dict = CODE_SRC_DICT.copy()
107 code_src_dict['scope'] = 'public'
108
109 code_src = code_sources.CodeSource(mock, code_src_dict)
110
111 self.client.code_sources.update.return_value = code_src
112
113 result = self.call(
114 code_src_cmd.Update,
115 app_args=['hello_module', 'hello.py', '--public']
116 )
117
118 self.assertEqual(
119 ('1-2-3-4', 'hello_module', '', '12345', 'public', '1', '1'),
120 result[1]
121 )
122
123 self.assertEqual(
124 'public',
125 self.client.code_sources.update.call_args[1]['scope']
126 )
127
128 @mock.patch('argparse.open', create=True)
129 def test_update_with_id(self, mock_open):
130 self.client.code_sources.update.return_value = CODE_SRC
131
132 result = self.call(
133 code_src_cmd.Update,
134 app_args=['1-2-3-4', 'hello.py']
135 )
136
137 self.assertEqual(
138 ('1-2-3-4', 'hello_module', '', '12345', 'private', '1', '1'),
139 result[1]
140 )
141
142 def test_list(self):
143 self.client.code_sources.list.return_value = [CODE_SRC]
144
145 result = self.call(code_src_cmd.List)
146
147 self.assertEqual(
148 [('1-2-3-4', 'hello_module', '', '12345', 'private', '1', '1')],
149 result[1]
150 )
151
152 def test_get(self):
153 self.client.code_sources.get.return_value = CODE_SRC
154
155 result = self.call(code_src_cmd.Get, app_args=['hello_module'])
156
157 self.assertEqual(
158 ('1-2-3-4', 'hello_module', '', '12345', 'private', '1', '1'),
159 result[1]
160 )
161
162 def test_delete(self):
163 self.call(code_src_cmd.Delete, app_args=['hello_module'])
164
165 self.client.code_sources.delete.assert_called_once_with(
166 'hello_module',
167 None
168 )
169
170 def test_get_content(self):
171 self.client.code_sources.get.return_value = CODE_SRC
172
173 self.call(code_src_cmd.GetContent, app_args=['hello_module'])
174
175 self.app.stdout.write.assert_called_with(CODE_SRC_CONTENT)