forked from vilmibm/seathree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
166 lines (143 loc) · 6.84 KB
/
tests.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import mock
from mock import Mock
from unittest2 import TestCase
import daemon
class TestTweetAuthoring(TestCase):
def setUp(self):
self.streamer = daemon.TranslationStreamer(*daemon.get_twitter_auth())
self.mock_tc = Mock()
self.streamer.set_twitter_client(self.mock_tc)
def test_one_tweet(self):
details = {
'user': {
'screen_name': 'foo',
},
'translated_text': 'bar'
}
self.streamer.tweet(details)
self.mock_tc.update_status.assert_called_once_with(status='.@foo: bar')
def test_n_tweets(self):
details = {
'user': {
'screen_name': 'foo',
},
'translated_text': 'Once upon a time long long ago in a galaxy far far away away away some things happend to some people of varying aspects and intents and gerbils wondered why cosmonauts drank iced kool-aid from the cups of woe, the cups of prison, the cups of prospect, the cups of ultimate eventuality'
}
self.streamer.tweet(details)
self.assertEqual(self.mock_tc.update_status.call_count, 3)
calls = [
mock.call.update_status(status='.@foo: Once upon a time long long ago in a galaxy far far away away away some things happend to some people of varying aspects and...'),
mock.call.update_status(status='intents and gerbils wondered why cosmonauts drank iced kool-aid from the cups of woe, the cups of prison, the cups of prospect, the cups...'),
mock.call.update_status(status='of ultimate eventuality')
]
self.mock_tc.assert_has_calls(calls)
def test_short_second(self):
details = {
'user': {
'screen_name': 'nate_smith',
},
'translated_text': 'the universe, with its elegant endowment of shelves, of inexhaustible stairways for the traveler and latrines can only be the work of a god.'
}
self.streamer.tweet(details)
self.assertEqual(self.mock_tc.update_status.call_count, 2)
calls = [
mock.call.update_status(status='.@nate_smith: the universe, with its elegant endowment of shelves, of inexhaustible stairways for the traveler and latrines can only be...'),
mock.call.update_status(status='the work of a god.'),
]
self.mock_tc.assert_has_calls(calls)
class TestDetectOriginalTweet(TestCase):
def setUp(self):
self.streamer = daemon.TranslationStreamer(*daemon.get_twitter_auth())
def test_retweet(self):
data = {
'user': {
'screen_name': 'foo'
},
'retweeted_status':{'stuff':'things'},
'text': 'some retweet'
}
self.assertFalse(self.streamer.is_original_tweet(data))
def test_not_tweet(self):
data = {
'not': 'a tweet'
}
self.assertFalse(self.streamer.is_original_tweet(data))
def test_original_tweet(self):
data = {
'user':{
'screen_name': 'foobar'
},
'text': 'a tweet'
}
self.assertTrue(self.streamer.is_original_tweet(data))
class TestSigils(TestCase):
def setUp(self):
self.streamer = daemon.TranslationStreamer(*daemon.get_twitter_auth())
def testMarkSigils(self):
tweet = "@so_and_so look at this link http://some.link.com #awesome isn't it cool?"
expected_text = "XZ0 look at this link XZ1 XZ2 isn't it cool?"
expected_replaced = [
"@so_and_so",
"http://some.link.com",
"#awesome",
]
marked_text, replaced = self.streamer.mark_sigils(tweet)
self.assertEqual(marked_text, expected_text)
self.assertEqual(replaced, expected_replaced)
def test_restore_sigils(self):
marked_text = "XZ0 look at this link XZ1 XZ2 isn't it cool?"
replaced = [
"@so_and_so",
"http://some.link.com",
"#awesome",
]
expected_restored = "@so_and_so look at this link http://some.link.com #awesome isn't it cool?"
restored_text = self.streamer.restore_sigils(marked_text, replaced)
self.assertEqual(restored_text, expected_restored)
class TestOnSuccess(TestCase):
def setUp(self):
self.streamer = daemon.TranslationStreamer(*daemon.get_twitter_auth())
self.mock_tc = Mock()
self.mock_gc = Mock()
self.streamer.set_twitter_client(self.mock_tc)
self.streamer.set_google_client(self.mock_gc)
def test_not_a_tweet(self):
data = {
'some':'thing'
}
self.streamer.on_success(data)
self.assertEqual(self.mock_tc.update_status.call_count, 0)
self.assertEqual(self.mock_gc.translations.call_count, 0)
def test_a_tweet(self):
data = {
'user': {
'screen_name': 'foo',
},
'text': 'bar @someone #awesome http://www.foobarbaz.quux'
}
self.mock_gc.translations().list().execute.return_value = {
'translations': [{'translatedText':'quux XZ0 XZ1 XZ2'}]
}
self.mock_gc.translations.reset_mock()
self.streamer.on_success(data)
self.assertEqual(self.mock_gc.translations.call_count, 1)
self.mock_tc.update_status.assert_called_once_with(status='.@foo: quux @someone #awesome http://www.foobarbaz.quux')
def test_n_tweets(self):
data = {
'user': {
'screen_name': 'foo',
},
'text': 'Once upon a time long long ago in a galaxy far far away away away some things happend to some people of varying aspects and intents and gerbils wondered why cosmonauts drank iced kool-aid from the cups of woe, the cups of prison, the cups of prospect, the cups of ultimate eventuality'
}
self.mock_gc.translations().list().execute.return_value = {
'translations': [{'translatedText':'Thrice upon a time long long ago in a galaxy far far away away away some things happend to some people of varying aspects and intents and gerbils wondered why cosmonauts drank iced kool-aid from the cups of woe, the cups of prison, the cups of prospect, the cups of ultimate eventuality'}]
}
self.mock_gc.translations.reset_mock()
self.streamer.on_success(data)
self.assertEqual(self.mock_gc.translations.call_count, 1)
calls = [
mock.call.update_status(status='.@foo: Thrice upon a time long long ago in a galaxy far far away away away some things happend to some people of varying aspects and...'),
mock.call.update_status(status='intents and gerbils wondered why cosmonauts drank iced kool-aid from the cups of woe, the cups of prison, the cups of prospect, the cups...'),
mock.call.update_status(status='of ultimate eventuality')
]
self.mock_tc.assert_has_calls(calls)