Skip to content

Commit

Permalink
Add max args validation
Browse files Browse the repository at this point in the history
  • Loading branch information
myslak71 committed Oct 31, 2019
1 parent 2cc706e commit 472ed37
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ String may also be passed as a `date`:
Available methods (note that all arguments except `self` are keyword only):
```
def search_nip(
self, *, nip: str, date: Optional[datetime.date] = None, raw: bool = False
) -> Tuple[Subject, str]:
*, nip: str, date: [datetime.date] = datetime.date.today(), raw: bool = False) -> Tuple[Subject, str]:
```
```
def search_nips(
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ responses==0.10.6
########################
safety==1.8.5

# Version release
# Releasing
########################
bumpversion==0.5.3
towncrier==19.2.0

# Docs
########################
Expand Down
7 changes: 4 additions & 3 deletions src/vater/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ class ClientError(Exception):
class MaximumArgumentsNumberExceeded(ClientError):
"""Raised when arguments number exceeds allowed maximum."""

def __init__(self, parameter_name: str, max: int):
def __init__(self, parameter_name: str, maximum: int) -> None:
"""Assign parameter name."""
self.parameter_name = parameter_name
self.maximum = maximum

def __str__(self):
def __str__(self) -> str:
"""Get error representation."""
return (
f"{self.__class__.__name__}: number of {self.parameter_name} "
f"exceeds allowed maximum: {self.max}"
f"exceeds allowed maximum: {self.maximum}"
)
7 changes: 5 additions & 2 deletions src/vater/request_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ def __init__(self, url_pattern: str, many: bool = False) -> None:

def validate(self) -> None:
"""Validate given parameters."""
parameter = next(iter(self.kwargs)) # type: ignore
if not self.many:
return

if self.many and len(self.kwargs[parameter]) > self.PARAM_LIMIT: # type: ignore
parameter = ({*self.kwargs} - {"raw", "date"}).pop() # type: ignore

if len(self.kwargs[parameter]) > self.PARAM_LIMIT: # type: ignore
raise MaximumArgumentsNumberExceeded(parameter, self.PARAM_LIMIT)

def result(self) -> Union[dict, Tuple[Union[List[Subject], Subject], str]]:
Expand Down
20 changes: 19 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import responses
from freezegun import freeze_time

from vater.errors import ERROR_CODE_MAPPING, InvalidRequestData, UnknownExternalApiError
from vater.errors import (
ERROR_CODE_MAPPING,
InvalidRequestData,
MaximumArgumentsNumberExceeded,
UnknownExternalApiError,
)
from vater.models import Company, Subject
from vater.request_types import SearchRequest


class TestSubjectSearch:
Expand Down Expand Up @@ -384,3 +390,15 @@ def test_api_returns_500(self, client):
'UnknownExternalApiError: status code: 500, data: {"message": "Uknown error"}'
in str(exception_info.value)
)

def test_max_args_exceeded(self, client):
"""Test that `MaximumArgumentsNumberExceeded` when number of args exceeds allowed maximum."""
with pytest.raises(MaximumArgumentsNumberExceeded) as exception_info:
client.search_nips(
raw=True, nips=["7171717171"] * (SearchRequest.PARAM_LIMIT + 1)
)

assert str(exception_info.value) == (
"MaximumArgumentsNumberExceeded: number of nips exceeds allowed maximum: "
f"{SearchRequest.PARAM_LIMIT}"
)

0 comments on commit 472ed37

Please sign in to comment.