Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 1 | # 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 | |
| 16 | from __future__ import absolute_import |
| 17 | import unittest |
| 18 | import os |
| 19 | from xosgenx.validator import XProtoValidator |
| 20 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 21 | from mock import patch |
| 22 | import yaml |
| 23 | |
| 24 | # Generate other formats from xproto |
| 25 | |
| 26 | |
| 27 | class 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 | |
Matteo Scandolo | 4dedbe6 | 2019-06-05 12:08:17 -0700 | [diff] [blame] | 124 | def test_bookkeeping_state_allowed(self): |
| 125 | args = XOSProcessorArgs() |
| 126 | args.files = ["/tmp/testvalidator.xproto"] |
| 127 | |
| 128 | open("/tmp/testvalidator.xproto", "w").write(""" |
| 129 | |
| 130 | message BackupOperation (XOSBase){ |
| 131 | option custom_python=True; |
| 132 | optional string uuid = 7 [ |
| 133 | help_text = "unique identifer of this request", |
| 134 | bookkeeping_state = True, |
| 135 | max_length = 80]; |
| 136 | } |
| 137 | """) |
| 138 | args.target = "modeldefs.xtarget" |
| 139 | |
| 140 | with patch.object(XProtoValidator, "print_errors", autospec=True) as print_errors: |
| 141 | print_errors.return_value = None |
| 142 | |
| 143 | output = XOSProcessor.process(args) |
| 144 | |
| 145 | print_errors.assert_not_called() |
| 146 | |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 147 | if __name__ == "__main__": |
| 148 | unittest.main() |