Skip to content

Commit

Permalink
test: increase unit test coverage (#52)
Browse files Browse the repository at this point in the history
* test: increase unit test coverage

* Update tests.yml

---------

Co-authored-by: Liam Beckman <[email protected]>
  • Loading branch information
uniqueg and lbeckman314 authored Jan 8, 2024
1 parent e67ab6e commit 93ad675
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 188 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ jobs:
run: pip install .

- name: Lint with Flake8
run: flake8 .
run: flake8 --max-line-length=120 .

- name: Run unit tests
run: coverage run --source tes -m pytest -W ignore::DeprecationWarning
run: |
pytest \
--cov=tes/ \
--cov-branch \
--cov-report=term-missing \
--cov-fail-under=99
6 changes: 3 additions & 3 deletions tes/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ def check_success(data: Task) -> bool:
time.sleep(0.5)

def _request_params(
self, data: Optional[str] = None,
params: Optional[Dict] = None
self, data: Optional[str] = None, params: Optional[Dict] = None
) -> Dict[str, Any]:
kwargs: Dict[str, Any] = {}
kwargs['timeout'] = self.timeout
kwargs['headers'] = {}
kwargs['headers']['Content-type'] = 'application/json'
kwargs['auth'] = (self.user, self.password)
if self.user is not None and self.password is not None:
kwargs['auth'] = (self.user, self.password)
if data:
kwargs['data'] = data
if params:
Expand Down
32 changes: 14 additions & 18 deletions tes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@
from typing import Any, Dict, List, Optional, Tuple, Type, Union


@attrs
@attrs(repr=False)
class _ListOfValidator(object):
type: Type = attrib()

def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
def __call__(self, inst, attr, value) -> None:
if not all([isinstance(n, self.type) for n in value]):
raise TypeError(
"'{attr.name}' must be a list of {self.type!r} (got {value!r} "
"that is a list of {values[0].__class__!r}).",
attr, self.type, value,
f"'{attr.name}' must be a list of {self.type!r} (got "
f"{value!r}", attr
)

def __repr__(self) -> str:
Expand Down Expand Up @@ -60,15 +56,15 @@ def strconv(value: Any) -> Any:
# since an int64 value is encoded as a string in json we need to handle
# conversion
def int64conv(value: Optional[str]) -> Optional[int]:
if value is not None:
return int(value)
return value
if value is None:
return value
return int(value)


def timestampconv(value: Optional[str]) -> Optional[datetime]:
if value is not None:
return dateutil.parser.parse(value)
return value
if value is None:
return value
return dateutil.parser.parse(value)


def datetime_json_handler(x: Any) -> str:
Expand Down Expand Up @@ -294,7 +290,7 @@ def is_valid(self) -> Tuple[bool, Union[None, TypeError]]:
for e in self.executors:
if e.image is None:
errs.append("Executor image must be provided")
if len(e.command) == 0:
if e.command is None or len(e.command) == 0:
errs.append("Executor command must be provided")
if e.stdin is not None:
if not os.path.isabs(e.stdin):
Expand All @@ -306,8 +302,8 @@ def is_valid(self) -> Tuple[bool, Union[None, TypeError]]:
if not os.path.isabs(e.stderr):
errs.append("Executor stderr must be an absolute path")
if e.env is not None:
for k, v in e.env:
if not isinstance(k, str) and not isinstance(k, str):
for k, v in e.env.items():
if not isinstance(k, str) and not isinstance(v, str):
errs.append(
"Executor env keys and values must be StrType"
)
Expand Down Expand Up @@ -339,7 +335,7 @@ def is_valid(self) -> Tuple[bool, Union[None, TypeError]]:
errs.append("Volume paths must be absolute")

if self.tags is not None:
for k, v in self.tags:
for k, v in self.tags.items():
if not isinstance(k, str) and not isinstance(k, str):
errs.append(
"Tag keys and values must be StrType"
Expand Down
26 changes: 12 additions & 14 deletions tes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ def __init__(self, *args, **kwargs):


def unmarshal(j: Any, o: Type, convert_camel_case=True) -> Any:
m: Any = None
if isinstance(j, str):
m = json.loads(j)
elif isinstance(j, dict):
m = j
try:
m = json.loads(j)
except json.decoder.JSONDecodeError:
pass
elif j is None:
return None
else:
raise TypeError("j must be a str, a dict or None")
m = j

if not isinstance(m, dict):
raise TypeError("j must be a dictionary, a JSON string evaluation to "
"a dictionary, or None")

d: Dict[str, Any] = {}
if convert_camel_case:
Expand Down Expand Up @@ -77,16 +83,8 @@ def _unmarshal(v: Any, obj: Type) -> Any:
field = v
omap = fullOmap.get(o.__name__, {})
if k in omap:
if isinstance(omap[k], tuple):
try:
obj = omap[k][0]
field = _unmarshal(v, obj)
except Exception:
obj = omap[k][1]
field = _unmarshal(v, obj)
else:
obj = omap[k]
field = _unmarshal(v, obj)
obj = omap[k]
field = _unmarshal(v, obj)
r[k] = field

try:
Expand Down
Empty file added tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ coverage>=6.5.0
coveralls>=3.3.1
flake8>=5.0.4
pytest>=7.2.1
pytest-cov>=4.0.0
requests_mock>=1.10.0
Loading

0 comments on commit 93ad675

Please sign in to comment.