11import logging
22import os
33import unittest
4+ from unittest import mock
45
5- from pyms .config .conf import Config
6+ from pyms .config .conf import get_conf
67from pyms .config .confile import ConfFile
78from pyms .constants import CONFIGMAP_FILE_ENVIRONMENT , LOGGER_NAME
89from pyms .exceptions import AttrDoesNotExistException , ConfigDoesNotFoundException , ServiceDoesNotExistException
@@ -67,6 +68,14 @@ def test_equal_instances_ok2(self):
6768 config2 = {"test_1" : {"test_1_1" : "a" , "test_1_2" : "b" }}
6869 self .assertEqual (config1 , config2 )
6970
71+ def test_equal_instances_ko (self ):
72+ config = ConfFile (config = {"test-1" : {"test-1-1" : "a" }})
73+ no_valid_type = ConfigDoesNotFoundException
74+
75+ result = config == no_valid_type
76+
77+ self .assertEqual (result , False )
78+
7079 def test_dictionary_attribute_not_exists (self ):
7180 config = ConfFile (config = {"test-1" : "a" })
7281 with self .assertRaises (AttrDoesNotExistException ):
@@ -89,22 +98,6 @@ def test_example_test_json_file(self):
8998 self .assertEqual (config .my_ms .test_var , "general" )
9099
91100
92- class ConfServiceTests (unittest .TestCase ):
93-
94- def test_config_with_service (self ):
95- class MyService (Config ):
96- service = "service"
97-
98- config = MyService ()
99- configuration = config .config (config = {"service" : {"service1" : "a" , "service2" : "b" }})
100- self .assertEqual (configuration .service1 , "a" )
101-
102- def test_config_with_service_not_exist (self ):
103- config = Config ()
104- with self .assertRaises (ServiceDoesNotExistException ):
105- configuration = config .config (config = {"service" : {"service1" : "a" , "service2" : "b" }})
106-
107-
108101class ConfNotExistTests (unittest .TestCase ):
109102 def test_empty_conf (self ):
110103 config = ConfFile (empty_init = True )
@@ -119,5 +112,39 @@ def test_empty_conf_three_levels(self):
119112 self .assertEqual (config .my_ms .level_two .level_three , {})
120113
121114
122- if __name__ == '__main__' :
123- unittest .main ()
115+
116+ class GetConfig (unittest .TestCase ):
117+ BASE_DIR = os .path .dirname (os .path .abspath (__file__ ))
118+
119+ def setUp (self ):
120+ os .environ [CONFIGMAP_FILE_ENVIRONMENT ] = os .path .join (self .BASE_DIR , "config-tests.yml" )
121+
122+ def tearDown (self ):
123+ del os .environ [CONFIGMAP_FILE_ENVIRONMENT ]
124+
125+ def test_default (self ):
126+ config = get_conf (service = "my-ms" )
127+
128+ assert config .APP_NAME == "Python Microservice"
129+ assert config .subservice1 .test == "input"
130+
131+ @mock .patch ('pyms.config.conf.ConfFile' )
132+ def test_memoized (self , mock_confile ):
133+ mock_confile .pyms = {}
134+ get_conf (service = "pyms" )
135+ get_conf (service = "pyms" )
136+
137+ mock_confile .assert_called_once ()
138+
139+ @mock .patch ('pyms.config.conf.ConfFile' )
140+ def test_without_memoize (self , mock_confile ):
141+ mock_confile .pyms = {}
142+ get_conf (service = "pyms" , memoize = False )
143+ get_conf (service = "pyms" , memoize = False )
144+
145+ assert mock_confile .call_count == 2
146+
147+ @mock .patch ('pyms.config.conf.ConfFile' )
148+ def test_without_params (self , mock_confile ):
149+ with self .assertRaises (ServiceDoesNotExistException ):
150+ get_conf ()
0 commit comments