-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
runtime_version_test.py
124 lines (108 loc) · 3.96 KB
/
runtime_version_test.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Protocol Buffers - Google's data interchange format
# Copyright 2008 Google Inc. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
"""Tests for google.protobuf.runtime_version."""
__author__ = 'shaod@google.com (Dennis Shao)'
import unittest
from google.protobuf import runtime_version
class RuntimeVersionTest(unittest.TestCase):
def test_invalid_version(self):
with self.assertRaisesRegex(
runtime_version.VersionError, 'Invalid gencode version: -1.-2.-3'
):
runtime_version.ValidateProtobufRuntimeVersion(
runtime_version.DOMAIN, -1, -2, -3, '', 'dummy.proto'
)
def test_cross_domain_disallowed(self):
gencode_domain = runtime_version.Domain.GOOGLE_INTERNAL
runtime_domain = runtime_version.Domain.PUBLIC
with self.assertRaisesRegex(
runtime_version.VersionError,
'Detected mismatched Protobuf Gencode/Runtime domains when loading'
f' foo.proto: gencode {gencode_domain.name} runtime'
f' {runtime_domain.name}',
):
runtime_version.ValidateProtobufRuntimeVersion(
gencode_domain, 1, 2, 3, '', 'foo.proto'
)
def test_mismatched_major_disallowed(self):
gencode_version_string = f'{1}.{runtime_version.MINOR}.{runtime_version.PATCH}{runtime_version.SUFFIX}'
runtime_version_string = f'{runtime_version.MAJOR}.{runtime_version.MINOR}.{runtime_version.PATCH}{runtime_version.SUFFIX}'
with self.assertRaisesRegex(
runtime_version.VersionError,
'Detected mismatched Protobuf Gencode/Runtime major versions when'
f' loading foo.proto: gencode {gencode_version_string} runtime'
f' {runtime_version_string}',
):
runtime_version.ValidateProtobufRuntimeVersion(
runtime_version.DOMAIN,
1,
runtime_version.MINOR,
runtime_version.PATCH,
runtime_version.SUFFIX,
'foo.proto',
)
def test_same_version_allowed(self):
runtime_version.ValidateProtobufRuntimeVersion(
runtime_version.DOMAIN,
runtime_version.MAJOR,
runtime_version.MINOR,
runtime_version.PATCH,
runtime_version.SUFFIX,
'dummy.proto',
)
def test_newer_runtime_version_allowed(self):
runtime_version.ValidateProtobufRuntimeVersion(
runtime_version.DOMAIN,
runtime_version.MAJOR,
runtime_version.MINOR - 1,
runtime_version.PATCH,
runtime_version.SUFFIX,
'dummy.proto',
)
def test_older_runtime_version_disallowed(self):
with self.assertRaisesRegex(
runtime_version.VersionError,
'Detected incompatible Protobuf Gencode/Runtime versions when'
' loading foo.proto',
):
runtime_version.ValidateProtobufRuntimeVersion(
runtime_version.DOMAIN,
runtime_version.MAJOR,
runtime_version.MINOR + 1,
runtime_version.PATCH,
runtime_version.SUFFIX,
'foo.proto',
)
with self.assertRaisesRegex(
runtime_version.VersionError,
'Detected incompatible Protobuf Gencode/Runtime versions when'
' loading foo.proto',
):
runtime_version.ValidateProtobufRuntimeVersion(
runtime_version.DOMAIN,
runtime_version.MAJOR,
runtime_version.MINOR,
runtime_version.PATCH + 1,
runtime_version.SUFFIX,
'foo.proto',
)
def test_different_suffix_disallowed(self):
with self.assertRaisesRegex(
runtime_version.VersionError,
'Detected mismatched Protobuf Gencode/Runtime version suffixes when'
' loading foo.proto',
):
runtime_version.ValidateProtobufRuntimeVersion(
runtime_version.DOMAIN,
runtime_version.MAJOR,
runtime_version.MINOR,
runtime_version.PATCH,
'-noflag',
'foo.proto',
)
if __name__ == '__main__':
unittest.main()