Пример #1
0
    def test_public_resort_gets_item_sort_key_and_calls_list_sort_on_items(
            self, monkeypatch, tmp_folder):
        """Check resorting internal calls and call counts.
        Testing for: def resort(self, sort_by).

        :param monkeypatch: pytest monkey patch fixture
        :type monkeypatch: _pytest.monkeypatch.MonkeyPatch
        :param tmp_folder: Test params and dummy test folder factory fixture pair.
        :type tmp_folder: (dict, dirtools.tests.factory.DummyFolderFactory)
        """
        params, factory = tmp_folder
        scan = Folder(factory.path, params['sort_by'], params['level'])

        # .resort()
        resort = Mock(side_effect=scan.resort)
        monkeypatch.setattr(scan, 'resort', resort)

        # Check if it's called at the end of async process.
        _ = scan.items()
        resort.assert_called_once_with(params['sort_by'])
        # Also check if raises TypeError comes from _get_item_sort_key()
        with pytest.raises(TypeError):
            scan.resort('foo')
        resort.reset_mock()

        # ._get_item_sort_key()
        sort_key, reverse = scan._get_item_sort_key(params['sort_by'])
        get_item_sort_key = Mock(return_value=(sort_key, reverse))
        monkeypatch.setattr(scan, '_get_item_sort_key', get_item_sort_key)

        # deque()
        deque_func = Mock(side_effect=scanner.deque)
        monkeypatch.setattr(scanner, 'deque', deque_func)

        # sorted()
        sorted_func = Mock(side_effect=sorted)
        monkeypatch.setattr(builtins, 'sorted', sorted_func)

        # Let's resort
        scan.resort(params['sort_by'])
        get_item_sort_key.assert_called_once_with(params['sort_by'])
        deque_func.assert_called_once()
        sorted_func.assert_called_once()
Пример #2
0
    def test_public_methods_should_block_before_processing(
            self, monkeypatch, tmp_folder):
        """Make sure public methods and properties blocks until scanning
        finishes.

        :param monkeypatch: pytest monkey patch fixture
        :type monkeypatch: _pytest.monkeypatch.MonkeyPatch
        :param tmp_folder: Test params and dummy test folder factory fixture pair.
        :type tmp_folder: (dict, dirtools.tests.factory.DummyFolderFactory)
        """
        params, factory = tmp_folder
        # Mock the _await internal function first
        scan = Folder(factory.path, params['sort_by'], params['level'])
        _ = scan.items()

        blocker = Mock()
        monkeypatch.setattr(scan, '_await', blocker)

        # len(scan)
        assert len(scan) == factory.total_items
        blocker.assert_called_once()
        blocker.reset_mock()

        # .total_size
        assert scan.total_size == factory.total_size
        blocker.assert_called_once()
        blocker.reset_mock()

        # .items
        _ = scan.items()
        blocker.assert_called_once()
        blocker.reset_mock()

        # .cleanup_items() calls human2bytes once
        _human2bytes = Mock(side_effect=human2bytes)
        monkeypatch.setattr('dirtools.utils.human2bytes', _human2bytes)
        factory_size_human = bytes2human(factory.total_size, precision=11)
        with pytest.raises(StopIteration):
            next(scan.cleanup_items(factory_size_human))
        _human2bytes.assert_called_once_with(factory_size_human)
        blocker.assert_called_once()
        blocker.reset_mock()

        # So give -1 to make it block actually, run only for >0 sizes
        if factory.total_size > 0:
            sh_rmtree = Mock()
            os_remove = Mock()
            monkeypatch.setattr('shutil.rmtree', sh_rmtree)
            monkeypatch.setattr(os, 'remove', os_remove)
            # Attempt to remove single item
            next(
                scan.cleanup_items(
                    bytes2human(factory.total_size - 1, precision=11)))
            blocker.assert_called_once()
            blocker.reset_mock()
            assert sh_rmtree.called or os_remove.called

        # .resort() called at the end of async loop, so it shouldn't _await
        scan.resort(params['sort_by'])
        blocker.assert_not_called()
        blocker.reset_mock()