|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# ,---------, ____ _ __ |
| 4 | +# | ,-^-, | / __ )(_) /_______________ _____ ___ |
| 5 | +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ |
| 6 | +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ |
| 7 | +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ |
| 8 | +# |
| 9 | +# Copyright (C) 2024 Bitcraze AB |
| 10 | +# |
| 11 | +# This program is free software: you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU General Public License as published by |
| 13 | +# the Free Software Foundation, in version 3. |
| 14 | +# |
| 15 | +# This program is distributed in the hope that it will be useful, |
| 16 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +# GNU General Public License for more details. |
| 19 | +# |
| 20 | +# You should have received a copy of the GNU General Public License |
| 21 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | +import unittest |
| 23 | +from unittest.mock import ANY |
| 24 | +from unittest.mock import mock_open |
| 25 | +from unittest.mock import patch |
| 26 | + |
| 27 | +import yaml |
| 28 | + |
| 29 | +from cflib.localization import ParamFileManager |
| 30 | + |
| 31 | + |
| 32 | +class TestParamFileManager(unittest.TestCase): |
| 33 | + def setUp(self): |
| 34 | + self.data = { |
| 35 | + 'type': 'persistent_param_state', |
| 36 | + 'version': '1', |
| 37 | + } |
| 38 | + |
| 39 | + @patch('yaml.safe_load') |
| 40 | + def test_that_read_open_correct_file(self, mock_yaml_load): |
| 41 | + # Fixture |
| 42 | + mock_yaml_load.return_value = self.data |
| 43 | + file_name = 'some/name.yaml' |
| 44 | + |
| 45 | + # Test |
| 46 | + with patch('builtins.open', new_callable=mock_open()) as mock_file: |
| 47 | + ParamFileManager.read(file_name) |
| 48 | + |
| 49 | + # Assert |
| 50 | + mock_file.assert_called_with(file_name, 'r') |
| 51 | + |
| 52 | + @patch('yaml.safe_load') |
| 53 | + def test_that_missing_file_type_raises(self, mock_yaml_load): |
| 54 | + # Fixture |
| 55 | + self.data.pop('type') |
| 56 | + mock_yaml_load.return_value = self.data |
| 57 | + |
| 58 | + # Test |
| 59 | + # Assert |
| 60 | + with self.assertRaises(Exception): |
| 61 | + with patch('builtins.open', new_callable=mock_open()): |
| 62 | + ParamFileManager.read('some/name.yaml') |
| 63 | + |
| 64 | + @patch('yaml.safe_load') |
| 65 | + def test_that_wrong_file_type_raises(self, mock_yaml_load): |
| 66 | + # Fixture |
| 67 | + self.data['type'] = 'wrong_type' |
| 68 | + mock_yaml_load.return_value = self.data |
| 69 | + |
| 70 | + # Test |
| 71 | + # Assert |
| 72 | + with self.assertRaises(Exception): |
| 73 | + with patch('builtins.open', new_callable=mock_open()): |
| 74 | + ParamFileManager.read('some/name.yaml') |
| 75 | + |
| 76 | + @patch('yaml.safe_load') |
| 77 | + def test_that_missing_version_raises(self, mock_yaml_load): |
| 78 | + |
| 79 | + # Fixture |
| 80 | + self.data.pop('version') |
| 81 | + mock_yaml_load.return_value = self.data |
| 82 | + |
| 83 | + # Test |
| 84 | + # Assert |
| 85 | + with self.assertRaises(Exception): |
| 86 | + with patch('builtins.open', new_callable=mock_open()): |
| 87 | + ParamFileManager.read('some/name.yaml') |
| 88 | + |
| 89 | + @patch('yaml.safe_load') |
| 90 | + def test_that_wrong_version_raises(self, mock_yaml_load): |
| 91 | + # Fixture |
| 92 | + self.data['version'] = 'wrong_version' |
| 93 | + mock_yaml_load.return_value = self.data |
| 94 | + |
| 95 | + # Test |
| 96 | + # Assert |
| 97 | + with self.assertRaises(Exception): |
| 98 | + with patch('builtins.open', new_callable=mock_open()): |
| 99 | + ParamFileManager.read('some/name.yaml') |
| 100 | + |
| 101 | + @patch('yaml.safe_load') |
| 102 | + def test_that_no_data_returns_empty_default_data(self, mock_yaml_load): |
| 103 | + # Fixture |
| 104 | + mock_yaml_load.return_value = self.data |
| 105 | + |
| 106 | + # Test |
| 107 | + with patch('builtins.open', new_callable=mock_open()): |
| 108 | + actual_params = ParamFileManager.read('some/name.yaml') |
| 109 | + |
| 110 | + # Assert |
| 111 | + self.assertEqual(0, len(actual_params)) |
| 112 | + |
| 113 | + @patch('yaml.dump') |
| 114 | + def test_file_end_to_end_write_read(self, mock_yaml_dump): |
| 115 | + # Fixture |
| 116 | + fixture_file = 'test/localization/fixtures/parameters.yaml' |
| 117 | + |
| 118 | + file = open(fixture_file, 'r') |
| 119 | + expected = yaml.safe_load(file) |
| 120 | + file.close() |
| 121 | + |
| 122 | + # Test |
| 123 | + params = ParamFileManager.read(fixture_file) |
| 124 | + with patch('builtins.open', new_callable=mock_open()): |
| 125 | + ParamFileManager.write('some/name.yaml', params=params) |
| 126 | + |
| 127 | + # Assert |
| 128 | + mock_yaml_dump.assert_called_with(expected, ANY) |
| 129 | + |
| 130 | + @patch('yaml.dump') |
| 131 | + def test_file_write_to_correct_file(self, mock_yaml_dump): |
| 132 | + # Fixture |
| 133 | + file_name = 'some/name.yaml' |
| 134 | + |
| 135 | + # Test |
| 136 | + with patch('builtins.open', new_callable=mock_open()) as mock_file: |
| 137 | + ParamFileManager.write(file_name) |
| 138 | + |
| 139 | + # Assert |
| 140 | + mock_file.assert_called_with(file_name, 'w') |
0 commit comments