python-betterproto/tests/test_deprecated.py
Adrien 4e9a17c227
Add warnings when calling deprecated method (#596)
* Add test

* To run the workflow

* Fix import

* Format

* Add warning

* Fix indentation

* Test deprecated method

* More test

* Format

* Add import if needed

---------

Co-authored-by: Adrien Vannson <adrien.vannson@gardacp.com>
2024-08-14 21:33:43 +01:00

65 lines
1.5 KiB
Python

import warnings
import pytest
from tests.mocks import MockChannel
from tests.output_betterproto.deprecated import (
Empty,
Message,
Test,
TestServiceStub,
)
@pytest.fixture
def message():
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
return Message(value="hello")
def test_deprecated_message():
with pytest.warns(DeprecationWarning) as record:
Message(value="hello")
assert len(record) == 1
assert str(record[0].message) == f"{Message.__name__} is deprecated"
def test_message_with_deprecated_field(message):
with pytest.warns(DeprecationWarning) as record:
Test(message=message, value=10)
assert len(record) == 1
assert str(record[0].message) == f"{Test.__name__}.message is deprecated"
def test_message_with_deprecated_field_not_set(message):
with pytest.warns(None) as record:
Test(value=10)
assert not record
def test_message_with_deprecated_field_not_set_default(message):
with pytest.warns(None) as record:
_ = Test(value=10).message
assert not record
@pytest.mark.asyncio
async def test_service_with_deprecated_method():
stub = TestServiceStub(MockChannel([Empty(), Empty()]))
with pytest.warns(DeprecationWarning) as record:
await stub.deprecated_func(Empty())
assert len(record) == 1
assert str(record[0].message) == f"TestService.deprecated_func is deprecated"
with pytest.warns(None) as record:
await stub.func(Empty())
assert not record