MCPcopy
hub / github.com/danielgtaylor/python-betterproto / test_binary_compatibility

Function test_binary_compatibility

tests/test_inputs.py:201–225  ·  view source on GitHub ↗
(repeat, test_data: TestData)

Source from the content-addressed store, hash-verified

199
200@pytest.mark.parametrize("test_data", test_cases.messages_with_json, indirect=True)
201def test_binary_compatibility(repeat, test_data: TestData) -> None:
202 plugin_module, reference_module, json_data = test_data
203
204 for sample in json_data:
205 reference_instance = Parse(sample.json, reference_module().Test())
206 reference_binary_output = reference_instance.SerializeToString()
207
208 for _ in range(repeat):
209 plugin_instance_from_json: betterproto.Message = (
210 plugin_module.Test().from_json(sample.json)
211 )
212 plugin_instance_from_binary = plugin_module.Test.FromString(
213 reference_binary_output
214 )
215
216 # Generally this can't be relied on, but here we are aiming to match the
217 # existing Python implementation and aren't doing anything tricky.
218 # https://developers.google.com/protocol-buffers/docs/encoding#implications
219 assert bytes(plugin_instance_from_json) == reference_binary_output
220 assert bytes(plugin_instance_from_binary) == reference_binary_output
221
222 assert plugin_instance_from_json == plugin_instance_from_binary
223 assert dict_replace_nans(
224 plugin_instance_from_json.to_dict()
225 ) == dict_replace_nans(plugin_instance_from_binary.to_dict())

Callers

nothing calls this directly

Calls 5

dict_replace_nansFunction · 0.85
SerializeToStringMethod · 0.80
from_jsonMethod · 0.80
FromStringMethod · 0.80
to_dictMethod · 0.45

Tested by

no test coverage detected