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

Commit 5bd83d3

Browse files
committed
simplified tests
1 parent 78e201d commit 5bd83d3

2 files changed

Lines changed: 25 additions & 42 deletions

File tree

tests/test_json.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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]

tests/test_message.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,12 @@ class Color(proto.Enum):
326326
)
327327

328328

329-
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")
329+
@pytest.mark.parametrize("expect_proto_7_plus", [True, False])
330+
def test_serialize_to_dict_float_precision(expect_proto_7_plus):
331+
if ((expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) < 7)) or (
332+
(not expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) >= 7)
333+
):
334+
pytest.skip("installed proto version does not match test")
332335

333336
class Squid(proto.Message):
334337
mass_kg = proto.Field(proto.FLOAT, number=1)
@@ -337,26 +340,15 @@ class Squid(proto.Message):
337340

338341
with pytest.warns(DeprecationWarning) as warnings:
339342
s_dict = Squid.to_dict(s, float_precision=3)
340-
assert s_dict["mass_kg"] == 3.14
341343
assert len(warnings) == 1
342344
assert "`float_precision` will be removed" in warnings[0].message.args[0]
343-
344-
345-
def test_serialize_to_dict_float_precision_7_plus():
346-
if int(proto.message._PROTOBUF_MAJOR_VERSION) < 7:
347-
pytest.skip("unsupported protobuf version for test")
348-
349-
class Squid(proto.Message):
350-
mass_kg = proto.Field(proto.FLOAT, number=1)
351-
352-
s = Squid(mass_kg=3.141592)
353-
354-
with pytest.warns(DeprecationWarning) as warnings:
355-
s_dict = Squid.to_dict(s, float_precision=3)
345+
# for protobuf <7, expect truncated float
346+
if expect_proto_7_plus:
356347
assert s_dict["mass_kg"] == pytest.approx(3.141592)
357-
358-
assert len(warnings) == 1
359-
assert "`float_precision` was removed" in warnings[0].message.args[0]
348+
assert "`float_precision` was removed" in warnings[0].message.args[0]
349+
else:
350+
assert s_dict["mass_kg"] == pytest.approx(3.14)
351+
assert "`float_precision` will be removed" in warnings[0].message.args[0]
360352

361353

362354
def test_unknown_field_deserialize():

0 commit comments

Comments
 (0)