gecko-dev/taskcluster/taskgraph/test/test_util_schema.py
Dustin J. Mitchell 53b7bc583c Bug 1334167: allow by-project for cron jobs' when property; r=Callek
This requires moving the schema utilities to their own util module.

MozReview-Commit-ID: KR5xSJ9ak5Y

--HG--
extra : rebase_source : 1c1f6bfb6a08deb8c0be4b2b58db02d85aafeb89
2017-02-01 00:30:52 +00:00

115 lines
3.3 KiB
Python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, print_function, unicode_literals
import unittest
from mozunit import main
from taskgraph.util.schema import (
validate_schema,
resolve_keyed_by,
)
from voluptuous import Schema
schema = Schema({
'x': int,
'y': basestring,
})
class TestValidateSchema(unittest.TestCase):
def test_valid(self):
validate_schema(schema, {'x': 10, 'y': 'foo'}, "pfx")
def test_invalid(self):
try:
validate_schema(schema, {'x': 'not-int'}, "pfx")
self.fail("no exception raised")
except Exception, e:
self.failUnless(str(e).startswith("pfx\n"))
class TestResolveKeyedBy(unittest.TestCase):
def test_no_by(self):
self.assertEqual(
resolve_keyed_by({'x': 10}, 'z', 'n'),
{'x': 10})
def test_no_by_dotted(self):
self.assertEqual(
resolve_keyed_by({'x': {'y': 10}}, 'x.z', 'n'),
{'x': {'y': 10}})
def test_no_by_not_dict(self):
self.assertEqual(
resolve_keyed_by({'x': 10}, 'x.y', 'n'),
{'x': 10})
def test_no_by_not_by(self):
self.assertEqual(
resolve_keyed_by({'x': {'a': 10}}, 'x', 'n'),
{'x': {'a': 10}})
def test_no_by_empty_dict(self):
self.assertEqual(
resolve_keyed_by({'x': {}}, 'x', 'n'),
{'x': {}})
def test_no_by_not_only_by(self):
self.assertEqual(
resolve_keyed_by({'x': {'by-y': True, 'a': 10}}, 'x', 'n'),
{'x': {'by-y': True, 'a': 10}})
def test_match_nested_exact(self):
self.assertEqual(
resolve_keyed_by(
{'f': 'shoes', 'x': {'y': {'by-f': {'shoes': 'feet', 'gloves': 'hands'}}}},
'x.y', 'n'),
{'f': 'shoes', 'x': {'y': 'feet'}})
def test_match_regexp(self):
self.assertEqual(
resolve_keyed_by(
{'f': 'shoes', 'x': {'by-f': {'s?[hH]oes?': 'feet', 'gloves': 'hands'}}},
'x', 'n'),
{'f': 'shoes', 'x': 'feet'})
def test_match_partial_regexp(self):
self.assertEqual(
resolve_keyed_by(
{'f': 'shoes', 'x': {'by-f': {'sh': 'feet', 'default': 'hands'}}},
'x', 'n'),
{'f': 'shoes', 'x': 'hands'})
def test_match_default(self):
self.assertEqual(
resolve_keyed_by(
{'f': 'shoes', 'x': {'by-f': {'hat': 'head', 'default': 'anywhere'}}},
'x', 'n'),
{'f': 'shoes', 'x': 'anywhere'})
def test_match_extra_value(self):
self.assertEqual(
resolve_keyed_by(
{'f': {'by-foo': {'x': 10, 'y': 20}}},
'f', 'n',
foo='y'),
{'f': 20})
def test_no_match(self):
self.assertRaises(
Exception, resolve_keyed_by,
{'f': 'shoes', 'x': {'by-f': {'hat': 'head'}}}, 'x', 'n')
def test_multiple_matches(self):
self.assertRaises(
Exception, resolve_keyed_by,
{'f': 'hats', 'x': {'by-f': {'hat.*': 'head', 'ha.*': 'hair'}}}, 'x', 'n')
if __name__ == '__main__':
main()