Skip to content

Commit

Permalink
🤖 black formatting files before release
Browse files Browse the repository at this point in the history
  • Loading branch information
codedawi committed Aug 31, 2020
1 parent a6d3555 commit 6471a5e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 46 deletions.
13 changes: 8 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import codecs
from setuptools import setup, find_packages
import os

from setuptools import find_packages, setup


def read(name: str):
file_path = os.path.join(os.path.dirname(__file__), name)
return codecs.open(file_path, encoding="utf-8").read()


setup(
name="pytest-schema",
use_scm_version=True,
Expand All @@ -21,9 +24,9 @@ def read(name: str):
"pytest>=3.5.0",
"schema>=0.7.0",
],
setup_requires=['setuptools_scm'],
packages=find_packages('src'),
package_dir={'': 'src'},
setup_requires=["setuptools_scm"],
packages=find_packages("src"),
package_dir={"": "src"},
include_package_data=True,
zip_safe=False,
classifiers=[
Expand Down
5 changes: 2 additions & 3 deletions src/pytest_schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pytest_schema.helpers import exact_schema, like_schema, schema
from pytest_schema.schema import Schema, SchemaError
from pytest_schema.helpers import schema, like_schema, exact_schema
from pytest_schema.types import (
And,
Enum,
Expand All @@ -12,7 +12,6 @@
Use,
)


__all__ = [
"Schema",
"schema",
Expand All @@ -27,4 +26,4 @@
"Or",
"Regex",
"Use",
]
]
2 changes: 1 addition & 1 deletion src/pytest_schema/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def like_schema(value: Any) -> Schema:
Returns:
Schema: initialized and configured class with non exact match requirements
Example:
✅ assert like\_schema({ "status": int }) == {"status": 404}
Expand Down
13 changes: 8 additions & 5 deletions src/pytest_schema/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def match(exact: bool = True) -> Callable:
Args:
exact (bool, optional): Whether validated against exact or allow skip keys.
"""

def _decorator(f: Callable):
@wraps(f)
def _wrapper(self, value: Any) -> bool:
Expand All @@ -25,11 +26,11 @@ def _wrapper(self, value: Any) -> bool:
Returns:
bool: binary true or false state of returned value
"""
# store original configured value
# store original configured value
original: bool = self._ignore_extra_keys

# toggle property to opposite of `exact` param
self._ignore_extra_keys = not exact
# toggle property to opposite of `exact` param
self._ignore_extra_keys = not exact

try:
# call method an store result
Expand All @@ -38,8 +39,10 @@ def _wrapper(self, value: Any) -> bool:
# raise any SchemaErrors after toggling cls
# state back to original value
self._ignore_extra_keys = original

# determine binary (true or false state)
return bool(response)
return bool(response)

return _wrapper

return _decorator
19 changes: 11 additions & 8 deletions src/pytest_schema/schema.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
from typing import Any, Callable, Optional

from schema import Schema as BaseSchema, SchemaError
from schema import Schema as BaseSchema
from schema import SchemaError

from pytest_schema.match import match


class Schema(BaseSchema):
"""
Extention of BaseSchema class to implement \
method to help using with `assert` in tests.
"""

def __init__(
self,
schema: Any,
error: Optional[str] = None,
ignore_extra_keys: Optional[bool] = True,
name: Optional[str] = None,
description: Optional[str] = None,
as_reference: Optional[bool] = False
as_reference: Optional[bool] = False,
):
super().__init__(
schema,
error=error,
ignore_extra_keys=ignore_extra_keys,
name=name,
description=description,
as_reference=as_reference
as_reference=as_reference,
)

def __eq__(self, value: Any) -> bool:
Expand All @@ -39,7 +43,7 @@ def __eq__(self, value: Any) -> bool:
True when `value` is validate against schema.
"""
return bool(self.validate(value))

def __ne__(self, value: Any) -> bool:
"""
Compares Schema against value provided when using the `!=` comparsion.
Expand All @@ -64,7 +68,7 @@ def exact(self, value: Any) -> bool:
True when `value` is validate against schema exactly.
"""
return self.validate(value)

@match(exact=True)
def not_exact(self, value: Any) -> bool:
"""
Expand Down Expand Up @@ -96,12 +100,12 @@ def like(self, value: Any) -> bool:
True when `value` is validate against schema.
"""
return self.validate(value)

@match(exact=False)
def not_like(self, value: Any) -> bool:
"""
Compares Schema against value, does **NOT** require it to be an exact match.
Args:
value (Any): data to validate against schema class
Expand All @@ -112,4 +116,3 @@ def not_like(self, value: Any) -> bool:
True when `value` is **NOT** validate against schema.
"""
return not self.is_valid(value)

4 changes: 3 additions & 1 deletion src/pytest_schema/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from schema import And, Or, Hook, Use, Optional, Regex, Forbidden, Literal
from schema import And, Forbidden, Hook, Literal, Optional, Or, Regex, Use

from pytest_schema.schema import Schema


class Enum(Schema):
"""Simple interface to create Enum like schema."""

def __init__(self, *value):
"""
Initialize schema with list or tuple like values.
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pytest_plugins = "pytester"
pytest_plugins = "pytester"
23 changes: 6 additions & 17 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
import pytest
from pytest_schema import schema, SchemaError

from pytest_schema import SchemaError, schema


@pytest.mark.parametrize(
"value, basic_schema",
[
(
{
"id": 1,
"username": "helloworld"
},
{"id": 1, "username": "helloworld"},
{
"id": int,
"username": str,
}
),
(
{
"hello": {
"hey": "world"
}
},
{
"hello": dict
}
)
]
),
({"hello": {"hey": "world"}}, {"hello": dict}),
],
)
def test_schema_basic_dicts(value, basic_schema):

Expand Down
11 changes: 6 additions & 5 deletions tests/test_standard_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from pytest_schema import schema, SchemaError

from pytest_schema import SchemaError, schema


@pytest.mark.parametrize(
Expand All @@ -13,7 +14,7 @@
(("hello", "world"), tuple),
(["hello", "world"], list),
(lambda k: True, callable),
]
],
)
def test_schema_standard_types(value, type_schema):

Expand All @@ -31,7 +32,7 @@ def test_schema_standard_types(value, type_schema):
(("hello", "world"), str),
(["hello", "world"], str),
(lambda k: True, None),
]
],
)
def test_schema_notequal_standard_types(value, type_schema):

Expand All @@ -49,9 +50,9 @@ def test_schema_notequal_standard_types(value, type_schema):
(("hello", "world"), list),
(["hello", "world"], dict),
(lambda k: True, bool),
]
],
)
def test_schema_notequal_standard_types(value, type_schema):

with pytest.raises(SchemaError):
assert schema(type_schema) == value
assert schema(type_schema) == value

0 comments on commit 6471a5e

Please sign in to comment.