-
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.
Added search request & response payload structure
- Loading branch information
Showing
4 changed files
with
232 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import random | ||
import string | ||
from unittest import TestCase | ||
|
||
from Tribler.community.popular.payload import SearchResponsePayload, SearchResponseItemPayload | ||
from Tribler.pyipv8.ipv8.messaging.serialization import Serializer | ||
|
||
|
||
class TestSerializer(TestCase): | ||
|
||
def setUp(self): | ||
self.serializer = Serializer() | ||
|
||
def test_search_result_payload_serialization(self): | ||
""" | ||
Test serialization & deserialization of search payload | ||
:return: | ||
""" | ||
|
||
def random_string(size=6, chars=string.ascii_uppercase + string.digits): | ||
return ''.join(random.choice(chars) for _ in range(size)) | ||
|
||
def random_infohash(): | ||
return ''.join(random.choice('0123456789abcdef') for _ in range(20)) | ||
|
||
# sample search response items | ||
sample_items = [] | ||
for index in range(10): | ||
infohash = random_infohash() | ||
name = random_string() | ||
length = random.randint(1000, 9999) | ||
num_files = random.randint(1, 10) | ||
category_list = ['video', 'audio'] | ||
creation_date = random.randint(1000000, 111111111) | ||
seeders = random.randint(10, 200) | ||
leechers = random.randint(5, 1000) | ||
cid = random_string(size=20) | ||
|
||
sample_items.append(SearchResponseItemPayload(infohash, name, length, num_files, category_list, | ||
creation_date, seeders, leechers, cid)) | ||
|
||
# Search identifier | ||
identifier = 111 | ||
|
||
# Serialize the results | ||
results = '' | ||
for item in sample_items: | ||
results += self.serializer.pack_multiple(item.to_pack_list()) | ||
serialized_results = self.serializer.pack_multiple(SearchResponsePayload(identifier, results).to_pack_list()) | ||
|
||
# De-serialize the response payload and check the identifier and get the results | ||
response_format = SearchResponsePayload.format_list | ||
(search_results, _) = self.serializer.unpack_multiple(response_format, serialized_results) | ||
|
||
# De-serialize each individual search result items | ||
item_format = SearchResponseItemPayload.format_list | ||
(all_items, _) = self.serializer.unpack_multiple_as_list(item_format, search_results[1]) | ||
for index in xrange(len(all_items)): | ||
response_item = SearchResponseItemPayload.from_unpack_list(*all_items[index]) | ||
sample_item = sample_items[index] | ||
|
||
self.assertEqual(sample_item.infohash, response_item.infohash) | ||
self.assertEqual(sample_item.name, response_item.name) | ||
self.assertEqual(sample_item.length, response_item.length) | ||
self.assertEqual(sample_item.num_files, response_item.num_files) | ||
self.assertEqual(sample_item.creation_date, response_item.creation_date) | ||
self.assertEqual(sample_item.category_list, response_item.category_list) | ||
self.assertEqual(sample_item.seeders, response_item.seeders) | ||
self.assertEqual(sample_item.leechers, response_item.leechers) | ||
self.assertEqual(sample_item.cid, response_item.cid) |
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
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