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