core: add more tests for stat/datetime guessing

This commit is contained in:
Dima Gerasimov 2021-02-14 12:51:34 +00:00 committed by karlicoss
parent 4012f9b7c2
commit 6239879245
4 changed files with 59 additions and 7 deletions

View file

@ -447,18 +447,64 @@ def _stat_iterable(it: Iterable[C]) -> Any:
return res
def test_stat_iterable() -> None:
from datetime import datetime, timedelta
from typing import NamedTuple
dd = datetime.utcfromtimestamp(123)
day = timedelta(days=3)
X = NamedTuple('X', [('x', int), ('d', datetime)])
def it():
yield RuntimeError('oops!')
for i in range(2):
yield X(x=i, d=dd + day * i)
yield RuntimeError('bad!')
for i in range(3):
yield X(x=i * 10, d=dd + day * (i * 10))
yield X(x=123, d=dd + day * 50)
res = _stat_iterable(it())
assert res['count'] == 1 + 2 + 1 + 3 + 1
assert res['errors'] == 1 + 1
assert res['last'] == dd + day * 50
# experimental, not sure about it..
def guess_datetime(x: Any) -> Optional[datetime]:
# todo support datacalsses
asdict = getattr(x, '_asdict', None)
if asdict is None:
# todo hmm implement withoutexception..
try:
d = asdict(x)
except:
return None
# todo check if there are multiple?
for k, v in asdict().items():
for k, v in d.items():
if isinstance(v, datetime):
return v
return None
def test_guess_datetime() -> None:
from datetime import datetime
from dataclasses import dataclass
from typing import NamedTuple
dd = isoparse('2021-02-01T12:34:56Z')
# ugh.. https://github.com/python/mypy/issues/7281
A = NamedTuple('A', [('x', int)])
B = NamedTuple('B', [('x', int), ('created', datetime)])
assert guess_datetime(A(x=4)) is None
assert guess_datetime(B(x=4, created=dd)) == dd
@dataclass
class C:
a: datetime
x: int
assert guess_datetime(C(a=dd, x=435)) == dd
# TODO not sure what to return when multiple datetime fields?
# TODO test @property?
def asdict(thing) -> Json:
# todo primitive?

4
tests/cli.py Normal file
View file

@ -0,0 +1,4 @@
from subprocess import check_call
def test_lists_modules() -> None:
check_call(['hpi', 'modules'])

View file

@ -13,3 +13,4 @@ we can run against the tests in my.core directly.
from my.core.core_config import *
from my.core.error import *
from my.core.util import *
from my.core.common import *

View file

@ -24,6 +24,7 @@ commands =
pip install orgparse
python3 -m pytest \
tests/cli.py \
tests/core.py \
tests/misc.py \
tests/get_files.py \
@ -33,8 +34,8 @@ commands =
tests/location.py \
tests/tz.py \
tests/calendar.py \
tests/config.py
hpi modules
tests/config.py \
{posargs}
[testenv:demo]