Implement a fake SDL database backend
[ric-plt/sdlpy.git] / ricsdl-package / tests / test_configuration.py
1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2018-2019 Nokia.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 #
17 # This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 # platform project (RICP).
19 #
20
21
22 import pytest
23 from ricsdl.configuration import _Configuration
24 from ricsdl.configuration import DbBackendType
25
26
27 @pytest.fixture()
28 def config_fixture(request, monkeypatch):
29     monkeypatch.setenv('DBAAS_SERVICE_HOST', '10.20.30.40')
30     monkeypatch.setenv('DBAAS_SERVICE_PORT', '10000')
31     monkeypatch.setenv('DBAAS_SERVICE_SENTINEL_PORT', '11000')
32     monkeypatch.setenv('DBAAS_MASTER_NAME', 'my-master')
33     request.cls.config = _Configuration(fake_db_backend=None)
34
35
36 @pytest.fixture
37 def fake_db_config_fixture(request, monkeypatch):
38     monkeypatch.delenv('DBAAS_SERVICE_HOST', raising=False)
39     monkeypatch.delenv('DBAAS_SERVICE_PORT', raising=False)
40     monkeypatch.delenv('DBAAS_SERVICE_SENTINEL_PORT', raising=False)
41     monkeypatch.delenv('DBAAS_MASTER_NAME', raising=False)
42     request.cls.config = _Configuration(fake_db_backend='dict')
43
44
45 class TestConfiguration:
46     def test_get_params_function_returns_read_configuration(self, config_fixture):
47         expected_config = _Configuration.Params(db_host='10.20.30.40', db_port='10000',
48                                                 db_sentinel_port='11000',
49                                                 db_sentinel_master_name='my-master',
50                                                 db_type=DbBackendType.REDIS)
51         assert expected_config == self.config.get_params()
52
53     def test_get_params_function_can_return_fake_db_configuration(self, fake_db_config_fixture):
54         expected_config = _Configuration.Params(db_host=None, db_port=None,
55                                                 db_sentinel_port=None,
56                                                 db_sentinel_master_name=None,
57                                                 db_type=DbBackendType.FAKE_DICT)
58         assert expected_config == self.config.get_params()
59
60     def test_get_params_function_can_raise_exception_if_wrong_fake_db_type(self):
61         with pytest.raises(ValueError, match=r"Configuration error"):
62             _Configuration(fake_db_backend='bad value')
63
64
65     def test_configuration_object_string_representation(self, config_fixture):
66         expected_config_info = {'DB host': '10.20.30.40',
67                                 'DB port': '10000',
68                                 'DB master sentinel': 'my-master',
69                                 'DB sentinel port': '11000',
70                                 'DB type': 'REDIS'}
71         assert str(self.config) == str(expected_config_info)
72
73     def test_configuration_object_string_representation_if_fake_db(self, fake_db_config_fixture):
74         expected_config_info = {'DB host': None,
75                                 'DB port': None,
76                                 'DB master sentinel': None,
77                                 'DB sentinel port': None,
78                                 'DB type': 'FAKE_DICT'}
79         assert str(self.config) == str(expected_config_info)