-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2208 from devos50/recheck_rss_feeds_endpoint
Implemented endpoint to refresh rss feeds, written tests for Tribler.Core.Modules.channel package
- Loading branch information
Showing
11 changed files
with
199 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
This package contains tests for the channel management objects. | ||
""" |
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,24 @@ | ||
from Tribler.Test.Core.base_test import TriblerCoreTest, MockObject | ||
|
||
|
||
class BaseTestChannel(TriblerCoreTest): | ||
|
||
def setUp(self, annotate=True): | ||
""" | ||
Setup some classes and files that are used by the tests in this module. | ||
""" | ||
super(BaseTestChannel, self).setUp(annotate=annotate) | ||
|
||
self.fake_session = MockObject() | ||
self.fake_session.get_state_dir = lambda: self.session_base_dir | ||
self.fake_session.add_observer = lambda a, b, c: False | ||
|
||
fake_notifier = MockObject() | ||
fake_notifier.add_observer = lambda a, b, c, d: False | ||
fake_notifier.notify = lambda a, b, c, d: False | ||
self.fake_session.notifier = fake_notifier | ||
|
||
self.fake_channel_community = MockObject() | ||
self.fake_channel_community.get_channel_id = lambda: 42 | ||
self.fake_channel_community.cid = 'a' * 20 | ||
self.fake_channel_community.get_channel_name = lambda: "my fancy channel" |
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,27 @@ | ||
from Tribler.Core.Modules.channel.channel import ChannelObject | ||
from Tribler.Core.Modules.channel.channel_rss import ChannelRssParser | ||
from Tribler.Test.Core.Modules.Channel.base_test_channel import BaseTestChannel | ||
|
||
|
||
class TestChannel(BaseTestChannel): | ||
""" | ||
This class contains some tests for the ChannelObject class. | ||
""" | ||
|
||
def setUp(self, annotate=True): | ||
""" | ||
Setup the tests by creating the ChannelObject instance. | ||
""" | ||
super(TestChannel, self).setUp(annotate=annotate) | ||
self.channel_object = ChannelObject(self.fake_session, self.fake_channel_community) | ||
|
||
def test_get_channel_id(self): | ||
self.assertEqual(self.channel_object.channel_id, 42) | ||
|
||
def test_get_channel_name(self): | ||
self.assertEqual(self.channel_object.name, "my fancy channel") | ||
|
||
def test_get_rss_feed_url_list(self): | ||
rss_parser = ChannelRssParser(self.fake_session, self.fake_channel_community, 'a') | ||
self.channel_object._rss_feed_dict['a'] = rss_parser | ||
self.assertEqual(self.channel_object.get_rss_feed_url_list(), ['a']) |
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,72 @@ | ||
import os | ||
from twisted.internet.defer import DeferredList | ||
from Tribler.Core.Modules.channel.cache import SimpleCache | ||
from Tribler.Core.Modules.channel.channel_rss import ChannelRssParser, RSSFeedParser | ||
from Tribler.Test.Core.Modules.Channel.base_test_channel import BaseTestChannel | ||
from Tribler.Test.Core.base_test import TriblerCoreTest | ||
from Tribler.Test.test_as_server import TESTS_DATA_DIR | ||
|
||
|
||
class TestChannelRss(BaseTestChannel): | ||
|
||
def setUp(self, annotate=True): | ||
""" | ||
Setup the tests by creating the ChannelRssParser instance. | ||
""" | ||
super(TestChannelRss, self).setUp(annotate=annotate) | ||
self.channel_rss = ChannelRssParser(self.fake_session, self.fake_channel_community, 'a') | ||
|
||
def test_task_scrape_no_stop(self): | ||
self.channel_rss._task_scrape() | ||
self.assertTrue(self.channel_rss.is_pending_task_active("rss_scrape")) | ||
|
||
def test_task_scrape_stop(self): | ||
self.channel_rss._to_stop = True | ||
self.channel_rss._task_scrape() | ||
self.assertFalse(self.channel_rss.is_pending_task_active("rss_scrape")) | ||
|
||
def test_initialize(self): | ||
self.channel_rss.initialize() | ||
self.assertTrue(self.channel_rss.is_pending_task_active("rss_scrape")) | ||
|
||
def test_shutdown(self): | ||
self.channel_rss.initialize() | ||
cache_path = self.channel_rss._url_cache._file_path | ||
self.channel_rss._url_cache.add('a') | ||
self.channel_rss.shutdown() | ||
self.assertTrue(os.path.exists(cache_path)) | ||
self.assertFalse(self.channel_rss.is_pending_task_active("rss_scrape")) | ||
|
||
def test_parse_rss_feed(self): | ||
self.channel_rss.rss_url = os.path.join(TESTS_DATA_DIR, 'test_rss.xml') | ||
self.channel_rss._url_cache = SimpleCache(os.path.join(self.session_base_dir, 'cache.txt')) | ||
self.assertIsInstance(self.channel_rss.parse_feed(), DeferredList) | ||
|
||
def test_parse_feed_stopped(self): | ||
self.channel_rss.rss_url = os.path.join(TESTS_DATA_DIR, 'test_rss.xml') | ||
self.channel_rss._url_cache = SimpleCache(os.path.join(self.session_base_dir, 'cache.txt')) | ||
self.channel_rss._to_stop = True | ||
self.assertIsNone(self.channel_rss.parse_feed()) | ||
|
||
|
||
class TestRssParser(TriblerCoreTest): | ||
|
||
def test_parse_html(self): | ||
parser = RSSFeedParser() | ||
self.assertEqual(parser._parse_html("<p>Hi</p>"), set()) | ||
self.assertEqual(parser._parse_html("<a href='abc'></a>"), {'abc'}) | ||
self.assertEqual(parser._parse_html("<a href='abc'><img src='def'/></a>"), {'abc', 'def'}) | ||
|
||
def test_html2plaintext(self): | ||
parser = RSSFeedParser() | ||
self.assertEqual(parser._html2plaintext("<p>test</p>"), "test\n") | ||
self.assertEqual(parser._html2plaintext("test"), "test\n") | ||
self.assertEqual(parser._html2plaintext("<p>test\ntest2</p><p>test3</p>"), "test\ntest2\ntest3\n") | ||
|
||
def test_parse(self): | ||
parser = RSSFeedParser() | ||
for rss_item in parser.parse(os.path.join(TESTS_DATA_DIR, 'test_rss.xml'), | ||
SimpleCache(os.path.join(self.session_base_dir, 'cache.txt'))): | ||
self.assertEqual(len(rss_item['thumbnail_list']), 1) | ||
self.assertEqual(rss_item['title'], "ubuntu-15.04-desktop-amd64.iso") | ||
self.assertEqual(rss_item['description'], '') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
from Tribler.Test.test_as_server import AbstractServer | ||
|
||
|
||
class MockObject(object): | ||
""" | ||
This class is used to create as base class for fake (mocked) objects. | ||
""" | ||
pass | ||
|
||
|
||
class TriblerCoreTest(AbstractServer): | ||
pass |
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