41
41
import gc
42
42
import operator
43
43
import struct
44
+ import sys
44
45
45
46
from google .apputils import basetest
46
47
from google .protobuf import unittest_import_pb2
@@ -1556,6 +1557,20 @@ def assertInitialized(self, proto):
1556
1557
1557
1558
def assertNotInitialized (self , proto ):
1558
1559
self .assertFalse (proto .IsInitialized ())
1560
+ try :
1561
+ proto .SerializeToString ()
1562
+ except message .EncodeError :
1563
+ return
1564
+ except :
1565
+ # C++ implementation in opensource do not consider the catched
1566
+ # exception google.protobuf.message.EncodeError same as
1567
+ # message.EncodeError. Add an additional catch to deal with it.
1568
+ if api_implementation .Type () == 'python' :
1569
+ raise self .failureException ('message.EncodeError not raised' )
1570
+ self .assertEqual ('<class \' google.protobuf.message.EncodeError\' >' ,
1571
+ str (sys .exc_info ()[0 ]))
1572
+ else :
1573
+ raise self .failureException ('message.EncodeError not raised' )
1559
1574
# "Partial" serialization doesn't care if message is uninitialized.
1560
1575
proto .SerializePartialToString ()
1561
1576
@@ -2485,11 +2500,23 @@ def _CheckRaises(self, exc_class, callable_obj, exception):
2485
2500
# Check if the exception message is the right one.
2486
2501
self .assertEqual (exception , str (ex ))
2487
2502
return
2503
+ except :
2504
+ # C++ implementation in opensource do not consider the catched
2505
+ # exception google.protobuf.message.EncodeError same as
2506
+ # message.EncodeError. Add an additional catch to deal with it.
2507
+ if api_implementation .Type () == 'python' :
2508
+ raise self .failureException ('%s not raised' % str (exc_class ))
2509
+ self .assertEqual (exception , str (sys .exc_info ()[1 ]))
2488
2510
else :
2489
2511
raise self .failureException ('%s not raised' % str (exc_class ))
2490
2512
2491
2513
def testSerializeUninitialized (self ):
2492
2514
proto = unittest_pb2 .TestRequired ()
2515
+ self ._CheckRaises (
2516
+ message .EncodeError ,
2517
+ proto .SerializeToString ,
2518
+ 'Message protobuf_unittest.TestRequired is missing required fields: '
2519
+ 'a,b,c' )
2493
2520
# Shouldn't raise exceptions.
2494
2521
partial = proto .SerializePartialToString ()
2495
2522
@@ -2500,10 +2527,18 @@ def testSerializeUninitialized(self):
2500
2527
self .assertFalse (proto2 .HasField ('a' ))
2501
2528
2502
2529
proto .a = 1
2530
+ self ._CheckRaises (
2531
+ message .EncodeError ,
2532
+ proto .SerializeToString ,
2533
+ 'Message protobuf_unittest.TestRequired is missing required fields: b,c' )
2503
2534
# Shouldn't raise exceptions.
2504
2535
partial = proto .SerializePartialToString ()
2505
2536
2506
2537
proto .b = 2
2538
+ self ._CheckRaises (
2539
+ message .EncodeError ,
2540
+ proto .SerializeToString ,
2541
+ 'Message protobuf_unittest.TestRequired is missing required fields: c' )
2507
2542
# Shouldn't raise exceptions.
2508
2543
partial = proto .SerializePartialToString ()
2509
2544
@@ -2533,13 +2568,25 @@ def testSerializeUninitializedSubMessage(self):
2533
2568
proto .SerializeToString ()
2534
2569
2535
2570
proto .optional_message .a = 1
2571
+ self ._CheckRaises (
2572
+ message .EncodeError ,
2573
+ proto .SerializeToString ,
2574
+ 'Message protobuf_unittest.TestRequiredForeign '
2575
+ 'is missing required fields: '
2576
+ 'optional_message.b,optional_message.c' )
2536
2577
2537
2578
proto .optional_message .b = 2
2538
2579
proto .optional_message .c = 3
2539
2580
proto .SerializeToString ()
2540
2581
2541
2582
proto .repeated_message .add ().a = 1
2542
2583
proto .repeated_message .add ().b = 2
2584
+ self ._CheckRaises (
2585
+ message .EncodeError ,
2586
+ proto .SerializeToString ,
2587
+ 'Message protobuf_unittest.TestRequiredForeign is missing required fields: '
2588
+ 'repeated_message[0].b,repeated_message[0].c,'
2589
+ 'repeated_message[1].a,repeated_message[1].c' )
2543
2590
2544
2591
proto .repeated_message [0 ].b = 2
2545
2592
proto .repeated_message [0 ].c = 3
0 commit comments