This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
utils_test.py
125 lines (103 loc) · 4.71 KB
/
utils_test.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
#!/usr/bin/python
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at: http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distrib-
# uted under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
# specific language governing permissions and limitations under the License.
"""Unit tests for utils.py."""
import datetime
import time
import test_utils
import utils
class UtilsSetAndTestTests(test_utils.BaseTest):
def setUp(self):
super(UtilsSetAndTestTests, self).setUp()
self.set_called = 0
self.test_called = 0
self.total_sleep_time = 0
def MockSleep(self, seconds):
self.total_sleep_time += seconds
def MockSet(self):
self.set_called += 1
def MockTestTrue(self):
self.test_called += 1
return True
def MockTestFalse(self):
self.test_called += 1
return False
def testSetAndTest_testPasses(self):
self.mox.stubs.Set(time, 'sleep', self.MockSleep)
self.assertTrue(utils.SetAndTest(self.MockSet, self.MockTestTrue))
self.assertEqual(1, self.set_called)
self.assertEqual(1, self.test_called)
def testSetAndTest_testFails(self):
self.mox.stubs.Set(time, 'sleep', self.MockSleep)
self.assertFalse(
utils.SetAndTest(self.MockSet, self.MockTestFalse, num_tries=5))
self.assertEqual(1, self.set_called)
self.assertEqual(5, self.test_called)
def testSetAndTest_DefaultSleepTime(self):
# by default, total sleep time should be around 1 second
self.mox.stubs.Set(time, 'sleep', self.MockSleep)
self.assertFalse(
utils.SetAndTest(self.MockSet, self.MockTestFalse))
self.assertTrue(0.9 < self.total_sleep_time < 1.1)
def testSetAndTest_noSleep(self):
def RaisingSleep(unused_seconds):
raise ValueError
self.mox.stubs.Set(time, 'sleep', RaisingSleep)
self.assertFalse(utils.SetAndTest(
self.MockSet, self.MockTestFalse, num_tries=5, sleep_delta=0))
def testIsValidEmail(self):
self.assertTrue(utils.IsValidEmail('[email protected]'))
self.assertTrue(utils.IsValidEmail('[email protected]'))
self.assertTrue(utils.IsValidEmail('[email protected]'))
self.assertFalse(utils.IsValidEmail('a@b@c'))
self.assertFalse(utils.IsValidEmail('@domain.com'))
self.assertFalse(utils.IsValidEmail('domain.com'))
self.assertFalse(utils.IsValidEmail('user'))
self.assertFalse(utils.IsValidEmail('a@b'))
self.assertFalse(utils.IsValidEmail('a@b.'))
def testStripHtmlTags(self):
self.assertEqual('ac', utils.StripHtmlTags('a<b>c</b>'))
self.assertEqual('link', utils.StripHtmlTags('<a href="URL">link</a>'))
self.assertEqual('justsomejs', utils.StripHtmlTags(
'just<script>some</script>js'))
# Keeps entity and charrefs.
self.assertEqual('foo & bar', utils.StripHtmlTags('foo & bar'))
self.assertEqual('foo { bar', utils.StripHtmlTags('foo { bar'))
self.assertEqual('foo ø bar', utils.StripHtmlTags('foo ø bar'))
def testStripHtmlTags_whitelist(self):
# Keep whitelisted tags, but remove the rest
self.assertEqual('a<b>c</b>d', utils.StripHtmlTags('a<b>c</b><br>d',
tag_whitelist=['b']))
# Remove attributes even from the whitelisted tags
self.assertEqual(
'a<b>c</b>',
utils.StripHtmlTags('a<b attr="window.alert(\'ha\')">c</b>',
tag_whitelist=['b']))
def testStripHtmlTags_subChar(self):
# Replace tags with a specific character
self.assertEqual('a c d', utils.StripHtmlTags('a<b>c</b><br>d',
tag_sub=' '))
def testUtcToTimestamp(self):
dt = datetime.datetime(2013, 11, 19, 12, 58, 24)
self.assertEqual(dt, utils.TimestampToUtc(utils.UtcToTimestamp(dt)))
dt = datetime.datetime(2013, 11, 19, 12, 58, 24, 123)
self.assertEqual(dt, utils.TimestampToUtc(utils.UtcToTimestamp(dt)))
def testShortAge(self):
now = datetime.datetime.utcnow()
seconds = lambda s: datetime.timedelta(seconds=s)
self.assertEqual('just now', utils.ShortAge(now + seconds(100)))
self.assertEqual('just now', utils.ShortAge(now))
self.assertEqual('just now', utils.ShortAge(now - seconds(58)))
self.assertEqual('2m ago', utils.ShortAge(now - seconds(115)))
self.assertEqual('2h ago', utils.ShortAge(now - seconds(110 * 60)))
self.assertEqual('4d ago', utils.ShortAge(now - seconds(4 * 24 * 3500)))
if __name__ == '__main__':
test_utils.main()