Changeset View
Standalone View
swh/lister/pypi/tests/test_lister.py
- This file was added.
# Copyright (C) 2019 the Software Heritage developers | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
import requests_mock | |||||
import unittest | |||||
from unittest.mock import patch | |||||
from swh.lister.pypi.lister import PyPILister | |||||
from swh.lister.core.tests.test_lister import HttpSimpleListerTester | |||||
lister = PyPILister() | |||||
expected_packages = ['0lever-so', '0lever-utils', '0-orchestrator', '0wned'] | |||||
expected_model = { | |||||
'uid': 'arrow', | |||||
'name': 'arrow', | |||||
'full_name': 'arrow', | |||||
'html_url': 'https://pypi.org/pypi/arrow/json', | |||||
'origin_url': 'https://pypi.org/project/arrow/', | |||||
'origin_type': 'pypi', | |||||
} | |||||
class PyPIListerTester(HttpSimpleListerTester, unittest.TestCase): | |||||
Lister = PyPILister | |||||
PAGE = 'https://pypi.org/simple/' | |||||
lister_subdir = 'pypi' | |||||
good_api_response_file = 'api_response.html' | |||||
entries = 4 | |||||
@requests_mock.Mocker() | |||||
def test_list_packages(self, http_mocker): | |||||
"""List packages from simple api page should retrieve all packages within | |||||
ardumont: Same, add docstrings to explain the test's goal.
Something like:
```
List packages from… | |||||
""" | |||||
http_mocker.get(self.PAGE, text=self.mock_response) | |||||
fl = self.get_fl() | |||||
packages = fl.list_packages(self.get_api_response(0)) | |||||
for package in expected_packages: | |||||
assert package in packages | |||||
def test_transport_response_simplified(self): | |||||
"""Test model created by the lister | |||||
""" | |||||
fl = self.get_fl() | |||||
model = fl.transport_response_simplified(['arrow']) | |||||
assert len(model) == 1 | |||||
for key, values in model[0].items(): | |||||
assert values == expected_model[key] | |||||
def test_task_dict(self): | |||||
"""Test the task creation of lister | |||||
Not Done Inline ActionsChange the comment prior to the method into a docstring. ardumont: Change the comment prior to the method into a docstring.
| |||||
Not Done Inline ActionsThere is a funny thing happening when I am putting doccsting to the method. nahimilega: There is a funny thing happening when I am putting doccsting to the method.
As soon as I add… | |||||
Not Done Inline ActionsYes, that's another issue though. If you remove the with patch as stated just below, you should not have the problem anymore. Also, once in a while, it's ok to use # noqa at the end of the statement to make flake8 silent so, that could become: with patch .... as mock_create_tasks: # noqa ... But i'd prefer you simplify the test to the suggestion below. ardumont: Yes, that's another issue though.
If you remove the `with patch` as stated just below, you… | |||||
""" | |||||
with patch('swh.lister.pypi.lister.utils.create_task_dict') as mock_create_tasks: # noqa | |||||
lister.task_dict(origin_type='pypi', origin_url='https://abc', | |||||
name='test_pack', html_url='https://def') | |||||
mock_create_tasks.assert_called_once_with( | |||||
'load-pypi', 'recurring', 'test_pack', 'https://abc', | |||||
Not Done Inline ActionsAs lister.task_dict does not have side-effects (as in for example, api call, write to files, etc...), you can call directly it and check the output. That'd simplify the test here. # given (<- usually assertion definitions here) expected_task = { ... } # when (<- function or code to actually test, only 1 instruction usual to make stuff clearer) actual_task = lister.task_dict(...) # then (<- as many checks as you want) self.assertEqual(actual_task, expected_task) ardumont: As `lister.task_dict` does not have side-effects (as in for example, api call, write to files… | |||||
Not Done Inline ActionsI don't think we can do it because the output of task_dict has a datetime object of current date. {'policy': 'recurring', 'type': 'load-pypi', 'next_run': datetime.datetime(2019, 7, 18, 11, 33, 46, 211689, tzinfo=datetime.timezone.utc), 'arguments': {'args': ('test_pack', 'https://abc'), 'kwargs': {'project_metadata_url': 'https://def'}}} Now two things can be done
Which option shall I choose ? nahimilega: I don't think we can do it because the output of `task_dict` has a datetime object of current… | |||||
Not Done Inline ActionsThe python datetime thing is really annoying. But fine, let's keep the test as is for now. ardumont: The python datetime thing is really annoying.
I consider it a side-effect and that's usually… | |||||
project_metadata_url='https://def') |
Same, add docstrings to explain the test's goal.
Something like: