Skip to content

Commit

Permalink
🧪💅 Restructure test_istr to use new fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Jan 14, 2024
1 parent b0a41d7 commit 7513e6a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 65 deletions.
8 changes: 0 additions & 8 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ disable_error_code =
call-arg,
index,

[mypy-test_istr]
disable_error_code =
ignore-without-code,
misc,
no-untyped-call,
no-untyped-def,
operator,

[mypy-test_multidict]
disable_error_code = call-arg

Expand Down
107 changes: 50 additions & 57 deletions tests/test_istr.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,75 @@
import gc
import sys
from typing import Type
from typing import Callable, Type

import pytest

from multidict._compat import USE_EXTENSIONS
from multidict._multidict_py import istr as _istr # noqa: E402

if USE_EXTENSIONS:
from multidict._multidict import istr # type: ignore
else:
from multidict import istr
IMPLEMENTATION = getattr(sys, "implementation") # to suppress mypy error


IMPLEMENTATION = getattr(sys, "implementation") # to suppress mypy error
def test_ctor(case_insensitive_str_class: Type[str]) -> None:
s = case_insensitive_str_class()
assert "" == s


def test_ctor_str(case_insensitive_str_class: Type[str]) -> None:
s = case_insensitive_str_class("aBcD")
assert "aBcD" == s


class IStrMixin:
cls = Type[istr]
def test_ctor_istr(case_insensitive_str_class: Type[str]) -> None:
s = case_insensitive_str_class("A")
s2 = case_insensitive_str_class(s)
assert "A" == s
assert s == s2

def test_ctor(self):
s = self.cls()
assert "" == s

def test_ctor_str(self):
s = self.cls("aBcD")
assert "aBcD" == s
def test_ctor_buffer(case_insensitive_str_class: Type[str]) -> None:
s = case_insensitive_str_class(b"aBc")
assert "b'aBc'" == s

def test_ctor_istr(self):
s = self.cls("A")
s2 = self.cls(s)
assert "A" == s
assert s == s2

def test_ctor_buffer(self):
s = self.cls(b"aBc")
assert "b'aBc'" == s
def test_ctor_repr(case_insensitive_str_class: Type[str]) -> None:
s = case_insensitive_str_class(None)
assert "None" == s

def test_ctor_repr(self):
s = self.cls(None)
assert "None" == s

def test_str(self):
s = self.cls("aBcD")
s1 = str(s)
assert s1 == "aBcD"
assert type(s1) is str
def test_str(case_insensitive_str_class: Type[str]) -> None:
s = case_insensitive_str_class("aBcD")
s1 = str(s)
assert s1 == "aBcD"
assert type(s1) is str

def test_eq(self):
s1 = "Abc"
s2 = self.cls(s1)
assert s1 == s2

def test_eq(case_insensitive_str_class: Type[str]) -> None:
s1 = "Abc"
s2 = case_insensitive_str_class(s1)
assert s1 == s2

class TestPyIStr(IStrMixin):
cls = _istr

@staticmethod
def _create_strs():
_istr("foobarbaz")
istr2 = _istr()
_istr(istr2)
@pytest.fixture
def create_istrs(case_insensitive_str_class: Type[str]) -> Callable[[], None]:
"""Make a callable populating memory with a few ``istr`` objects."""

@pytest.mark.skipif(
IMPLEMENTATION.name != "cpython", reason="PyPy has different GC implementation"
)
def test_leak(self):
gc.collect()
cnt = len(gc.get_objects())
for _ in range(10000):
self._create_strs()
def _create_strs() -> None:
case_insensitive_str_class("foobarbaz")
istr2 = case_insensitive_str_class()
case_insensitive_str_class(istr2)

gc.collect()
cnt2 = len(gc.get_objects())
assert abs(cnt - cnt2) < 10 # on PyPy these numbers are not equal
return _create_strs


if USE_EXTENSIONS:
@pytest.mark.skipif(
IMPLEMENTATION.name != "cpython",
reason="PyPy has different GC implementation",
)
def test_leak(create_istrs: Callable[[], None]) -> None:
gc.collect()
cnt = len(gc.get_objects())
for _ in range(10000):
create_istrs()

class TestIStr(IStrMixin):
cls = istr
gc.collect()
cnt2 = len(gc.get_objects())
assert abs(cnt - cnt2) < 10 # on PyPy these numbers are not equal

0 comments on commit 7513e6a

Please sign in to comment.