forked from Sinar/sinar_notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sinar_notifier.py
113 lines (87 loc) · 3.85 KB
/
test_sinar_notifier.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
104
105
106
107
108
109
110
111
112
113
import unittest
from bot import TwitterNotifier, FortuneNotifier, AnnouncementNotifier
from mock import Mock, patch, call
__author__ = 'lowks'
class SinarNotifierTests(unittest.TestCase):
@patch("__builtin__.open")
@patch("twitter.Api.VerifyCredentials")
@patch("yaml.load")
def setUp(self, mock_load_config, mock_twitter, mock_open):
mock_open.return_value = "config.yaml"
self.base_auth = dict(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token_key',
access_token_secret='access_token_secret')
self.statuses = dict(status=['Status1', 'Status2'], motd='I am the MOTD')
def load_config(filename):
if "config.yaml" in filename:
return self.base_auth
elif "statuses.yaml" in filename:
return self.statuses
mock_load_config.side_effect = load_config
mock_twitter.return_value = True
self.bot = TwitterNotifier()
self.fortune_notifier = FortuneNotifier()
def tearDown(self):
pass
def test_generate_message_not_implemented(self):
"""Assert that generate_message has not been implemented yet"""
with self.assertRaises(NotImplementedError):
self.bot.generate_message()
@patch("twitter.Api.VerifyCredentials")
def test_generate_message_exception(self, mock_twitter):
"""Assert that generate_message Exception: Fail Credentials"""
mock_twitter.return_value = False
with self.assertRaises(Exception):
self.bot.generate_message()
def test_notify_exception(self):
"""Assert that when generate_message
is empty notify will generate Exception
"""
self.bot.message = ""
with self.assertRaises(Exception):
self.bot.notify()
@patch("bot.TwitterNotifier.generate_message")
@patch("twitter.Api.PostUpdate")
def test_notify(self, mock_twitter, mock_twitter_notifier_message):
"""Assert that when generate_message \
is empty notify will generate Exception
"""
self.bot.message = "gugu"
self.bot.notify()
self.assertIn(call('gugu'), mock_twitter.mock_calls)
@patch("__builtin__.open")
@patch("twitter.Api.VerifyCredentials")
@patch("yaml.load")
def test_fortune_notifier(self, mock_load_config, mock_twitter, mock_open):
"""Assert that FortuneNotifier works"""
mock_open.return_value = "config.yaml"
def load_config(filename):
if "config.yaml" in filename:
return self.base_auth
elif "statuses.yaml" in filename:
return self.statuses
mock_load_config.side_effect = load_config
self.fortune_notifier = FortuneNotifier()
mock_open.return_value = "statuses.yaml"
self.fortune_notifier.generate_message()
self.assertIn(self.fortune_notifier.message,
['Status1', 'Status2'])
@patch("__builtin__.open")
@patch("twitter.Api.VerifyCredentials")
@patch("yaml.load")
def test_announcement_notifier(self, mock_load_config,
mock_twitter, mock_open):
"""Assert that AnnouncementNotifier works"""
mock_open.return_value = "config.yaml"
def load_config(filename):
if "config.yaml" in filename:
return self.base_auth
elif "statuses.yaml" in filename:
return self.statuses
mock_load_config.side_effect = load_config
self.announcement_notifier = AnnouncementNotifier()
mock_open.return_value = "statuses.yaml"
self.announcement_notifier.generate_message()
self.assertEqual(self.announcement_notifier.message,
'I am the MOTD')