From 89caada79668edee11e28ab131c1f934c8c7d739 Mon Sep 17 00:00:00 2001 From: Martijn de Vos Date: Wed, 27 Oct 2021 10:12:15 +0200 Subject: [PATCH] Avoid using default category/filters in tests --- .../category_filter/category.py | 8 ++-- .../category_filter/family_filter.py | 3 +- .../category_filter/tests/test_category.py | 37 ++++++++++++------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/tribler-core/tribler_core/components/metadata_store/category_filter/category.py b/src/tribler-core/tribler_core/components/metadata_store/category_filter/category.py index 82fc41bb4b0..34d2b12fcd3 100644 --- a/src/tribler-core/tribler_core/components/metadata_store/category_filter/category.py +++ b/src/tribler-core/tribler_core/components/metadata_store/category_filter/category.py @@ -35,15 +35,17 @@ class Category: __size_change = 1024 * 1024 _logger = logging.getLogger("Category") - category_info = getCategoryInfo(CATEGORY_CONFIG_FILE) - category_info.sort(key=cmp_to_key(cmp_rank)) + def __init__(self, xxx_filter=default_xxx_filter): + self.category_info = getCategoryInfo(CATEGORY_CONFIG_FILE) + self.category_info.sort(key=cmp_to_key(cmp_rank)) + self.xxx_filter = xxx_filter def calculateCategory(self, torrent_dict, display_name): """ Calculate the category for a given torrent_dict of a torrent file. :return a list of categories this torrent belongs to. """ - is_xxx = default_xxx_filter.isXXXTorrent( + is_xxx = self.xxx_filter.isXXXTorrent( files_list=recursive_unicode(torrent_dict[b'info']["files"] if "files" in torrent_dict[b'info'] else []), torrent_name=torrent_dict[b'info'].get(b"name", b'').decode('utf-8'), tracker=torrent_dict[b'info'].get(b"announce", b'').decode('utf-8')) diff --git a/src/tribler-core/tribler_core/components/metadata_store/category_filter/family_filter.py b/src/tribler-core/tribler_core/components/metadata_store/category_filter/family_filter.py index 69ce82167dc..a3386352034 100644 --- a/src/tribler-core/tribler_core/components/metadata_store/category_filter/family_filter.py +++ b/src/tribler-core/tribler_core/components/metadata_store/category_filter/family_filter.py @@ -35,7 +35,8 @@ def initTerms(filename): class XXXFilter: _logger = logging.getLogger("XXXFilter") - xxx_terms, xxx_searchterms = initTerms(termfilename) + def __init__(self): + self.xxx_terms, self.xxx_searchterms = initTerms(termfilename) def _getWords(self, string): return [a.lower() for a in WORDS_REGEXP.findall(string)] diff --git a/src/tribler-core/tribler_core/components/metadata_store/category_filter/tests/test_category.py b/src/tribler-core/tribler_core/components/metadata_store/category_filter/tests/test_category.py index 4b807d21e82..14d0a48a166 100644 --- a/src/tribler-core/tribler_core/components/metadata_store/category_filter/tests/test_category.py +++ b/src/tribler-core/tribler_core/components/metadata_store/category_filter/tests/test_category.py @@ -1,36 +1,47 @@ -from tribler_core.components.metadata_store.category_filter.category import cmp_rank, default_category_filter -from tribler_core.components.metadata_store.category_filter.family_filter import default_xxx_filter +import pytest +from tribler_core.components.metadata_store.category_filter.category import Category, cmp_rank +from tribler_core.components.metadata_store.category_filter.family_filter import XXXFilter -default_xxx_filter.xxx_terms.add("term1") +@pytest.fixture(name="xxx_filter") +def fixture_xxx_filter(): + return XXXFilter() -def test_get_category_names(): - assert len(default_category_filter.category_info) == 10 +@pytest.fixture(name="category_filter") +def fixture_category_filter(xxx_filter): + return Category(xxx_filter=xxx_filter) -def test_calculate_category_multi_file(): + +def test_get_category_names(category_filter): + assert len(category_filter.category_info) == 10 + + +def test_calculate_category_multi_file(category_filter): torrent_info = {b"info": {b"files": [{b"path": [b"my", b"path", b"video.avi"], b"length": 1234}]}, b"announce": b"http://tracker.org", b"comment": b"lorem ipsum"} - assert default_category_filter.calculateCategory(torrent_info, "my torrent") == "VideoClips" + assert category_filter.calculateCategory(torrent_info, "my torrent") == "VideoClips" -def test_calculate_category_single_file(): +def test_calculate_category_single_file(category_filter): torrent_info = {b"info": {b"name": b"my_torrent", b"length": 1234}, b"announce-list": [[b"http://tracker.org"]], b"comment": b"lorem ipsum"} - assert default_category_filter.calculateCategory(torrent_info, "my torrent"), "other" + assert category_filter.calculateCategory(torrent_info, "my torrent"), "other" -def test_calculate_category_xxx(): +def test_calculate_category_xxx(category_filter, xxx_filter): + xxx_filter.xxx_terms.add("term1") torrent_info = {b"info": {b"name": b"term1", b"length": 1234}, b"announce-list": [[b"http://tracker.org"]], b"comment": b"lorem ipsum"} - assert default_category_filter.calculateCategory(torrent_info, "my torrent") == "xxx" + assert category_filter.calculateCategory(torrent_info, "my torrent") == "xxx" -def test_calculate_category_invalid_announce_list(): +def test_calculate_category_invalid_announce_list(category_filter, xxx_filter): + xxx_filter.xxx_terms.add("term1") torrent_info = {b"info": {b"name": b"term1", b"length": 1234}, b"announce-list": [[]], b"comment": b"lorem ipsum"} - assert default_category_filter.calculateCategory(torrent_info, "my torrent") == "xxx" + assert category_filter.calculateCategory(torrent_info, "my torrent") == "xxx" def test_cmp_rank():