Skip to content

Commit

Permalink
migrate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 6, 2024
1 parent 441e25f commit f80476b
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 165 deletions.
148 changes: 69 additions & 79 deletions tests/test_cached_property.py
Original file line number Diff line number Diff line change
@@ -1,134 +1,124 @@
import platform
from operator import not_

import pytest

from propcache import _helpers, _helpers_py
from propcache._helpers import cached_property

IS_PYPY = platform.python_implementation() == "PyPy"
def test_cached_property(propcache_module) -> None:
class A:
def __init__(self):
self._cache = {}

@propcache_module.cached_property
def prop(self):
return 1

class CachedPropertyMixin:
cached_property = NotImplemented
a = A()
assert a.prop == 1

def test_cached_property(self) -> None:
class A:
def __init__(self):
self._cache = {}

@self.cached_property # type: ignore[misc]
def prop(self):
return 1
def test_cached_property_class(propcache_module) -> None:
class A:
def __init__(self):
"""Init."""
# self._cache not set because its never accessed in this test

a = A()
assert a.prop == 1
@propcache_module.cached_property
def prop(self):
"""Docstring."""

def test_cached_property_class(self) -> None:
class A:
def __init__(self):
"""Init."""
# self._cache not set because its never accessed in this test
assert isinstance(A.prop, propcache_module.cached_property)
assert A.prop.__doc__ == "Docstring."

@self.cached_property # type: ignore[misc]
def prop(self):
"""Docstring."""

assert isinstance(A.prop, self.cached_property)
assert A.prop.__doc__ == "Docstring."
def test_cached_property_without_cache(propcache_module) -> None:
class A:

def test_cached_property_without_cache(self) -> None:
class A:
__slots__ = ()

__slots__ = ()
def __init__(self):
pass

def __init__(self):
pass
@propcache_module.cached_property
def prop(self):
"""Mock property."""

@self.cached_property # type: ignore[misc]
def prop(self):
"""Mock property."""
a = A()

a = A()
with pytest.raises(AttributeError):
a.prop = 123

with pytest.raises(AttributeError):
a.prop = 123

def test_cached_property_check_without_cache(self) -> None:
class A:
def test_cached_property_check_without_cache(propcache_module) -> None:
class A:

__slots__ = ()
__slots__ = ()

def __init__(self):
pass
def __init__(self):
pass

@self.cached_property # type: ignore[misc]
def prop(self):
"""Mock property."""
@propcache_module.cached_property
def prop(self):
"""Mock property."""

a = A()
with pytest.raises((TypeError, AttributeError)):
assert a.prop == 1
a = A()
with pytest.raises((TypeError, AttributeError)):
assert a.prop == 1


class A:
def __init__(self):
self._cache = {}
def test_cached_property_caching(propcache_module) -> None:

@cached_property
def prop(self):
"""Docstring."""
return 1
class A:
def __init__(self):
self._cache = {}

@propcache_module.cached_property
def prop(self):
"""Docstring."""
return 1

def test_cached_property():
a = A()
assert 1 == a.prop


def test_cached_property_class():
assert isinstance(A.prop, cached_property)
assert "Docstring." == A.prop.__doc__


class TestPyCachedProperty(CachedPropertyMixin):
cached_property = _helpers_py.cached_property # type: ignore[assignment]
def test_cached_property_class_docstring(propcache_module) -> None:

class A:
def __init__(self):
self._cache = {}

if (
not _helpers.NO_EXTENSIONS
and not IS_PYPY
and hasattr(_helpers, "cached_property_c")
):
@propcache_module.cached_property
def prop(self):
"""Docstring."""
return 1

class TestCCachedProperty(CachedPropertyMixin):
cached_property = _helpers.cached_property_c # type: ignore[assignment, attr-defined, unused-ignore] # noqa: E501
assert isinstance(A.prop, propcache_module.cached_property)
assert "Docstring." == A.prop.__doc__


def test_set_name():
def test_set_name(propcache_module) -> None:
"""Test that the __set_name__ method is called and checked."""

class A:

@cached_property
@propcache_module.cached_property
def prop(self):
"""Docstring."""

A.prop.__set_name__(A, "prop")

with pytest.raises(
TypeError, match=r"Cannot assign the same cached_property to two "
):
match = r"Cannot assign the same cached_property to two "
with pytest.raises(TypeError, match=match):
A.prop.__set_name__(A, "something_else")


def test_get_without_set_name():
def test_get_without_set_name(propcache_module) -> None:
"""Test that get without __set_name__ fails."""
cp = cached_property(not_)
cp = propcache_module.cached_property(not_)

class A:
"""A class."""

A.cp = cp
with pytest.raises(TypeError, match=r"Cannot use cached_property instance "):
_ = A().cp
A.cp = cp # type: ignore[attr-defined]
match = r"Cannot use cached_property instance "
with pytest.raises(TypeError, match=match):
_ = A().cp # type: ignore[attr-defined]
154 changes: 68 additions & 86 deletions tests/test_under_cached_property.py
Original file line number Diff line number Diff line change
@@ -1,119 +1,101 @@
import platform

import pytest

from propcache import _helpers, _helpers_py
from propcache._helpers import under_cached_property

IS_PYPY = platform.python_implementation() == "PyPy"


class CachedPropertyMixin:
under_cached_property = NotImplemented
def test_under_cached_property(propcache_module) -> None:
class A:
def __init__(self):
self._cache = {}

def test_under_cached_property(self) -> None:
class A:
def __init__(self):
self._cache = {}

@self.under_cached_property # type: ignore[misc]
def prop(self):
return 1

a = A()
assert a.prop == 1
@propcache_module.under_cached_property
def prop(self):
return 1

def test_under_cached_property_class(self) -> None:
class A:
def __init__(self):
"""Init."""
# self._cache not set because its never accessed in this test
a = A()
assert a.prop == 1

@self.under_cached_property # type: ignore[misc]
def prop(self):
"""Docstring."""

assert isinstance(A.prop, self.under_cached_property)
assert A.prop.__doc__ == "Docstring."
def test_under_cached_property_class(propcache_module) -> None:
class A:
def __init__(self):
"""Init."""
# self._cache not set because its never accessed in this test

def test_under_cached_property_assignment(self) -> None:
class A:
def __init__(self):
self._cache = {}
@propcache_module.under_cached_property
def prop(self):
"""Docstring."""

@self.under_cached_property # type: ignore[misc]
def prop(self):
"""Mock property."""
assert isinstance(A.prop, propcache_module.under_cached_property)
assert A.prop.__doc__ == "Docstring."

a = A()

with pytest.raises(AttributeError):
a.prop = 123
def test_under_cached_property_assignment(propcache_module) -> None:
class A:
def __init__(self):
self._cache = {}

def test_under_cached_property_without_cache(self) -> None:
class A:
def __init__(self):
pass
@propcache_module.under_cached_property
def prop(self):
"""Mock property."""

@self.under_cached_property # type: ignore[misc]
def prop(self):
"""Mock property."""
a = A()

a = A()
with pytest.raises(AttributeError):
a.prop = 123

with pytest.raises(AttributeError):
a.prop = 123

def test_under_cached_property_check_without_cache(self) -> None:
class A:
def __init__(self):
pass
def test_under_cached_property_without_cache(propcache_module) -> None:
class A:
def __init__(self):
pass

@self.under_cached_property # type: ignore[misc]
def prop(self):
"""Mock property."""
@propcache_module.under_cached_property
def prop(self):
"""Mock property."""

a = A()
with pytest.raises(AttributeError):
assert a.prop == 1
a = A()

with pytest.raises(AttributeError):
a.prop = 123

class A:
def __init__(self):
self._cache = {}

@under_cached_property
def prop(self):
"""Docstring."""
return 1
def test_under_cached_property_check_without_cache(propcache_module) -> None:
class A:
def __init__(self):
pass

@propcache_module.under_cached_property
def prop(self):
"""Mock property."""

def test_under_cached_property():
a = A()
assert 1 == a.prop

with pytest.raises(AttributeError):
assert a.prop == 1

def test_under_cached_property_class():
assert isinstance(A.prop, under_cached_property)
assert "Docstring." == A.prop.__doc__

def test_under_cached_property_caching(propcache_module) -> None:

def test_under_cached_property_assignment():
a = A()
class A:
def __init__(self):
self._cache = {}

with pytest.raises(AttributeError):
a.prop = 123
@propcache_module.under_cached_property
def prop(self):
"""Docstring."""
return 1

a = A()
assert 1 == a.prop

class TestPyCachedProperty(CachedPropertyMixin):
under_cached_property = _helpers_py.under_cached_property # type: ignore[assignment] # noqa: E501

def test_under_cached_property_class_docstring(propcache_module) -> None:
class A:
def __init__(self):
self._cache = {}

if (
not _helpers.NO_EXTENSIONS
and not IS_PYPY
and hasattr(_helpers, "under_cached_property_c")
):
@propcache_module.under_cached_property
def prop(self):
"""Docstring."""
return 1

class TestCCachedProperty(CachedPropertyMixin):
under_cached_property = _helpers.under_cached_property_c # type: ignore[assignment, attr-defined, unused-ignore] # noqa: E501
assert isinstance(A.prop, propcache_module.under_cached_property)
assert "Docstring." == A.prop.__doc__

0 comments on commit f80476b

Please sign in to comment.