• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2018 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16import unittest
17from unittest import TestCase
18
19from mock import patch
20
21from acts.config.file_loader import ConfigFileParser
22from acts.config.file_loader import JsonFileParser
23from acts.config.file_loader import ConfigFileLoader
24
25
26class ConfigFileParserTest(TestCase):
27    """Tests the ConfigFileParser class."""
28
29    def test_parse_raises_not_implemented_error(self):
30        """Tests that calling parse() raises NotImplementedError"""
31        with self.assertRaises(NotImplementedError):
32            ConfigFileParser().parse('')
33
34
35class JsonFileParserTest(TestCase):
36    """Tests the JsonFileParser class."""
37
38    @patch('acts.utils.load_config')
39    def test_parse_calls_util_load_config(self, mocked_load_config):
40        """Tests that parse() calls util.load_config()."""
41        file_path = '/file/path'
42        JsonFileParser().parse(file_path)
43
44        mocked_load_config.assert_called_with(file_path, log_errors=False)
45
46
47class ConfigFileParserPass(ConfigFileParser):
48    def parse(self, file_path):
49        return {'config': 'okay'}
50
51
52class ConfigFileParserFail(ConfigFileParser):
53    def parse(self, file_path):
54        raise ValueError('Not Okay')
55
56
57class ConfigFileLoaderTest(TestCase):
58    """Tests the ConfigFileLoader class."""
59
60    def test_file_parsers_are_all_subclasses_of_config_file_parser(self):
61        """Tests file_parsers is a list of subclasses of ConfigFileParser."""
62        for index, file_parser in enumerate(ConfigFileLoader._file_parsers):
63            self.assertTrue(
64                issubclass(type(file_parser), ConfigFileParser),
65                'Entry %s of file_parsers is of type %s, and is not a '
66                'ConfigFileParser' % (index, type(file_parser)))
67
68    def test_load_config_from_file_successfully_parses_file(self):
69        """Tests that load_config_from_file can successfully parse a file."""
70        ConfigFileLoader._file_parsers = [ConfigFileParserPass()]
71        try:
72            ConfigFileLoader().load_config_from_file('/file/path')
73        except Exception as e:
74            self.fail('Raised an error upon successful file parsing: %s' % e)
75
76    def test_load_config_from_file_raises_error_on_all_failed_parse(self):
77        """Tests that load_config_from_file raises an error if parsers fail."""
78        ConfigFileLoader._file_parsers = [ConfigFileParserFail()]
79        with self.assertRaises(ValueError):
80            ConfigFileLoader().load_config_from_file('/file/path')
81
82    def test_load_config_from_file_passes_after_the_first_success(self):
83        """Tests that a failure can happen, but a success will go through."""
84        ConfigFileLoader._file_parsers = [ConfigFileParserFail(),
85                                          ConfigFileParserPass()]
86        try:
87            ConfigFileLoader().load_config_from_file('/file/path')
88        except Exception as e:
89            self.fail('Raised an error upon successful file parsing: %s' % e)
90
91
92if __name__ == '__main__':
93    unittest.main()
94