diff --git a/pytest.ini b/pytest.ini index 5228471..b6406e2 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,12 +1,11 @@ [pytest] -# for now, running with with_my pytest tests/reddit.py -# discover __init__,py as well -# TODO also importing too much with it... -# TODO FIXME wtf is this?? -python_files = reddit.py +# discover files that don't follow test_ naming. Useful to keep tests along with the source code +python_files = *.py addopts = + -rap --verbose - + # TODO hmm, not sure... guess it makes sense considering all the ext modules.. + --continue-on-collection-errors # otherwise it won't discover doctests # eh? importing too much # --doctest-modules diff --git a/tests/README.org b/tests/README.org deleted file mode 100644 index c1c8015..0000000 --- a/tests/README.org +++ /dev/null @@ -1,3 +0,0 @@ -At the moment, some tests here are specific to my data. - -If you know any good public data sources I could test against instead, please let me know! diff --git a/tests/bluemaestro.py b/tests/bluemaestro.py index 144e4f4..420dab8 100644 --- a/tests/bluemaestro.py +++ b/tests/bluemaestro.py @@ -1,11 +1,13 @@ #!/usr/bin/env python3 from pathlib import Path - from more_itertools import one +from .common import skip_if_not_karlicoss + import pytest # type: ignore +@skip_if_not_karlicoss def test() -> None: from my.bluemaestro import measurements res = list(measurements()) diff --git a/tests/commits.py b/tests/commits.py index f49a34f..ffc7d66 100644 --- a/tests/commits.py +++ b/tests/commits.py @@ -1,7 +1,8 @@ +from .common import skip_if_not_karlicoss as pytestmark + from more_itertools import ilen -from my.coding.commits import commits - -def test(): +def test() -> None: + from my.coding.commits import commits all_commits = commits() assert ilen(all_commits) > 10 diff --git a/tests/common.py b/tests/common.py new file mode 100644 index 0000000..ba5def3 --- /dev/null +++ b/tests/common.py @@ -0,0 +1,7 @@ +import os + +import pytest + +skip_if_not_karlicoss = pytest.mark.skipif( + 'HPI_TESTS_KARLICOSS' not in os.environ, reason='test only works on @karlicoss data for now', +) diff --git a/tests/config.py b/tests/config.py index 2496002..fdcdebf 100644 --- a/tests/config.py +++ b/tests/config.py @@ -18,10 +18,10 @@ def setup_notes_path(notes: Path) -> None: def test_dynamic_configuration(notes: Path) -> None: setup_notes_path(notes) - from my.body.weight import dataframe - weight_df = dataframe() + from my.body.weight import from_orgmode + weights = [0.0 if isinstance(x, Exception) else x.value for x in from_orgmode()] - assert list(weight_df['weight'].fillna(0.0)) == [ + assert weights == [ 0.0, 62.0, 0.0, diff --git a/tests/emfit.py b/tests/emfit.py index cd88945..8a779e4 100644 --- a/tests/emfit.py +++ b/tests/emfit.py @@ -1,7 +1,10 @@ -from my.emfit import datas +from .common import skip_if_not_karlicoss as pytestmark def test() -> None: + from my.emfit import datas + # TODO this should be implement via sanity checks/stat instead? + # the same code will be used for tests & for user reports ds = [x for x in datas() if not isinstance(x, Exception)] for d in ds: assert d.start.tzinfo is not None @@ -10,7 +13,10 @@ def test() -> None: assert d.sleep_end.tzinfo is not None +from .common import skip_if_not_karlicoss +@skip_if_not_karlicoss def test_tz() -> None: + from my.emfit import datas # TODO check errors too? ds = [x for x in datas() if not isinstance(x, Exception)] diff --git a/tests/foursquare.py b/tests/foursquare.py index ae9612e..a9169ff 100644 --- a/tests/foursquare.py +++ b/tests/foursquare.py @@ -1,6 +1,7 @@ -from my.foursquare import get_checkins +from .common import skip_if_not_karlicoss as pytestmark -def test_checkins(): +def test_checkins() -> None: + from my.foursquare import get_checkins # todo reuse stats? checkins = get_checkins() assert len(checkins) > 100 diff --git a/tests/github.py b/tests/github.py index f4ca4b5..5fb5fb9 100644 --- a/tests/github.py +++ b/tests/github.py @@ -1,16 +1,15 @@ -#!/usr/bin/env python3 +from .common import skip_if_not_karlicoss as pytestmark + from more_itertools import ilen - -from my.coding.github import get_events - # todo test against stats? not sure.. maybe both -def test_gdpr(): +def test_gdpr() -> None: import my.github.gdpr as gdpr assert ilen(gdpr.events()) > 100 -def test(): +def test() -> None: + from my.coding.github import get_events events = get_events() assert ilen(events) > 100 for e in events: diff --git a/tests/hypothesis.py b/tests/hypothesis.py index 9ef8880..f5ee99e 100644 --- a/tests/hypothesis.py +++ b/tests/hypothesis.py @@ -1,7 +1,6 @@ -#!/usr/bin/env python3 +from .common import skip_if_not_karlicoss as pytestmark -from my.hypothesis import pages, highlights - -def test(): +def test() -> None: + from my.hypothesis import pages, highlights assert len(list(pages())) > 10 assert len(list(highlights())) > 10 diff --git a/tests/instapaper.py b/tests/instapaper.py index ae685e6..153a716 100644 --- a/tests/instapaper.py +++ b/tests/instapaper.py @@ -1,5 +1,6 @@ -from my.instapaper import pages +from .common import skip_if_not_karlicoss as pytestmark - -def test_pages(): +def test_pages() -> None: + # TODO ugh. need lazy import to simplify testing? + from my.instapaper import pages assert len(list(pages())) > 3 diff --git a/tests/jawbone.py b/tests/jawbone.py index b6630a4..c53459d 100644 --- a/tests/jawbone.py +++ b/tests/jawbone.py @@ -1,11 +1,10 @@ -# TODO depends on my private data... move somewhere, and exclude from CI somehow? +from .common import skip_if_not_karlicoss as pytestmark from datetime import date, time -from my.jawbone import sleeps_by_date - # todo private test.. move away -def test_tz(): +def test_tz() -> None: + from my.jawbone import sleeps_by_date sleeps = sleeps_by_date() for s in sleeps.values(): assert s.sleep_start.tzinfo is not None diff --git a/tests/lastfm.py b/tests/lastfm.py index a93fc4b..43e8f41 100644 --- a/tests/lastfm.py +++ b/tests/lastfm.py @@ -1,13 +1,16 @@ +from .common import skip_if_not_karlicoss as pytestmark + +# todo maybe belongs to common from more_itertools import ilen -from my.lastfm import scrobbles - -def test(): +def test() -> None: + from my.lastfm import scrobbles assert ilen(scrobbles()) > 1000 -def test_datetime_ascending(): +def test_datetime_ascending() -> None: + from my.lastfm import scrobbles from more_itertools import pairwise for a, b in pairwise(scrobbles()): assert a.dt <= b.dt diff --git a/tests/orgmode.py b/tests/orgmode.py index aa30114..d213a5e 100644 --- a/tests/orgmode.py +++ b/tests/orgmode.py @@ -1,6 +1,9 @@ from my import orgmode from my.core.orgmode import collect +from .common import skip_if_not_karlicoss + +@skip_if_not_karlicoss def test() -> None: # meh results = list(orgmode.query().collect_all(lambda n: [n] if 'python' in n.tags else [])) diff --git a/tests/reddit.py b/tests/reddit.py index 6a660ea..964d0f5 100644 --- a/tests/reddit.py +++ b/tests/reddit.py @@ -1,3 +1,5 @@ +from .common import skip_if_not_karlicoss as pytestmark + from datetime import datetime import pytz diff --git a/tests/rtm.py b/tests/rtm.py index efb9a4f..93378b6 100644 --- a/tests/rtm.py +++ b/tests/rtm.py @@ -1,6 +1,7 @@ -from my.rtm import all_tasks +from .common import skip_if_not_karlicoss as pytestmark -def test(): +def test() -> None: + from my.rtm import all_tasks tasks = all_tasks() assert len([t for t in tasks if 'gluons' in t.title]) > 0 diff --git a/tests/smscalls.py b/tests/smscalls.py index 3303e1c..51150f0 100644 --- a/tests/smscalls.py +++ b/tests/smscalls.py @@ -1,7 +1,9 @@ -from my.smscalls import calls, messages +from .common import skip_if_not_karlicoss as pytestmark +# TODO maybe instead detect if it has any data at all +# if none, then skip the test, say that user doesn't have any data? - -# TODO that's a pretty dumb test; perhaps can be generic.. -def test(): +# TODO implement via stat? +def test() -> None: + from my.smscalls import calls, messages assert len(list(calls())) > 10 assert len(list(messages())) > 10 diff --git a/tests/takeout.py b/tests/takeout.py index 2cfb7a3..bc746c2 100644 --- a/tests/takeout.py +++ b/tests/takeout.py @@ -8,12 +8,9 @@ from my.google.takeout.html import read_html from my.google.takeout.paths import get_last_takeout -def ilen(it): - # TODO more_itertools? - return len(list(it)) +from more_itertools import ilen - -def test_location_perf(): +def test_location_perf() -> None: # 2.80 s for 10 iterations and 10K points # TODO try switching to jq and see how it goes? not sure.. print(ilen(islice(LT.iter_locations(), 0, 10000))) @@ -30,7 +27,7 @@ import pytest # type: ignore 'My Activity/Search/MyActivity.html', ] ) -def test_parser(path: str): +def test_parser(path: str) -> None: path = 'Takeout/' + path tpath = get_last_takeout(path=path) assert tpath is not None @@ -39,7 +36,7 @@ def test_parser(path: str): print(len(results)) -def test_myactivity_search(): +def test_myactivity_search() -> None: path = 'Takeout/My Activity/Search/MyActivity.html' tpath = get_last_takeout(path=path) assert tpath is not None diff --git a/tests/tweets.py b/tests/tweets.py index 3c5aa4e..fefc24e 100644 --- a/tests/tweets.py +++ b/tests/tweets.py @@ -1,12 +1,16 @@ +from .common import skip_if_not_karlicoss as pytestmark +# todo current test doesn't depend on data, in principle... +# should make lazy loading the default.. + + from datetime import datetime import json import pytz -from my.twitter.archive import Tweet - -def test_tweet(): +def test_tweet() -> None: + from my.twitter.archive import Tweet raw = """ { "retweeted" : false, diff --git a/tests/youtube.py b/tests/youtube.py index b8c1aa8..d514061 100644 --- a/tests/youtube.py +++ b/tests/youtube.py @@ -1,11 +1,14 @@ # TODO move elsewhere? # these tests would only make sense with some existing data? although some of them would work for everyone.. # not sure what's a good way of handling this.. +from .common import skip_if_not_karlicoss as pytestmark -from my.media.youtube import get_watched, Watched +# TODO ugh. if i uncomment this here (on top level), then this test vvv fails +# from my.media.youtube import get_watched, Watched +# HPI_TESTS_KARLICOSS=true pytest -raps tests/tz.py tests/youtube.py - -def test(): +def test() -> None: + from my.media.youtube import get_watched, Watched watched = list(get_watched()) assert len(watched) > 1000 diff --git a/tox.ini b/tox.ini index 1a72477..80e5382 100644 --- a/tox.ini +++ b/tox.ini @@ -8,6 +8,8 @@ passenv = CI CI_* # deliberately set to nonexistent path to check the fallback logic setenv = MY_CONFIG = nonexistent commands = + # TODO core & modules should be tested separately? + pip install -e .[testing] # python -m pytest {posargs} @@ -23,19 +25,12 @@ commands = # my.body.weight dep pip install orgparse - python3 -m pytest \ - tests/cli.py \ - tests/core.py \ - tests/misc.py \ - tests/get_files.py \ - tests/config.py::test_environment_variable \ - tests/demo.py \ - tests/bluemaestro.py \ - tests/location.py \ - tests/tz.py \ - tests/calendar.py \ - tests/config.py \ - {posargs} + python3 -m pytest tests \ + # ignore some tests which might take a while to run on ci.. + --ignore tests/takeout.py \ + --ignore tests/extra/polar.py \ + --ignore tests/pdfs/test_pdfs.py \ + {posargs} [testenv:demo]