Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.

Commit 638c8f6

Browse files
committed
improved tests
1 parent 3b4a2e0 commit 638c8f6

3 files changed

Lines changed: 32 additions & 13 deletions

File tree

proto/message.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ def _message_to_map(
951951
warnings.warn(
952952
"The argument `float_precision` has been removed from Protobuf 7.x.",
953953
DeprecationWarning,
954+
stacklevel=3,
954955
)
955956

956957
return map_fn(cls.pb(instance), **kwargs)

tests/test_json.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,34 +253,33 @@ class Squid(proto.Message):
253253
assert re.search(r"massKg.*name", j)
254254

255255

256-
pytest.skipif(
257-
int(proto.message._PROTOBUF_MAJOR_VERSION) >= 7,
258-
"float_precision removed in protobuf 7.x"
259-
)
260256
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")
259+
261260
class Squid(proto.Message):
262261
name = proto.Field(proto.STRING, number=1)
263262
mass_kg = proto.Field(proto.FLOAT, number=2)
264263

265-
s = Squid(name="Steve", mass_kg=3.14159265)
264+
s = Squid(name="Steve", mass_kg=3.141592)
266265
j = Squid.to_json(s, float_precision=3, indent=None)
267266

268267
assert j == '{"name": "Steve", "massKg": 3.14}'
269268

270-
pytest.skipif(
271-
int(proto.message._PROTOBUF_MAJOR_VERSION) < 7,
272-
"unsupported protobuf version for test"
273-
)
269+
274270
def test_json_float_precision_7_plus():
271+
if int(proto.message._PROTOBUF_MAJOR_VERSION) < 7:
272+
pytest.skip("unsupported protobuf version for test")
273+
275274
class Squid(proto.Message):
276275
name = proto.Field(proto.STRING, number=1)
277276
mass_kg = proto.Field(proto.FLOAT, number=2)
278277

279-
s = Squid(name="Steve", mass_kg=3.14159265)
278+
s = Squid(name="Steve", mass_kg=3.141592)
280279
with pytest.warns(DeprecationWarning) as warnings:
281280
j = Squid.to_json(s, float_precision=3, indent=None)
282281

283-
assert j == '{"name": "Steve", "massKg": 3.14159265}'
282+
assert j == '{"name": "Steve", "massKg": 3.141592}'
284283

285284
assert len(warnings) == 1
286285
assert "`float_precision` has been removed" in warnings[0].message.args[0]

tests/test_message.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,36 @@ class Color(proto.Enum):
326326
)
327327

328328

329-
# TODO: https://github.com/googleapis/proto-plus-python/issues/390
330329
def test_serialize_to_dict_float_precision():
330+
if int(proto.message._PROTOBUF_MAJOR_VERSION) >= 7:
331+
pytest.skip("float_precision was removed in protobuf 7.x")
332+
331333
class Squid(proto.Message):
332334
mass_kg = proto.Field(proto.FLOAT, number=1)
333335

334-
s = Squid(mass_kg=3.14159265)
336+
s = Squid(mass_kg=3.141592)
335337

336338
s_dict = Squid.to_dict(s, float_precision=3)
337339
assert s_dict["mass_kg"] == 3.14
338340

339341

342+
def test_serialize_to_dict_float_precision_7_plus():
343+
if int(proto.message._PROTOBUF_MAJOR_VERSION) < 7:
344+
pytest.skip("unsupported protobuf version for test")
345+
346+
class Squid(proto.Message):
347+
mass_kg = proto.Field(proto.FLOAT, number=1)
348+
349+
s = Squid(mass_kg=3.141592)
350+
351+
with pytest.warns(DeprecationWarning) as warnings:
352+
s_dict = Squid.to_dict(s, float_precision=3)
353+
assert s_dict["mass_kg"] == pytest.approx(3.141592)
354+
355+
assert len(warnings) == 1
356+
assert "`float_precision` has been removed" in warnings[0].message.args[0]
357+
358+
340359
def test_unknown_field_deserialize():
341360
# This is a somewhat common setup: a client uses an older proto definition,
342361
# while the server sends the newer definition. The client still needs to be

0 commit comments

Comments
 (0)