script.module.torrent_parser/tests/test_all.py

56 lines
1.9 KiB
Python
Raw Normal View History

2017-05-23 08:35:40 +03:00
from __future__ import unicode_literals
import unittest
import collections
from torrent_parser import TorrentFileParser, parse_torrent_file
class Test(unittest.TestCase):
REAL_FILE = 'tests/testfiles/real.torrent'
NEG_FILE = 'tests/testfiles/neg.torrent'
2017-05-23 08:35:40 +03:00
def test_parse_torrent_file_use_shortcut(self):
parse_torrent_file(self.REAL_FILE)
2017-05-23 08:35:40 +03:00
def test_parse_torrent_file_use_class(self):
with open(self.REAL_FILE, 'rb') as fp:
2017-05-23 08:35:40 +03:00
TorrentFileParser(fp).parse()
def test_encoding_auto(self):
with open(self.REAL_FILE, 'rb') as fp:
TorrentFileParser(fp, encoding='auto').parse()
2017-05-23 08:35:40 +03:00
def test_parse_torrent_file_to_ordered_dict(self):
data = parse_torrent_file(self.REAL_FILE, True)
2017-05-23 08:35:40 +03:00
self.assertIsInstance(data, collections.OrderedDict)
with open(self.REAL_FILE, 'rb') as fp:
2017-05-23 08:35:40 +03:00
data = TorrentFileParser(fp, True).parse()
self.assertIsInstance(data, collections.OrderedDict)
def test_parse_correctness(self):
data = parse_torrent_file(self.REAL_FILE)
2017-06-20 10:41:54 +03:00
self.assertIn(['udp://tracker.publicbt.com:80/announce'],
2017-05-23 08:35:40 +03:00
data['announce-list'])
2017-06-20 10:41:54 +03:00
self.assertEqual(data['creation date'], 1409254242)
2017-05-23 08:35:40 +03:00
def test_parse_two_times(self):
with open(self.REAL_FILE, 'rb') as fp:
2017-05-23 08:35:40 +03:00
parser = TorrentFileParser(fp)
data = parser.parse()
2017-06-20 10:41:54 +03:00
self.assertIn(['udp://tracker.publicbt.com:80/announce'],
2017-05-23 08:35:40 +03:00
data['announce-list'])
2017-06-20 10:41:54 +03:00
self.assertEqual(data['creation date'], 1409254242)
2017-05-23 08:35:40 +03:00
data = parser.parse()
2017-06-20 10:41:54 +03:00
self.assertIn(['udp://tracker.publicbt.com:80/announce'],
2017-05-23 08:35:40 +03:00
data['announce-list'])
2017-06-20 10:41:54 +03:00
self.assertEqual(data['creation date'], 1409254242)
def test_int_is_negative(self):
data = parse_torrent_file(self.NEG_FILE)
self.assertEqual(data['neg'], -1)
if __name__ == '__main__':
unittest.main()