blob: 1a50082cb28000a200b1bb948516f51b8e76133b [file] [log] [blame]
Scott Baker08d10402019-04-08 16:19:59 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16from __future__ import absolute_import
17import unittest
18import os
19from xosgenx.validator import XProtoValidator
20from xosgenx.generator import XOSProcessor, XOSProcessorArgs
21from mock import patch
22import yaml
23
24# Generate other formats from xproto
25
26
27class XProtoValidatorTest(unittest.TestCase):
28 def test_suggested_max_length(self):
29 args = XOSProcessorArgs()
30 args.files = ["/tmp/testvalidator.xproto"]
31
32 open("/tmp/testvalidator.xproto", "w").write("""
33 option app_label = "test";
34
35 message Port (XOSBase){
36 required string foo = 1 [max_length=254];
37 }
38 """)
39 args.target = "modeldefs.xtarget"
40
41 with patch.object(XProtoValidator, "print_errors", autospec=True) as print_errors:
42 print_errors.return_value = None
43
44 output = XOSProcessor.process(args)
45
46 self.assertEqual(print_errors.call_count, 1)
47 validator = print_errors.call_args[0][0]
48
49 self.assertEqual(len(validator.errors), 1)
50 self.assertEqual(validator.errors[0]["severity"], "WARNING")
51 self.assertEqual(validator.errors[0]["message"], "max_length of 254 is close to suggested max_length of 256")
52
53 def test_max_length_okay(self):
54 args = XOSProcessorArgs()
55 args.files = ["/tmp/testvalidator.xproto"]
56
57 open("/tmp/testvalidator.xproto", "w").write("""
58 option app_label = "test";
59
60 message Port (XOSBase){
61 required string foo = 1 [max_length=256];
62 }
63 """)
64 args.target = "modeldefs.xtarget"
65
66 with patch.object(XProtoValidator, "print_errors", autospec=True) as print_errors:
67 print_errors.return_value = None
68
69 output = XOSProcessor.process(args)
70
71 self.assertEqual(print_errors.call_count, 0)
72
73 def test_max_length_zero(self):
74 args = XOSProcessorArgs()
75 args.files = ["/tmp/testvalidator.xproto"]
76
77 open("/tmp/testvalidator.xproto", "w").write("""
78 option app_label = "test";
79
80 message Port (XOSBase){
81 required string foo = 1 [max_length=0];
82 }
83 """)
84 args.target = "modeldefs.xtarget"
85
86 with patch.object(XProtoValidator, "print_errors", autospec=True) as print_errors:
87 print_errors.return_value = None
88
89 output = XOSProcessor.process(args)
90
91 self.assertEqual(print_errors.call_count, 1)
92 validator = print_errors.call_args[0][0]
93
94 self.assertEqual(len(validator.errors), 1)
95 self.assertEqual(validator.errors[0]["severity"], "ERROR")
96 self.assertEqual(validator.errors[0]["message"], "max_length should not be zero")
97
98
99 def test_charfield_missing_max_length(self):
100 args = XOSProcessorArgs()
101 args.files = ["/tmp/testvalidator.xproto"]
102
103 open("/tmp/testvalidator.xproto", "w").write("""
104 option app_label = "test";
105
106 message Port (XOSBase){
107 required string foo = 1 [];
108 }
109 """)
110 args.target = "modeldefs.xtarget"
111
112 with patch.object(XProtoValidator, "print_errors", autospec=True) as print_errors:
113 print_errors.return_value = None
114
115 output = XOSProcessor.process(args)
116
117 self.assertEqual(print_errors.call_count, 1)
118 validator = print_errors.call_args[0][0]
119
120 self.assertEqual(len(validator.errors), 1)
121 self.assertEqual(validator.errors[0]["severity"], "ERROR")
122 self.assertEqual(validator.errors[0]["message"], "String field should have a max_length or text=True")
123
124if __name__ == "__main__":
125 unittest.main()