-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtest_has_field.py
100 lines (80 loc) · 3.77 KB
/
test_has_field.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
import unittest
Test = None
TestRef = None
TestSs1 = None
TestSs1Thing = None
class HasFieldTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
global Test, TestRef, TestSs1, TestSs1Thing
from test_message_proto import Test, TestSs1, TestSs1Thing
from test_ref_message_proto import TestRef
def test_has_field_for_repeated_field_raises_value_error(self):
message = Test()
self.assertRaises(ValueError, message.HasField, 'list_fieldx')
def test_has_field_for_unset_scalar_field_returns_false(self):
message = Test()
self.assertFalse(message.HasField('timestamp'))
def test_has_field_for_set_scalar_field_returns_true(self):
message = Test()
message.timestamp = 123
self.assertTrue(message.HasField('timestamp'))
def test_has_field_for_cleared_scalar_field_returns_false(self):
message = Test()
message.timestamp = 123
message.ClearField('timestamp')
self.assertFalse(message.HasField('timestamp'))
def test_has_field_for_unset_message_field_returns_false(self):
message = Test()
self.assertFalse(message.HasField('substruct'))
def test_has_field_for_set_message_field_returns_true(self):
message = Test()
message.substruct.field2 = "something"
self.assertTrue(message.HasField('substruct'))
def test_has_field_for_cleared_message_field_returns_false(self):
message = Test()
message.substruct.field2 = "something"
message.ClearField('substruct')
self.assertFalse(message.HasField('substruct'))
def test_has_field_for_scalar_field_of_cleared_message_returns_false(self):
message = Test()
message.timestamp = 123
message.Clear()
self.assertFalse(message.HasField('timestamp'))
def test_has_field_for_message_field_of_cleared_message_returns_false(self):
message = Test()
message.substruct.field2 = "something"
message.Clear()
self.assertFalse(message.HasField('substruct'))
def test_has_field_for_indirectly_set_message_field_returns_true(self):
message = Test()
message.substruct.field3.field1 = 3.14159
self.assertTrue(message.HasField('substruct'))
def test_has_field_for_cleared_indirectly_set_message_field_returns_false(self):
message = Test()
message.substruct.field3.field1 = 3.14159
message.ClearField('substruct')
self.assertFalse(message.HasField('substruct'))
def test_has_field_for_message_field_set_by_repeated_field_returns_true(self):
message = Test()
message.substruct.list.append(3)
self.assertTrue(message.HasField('substruct'))
def test_has_field_for_message_field_set_by_repeated_message_field_returns_true(self):
message = Test()
message.substruct.list_object.append(TestSs1Thing())
self.assertTrue(message.HasField('substruct'))
def test_has_field_for_string_field_set_by_deserialization_returns_true(self):
ref_message = TestRef()
ref_message.field3 = "Hello World"
ref_message2 = TestRef.FromString(ref_message.SerializeToString())
self.assertTrue(ref_message2.HasField('field3'))
def test_has_field_for_scalar_field_set_by_deserialization_returns_true(self):
ref_message = TestRef()
ref_message.field1 = 1
ref_message2 = TestRef.FromString(ref_message.SerializeToString())
self.assertTrue(ref_message2.HasField('field1'))
def test_has_field_for_message_field_set_by_deserialization_returns_true(self):
ss1_message = TestSs1()
ss1_message.field3.field1 = 3.14159
ss1_message2 = TestSs1.FromString(ss1_message.SerializeToString())
self.assertTrue(ss1_message2.field3._is_present_in_parent)