Skip to content

Commit

Permalink
added py coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco-Sulla committed Jun 6, 2024
1 parent 730ade8 commit ca1147c
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 28 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build_primary_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
env:
CIBW_ARCHS: ${{ matrix.cibw_archs }}
CIBW_SKIP: "cp311-* cp312-* pp*"
CIBW_TEST_REQUIRES: pytest
CIBW_TEST_REQUIRES: pytest pytest-cov
CIBW_TEST_COMMAND: >
python -X faulthandler {package}/test/debug.py &&
python -X faulthandler -m pytest -p no:faulthandler -s {package}
python -X faulthandler -m pytest -p no:faulthandler -s {package} --cov=frozendict --cov-report=term-missing --cov-branch --cov-fail-under=100
- uses: actions/upload-artifact@v3
with:
Expand All @@ -51,7 +51,7 @@ jobs:
python-version: "${{ matrix.version }}"

- name: Install dependencies
run: pip install -U pip setuptools wheel pytest
run: pip install -U pip setuptools wheel pytest pytest-cov

- name: Build module
run: FROZENDICT_PURE_PY=1 python setup.py bdist_wheel
Expand All @@ -63,7 +63,7 @@ jobs:
run: pip install dist/*

- name: Test with pytest
run: pytest
run: pytest --cov=frozendict --cov-report=term-missing --cov-branch --cov-fail-under=100 --cov-config=pyproject_py.toml

- uses: actions/upload-artifact@v3
with:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/build_secondary_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ jobs:
env:
CIBW_ARCHS: ${{ matrix.cibw_archs }}
CIBW_SKIP: "cp311-* cp312-* pp*"
CIBW_TEST_REQUIRES: pytest
CIBW_TEST_REQUIRES: pytest pytest-cov
CIBW_TEST_COMMAND: >
python -X faulthandler {package}/test/debug.py &&
python -X faulthandler -m pytest -p no:faulthandler -s {package}
python -X faulthandler -m pytest -p no:faulthandler -s {package} --cov=frozendict --cov-report=term-missing --cov-branch --cov-fail-under=100
- uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -70,10 +70,10 @@ jobs:
CIBW_ARCHS: ${{ matrix.cibw_archs }}
CIBW_BUILD: "cp310-*"
CIBW_SKIP: "*-musllinux_*"
CIBW_TEST_REQUIRES: pytest
CIBW_TEST_REQUIRES: pytest pytest-cov
CIBW_TEST_COMMAND: >
python -X faulthandler {package}/test/debug.py &&
python -X faulthandler -m pytest -p no:faulthandler -s {package}
python -X faulthandler -m pytest -p no:faulthandler -s {package} --cov=frozendict --cov-report=term-missing --cov-branch --cov-fail-under=100
with:
package-dir: dist/frozendict.tar.gz

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ test/core.*
.eggs/
.idea/
.vs/
.coverage
2 changes: 2 additions & 0 deletions build_py.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rm -rf build dist *.egg-info
FROZENDICT_PURE_PY=1 ./setup.py $*
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[tool.cibuildwheel]
skip=["cp311-*", "cp312-*", "pp*"]

[tool.coverage.run]
omit = ["_frozendict_py.py"]
2 changes: 2 additions & 0 deletions pyproject_py.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.cibuildwheel]
skip=["cp311-*", "cp312-*", "pp*"]
20 changes: 11 additions & 9 deletions src/frozendict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@
Provides frozendict, a simple immutable dictionary.
"""

try:
try: # pragma: no cover
from ._frozendict import *
c_ext = True
# noinspection PyUnresolvedReferences
del _frozendict
except ImportError:
except ImportError: # pragma: no cover
from ._frozendict_py import *
c_ext = False

from .version import version as __version__
from . import monkeypatch
from .cool import *
from . import cool


def _getFrozendictJsonEncoder(BaseJsonEncoder = None):
if BaseJsonEncoder is None:
if BaseJsonEncoder is None: # pragma: no cover
from json.encoder import JSONEncoder

BaseJsonEncoder = JSONEncoder

class FrozendictJsonEncoderInternal(BaseJsonEncoder):
def default(self, obj):
if isinstance(obj, frozendict):
if isinstance(obj, frozendict): # pragma: no cover
# TODO create a C serializer
return dict(obj)

return BaseJsonEncoder.default(self, obj)
return BaseJsonEncoder.default(
self,
obj
) # pragma: no cover

return FrozendictJsonEncoderInternal

Expand All @@ -44,9 +48,9 @@ def default(self, obj):
del Mapping


if c_ext:
if c_ext: # pragma: no cover
__all__ = (frozendict.__name__, )
else:
else: # pragma: no cover
__all__ = _frozendict_py.__all__
del _frozendict_py

Expand All @@ -55,5 +59,3 @@ def default(self, obj):

__all__ += cool.__all__
__all__ += (FrozendictJsonEncoder.__name__, "FrozenOrderedDict")

del cool
2 changes: 1 addition & 1 deletion src/frozendict/_frozendict_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def frozendict_or(self, other, *_args, **_kwargs):
try:
# noinspection PyStatementEffect
frozendict.__reversed__
except AttributeError:
except AttributeError: # pragma: no cover
def frozendict_reversed(self, *_args, **_kwargs):
return reversed(tuple(self))

Expand Down
5 changes: 3 additions & 2 deletions src/frozendict/cool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

# fix for python 3.9-

if not issubclass(array, MutableSequence):
# coverage does not work here!
if not issubclass(array, MutableSequence): # pragma: no cover
# noinspection PyUnresolvedReferences
MutableSequence.register(array)

Expand Down Expand Up @@ -301,7 +302,7 @@ def deepfreeze(
except KeyError:
if frozen_type:
freeze = type_o
else:
else: # pragma: no cover
raise

return freeze(o_copy)
Expand Down
16 changes: 10 additions & 6 deletions src/frozendict/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def checkCExtension(*, warn, warn_c = False):

res = cool.c_ext

if warn and res == warn_c:
if warn and res == warn_c: # pragma: no cover
if warn_c:
msg = "C Extension version, monkeypatch will be not applied"
else:
Expand All @@ -25,7 +25,7 @@ def checkCExtension(*, warn, warn_c = False):
return res


def patchOrUnpatchJson(*, patch, warn = True):
def patchOrUnpatchJson(*, patch, warn = True): # pragma: no cover
if not checkCExtension(warn = warn):
return

Expand Down Expand Up @@ -78,7 +78,7 @@ def patchOrUnpatchJson(*, patch, warn = True):
json._default_encoder = default_json_encoder


def patchOrUnpatchOrjson(*, patch, warn = True):
def patchOrUnpatchOrjson(*, patch, warn = True): # pragma: no cover
if not checkCExtension(warn = warn):
return

Expand Down Expand Up @@ -118,7 +118,11 @@ def frozendictOrjsonDumps(obj, *args, **kwargs):
orjson.orjson.dumps = defaultOrjsonDumps


def patchOrUnpatchMutableMappingSubclasshook(*, patch, warn = True):
def patchOrUnpatchMutableMappingSubclasshook(
*,
patch,
warn = True
): # pragma: no cover
warn_c = True

if checkCExtension(warn = warn, warn_c = warn_c):
Expand Down Expand Up @@ -185,10 +189,10 @@ def patchOrUnpatchAll(*, patch, warn = True, raise_orjson = False):

try:
import orjson
except ImportError:
except ImportError: # pragma: no cover
if raise_orjson:
raise
else:
else: # pragma: no cover
patchOrUnpatchOrjson(patch = patch, warn = warn)

patchOrUnpatchMutableMappingSubclasshook(patch = patch, warn = warn)
Expand Down
46 changes: 44 additions & 2 deletions test/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import frozendict as cool
from frozendict import frozendict
from collections import OrderedDict
from collections.abc import MutableSequence, Sequence
from collections.abc import MutableSequence, Sequence, Iterable
from array import array
from types import MappingProxyType
from frozendict import FreezeError, FreezeWarning
Expand All @@ -14,6 +14,18 @@ def __init__(self, x):
self.x = x


class NoDictAndHash:
__slots__ = (
"x",
)

def __init__(self, x):
self.x = x

def __hash__(self):
raise TypeError()


class BaseSeq(Sequence):

def __init__(self, seq):
Expand Down Expand Up @@ -64,13 +76,25 @@ def after_cure_inverse():
return (frozendict(a=frozendict({1: 2}), y="mbuti"), )


@pytest.fixture
def no_cure_inverse():
return (frozendict(a=frozendict({1: 2})), )


@pytest.fixture
def a():
a = A(3)

return a


@pytest.fixture
def no_dict_and_hash():
res = NoDictAndHash(3)

return res


@pytest.fixture
def before_cure(a):
return {"x": [
Expand Down Expand Up @@ -170,7 +194,11 @@ def test_deepfreeze_inverse(before_cure_inverse, after_cure_inverse):
) == after_cure_inverse


def test_register_inverse(before_cure_inverse, after_cure_inverse):
def test_register_inverse(
before_cure_inverse,
after_cure_inverse,
no_cure_inverse,
):
with pytest.warns(FreezeWarning):
cool.register(
frozendict,
Expand All @@ -179,6 +207,10 @@ def test_register_inverse(before_cure_inverse, after_cure_inverse):
)

assert cool.deepfreeze(before_cure_inverse) == after_cure_inverse

cool.unregister(frozendict, inverse=True)

assert cool.deepfreeze(before_cure_inverse) == no_cure_inverse


def test_prefer_forward():
Expand All @@ -205,3 +237,13 @@ def test_original_immutate():

def test_enum(my_enum):
assert cool.deepfreeze(my_enum) is my_enum


def test_get_items():
with pytest.raises(TypeError):
cool.cool.getItems(5)


def test_no_dict_and_hash(no_dict_and_hash):
with pytest.raises(TypeError):
cool.deepfreeze(no_dict_and_hash)
42 changes: 42 additions & 0 deletions test/test_monkeypatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json

import frozendict as cool
import pytest
from frozendict import frozendict


class A:
pass


@pytest.fixture
def object_to_serialize():
return frozendict()


@pytest.fixture
def object_to_serialize_2():
return A()


@pytest.fixture
def serialized_object():
return "{}"


def test_get_json_encoder(
object_to_serialize,
object_to_serialize_2,
serialized_object,
):
cool.monkeypatch.patchOrUnpatchJson(patch=True, warn=False)
assert json.dumps(object_to_serialize) == serialized_object

with pytest.raises(TypeError):
json.dumps(object_to_serialize_2)

cool.monkeypatch.patchOrUnpatchJson(patch=False, warn=False)

if cool.c_ext:
with pytest.raises(TypeError):
json.dumps(object_to_serialize)

0 comments on commit ca1147c

Please sign in to comment.