Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 6, 2022
1 parent b0217f3 commit 8e3aa11
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import json
import struct
from asyncio import Future
from typing import List, Optional, Set

from binascii import unhexlify
from pony.orm import db_session
from pony.orm.dbapiprovider import OperationalError
from typing import List, Optional, Set

from ipv8.lazy_community import lazy_wrapper
from ipv8.messaging.lazy_payload import VariablePayload, vp_compile
from ipv8.requestcache import NumberCache, RandomNumberCache, RequestCache

from pony.orm import db_session
from pony.orm.dbapiprovider import OperationalError

from tribler_core.components.ipv8.tribler_community import TriblerCommunity
from tribler_core.components.metadata_store.db.orm_bindings.channel_metadata import LZ4_EMPTY_ARCHIVE, entries_to_chunk
from tribler_core.components.metadata_store.db.serialization import CHANNEL_TORRENT, COLLECTION_NODE, REGULAR_TORRENT
Expand All @@ -18,7 +19,7 @@
from tribler_core.components.metadata_store.remote_query_community.payload_checker import ObjState
from tribler_core.components.metadata_store.remote_query_community.settings import RemoteQueryCommunitySettings
from tribler_core.components.metadata_store.utils import RequestTimeoutException
from tribler_core.components.tag.community.tag_validator import is_valid_tag, validate_tag
from tribler_core.components.tag.community.tag_validator import is_valid_tag
from tribler_core.utilities.unicode import hexlify

BINARY_FIELDS = ("infohash", "channel_pk")
Expand Down Expand Up @@ -192,9 +193,15 @@ async def process_rpc_query(self, json_bytes: bytes):
:raises pony.orm.dbapiprovider.OperationalError: if an illegal query was performed.
"""
parameters = json.loads(json_bytes)
request_sanitized = sanitize_query(parameters, self.rqc_settings.max_response_size)
parameters['infohash_set'] = await self.mds.run_threaded(self.search_for_tags, parameters.get('tags'))
return await self.mds.get_entries_threaded(**request_sanitized)
sanitized_parameters = sanitize_query(parameters, self.rqc_settings.max_response_size)

# tags should be extracted because `get_entries_threaded` doesn't expect them as a parameter
tags = sanitized_parameters.pop('tags', None)

infohash_set = await self.mds.run_threaded(self.search_for_tags, tags)
sanitized_parameters['infohash_set'] = infohash_set # it could be None, it is expected

return await self.mds.get_entries_threaded(**sanitized_parameters)

@db_session
def search_for_tags(self, tags: Optional[List[str]]) -> Optional[Set[bytes]]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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 pony.orm import db_session

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.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
Expand All @@ -24,11 +26,14 @@ class TestRemoteSearchByTags(TestBase):
def setUp(self):
super().setUp()
self.metadata_store = None
self.tags_db = None
self.initialize(BasicRemoteQueryCommunity, 1)

async def tearDown(self):
if self.metadata_store:
self.metadata_store.shutdown()
if self.tags_db:
self.tags_db.shutdown()

await super().tearDown()

Expand All @@ -39,9 +44,10 @@ def create_node(self, *args, **kwargs):
default_eccrypto.generate_key("curve25519"),
disable_sync=True,
)
self.tags_db = TagDatabase(str(Path(self.temporary_directory()) / f"tags.db"))

kwargs['metadata_store'] = self.metadata_store
kwargs['tags_db'] = TagDatabase(str(Path(self.temporary_directory()) / f"tags.db"))
kwargs['tags_db'] = self.tags_db
kwargs['rqc_settings'] = RemoteQueryCommunitySettings()
return super().create_node(*args, **kwargs)

Expand All @@ -62,6 +68,7 @@ async def test_search_for_tags_only_valid_tags(self, mocked_get_infohashes: Mock

@patch.object(MetadataStore, 'get_entries_threaded', new_callable=AsyncMock)
async def test_process_rpc_query_no_tags(self, mocked_get_entries_threaded: AsyncMock):
# test that in case of missed tags, the remote search works like normal remote search
parameters = {'first': 0, 'infohash_set': None, 'last': 100}
json = dumps(parameters).encode('utf-8')

Expand All @@ -72,7 +79,9 @@ async def test_process_rpc_query_no_tags(self, mocked_get_entries_threaded: Asyn
mocked_get_entries_threaded.assert_called_with(**expected_parameters)

async def test_process_rpc_query_with_tags(self):
# TODO: add test description
# this is full test that checked whether search by tags works or not
#
# Test assumes that two databases were filled by the following data (TagsDatabase and MDS):
@db_session
def fill_tags_database():
TestTagDB.add_operation_set(
Expand All @@ -89,15 +98,24 @@ def fill_tags_database():
@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()
def _add(infohash):
torrent = {"infohash": infohash, "title": 'title', "tags": "", "size": 1, "status": NEW}
self.rqc.mds.TorrentMetadata.from_dict(torrent)

_add(b'infohash1')
_add(b'infohash2')
_add(b'infohash3')

fill_tags_database()
fill_mds()

# Then we try to query search for three tags: 'tag1', 'tag2', 'tag3'
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)
with db_session:
query_results = [r.to_dict() for r in await self.rqc.process_rpc_query(json)]

# Expected results: only one infohash (b'infohash1') should be returned.
result_infohash_list = [r['infohash'] for r in query_results]
assert result_infohash_list == [b'infohash1']

0 comments on commit 8e3aa11

Please sign in to comment.