forked from GoogleCloudPlatform/python-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_utils_test.py
More file actions
executable file
·98 lines (81 loc) · 3.09 KB
/
validation_utils_test.py
File metadata and controls
executable file
·98 lines (81 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit test for validation_utils.py"""
import argparse
import re
import pytest
import validation_utils
@pytest.mark.parametrize('container, field_name, field_type, expected', [
# Normal case, field present and correct type
({'present': 1}, 'present', int, 1),
({'present': '1'}, 'present', str, '1'),
({'present': [1]}, 'present', list, [1]),
({'present': {1: 2}}, 'present', dict, {1: 2}),
# Missing field replaced by default
({}, 'missing', str, ''),
# Valid conversions
({'str_to_int': '1'}, 'str_to_int', int, 1),
({'int_to_str': 1}, 'int_to_str', str, '1'),
# None
({'None_to_int': None}, 'None_to_int', int, 0),
({'None_to_str': None}, 'None_to_str', str, ''),
])
def test_get_field_value_valid(container, field_name, field_type, expected):
assert validation_utils.get_field_value(
container, field_name, field_type) == expected
@pytest.mark.parametrize('container, field_name, field_type', [
# Type conversion failures
({'bad_list_to_dict': [1]}, 'bad_list_to_dict', dict),
({'bad_list_to_str': [1]}, 'bad_list_to_str', str),
({'bad_dict_to_list': {1: 2}}, 'bad_dict_to_list', list),
({'bad_str_to_int': 'not_an_int'}, 'bad_str_to_int', int),
({'bad_str_to_list': 'abc'}, 'bad_str_to_list', list),
])
def test_get_field_value_invalid(container, field_name, field_type):
with pytest.raises(ValueError):
validation_utils.get_field_value(container, field_name, field_type)
def test_validate_arg_regex():
assert validation_utils.validate_arg_regex(
'abc', re.compile('a[b]c')) == 'abc'
with pytest.raises(argparse.ArgumentTypeError):
validation_utils.validate_arg_regex('abc', re.compile('a[d]c'))
@pytest.mark.parametrize('arg, expected', [
# Normal case, field present and correct type
('', {}),
('_A=1', {'_A': '1'}),
('_A=1,_B=2', {'_A': '1', '_B': '2'}),
# Repeated key is ok
('_A=1,_A=2', {'_A': '2'}),
# Extra = is ok
('_A=x=y=z,_B=2', {'_A': 'x=y=z', '_B': '2'}),
# No value is ok
('_A=', {'_A': ''}),
])
def test_validate_arg_dicts_valid(arg, expected):
assert validation_utils.validate_arg_dict(arg) == expected
@pytest.mark.parametrize('arg', [
# No key
',_A',
'_A,',
# Invalid variable name
'_Aa=1',
'_aA=1',
'0A=1',
])
def test_validate_arg_dicts_invalid(arg):
with pytest.raises(argparse.ArgumentTypeError):
validation_utils.validate_arg_dict(arg)
if __name__ == '__main__':
pytest.main([__file__])