Browse Source

Merge "Add unittest for os_service_types.data module."

Zuul 6 months ago
parent
commit
8b81c669bb
3 changed files with 80 additions and 0 deletions
  1. 18
    0
      os_service_types/tests/base.py
  2. 61
    0
      os_service_types/tests/test_data.py
  3. 1
    0
      test-requirements.txt

+ 18
- 0
os_service_types/tests/base.py View File

@@ -17,6 +17,8 @@
17 17
 
18 18
 import copy
19 19
 import datetime
20
+import os
21
+import tempfile
20 22
 
21 23
 from oslotest import base
22 24
 
@@ -231,3 +233,19 @@ class ServiceDataMixin(object):
231 233
             self.assertEqual(
232 234
                 data,
233 235
                 self.service_types.get_service_data(self.all_services[index]))
236
+
237
+
238
+class TemporaryFileMixin(base.BaseTestCase):
239
+
240
+    def create_temp_file(self, mode='w', suffix='', prefix='tmp', dir=None,
241
+                         text=False, delete=True):
242
+        fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir,
243
+                                    text=text)
244
+        fd = os.fdopen(fd, mode)
245
+        if delete:
246
+            self.addCleanup(self._delete_temp, fd, name)
247
+        return fd, name
248
+
249
+    def _delete_temp(self, fd, name):
250
+        fd.close()
251
+        os.unlink(name)

+ 61
- 0
os_service_types/tests/test_data.py View File

@@ -0,0 +1,61 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+# not use this file except in compliance with the License. You may obtain
5
+# 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, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+"""
16
+test_data
17
+------------
18
+
19
+Tests for `os_service_types.data` module.
20
+
21
+"""
22
+
23
+import json
24
+
25
+import six
26
+
27
+from os_service_types import data
28
+from os_service_types.tests import base
29
+
30
+
31
+if six.PY2:
32
+    # Python 2 has not FileNotFoundError exception
33
+    FileNotFoundError = IOError
34
+
35
+
36
+class TestData(base.TestCase, base.TemporaryFileMixin):
37
+
38
+    def setUp(self):
39
+        super(TestData, self).setUp()
40
+
41
+    def test_load(self):
42
+        json_data = {'some_key': 'some_value'}
43
+        filename = self.create_json(json_data)
44
+        actual_data = data.read_data(filename)
45
+        self.assertEqual(json_data, actual_data)
46
+
47
+    def test_load_service_types(self):
48
+        json_data = data.read_data('service-types.json')
49
+        for key in ["all_types_by_service_type", "forward",
50
+                    "primary_service_by_project", "reverse"]:
51
+            self.assertIn(key, json_data)
52
+
53
+    def test_load_non_existing(self):
54
+        self.assertRaises(FileNotFoundError, data.read_data,
55
+                          '/non-existing-file')
56
+
57
+    def create_json(self, json_data):
58
+        fd, name = self.create_temp_file(suffix='.json')
59
+        with fd:
60
+            json.dump(json_data, fd)
61
+        return name

+ 1
- 0
test-requirements.txt View File

@@ -15,3 +15,4 @@ openstackdocstheme>=1.18.1 # Apache-2.0
15 15
 keystoneauth1>=3.4.0 # Apache-2.0
16 16
 # releasenotes
17 17
 reno>=2.5.0 # Apache-2.0
18
+six>=1.10.0 # MIT

Loading…
Cancel
Save