Skip to content

Commit

Permalink
Increase coverage in tests/ folder
Browse files Browse the repository at this point in the history
Actually it will not add new tests, just fix some dead ones and add
`no cover` comments where it's needs.
  • Loading branch information
PerchunPak committed Jul 17, 2022
1 parent c1e8d27 commit 20cad3b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
14 changes: 7 additions & 7 deletions mcstatus/tests/protocol/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def test_write_buffer(self):
assert self.connection.flush() == bytearray.fromhex("027FAA")


class TCPSocketConnectionTest:
class TestTCPSocketConnection:
def setup_method(self):
self.test_addr = Address("localhost", 1234)

Expand All @@ -217,15 +217,15 @@ def setup_method(self):
self.connection = TCPSocketConnection(self.test_addr)

def test_flush(self):
with pytest.raises(TypeError):
with pytest.raises(NotImplementedError):
self.connection.flush()

def test_receive(self):
with pytest.raises(TypeError):
with pytest.raises(NotImplementedError):
self.connection.receive("") # type: ignore # This is desired to produce TypeError

def test_remaining(self):
with pytest.raises(TypeError):
with pytest.raises(NotImplementedError):
self.connection.remaining()

def test_read(self):
Expand All @@ -245,7 +245,7 @@ def test_write(self):
self.connection.socket.send.assert_called_once_with(bytearray.fromhex("7FAA")) # type: ignore[attr-defined]


class UDPSocketConnectionTest:
class TestUDPSocketConnection:
def setup_method(self):
self.test_addr = Address("localhost", 1234)

Expand All @@ -257,11 +257,11 @@ def setup_method(self):
self.connection = UDPSocketConnection(self.test_addr)

def test_flush(self):
with pytest.raises(TypeError):
with pytest.raises(NotImplementedError):
self.connection.flush()

def test_receive(self):
with pytest.raises(TypeError):
with pytest.raises(NotImplementedError):
self.connection.receive("") # type: ignore # This is desired to produce TypeError

def test_remaining(self):
Expand Down
5 changes: 3 additions & 2 deletions mcstatus/tests/test_deprecated_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ def test_deprecated_class_return_result():

def test_deprecated_function_with_methods():
with pytest.raises(ValueError):
deprecated(methods=["__str__"])(lambda: None)
deprecated(methods=["__str__"])(lambda: None) # pragma: no branch


def test_deprecated_class_without_methods():
test_cls: Any = type("TestCls", (), {"foo": lambda x: x}) # https://github.com/microsoft/pyright/discussions/3438
# https://github.com/microsoft/pyright/discussions/3438
test_cls: Any = type("TestCls", (), {"foo": lambda x: x}) # pragma: no branch
with pytest.raises(ValueError):
deprecated()(test_cls)

Expand Down
20 changes: 10 additions & 10 deletions mcstatus/tests/test_retry_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def func():


def test_sync_fail():
x = -1
already_ran = False

@retry(tries=2)
def func():
nonlocal x
x += 1
if x == 0:
nonlocal already_ran
if already_ran:
already_ran = True
raise OSError("First error")
elif x == 1:
else:
raise RuntimeError("Second error")

# We should get the last exception on failure (not OSError)
Expand All @@ -50,15 +50,15 @@ async def func():


def test_async_fail():
x = -1
already_ran = False

@retry(tries=2)
async def func():
nonlocal x
x += 1
if x == 0:
nonlocal already_ran
if already_ran:
already_ran = True
raise OSError("First error")
elif x == 1:
else:
raise RuntimeError("Second error")

# We should get the last exception on failure (not OSError)
Expand Down
4 changes: 2 additions & 2 deletions mcstatus/tests/test_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class FakeAsyncStream(asyncio.StreamReader):
async def read(self, n: int) -> bytes:
await asyncio.sleep(delay=2)
return bytes([0] * n)
raise NotImplementedError("This line should not be reached") # pragma: no cover


async def fake_asyncio_asyncio_open_connection(hostname: str, port: int):
Expand All @@ -25,7 +25,7 @@ def setup_method(self):
def test_tcp_socket_read(self):
try:
from asyncio.exceptions import TimeoutError # type: ignore # (Import for older versions)
except ImportError:
except ImportError: # pragma: no cover
from asyncio import TimeoutError

with patch("asyncio.open_connection", fake_asyncio_asyncio_open_connection):
Expand Down

0 comments on commit 20cad3b

Please sign in to comment.