@@ -253,9 +253,12 @@ class Squid(proto.Message):
253253 assert re .search (r"massKg.*name" , j )
254254
255255
256- def test_json_float_precision ():
257- if int (proto .message ._PROTOBUF_MAJOR_VERSION ) >= 7 :
258- pytest .skip ("float_precision was removed in protobuf 7.x" )
256+ @pytest .mark .parametrize ("expect_proto_7_plus" , [True , False ])
257+ def test_json_float_precision (expect_proto_7_plus ):
258+ if ((expect_proto_7_plus and int (proto .message ._PROTOBUF_MAJOR_VERSION ) < 7 )) or (
259+ (not expect_proto_7_plus and int (proto .message ._PROTOBUF_MAJOR_VERSION ) >= 7 )
260+ ):
261+ pytest .skip ("installed proto version does not match test" )
259262
260263 class Squid (proto .Message ):
261264 name = proto .Field (proto .STRING , number = 1 )
@@ -265,24 +268,12 @@ class Squid(proto.Message):
265268 s = Squid (name = "Steve" , mass_kg = 3.141592 )
266269 j = Squid .to_json (s , float_precision = 3 , indent = None )
267270
268- assert j == '{"name": "Steve", "massKg": 3.14}'
269271 assert len (warnings ) == 1
270- assert "`float_precision` will be removed" in warnings [0 ].message .args [0 ]
271-
272-
273- def test_json_float_precision_7_plus ():
274- if int (proto .message ._PROTOBUF_MAJOR_VERSION ) < 7 :
275- pytest .skip ("unsupported protobuf version for test" )
276-
277- class Squid (proto .Message ):
278- name = proto .Field (proto .STRING , number = 1 )
279- mass_kg = proto .Field (proto .FLOAT , number = 2 )
280-
281- s = Squid (name = "Steve" , mass_kg = 3.141592 )
282- with pytest .warns (DeprecationWarning ) as warnings :
283- j = Squid .to_json (s , float_precision = 3 , indent = None )
284272
285- assert j == '{"name": "Steve", "massKg": 3.141592}'
286-
287- assert len (warnings ) == 1
288- assert "`float_precision` was removed" in warnings [0 ].message .args [0 ]
273+ # for protobuf <7, expect truncated float
274+ if expect_proto_7_plus :
275+ assert j == '{"name": "Steve", "massKg": 3.141592}'
276+ assert "`float_precision` was removed" in warnings [0 ].message .args [0 ]
277+ else :
278+ assert j == '{"name": "Steve", "massKg": 3.14}'
279+ assert "`float_precision` will be removed" in warnings [0 ].message .args [0 ]
0 commit comments