-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·168 lines (145 loc) · 7.73 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
#!/usr/bin/env python
import praw
import bot
import re
import unittest
import argparse
import sys
import os
import time
class TestRegex(unittest.TestCase):
non_link_word = "Goat"
test_links=[
("https://de.m.wikipedia.org/wiki/Luftselbstverteidigungsstreitkr%C3%A4fte",
"https://de.wikipedia.org/wiki/Luftselbstverteidigungsstreitkr%C3%A4fte",
"Wikipedia regex failed.")
]
used_regex = set()
def helper_test_regex(test_string):
links = []
for (find_regex, replace_regex) in bot.regex:
for i in re.findall(find_regex, test_string):
#ensure that every regex has it's test case
TestRegex.used_regex.add(find_regex)
links.append(i)
return links
def test_detect_mobile(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string=mobile_link
self.assertEqual(TestRegex.helper_test_regex(test_string), [mobile_link], msg)
def test_not_detect_not_mobile(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string=non_mobile_link
self.assertEqual(TestRegex.helper_test_regex(test_string), [], msg)
def test_only_detect_mobile(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string=non_mobile_link + " " + mobile_link
self.assertEqual(TestRegex.helper_test_regex(test_string), [mobile_link], msg)
def test_detect_mobile_multiple_times(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string=mobile_link + " " + non_mobile_link + " " + mobile_link
self.assertEqual(TestRegex.helper_test_regex(test_string), [mobile_link, mobile_link], msg)
def test_detect_link_encased_in_brackets(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string= "(" + mobile_link + ")" + "[" + mobile_link + "]" + "{" + mobile_link + "}"
self.assertEqual(TestRegex.helper_test_regex(test_string), [mobile_link, mobile_link, mobile_link], msg)
def test_detect_mobile_multiple_times_with_other_text(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string= (TestRegex.non_link_word + " "
+ mobile_link + " " + TestRegex.non_link_word + " " + non_mobile_link
+ " " + mobile_link + " " + TestRegex.non_link_word)
self.assertEqual(TestRegex.helper_test_regex(test_string), [mobile_link, mobile_link], msg)
def test_replace_mobile(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string=mobile_link
self.assertEqual(bot.replace_links(test_string), [non_mobile_link], msg)
def test_not_replace_not_mobile(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string=non_mobile_link
self.assertEqual(bot.replace_links(test_string), [], msg)
def test_only_replace_mobile(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string=non_mobile_link + " " + mobile_link
self.assertEqual(bot.replace_links(test_string), [non_mobile_link], msg)
def test_replace_mobile_multiple_times(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string=mobile_link + " " + non_mobile_link + " " + mobile_link
self.assertEqual(bot.replace_links(test_string), [non_mobile_link, non_mobile_link], msg)
def test_replace_link_encased_in_brackets(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string= "(" + mobile_link + ")" + "[" + mobile_link + "]" + "{" + mobile_link + "}"
self.assertEqual(bot.replace_links(test_string), [non_mobile_link, non_mobile_link, non_mobile_link], msg)
def test_replace_mobile_multiple_times_with_other_text(self):
for (mobile_link, non_mobile_link, msg) in TestRegex.test_links:
test_string= (TestRegex.non_link_word + " "
+ mobile_link + " " + TestRegex.non_link_word + " " + non_mobile_link
+ " " + mobile_link + " " + TestRegex.non_link_word)
self.assertEqual(bot.replace_links(test_string), [non_mobile_link, non_mobile_link], msg)
def test_every_regex_has_been_tested(self):
bot_regex = set()
for (find_regex, replace_regex) in bot.regex:
bot_regex.add(find_regex)
self.assertEqual(bot_regex, TestRegex.used_regex)
class TestAPI(unittest.TestCase):
#testAPI=False
already_checked = set()
#fake original set
already_done_submissions = set()
already_done_comments = set()
reddit = praw.Reddit(user_agent="Non-mobile link tester by /u/faerbit")
submission_id = ""
@classmethod
def setUpClass(cls):
#if not TestAPI.testAPI:
# TestAPI.skipTest(self, "testAPI not specified")
cls.reddit.login("non-mobile-linkbot", os.environ["NON_MOBILE_LINKBOT_PASSWORD"])
#submit a link even if the RateLimit is met
submission = praw.objects.Submission
for attempt in range(12):
try:
submission = cls.reddit.submit("test", "non-mobile test",
text="https://de.m.wikipedia.org/wiki/Luftselbstverteidigungsstreitkr%C3%A4fte")
except praw.errors.RateLimitExceeded:
time.sleep(60)
continue
break
else:
raise Exception("Could not submit test link. Is reddit down?")
submission.add_comment("https://de.m.wikipedia.org/wiki/Luftselbstverteidigungsstreitkr%C3%A4fte")
cls.submission_id=submission.id
def helper_search_for_comment(comment_text):
accepted_author="Redditor(user_name='non-mobile-linkbot')"
comments = praw.helpers.flatten_tree(praw.objects.Submission.from_id(TestAPI.reddit, TestAPI.submission_id).comments)
for comment in comments:
if (comment.id not in TestAPI.already_checked and
comment.author == accepted_author and comment_text == comment.body):
TestAPI.already_checked.add(comment.id)
return comment.body
return False
def test_reply_function(self):
submission = praw.objects.Submission.from_id(TestAPI.reddit, TestAPI.submission_id)
comment = submission.comments[0]
text = "Testing reply function"
expected_text = text + bot.disclaimer
bot.reply(comment, text)
self.assertEqual(TestAPI.helper_search_for_comment(expected_text), expected_text)
def test_worker(self):
bot.worker(TestAPI.reddit, TestAPI.already_done_comments, TestAPI.already_done_submissions, "test")
text = "Non-mobile link: https://de.m.wikipedia.org/wiki/Luftselbstverteidigungsstreitkr%C3%A4fte"
expected_text = text + bot.disclaimer
#test for submission and comment correction
self.assertEqual(TestAPI.helper_search_for_comment(expected_text), expected_text)
self.assertEqual(TestAPI.helper_search_for_comment(expected_text), expected_text)
def test_worker_processes_every_link_only_once(self):
text = "Non-mobile link: https://de.m.wikipedia.org/wiki/Luftselbstverteidigungsstreitkr%C3%A4fte"
bot.worker(TestAPI.reddit, TestAPI.already_done_comments, TestAPI.already_done_submissions, "test")
self.assertFalse(TestAPI.helper_search_for_comment(text))
if __name__ == "__main__":
#if --test RAPI is passed test Reddit API
#parser = argparse.ArgumentParser()
#parser.add_argument("--test")
#arg=parser.parse_args()
#if arg.test == "RAPI":
# TestAPI.testAPI = True
#del sys.argv[1:]
unittest.main()