-
Notifications
You must be signed in to change notification settings - Fork 46
/
test_feeds.py
103 lines (83 loc) · 2.85 KB
/
test_feeds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
from irc3.testing import BotTestCase
from irc3.testing import MagicMock
from irc3.testing import asyncio
import datetime
import tempfile
import shutil
import os
def hook(entries):
return []
class Hook:
def __init__(self, bot):
pass
def __call__(self, entries):
return []
class Dispatcher:
def __init__(self, bot):
self.loop = bot.loop
self.reset()
def reset(self):
self.future = asyncio.Future(loop=self.loop)
return self.future
def __call__(self, messages):
self.future.set_result(list(messages))
class TestFeeds(BotTestCase):
name = 'irc3.plugins.feeds'
def setUp(self):
wd = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, wd)
self.wd = os.path.join(wd, 'feeds')
dt = datetime.datetime.now().strftime('%Y-%m-%dT%M:%M:OO-08:00')
self.patch_requests(
filename='tests/feed.atom',
DATE=dt,
)
def callFTU(self, **kwargs):
loop = kwargs.pop('loop', None)
config = dict(
directory=self.wd,
irc3='http://xxx',
dispatcher='tests.test_feeds.Dispatcher',
channels='#irc3', **kwargs
)
config = {
'includes': [self.name],
self.name: config
}
if loop:
config.update(loop=loop)
return super(TestFeeds, self).callFTU(**config)
def test_connection_made(self):
bot = self.callFTU()
bot.loop.call_later = MagicMock()
bot.notify('connection_made')
self.assertTrue(bot.loop.call_later.called)
def test_feed(self):
bot = self.callFTU(loop=asyncio.new_event_loop())
future = bot.feeds.dispatcher.reset()
bot.feeds.update()
bot.loop.run_until_complete(future)
assert future.result() == [
('#irc3', '[irc3] coverage https://github.com/gawel/irc3/commit/'
'ec82ae2c5f8b2954f0646a2177deb65ad9db712a')]
bot = self.callFTU(loop=asyncio.new_event_loop())
future = bot.feeds.dispatcher.reset()
bot.feeds.update()
bot.loop.run_until_complete(future)
assert future.result() == []
def test_hooked_feed(self):
bot = self.callFTU(hook='tests.test_feeds.hook',
loop=asyncio.new_event_loop())
future = bot.feeds.dispatcher.reset()
bot.feeds.update()
bot.loop.run_until_complete(future)
assert future.result() == []
def test_hooked_feed_with_class(self):
bot = self.callFTU(hook='tests.test_feeds.Hook',
loop=asyncio.new_event_loop())
assert isinstance(bot.feeds.hook, Hook)
future = bot.feeds.dispatcher.reset()
bot.feeds.update()
bot.loop.run_until_complete(future)
assert future.result() == []