Changeset View
Changeset View
Standalone View
Standalone View
swh/scheduler/tests/conftest.py
# Copyright (C) 2016-2020 The Software Heritage developers | # Copyright (C) 2016-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from datetime import datetime, timezone | from datetime import datetime, timezone | ||||
import os | import os | ||||
from typing import List | from typing import Dict, List | ||||
import pytest | import pytest | ||||
from swh.scheduler.model import ListedOrigin, Lister | from swh.scheduler.model import ListedOrigin, Lister | ||||
from swh.scheduler.tests.common import LISTERS | from swh.scheduler.tests.common import LISTERS | ||||
# make sure we are not fooled by CELERY_ config environment vars | # make sure we are not fooled by CELERY_ config environment vars | ||||
for var in [x for x in os.environ.keys() if x.startswith("CELERY")]: | for var in [x for x in os.environ.keys() if x.startswith("CELERY")]: | ||||
os.environ.pop(var) | os.environ.pop(var) | ||||
# test_cli tests depends on a en/C locale, so ensure it | # test_cli tests depends on a en/C locale, so ensure it | ||||
os.environ["LC_ALL"] = "C.UTF-8" | os.environ["LC_ALL"] = "C.UTF-8" | ||||
@pytest.fixture | @pytest.fixture | ||||
def stored_lister(swh_scheduler) -> Lister: | def stored_lister(swh_scheduler) -> Lister: | ||||
"""Store a lister in the scheduler and return its information""" | """Store a lister in the scheduler and return its information""" | ||||
return swh_scheduler.get_or_create_lister(**LISTERS[0]) | return swh_scheduler.get_or_create_lister(**LISTERS[0]) | ||||
@pytest.fixture | @pytest.fixture | ||||
def listed_origins(stored_lister) -> List[ListedOrigin]: | def visit_types() -> List[str]: | ||||
"""Return a (fixed) set of 1000 listed origins""" | """Possible visit types in `ListedOrigin`s""" | ||||
return [ | return ["git", "svn"] | ||||
@pytest.fixture | |||||
def listed_origins_by_type( | |||||
stored_lister: Lister, visit_types: List[str] | |||||
) -> Dict[str, List[ListedOrigin]]: | |||||
"""A fixed list of `ListedOrigin`s, for each `visit_type`.""" | |||||
count_per_type = 1000 | |||||
assert stored_lister.id | |||||
return { | |||||
visit_type: [ | |||||
ListedOrigin( | ListedOrigin( | ||||
lister_id=stored_lister.id, | lister_id=stored_lister.id, | ||||
url=f"https://example.com/{i:04d}.git", | url=f"https://{visit_type}.example.com/{i:04d}", | ||||
visit_type="git", | visit_type=visit_type, | ||||
last_update=datetime(2020, 6, 15, 16, 0, 0, i, tzinfo=timezone.utc), | last_update=datetime( | ||||
2020, 6, 15, 16, 0, 0, j * count_per_type + i, tzinfo=timezone.utc | |||||
), | |||||
) | ) | ||||
for i in range(1000) | for i in range(count_per_type) | ||||
] | ] | ||||
for j, visit_type in enumerate(visit_types) | |||||
} | |||||
@pytest.fixture | |||||
def listed_origins(listed_origins_by_type) -> List[ListedOrigin]: | |||||
"""Return a (fixed) set of listed origins""" | |||||
ardumont: why not plain `listed_origins_by_type.values()` ? | |||||
Done Inline ActionsIt converts List[List[ListedOrigin]] to List[ListedOrigin]. It's like a fold, with [] as the initial value of the accumulator vlorentz: It converts `List[List[ListedOrigin]]` to `List[ListedOrigin]`. It's like a fold, with `[]` as… | |||||
return sum(listed_origins_by_type.values(), []) |
why not plain listed_origins_by_type.values() ?