Debian Codegraph @

mistralclient/tests/unit/test_utils.py

raw symbols SCIP
1 # Copyright 2015 - StackStorm, Inc.
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 import os.path
16 import tempfile
17 import yaml
18
19 from mistralclient import utils
20 from oslo_serialization import jsonutils
21 from oslotest import base
22
23
24 ENV_DICT = {'k1': 'abc', 'k2': 123, 'k3': True}
25 ENV_STR = jsonutils.dumps(ENV_DICT)
26 ENV_YAML = yaml.safe_dump(ENV_DICT, default_flow_style=False)
27
28
29 class UtilityTest(base.BaseTestCase):
30
31 def test_load_empty(self):
32 self.assertDictEqual(dict(), utils.load_content(None))
33 self.assertDictEqual(dict(), utils.load_content(''))
34 self.assertDictEqual(dict(), utils.load_content('{}'))
35 self.assertListEqual(list(), utils.load_content('[]'))
36
37 def test_load_json_content(self):
38 self.assertDictEqual(ENV_DICT, utils.load_content(ENV_STR))
39
40 def test_load_json_file(self):
41 with tempfile.NamedTemporaryFile() as f:
42 f.write(ENV_STR.encode('utf-8'))
43 f.flush()
44 file_path = os.path.abspath(f.name)
45
46 self.assertDictEqual(ENV_DICT, utils.load_file(file_path))
47
48 def test_load_yaml_content(self):
49 self.assertDictEqual(ENV_DICT, utils.load_content(ENV_YAML))
50
51 def test_load_yaml_file(self):
52 with tempfile.NamedTemporaryFile() as f:
53 f.write(ENV_YAML.encode('utf-8'))
54 f.flush()
55 file_path = os.path.abspath(f.name)
56
57 self.assertDictEqual(ENV_DICT, utils.load_file(file_path))
58
59 def test_load_json(self):
60 with tempfile.NamedTemporaryFile() as f:
61 f.write(ENV_STR.encode('utf-8'))
62 f.flush()
63 self.assertDictEqual(ENV_DICT, utils.load_json(f.name))
64
65 self.assertDictEqual(ENV_DICT, utils.load_json(ENV_STR))