Esempio n. 1
0
    def test_internal_get_item_sort_key_functions_correctly_and_called_as_many_items(
            self, monkeypatch, tmp_folder):
        """Check the static item sorting key function returns correct keys and
        pairs, also check if it was called as many times needed for a scanning.
        Testing for: @staticmethod def _get_item_sort_key(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)
        """
        with pytest.raises(TypeError):
            Folder._get_item_sort_key('foo')

        params, factory = tmp_folder
        try:
            item = next(factory.items)
        except StopIteration:
            pass

        for sort_by in SortBy:
            pair = Folder._get_item_sort_key(sort_by=sort_by)
            assert isinstance(pair, tuple) and len(pair) == 2 \
                   and isinstance(pair[0], str) and isinstance(pair[1], bool)

            if params['total_items'] != 0:
                assert pair[0] in item.keys()

        scan = Folder(factory.path, params['sort_by'], params['level'])
        get_item_sort_key = Mock(
            return_value=Folder._get_item_sort_key(params['sort_by']))
        monkeypatch.setattr(scan, '_get_item_sort_key', get_item_sort_key)

        items = list(scan.items())
        # It is also called once on .resort() at the end of scanning
        call_count = len(items) + 1
        assert call_count == get_item_sort_key.call_count
        assert get_item_sort_key.call_args_list == call_count * [
            call(params['sort_by'])
        ]
Esempio n. 2
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()