| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import copy |
| 7 import os |
| 8 import sys |
| 9 import unittest |
| 10 |
| 11 root_dir = os.path.abspath(os.path.join( |
| 12 os.path.dirname(__file__), os.path.pardir, |
| 13 os.path.pardir, os.path.pardir, os.path.pardir)) |
| 14 sys.path.insert(0, os.path.join(root_dir, 'third_party', 'mock-1.0.1')) |
| 15 sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.path.pardir)) |
| 16 |
| 17 import mock |
| 18 |
| 19 from auto_bisect import config_validation |
| 20 |
| 21 |
| 22 class ConfigValidationTest(unittest.TestCase): # pragma: no cover |
| 23 |
| 24 def test_validate_config_empty_config(self): |
| 25 config_validation.validate_config(config={}, schema={}) |
| 26 |
| 27 def test_validate_config_with_missing_required_fails(self): |
| 28 with self.assertRaises(config_validation.ValidationFail): |
| 29 config_validation.validate_config( |
| 30 config={}, |
| 31 schema={'foo': {'type': 'integer', 'required': True}}) |
| 32 |
| 33 def test_validate_config_with_one_field_passes(self): |
| 34 config_validation.validate_config( |
| 35 config={'foo': 123}, |
| 36 schema={'foo': {'type': 'integer'}}) |
| 37 |
| 38 def test_validate_optional_field_passes(self): |
| 39 config_validation.validate_config( |
| 40 config={}, |
| 41 schema={'foo': {'type': 'integer'}}) |
| 42 |
| 43 def test_validate_not_in_schema_passes(self): |
| 44 config_validation.validate_config(config={'foo': 'asdf'}, schema={}) |
| 45 |
| 46 def test_validate_config_larger_passing_example(self): |
| 47 schema = { |
| 48 'str1': {'type': 'string'}, |
| 49 'str2': {'type': 'string'}, |
| 50 'int1': {'type': 'integer'}, |
| 51 'int2': {'type': 'integer'}, |
| 52 'rev1': {'type': 'revision'}, |
| 53 'rev2': {'type': 'revision'}, |
| 54 'bool1': {'type': 'boolean'}, |
| 55 'bool2': {'type': 'boolean'}, |
| 56 } |
| 57 config = { |
| 58 'str1': u'unicode-string', |
| 59 'str2': '', |
| 60 'int1': '12345', |
| 61 'int2': 12345, |
| 62 'rev1': '0123456789abcdeabcde0123456789abcdeabcde', |
| 63 'rev2': '12345', |
| 64 'bool1': True, |
| 65 'bool2': False, |
| 66 } |
| 67 config_validation.validate_config(config, schema) |
| 68 |
| 69 def test_validate_string_failure(self): |
| 70 with self.assertRaises(config_validation.ValidationFail): |
| 71 config_validation.validate_config( |
| 72 config={'x': 12345}, |
| 73 schema={'x': {'type': 'string'}}) |
| 74 |
| 75 def test_validate_integer_failure(self): |
| 76 with self.assertRaises(config_validation.ValidationFail): |
| 77 config_validation.validate_config( |
| 78 config={'x': '123a'}, |
| 79 schema={'x': {'type': 'integer'}}) |
| 80 |
| 81 def test_validate_revision_failure(self): |
| 82 with self.assertRaises(config_validation.ValidationFail): |
| 83 config_validation.validate_config( |
| 84 config={'x': 'abcdef'}, |
| 85 schema={'x': {'type': 'revision'}}) |
| 86 |
| 87 def test_validate_boolean_failure(self): |
| 88 with self.assertRaises(config_validation.ValidationFail): |
| 89 config_validation.validate_config( |
| 90 config={'x': 'true'}, |
| 91 schema={'x': {'type': 'boolean'}}) |
| 92 |
| 93 def test_validate_choice_failure(self): |
| 94 with self.assertRaises(config_validation.ValidationFail): |
| 95 config_validation.validate_config( |
| 96 config={'x': 3}, |
| 97 schema={'x': {'type': 'int', 'choices': [1, 2]}}) |
| 98 |
| 99 if __name__ == '__main__': |
| 100 unittest.main() # pragma: no cover |
| OLD | NEW |