-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
144 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...core/components/metadata_store/remote_query_community/tests/test_remote_search_by_tags.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from json import dumps | ||
from unittest.mock import AsyncMock, Mock, PropertyMock, patch | ||
|
||
from pony.orm import db_session | ||
|
||
from ipv8.keyvault.crypto import default_eccrypto | ||
from ipv8.test.base import TestBase | ||
from tribler_core.components.metadata_store.db.orm_bindings.channel_node import NEW | ||
from tribler_core.components.metadata_store.db.store import MetadataStore | ||
from tribler_core.components.metadata_store.remote_query_community.remote_query_community import RemoteQueryCommunity | ||
from tribler_core.components.metadata_store.remote_query_community.settings import RemoteQueryCommunitySettings | ||
from tribler_core.components.metadata_store.remote_query_community.tests.test_remote_query_community import \ | ||
BasicRemoteQueryCommunity | ||
from tribler_core.components.tag.db.tag_db import TagDatabase | ||
from tribler_core.components.tag.db.tests.test_tag_db import Tag, TestTagDB | ||
from tribler_core.utilities.path_util import Path | ||
|
||
|
||
class TestRemoteSearchByTags(TestBase): | ||
""" In this test set we will use only one node's instance as it is sufficient | ||
for testing remote search by tags | ||
""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.metadata_store = None | ||
self.initialize(BasicRemoteQueryCommunity, 1) | ||
|
||
async def tearDown(self): | ||
if self.metadata_store: | ||
self.metadata_store.shutdown() | ||
|
||
await super().tearDown() | ||
|
||
def create_node(self, *args, **kwargs): | ||
self.metadata_store = MetadataStore( | ||
Path(self.temporary_directory()) / f"mds.db", | ||
Path(self.temporary_directory()), | ||
default_eccrypto.generate_key("curve25519"), | ||
disable_sync=True, | ||
) | ||
|
||
kwargs['metadata_store'] = self.metadata_store | ||
kwargs['tags_db'] = TagDatabase(str(Path(self.temporary_directory()) / f"tags.db")) | ||
kwargs['rqc_settings'] = RemoteQueryCommunitySettings() | ||
return super().create_node(*args, **kwargs) | ||
|
||
@property | ||
def rqc(self) -> RemoteQueryCommunity: | ||
return self.overlay(0) | ||
|
||
@patch.object(RemoteQueryCommunity, 'tags_db', new=PropertyMock(return_value=None), create=True) | ||
async def test_search_for_tags_no_db(self): | ||
# test that in case of missed `tags_db`, function `search_for_tags` returns None | ||
assert self.rqc.search_for_tags(tags=['tag']) is None | ||
|
||
@patch.object(TagDatabase, 'get_infohashes') | ||
async def test_search_for_tags_only_valid_tags(self, mocked_get_infohashes: Mock): | ||
# test that function `search_for_tags` uses only valid tags | ||
self.rqc.search_for_tags(tags=['invalid tag', 'valid_tag']) | ||
mocked_get_infohashes.assert_called_with({'valid_tag'}) | ||
|
||
@patch.object(MetadataStore, 'get_entries_threaded', new_callable=AsyncMock) | ||
async def test_process_rpc_query_no_tags(self, mocked_get_entries_threaded: AsyncMock): | ||
parameters = {'first': 0, 'infohash_set': None, 'last': 100} | ||
json = dumps(parameters).encode('utf-8') | ||
|
||
await self.rqc.process_rpc_query(json) | ||
|
||
expected_parameters = {'infohash_set': None} | ||
expected_parameters.update(parameters) | ||
mocked_get_entries_threaded.assert_called_with(**expected_parameters) | ||
|
||
async def test_process_rpc_query_with_tags(self): | ||
# TODO: add test description | ||
@db_session | ||
def fill_tags_database(): | ||
TestTagDB.add_operation_set( | ||
self.rqc.tags_db, | ||
{ | ||
b'infohash1': [ | ||
Tag(name='tag1', count=2), | ||
], | ||
b'infohash2': [ | ||
Tag(name='tag2', count=1), | ||
] | ||
}) | ||
|
||
@db_session | ||
def fill_mds(): | ||
with db_session: | ||
torrent = {"infohash": b'infohash1', "title": 'title', "tags": "", "size": 1, "status": NEW} | ||
torrent_metadata = self.rqc.mds.TorrentMetadata.from_dict(torrent) | ||
torrent_metadata.sign() | ||
|
||
fill_tags_database() | ||
fill_mds() | ||
|
||
parameters = {'first': 0, 'infohash_set': None, 'last': 100, 'tags': ['tag1', 'tag2', 'tag3']} | ||
json = dumps(parameters).encode('utf-8') | ||
|
||
# TODO: add an assertion for the results | ||
await self.rqc.process_rpc_query(json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters