ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers config_test.py Source File

config_test.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2016 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009 http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 
00018 import os.path
00019 import unittest
00020 from mock import patch
00021 from tools.config import Config
00022 
00023 """
00024 Tests for config.py
00025 """
00026 
00027 class ConfigTests (unittest.TestCase):
00028     """
00029     Test cases for Config class
00030     """
00031 
00032     def setUp (self):
00033         """
00034         Called before each test case
00035 
00036         :return:
00037         """
00038         self.target  = "K64F"
00039 
00040     def tearDown (self):
00041         """
00042         Called after each test case
00043 
00044         :return:
00045         """
00046         pass
00047 
00048     @patch.object(Config, '_process_config_and_overrides')
00049     @patch('tools.config.json_file_to_dict')
00050     def test_init_app_config (self, mock_json_file_to_dict, _):
00051         """
00052         Test that the initialisation correctly uses app_config
00053 
00054         :param mock_json_file_to_dict: mock of function json_file_to_dict
00055         :param _: mock of function _process_config_and_overrides (not tested)
00056         :return:
00057         """
00058         app_config = "app_config"
00059         mock_return = {'config': 'test'}
00060         mock_json_file_to_dict.return_value = mock_return
00061 
00062         config = Config(self.target , app_config=app_config)
00063 
00064         mock_json_file_to_dict.assert_called_with(app_config)
00065         self.assertEqual(config.app_config_data, mock_return,
00066                          "app_config_data should be set to the returned value")
00067 
00068     @patch.object(Config, '_process_config_and_overrides')
00069     @patch('tools.config.json_file_to_dict')
00070     def test_init_no_app_config (self, mock_json_file_to_dict, _):
00071         """
00072         Test that the initialisation works without app config
00073 
00074         :param mock_json_file_to_dict: mock of function json_file_to_dict
00075         :param _: patch of function _process_config_and_overrides (not tested)
00076         :return:
00077         """
00078         config = Config(self.target )
00079 
00080         mock_json_file_to_dict.assert_not_called()
00081         self.assertEqual(config.app_config_data, {},
00082                          "app_config_data should be set an empty dictionary")
00083 
00084     @patch.object(Config, '_process_config_and_overrides')
00085     @patch('os.path.isfile')
00086     @patch('tools.config.json_file_to_dict')
00087     def test_init_no_app_config_with_dir (self, mock_json_file_to_dict, mock_isfile, _):
00088         """
00089         Test that the initialisation works without app config and with a
00090         specified top level directory
00091 
00092         :param mock_json_file_to_dict: mock of function json_file_to_dict
00093         :param _: patch of function _process_config_and_overrides (not tested)
00094         :return:
00095         """
00096         directory = '.'
00097         path = os.path.join('.', 'mbed_app.json')
00098         mock_return = {'config': 'test'}
00099         mock_json_file_to_dict.return_value = mock_return
00100         mock_isfile.return_value = True
00101 
00102         config = Config(self.target , [directory])
00103 
00104         mock_isfile.assert_called_with(path)
00105         mock_json_file_to_dict.assert_called_once_with(path)
00106         self.assertEqual(config.app_config_data, mock_return,
00107                          "app_config_data should be set to the returned value")
00108 
00109     @patch.object(Config, '_process_config_and_overrides')
00110     @patch('tools.config.json_file_to_dict')
00111     def test_init_override_app_config (self, mock_json_file_to_dict, _):
00112         """
00113         Test that the initialisation uses app_config instead of top_level_dir
00114         when both are specified
00115 
00116         :param mock_json_file_to_dict: mock of function json_file_to_dict
00117         :param _: patch of function _process_config_and_overrides (not tested)
00118         :return:
00119         """
00120         app_config = "app_config"
00121         directory = '.'
00122         mock_return = {'config': 'test'}
00123         mock_json_file_to_dict.return_value = mock_return
00124 
00125         config = Config(self.target , [directory], app_config=app_config)
00126 
00127         mock_json_file_to_dict.assert_called_once_with(app_config)
00128         self.assertEqual(config.app_config_data, mock_return,
00129                          "app_config_data should be set to the returned value")
00130 
00131 if __name__ == '__main__':
00132     unittest.main()