| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Tests for config_path.""" |
| 16 |
|
| 17 |
from absl.testing import absltest |
| 18 |
from absl.testing import parameterized |
| 19 |
from ml_collections.config_flags import config_path |
| 20 |
from ml_collections.config_flags.tests import fieldreference_config |
| 21 |
from ml_collections.config_flags.tests import mock_config |
| 22 |
|
| 23 |
|
| 24 |
class ConfigPathTest(parameterized.TestCase): |
| 25 |
|
| 26 |
def test_list_extra_index(self): |
| 27 |
"""Tries to index a non-indexable list element.""" |
| 28 |
|
| 29 |
test_config = mock_config.get_config() |
| 30 |
with self.assertRaises(IndexError): |
| 31 |
config_path.get_value('dict.list[0][0]', test_config) |
| 32 |
|
| 33 |
def test_list_out_of_range_get(self): |
| 34 |
"""Tries to access out-of-range value in list.""" |
| 35 |
|
| 36 |
test_config = mock_config.get_config() |
| 37 |
with self.assertRaises(IndexError): |
| 38 |
config_path.get_value('dict.list[2][1]', test_config) |
| 39 |
|
| 40 |
def test_list_out_of_range_set(self): |
| 41 |
"""Tries to override out-of-range value in list.""" |
| 42 |
|
| 43 |
test_config = mock_config.get_config() |
| 44 |
with self.assertRaises(IndexError): |
| 45 |
config_path.set_value('dict.list[2][1]', test_config, -1) |
| 46 |
|
| 47 |
def test_reading_non_existing_key(self): |
| 48 |
"""Tests reading non existing key from config.""" |
| 49 |
|
| 50 |
test_config = mock_config.get_config() |
| 51 |
with self.assertRaises(KeyError): |
| 52 |
config_path.set_value('dict.not_existing_key', test_config, 1) |
| 53 |
|
| 54 |
def test_reading_setting_existing_key_in_dict(self): |
| 55 |
"""Tests setting non existing key from dict inside config.""" |
| 56 |
|
| 57 |
test_config = mock_config.get_config() |
| 58 |
with self.assertRaises(KeyError): |
| 59 |
config_path.set_value('dict.not_existing_key.key', test_config, 1) |
| 60 |
|
| 61 |
def test_empty_key(self): |
| 62 |
"""Tests calling an empty key update.""" |
| 63 |
|
| 64 |
test_config = mock_config.get_config() |
| 65 |
with self.assertRaises(ValueError): |
| 66 |
config_path.set_value('', test_config, None) |
| 67 |
|
| 68 |
def test_field_reference_types(self): |
| 69 |
"""Tests whether types of FieldReference fields are valid.""" |
| 70 |
test_config = fieldreference_config.get_config() |
| 71 |
|
| 72 |
paths = ['ref_nodefault', 'ref'] |
| 73 |
paths_types = [int, int] |
| 74 |
|
| 75 |
config_types = [config_path.get_type(path, test_config) for path in paths] |
| 76 |
self.assertEqual(paths_types, config_types) |
| 77 |
|
| 78 |
@parameterized.parameters( |
| 79 |
('float', float), |
| 80 |
('integer', int), |
| 81 |
('string', str), |
| 82 |
('bool', bool), |
| 83 |
('dict', dict), |
| 84 |
('dict.float', float), |
| 85 |
('dict.list', list), |
| 86 |
('list', list), |
| 87 |
('list[0]', int), |
| 88 |
('object.float', float), |
| 89 |
('object.integer', int), |
| 90 |
('object.string', str), |
| 91 |
('object.bool', bool), |
| 92 |
('object.dict', dict), |
| 93 |
('object.dict.float', float), |
| 94 |
('object.dict.list', list), |
| 95 |
('object.list', list), |
| 96 |
('object.list[0]', int), |
| 97 |
('object.tuple', tuple), |
| 98 |
('object_reference.float', float), |
| 99 |
('object_reference.integer', int), |
| 100 |
('object_reference.string', str), |
| 101 |
('object_reference.bool', bool), |
| 102 |
('object_reference.dict', dict), |
| 103 |
('object_reference.dict.float', float), |
| 104 |
('object_copy.float', float), |
| 105 |
('object_copy.integer', int), |
| 106 |
('object_copy.string', str), |
| 107 |
('object_copy.bool', bool), |
| 108 |
('object_copy.dict', dict), |
| 109 |
('object_copy.dict.float', float), |
| 110 |
) |
| 111 |
def test_types(self, path, path_type): |
| 112 |
"""Tests whether various types of objects are valid.""" |
| 113 |
test_config = mock_config.get_config() |
| 114 |
self.assertEqual(path_type, config_path.get_type(path, test_config)) |
| 115 |
|
| 116 |
if __name__ == '__main__': |
| 117 |
absltest.main() |