Page MenuHomeSoftware Heritage

swh.lister.cran: Add description in task_dict
ClosedPublic

Authored by nahimilega on Jun 26 2019, 4:08 PM.

Details

Summary

Add description in task_dict method because
the only metadata that can be found for a
package at CRAN is its decsription. That can
only br achived from the build in API in R,
which ister is already using. Hence instead of
getting metadata in loader, it is passed
by lister

Diff Detail

Repository
rDLS Listers
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

swh/lister/cran/tests/test_tasks.py
29–32 ↗(On Diff #5516)

I an not sure this this the correct way of adding the tests which were mentioned by @vlorentz in D1644.
Please guide me the correct way.

No, when you do this, mock_create_tasks.assert_called_once_with() checks it was called a single time, and that single time is mock_create_tasks(). So what you are actually testing is that swh.scheduler.utils.create_task_dict is never called by the lister itself.

No, when you do this, mock_create_tasks.assert_called_once_with() checks it was called a single time, and that single time is mock_create_tasks(). So what you are actually testing is that swh.scheduler.utils.create_task_dict is never called by the lister itself.

Oh ok, so how should I write the test case to check that description is not deleted in future.
I tried this but it is also not working

with patch('swh.scheduler.utils.create_task_dict') as mock_create_tasks:
    lister.task_dict()
mock_create_tasks.assert_called_once_with()

Can you please help me with this

What is the error message?

(swh) archit@work-pc:~/swh-environment/swh-lister/swh/lister/cran$ tox .

...
swh/lister/cran/tests/test_tasks.py .F                                 [100%]

================================== FAILURES ==================================
________________________________ test_lister _________________________________

lister = <MagicMock name='CRANLister' id='140463269756880'>
swh_app = <Celery celery.tests at 0x7fc028e83cf8>
celery_session_worker = <Worker: gen7127@work-pc (running)>

    @patch('swh.lister.cran.tasks.CRANLister')
    def test_lister(lister, swh_app, celery_session_worker):
        # setup the mocked CRANLister
        lister.return_value = lister
        lister.run.return_value = None
    
        res = swh_app.send_task(
            'swh.lister.cran.tasks.CRANListerTask')
        assert res
        res.wait()
        assert res.successful()
    
        lister.assert_called_once_with()
        lister.db_last_index.assert_not_called()
        lister.run.assert_called_once_with()
    
        with patch('swh.scheduler.utils.create_task_dict') as mock_create_tasks:
            lister.task_dict()
>       mock_create_tasks.assert_called_once_with()

swh/lister/cran/tests/test_tasks.py:31: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

_mock_self = <MagicMock name='create_task_dict' id='140463269630696'>
args = (), kwargs = {}
self = <MagicMock name='create_task_dict' id='140463269630696'>
msg = "Expected 'create_task_dict' to be called once. Called 0 times."

    def assert_called_once_with(_mock_self, *args, **kwargs):
        """assert that the mock was called exactly once and that that call was
        with the specified arguments."""
        self = _mock_self
        if not self.call_count == 1:
            msg = ("Expected '%s' to be called once. Called %s times." %
                   (self._mock_name or 'mock', self.call_count))
>           raise AssertionError(msg)
E           AssertionError: Expected 'create_task_dict' to be called once. Called 0 times.

/usr/lib/python3.7/unittest/mock.py:839: AssertionError

It means swh.scheduler.utils.create_task_dict was never called.

This is because the lister imports from swh.scheduler.utils import create_task_dict before it was patched. Instead, you should patch swh.lister.cran.lister.create_task_dict.

It means swh.scheduler.utils.create_task_dict was never called.

This is because the lister imports from swh.scheduler.utils import create_task_dict before it was patched. Instead, you should patch swh.lister.cran.lister.create_task_dict.

This I tried with patch swh.lister.cran.lister.create_task_dict.

with patch('swh.lister.cran.lister.create_task_dict') as mock_create_tasks:
    lister.task_dict()
mock_create_tasks.assert_called_once_with()

But still the same error showing mock_create_tasks never called

    @patch('swh.lister.cran.tasks.CRANLister')
    def test_lister(lister, swh_app, celery_session_worker):
        # setup the mocked CRANLister
        lister.return_value = lister
        lister.run.return_value = None
    
        res = swh_app.send_task(
            'swh.lister.cran.tasks.CRANListerTask')
        assert res
        res.wait()
        assert res.successful()
    
        lister.assert_called_once_with()
        lister.db_last_index.assert_not_called()
        lister.run.assert_called_once_with()
    
        with patch('swh.lister.cran.lister.create_task_dict') as mock_create_tasks:
            lister.task_dict()
>       mock_create_tasks.assert_called_once_with()

swh/lister/cran/tests/test_tasks.py:31: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

_mock_self = <MagicMock name='create_task_dict' id='140059418513592'>
args = (), kwargs = {}
self = <MagicMock name='create_task_dict' id='140059418513592'>
msg = "Expected 'create_task_dict' to be called once. Called 0 times."

    def assert_called_once_with(_mock_self, *args, **kwargs):
        """assert that the mock was called exactly once and that that call was
        with the specified arguments."""
        self = _mock_self
        if not self.call_count == 1:
            msg = ("Expected '%s' to be called once. Called %s times." %
                   (self._mock_name or 'mock', self.call_count))
>           raise AssertionError(msg)
E           AssertionError: Expected 'create_task_dict' to be called once. Called 0 times.

Got it, it's because you added that check in a test where swh.lister.cran.tasks.CRANLister is mocked, so when you do lister.run() nothing actually happens. (You can check, even lister.thisIsNotARealFunction() wouldn't crash)

Got it, it's because you added that check in a test where swh.lister.cran.tasks.CRANLister is mocked, so when you do lister.run() nothing actually happens. (You can check, even lister.thisIsNotARealFunction() wouldn't crash)

True I tested it.

To avoid this I made another function like this

def test_task_dict(lister):
    with patch('swh.lister.cran.lister.create_task_dict') as mock_create_tasks:
        lister.task_dict()
    mock_create_tasks.assert_called_once_with()

But is giving error showing fixture 'lister' not found

collected 3 items                                                              

swh/lister/cran/tests/test_tasks.py .E.                                  [100%]

==================================== ERRORS ====================================
______________________ ERROR at setup of test_description ______________________
file /home/archit/swh-environment/swh-lister/swh/lister/cran/tests/test_tasks.py, line 13
  def test_description(lister):
E       fixture 'lister' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, celery_app, celery_config, celery_enable_logging, celery_includes, celery_parameters, celery_session_app, celery_session_worker, celery_worker, celery_worker_parameters, celery_worker_pool, cov, depends_on_current_app, doctest_namespace, monkeypatch, no_cover, postgresql, postgresql_proc, pytestconfig, record_property, record_xml_attribute, record_xml_property, recwarn, requests_mock, swh_app, swh_scheduler, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, use_celery_app_trap
>       use 'pytest --fixtures [testpath]' for help on them.

/home/archit/swh-environment/swh-lister/swh/lister/cran/tests/test_tasks.py:13

Sorry, I am sure it is pretty easy to fix but I am still not able to get it.

Remove the argument of the test

Remove the argument of the test

If lister argument is removed then it gives error 'lister' is not defined

    def test_task_dict():
        with patch('swh.lister.cran.lister.create_task_dict') as mock_create_tasks:
>           lister.task_dict()
E           NameError: name 'lister' is not defined

You must instantiate CRANLister

Add test to avoid removal of description in future

Cool!

Now, could you move it to a different file (it does not test the lister task itself, but a core feature of the lister)?
Also, could you add another test where the project_metadata is not empty?

Shifted the test to another file.

It would be nice to have a more functional test, checking the whole behavior of the lister; but this will do for now. Thanks!

This revision is now accepted and ready to land.Jun 27 2019, 12:26 PM
This revision was automatically updated to reflect the committed changes.