Skip to content

Commit

Permalink
core: add more tests for stat/datetime guessing
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Feb 14, 2021
1 parent 4012f9b commit 6239879
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
56 changes: 51 additions & 5 deletions my/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,64 @@ def funcit():
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?
Expand Down
4 changes: 4 additions & 0 deletions tests/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from subprocess import check_call

def test_lists_modules() -> None:
check_call(['hpi', 'modules'])
1 change: 1 addition & 0 deletions tests/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
from my.core.core_config import *
from my.core.error import *
from my.core.util import *
from my.core.common import *
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ commands =
pip install orgparse

python3 -m pytest \
tests/cli.py \
tests/core.py \
tests/misc.py \
tests/get_files.py \
Expand All @@ -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]
Expand Down

0 comments on commit 6239879

Please sign in to comment.