Changeset View
Standalone View
swh/lister/launchpad/tests/test_lister.py
- This file was added.
# Copyright (C) 2017-2020 The Software Heritage developers | |||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
import re | |||||
import unittest | |||||
import requests_mock | |||||
ardumont: I did not fully realize earlier.
Does that test currently load a real launchpad instance?
(i… | |||||
Done Inline ActionsYes it does, what would you recommend to use to mock launchpadlib data ? legau: Yes it does, what would you recommend to use to mock launchpadlib data ? | |||||
Done Inline ActionsWe are using pytest and this allows to define fixtures like requests_mock_datadir. So as a first step, i'd look if there is any existing fixture around launchpadlib [1] If there is no fixture yet, you can try to define one which would mock the launchpadlib calls to return a fixed dataset of repositories to list. Just define a json file in the "tests/data/launchpadlib/dataset.json" and make the fixture returns it. Then in the tests, you can further ensure the data listed and the test dataset are consistent together. We use pytest-mocker for that, for example [2] [1] https://pytest.readthedocs.io/en/2.7.3/plugins_index/index.html ardumont: We are using pytest and this allows to define fixtures like requests_mock_datadir.
So as a… | |||||
from swh.lister.core.tests.test_lister import HttpListerTester | |||||
from ..lister import LaunchpadLister | |||||
class LaunchpadListerTester(HttpListerTester, unittest.TestCase): | |||||
Lister = LaunchpadLister | |||||
test_re = re.compile(r'/repositories\?since=([^?&]+)') | |||||
lister_subdir = 'github' | |||||
good_api_response_file = 'data/api.github.com/first_response.json' | |||||
bad_api_response_file = 'data/api.github.com/empty_response.json' | |||||
first_index = 0 | |||||
last_index = 100 | |||||
entries_per_page = 75 | |||||
convert_type = int | |||||
@requests_mock.Mocker() | |||||
Not Done Inline Actionswe have a datadir fixture [1] for that, "inject" datadir in the test signature and: response_filepath = os.path.join(datadir, f'response{page}.json') with open(response_filepath, encoding='utf-8') as f: ... ` [1] https://forge.softwareheritage.org/source/swh-core/browse/master/swh/core/pytest_plugin.py$0-121 ardumont: we have a `datadir` fixture [1] for that, "inject" datadir in the test signature and:
```… | |||||
def test_scheduled_tasks(self, http_mocker): | |||||
self.scheduled_tasks_test( | |||||
'data/api.launchpad.com/next_response.json', 876, http_mocker) | |||||
def test_lister_launchpad(swh_listers, requests_mock_datadir): | |||||
"""Simple launchpad listing should create scheduled tasks | |||||
""" | |||||
lister = swh_listers['launchpad'] | |||||
lister.run() | |||||
r = lister.scheduler.search_tasks(task_type='load-git') | |||||
assert len(r) == 100 | |||||
for row in r: | |||||
assert row['type'] == 'load-git' | |||||
# arguments check | |||||
args = row['arguments']['args'] | |||||
assert len(args) == 0 | |||||
# kwargs | |||||
kwargs = row['arguments']['kwargs'] | |||||
url = kwargs['url'] | |||||
assert url.startswith('https://git.launchpad.net') | |||||
assert row['policy'] == 'recurring' | |||||
assert row['priority'] is None |
I did not fully realize earlier.
Does that test currently load a real launchpad instance?
(i believe so and the diff description seems to agree ;)
I don't see the data test file.
requests_mock_datadir is a fixture to transform http requests into lookup within the ./data/<https_server-name>/remaining-file-named-after-remaining-http-query-parameters...