-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
198 lines (161 loc) · 7.44 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import six
import unittest
if six.PY3:
from unittest import mock
next_method = '__next__'
else:
import mock
next_method = 'next'
from tweepy import API
from tweepy.models import Status, User
from twitter_email_hunter import search_emails
from twitter_email_hunter.utils import extract_email
from twitter_email_hunter.models import Tweets, Result
class ExtractEmailTestCase(unittest.TestCase):
def test_extract_email(self):
text = 'Some text test-1@example.com'
self.assertEqual(
'test-1@example.com', extract_email(text, 'example.com'))
text = 'Some text test-1@example.com with something at the end'
self.assertEqual(
'test-1@example.com', extract_email(text, 'example.com'))
text = ('Some text test-1@example.com with something at the'
'end and other email like john@example.com')
self.assertEqual(
'test-1@example.com', extract_email(text, 'example.com'))
text = ('Some text test-1@example1.com with something at the'
'end and other email like john@example.net')
self.assertEqual(
'john@example.net', extract_email(text, 'example.net'))
text = 'test-1@example.com at the begginning'
self.assertEqual(
'test-1@example.com', extract_email(text, 'example.com'))
def test_extract_with_domain_and_brackets(self):
text = 'Some text bewtween brackets (test-1@example.com)'
self.assertEqual(
'test-1@example.com', extract_email(text, 'example.com'))
text = 'Some text start with bracket (test-1@example.com'
self.assertEqual(
'test-1@example.com', extract_email(text, 'example.com'))
text = 'Some text bewtween brackets {test-1@example.com}'
self.assertEqual(
'test-1@example.com', extract_email(text, 'example.com'))
text = 'Some text bewtween brackets [test-1@example.com]'
self.assertEqual(
'test-1@example.com', extract_email(text, 'example.com'))
def test_extract_email_without_domain(self):
text = 'Some text test-1@example.com'
self.assertEqual(
'test-1@example.com', extract_email(text))
text = 'Some text test-1@example.com with something at the end'
self.assertEqual(
'test-1@example.com', extract_email(text))
# Finds first email
text = ('Some text test-1@example.com with something at the'
'end and other email like john@example.com')
self.assertEqual(
'test-1@example.com', extract_email(text))
text = 'test-1@example.com at the begginning'
self.assertEqual(
'test-1@example.com', extract_email(text))
def test_extract_without_domain_and_brackets(self):
text = 'Some text bewtween brackets (test-1@example.com)'
self.assertEqual(
'test-1@example.com', extract_email(text))
text = 'Some text start with bracket (test-1@example.com'
self.assertEqual(
'test-1@example.com', extract_email(text))
text = 'Some text bewtween brackets {test-1@example.com}'
self.assertEqual(
'test-1@example.com', extract_email(text))
text = 'Some text bewtween brackets [test-1@example.com]'
self.assertEqual(
'test-1@example.com', extract_email(text))
class TweetsIteratorTestCase(unittest.TestCase):
def test_iterator_requests_tweets_for_the_first_time(self):
with mock.patch.object(API, 'user_timeline') as m:
api = API()
mocked_tweets = [
mock.MagicMock(spec=Status, id=1),
mock.MagicMock(spec=Status, id=2),
mock.MagicMock(spec=Status, id=3),
]
m.return_value = mocked_tweets
tweets = Tweets(api=api, username="rmotr_com")
self.assertEqual(next(tweets), mocked_tweets[0])
self.assertEqual(next(tweets), mocked_tweets[1])
self.assertEqual(next(tweets), mocked_tweets[2])
m.assert_called_once_with('rmotr_com', count=200)
def test_iterator_paginates_time(self):
with mock.patch.object(API, 'user_timeline') as m:
api = API()
mocked_tweets_page_1 = [
mock.MagicMock(spec=Status, id=1),
mock.MagicMock(spec=Status, id=2),
mock.MagicMock(spec=Status, id=3),
]
m.return_value = mocked_tweets_page_1
tweets = Tweets(api=api, username="rmotr_com", count=3)
self.assertEqual(next(tweets).id, 1)
self.assertEqual(next(tweets).id, 2)
self.assertEqual(next(tweets).id, 3)
m.assert_called_once_with('rmotr_com', count=3)
m.reset_mock()
mocked_tweets_page_2 = [
mock.MagicMock(spec=Status, id=3), # API repeats lasts tweet
mock.MagicMock(spec=Status, id=4),
mock.MagicMock(spec=Status, id=5),
]
m.return_value = mocked_tweets_page_2
self.assertEqual(next(tweets).id, 4)
self.assertEqual(next(tweets).id, 5)
m.assert_called_once_with('rmotr_com', max_id=3, count=3)
class SearchEmailTestCase(unittest.TestCase):
def test_search_email_finds_email(self):
api = mock.MagicMock(spec=API)
iteration = {'counter': -1}
mocked_tweets = [
mock.MagicMock(spec=Status, id=1, text="???"),
mock.MagicMock(
spec=Status, id=2,
text="Tweet text santiago@rmotr.com.",
author=mock.MagicMock(spec=User, screen_name='rmotr_com')),
mock.MagicMock(spec=Status, id=3, text="???")
]
def _next(_self):
iteration['counter'] += 1
return mocked_tweets[iteration['counter']]
with mock.patch.object(Tweets, next_method, new=_next):
results = search_emails(api, 'rmotr_com', 'rmotr.com')
result = next(results)
self.assertIsNotNone(result)
self.assertEqual(result.tweet, mocked_tweets[1])
self.assertEqual(result.email, 'santiago@rmotr.com')
class ResultTestCase(unittest.TestCase):
def test_result_equality(self):
"A result should be equal if contains the same email"
author1 = mock.MagicMock(spec=User, screen_name='rmotr_com')
author2 = mock.MagicMock(spec=User, screen_name='santiagobasulto')
self.assertEqual(
Result(email='questions@rmotr.com',
tweet=mock.MagicMock(spec=Status, id=1,
text="???", author=author1)),
Result(email='questions@rmotr.com',
tweet=mock.MagicMock(spec=Status, id=2,
text="xxx", author=author2)),
)
tweet = mock.MagicMock(spec=Status, id=1, text="???", author=author2)
self.assertEqual(
Result(email='questions@rmotr.com', tweet=tweet),
Result(email='questions@rmotr.com', tweet=tweet),
)
self.assertNotEqual(
Result(email='questions@rmotr.com', tweet=tweet),
Result(email='santiago@rmotr.com', tweet=tweet),
)
result_list = [
Result(email='questions@rmotr.com', tweet=tweet),
Result(email='santiago@rmotr.com', tweet=tweet),
Result(email='martin@rmotr.com', tweet=tweet)
]
self.assertTrue('questions@rmotr.com' in result_list)