Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Apr 25, 2024
1 parent b16f7da commit c292ce4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ def mock_send(**kwargs):
search_txt = "foo"
await do_request(
rest_api,
f'search/remote?fts_text={search_txt}&max_rowid=1',
'search/remote',
params={
'fts_text': search_txt,
'filter': 'bar',
'max_rowid': 1
},
request_type="PUT",
expected_code=200,
expected_json={"request_uuid": str(request_uuid), "peers": peers},
)
assert sent['txt_filter'] == f'"{search_txt}"'
assert sent['txt_filter'] == f'"{search_txt}" "bar"'
sent.clear()

# Test querying channel data by public key, e.g. for channel preview purposes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from time import time
from typing import Set
from unittest.mock import MagicMock, Mock, patch

Expand All @@ -15,15 +16,25 @@
from tribler.core.utilities.unicode import hexlify
from tribler.core.utilities.utilities import random_infohash, to_fts_query

POPULAR_ENDPOINT = "metadata/torrents/popular"


@pytest.fixture(name="needle_in_haystack_mds")
def fixture_needle_in_haystack_mds(metadata_store):
num_hay = 100

def _put_torrent_with_seeders(name):
infohash = random_infohash()
state = metadata_store.TorrentState(infohash=infohash, seeders=100, leechers=100, has_data=1,
last_check=int(time()))
metadata_store.TorrentMetadata(title=name, infohash=infohash, public_key=b'', health=state,
metadata_type=REGULAR_TORRENT)

with db_session:
for x in range(0, num_hay):
metadata_store.TorrentMetadata(title='hay ' + str(x), infohash=random_infohash(), public_key=b'')
metadata_store.TorrentMetadata(title='needle', infohash=random_infohash(), public_key=b'')
metadata_store.TorrentMetadata(title='needle2', infohash=random_infohash(), public_key=b'')
_put_torrent_with_seeders('needle 1')
_put_torrent_with_seeders('needle 2')
return metadata_store


Expand Down Expand Up @@ -83,33 +94,18 @@ async def test_check_torrent_query(rest_api):
await do_request(rest_api, f"metadata/torrents/{infohash}/health?timeout=wrong_value&refresh=1", expected_code=400)


@patch.object(DatabaseEndpoint, 'add_download_progress_to_metadata_list', Mock())
async def test_get_popular_torrents(rest_api, endpoint, metadata_store):
"""
Test that the endpoint responds with its known entries.
"""
fake_entry = {
"name": "Torrent Name",
"category": "",
"infohash": "ab" * 20,
"size": 1,
"num_seeders": 1234,
"num_leechers": 123,
"last_tracker_check": 17000000,
"created": 15000000,
"tag_processor_version": 1,
"type": REGULAR_TORRENT,
"id": 0,
"origin_id": 0,
"public_key": "ab" * 64,
"status": 2,
"statements": []
}
fake_state = Mock(return_value=Mock(get_progress=Mock(return_value=0.5)))
metadata_store.get_entries = Mock(return_value=[Mock(to_simple_dict=Mock(return_value=fake_entry.copy()))])
endpoint.download_manager.get_download = Mock(return_value=Mock(get_state=fake_state))
response = await do_request(rest_api, "metadata/torrents/popular")
""" Test that the endpoint responds with its known entries."""
response = await do_request(rest_api, POPULAR_ENDPOINT)
assert len(response['results']) == 2 # as there are two torrents with seeders and leechers

assert response == {'results': [{**fake_entry, **{"progress": 0.5}}], 'first': 1, 'last': 50}

@patch.object(DatabaseEndpoint, 'add_download_progress_to_metadata_list', Mock())
async def test_get_popular_torrents_with_filter(rest_api, endpoint, metadata_store):
""" Test that the endpoint responds with its known entries with a filter."""
response = await do_request(rest_api, POPULAR_ENDPOINT, params={'filter': '2'})
assert response['results'][0]['name'] == 'needle 2'


async def test_get_popular_torrents_filter_xxx(rest_api, endpoint, metadata_store):
Expand All @@ -136,7 +132,7 @@ async def test_get_popular_torrents_filter_xxx(rest_api, endpoint, metadata_stor
fake_state = Mock(return_value=Mock(get_progress=Mock(return_value=0.5)))
metadata_store.get_entries = Mock(return_value=[Mock(to_simple_dict=Mock(return_value=fake_entry.copy()))])
endpoint.download_manager.get_download = Mock(return_value=Mock(get_state=fake_state))
response = await do_request(rest_api, "metadata/torrents/popular", params={"hide_xxx": 1})
response = await do_request(rest_api, POPULAR_ENDPOINT, params={"hide_xxx": 1})

fake_entry["statements"] = [] # Should be stripped
assert response == {'results': [{**fake_entry, **{"progress": 0.5}}], 'first': 1, 'last': 50}
Expand Down Expand Up @@ -167,7 +163,7 @@ async def test_get_popular_torrents_no_db(rest_api, endpoint, metadata_store):
metadata_store.get_entries = Mock(return_value=[Mock(to_simple_dict=Mock(return_value=fake_entry.copy()))])
endpoint.download_manager.get_download = Mock(return_value=Mock(get_state=fake_state))
endpoint.tribler_db = None
response = await do_request(rest_api, "metadata/torrents/popular")
response = await do_request(rest_api, POPULAR_ENDPOINT)

assert response == {'results': [{**fake_entry, **{"progress": 0.5}}], 'first': 1, 'last': 50}

Expand All @@ -176,18 +172,17 @@ async def test_search(rest_api):
"""
Test a search query that should return a few new type channels
"""

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle', expected_code=200)
assert len(parsed["results"]) == 1
assert len(parsed["results"]) == 2

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=hay', expected_code=200)
assert len(parsed["results"]) == 50

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle&type=torrent', expected_code=200)
assert parsed["results"][0]['name'] == 'needle'
assert parsed["results"][0]['name'] == 'needle 2'

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle&sort_by=name', expected_code=200)
assert len(parsed["results"]) == 1
assert len(parsed["results"]) == 2


async def test_search_by_tags(rest_api):
Expand All @@ -203,7 +198,7 @@ def mocked_get_subjects_intersection(*_, objects: Set[str], **__):

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle&tags=missed_tag',
expected_code=200)
assert len(parsed["results"]) == 1
assert len(parsed["results"]) == 2


async def test_search_with_include_total_and_max_rowid(rest_api):
Expand All @@ -212,12 +207,12 @@ async def test_search_with_include_total_and_max_rowid(rest_api):
"""

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle', expected_code=200)
assert len(parsed["results"]) == 1
assert len(parsed["results"]) == 2
assert "total" not in parsed
assert "max_rowid" not in parsed

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle&include_total=1', expected_code=200)
assert parsed["total"] == 1
assert parsed["total"] == 2
assert parsed["max_rowid"] == 102

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=hay&include_total=1', expected_code=200)
Expand All @@ -234,15 +229,28 @@ async def test_search_with_include_total_and_max_rowid(rest_api):
assert len(parsed["results"]) == 19

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle&sort_by=name', expected_code=200)
assert len(parsed["results"]) == 1
assert len(parsed["results"]) == 2

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle&sort_by=name&max_rowid=20',
expected_code=200)
assert len(parsed["results"]) == 0

parsed = await do_request(rest_api, 'metadata/search/local?fts_text=needle&sort_by=name&max_rowid=200',
expected_code=200)
assert len(parsed["results"]) == 1
assert len(parsed["results"]) == 2


async def test_search_with_filter(rest_api):
""" Test search queries with a filter """
response = await do_request(
rest_api,
'metadata/search/local',
params={
'fts_text': 'needle',
'filter': '1'
},
expected_code=200)
assert response["results"][0]['name'] == 'needle 1'


async def test_completions_no_query(rest_api):
Expand Down

0 comments on commit c292ce4

Please sign in to comment.