-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_tests.py
More file actions
159 lines (138 loc) · 4.59 KB
/
utils_tests.py
File metadata and controls
159 lines (138 loc) · 4.59 KB
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
#!/usr/bin/env python3
"""
Tests for the utils.py lirary
"""
import unittest
import requests
import utils
__author__ = "Shawn Carter"
__version__ = "Fall 2021"
__pylint__ = "v1.8.3"
class FakeResponse:
#pylint: disable=too-few-public-methods
"""
A fake response object used for providing a consistent input to
the utils module.
"""
text = ""
def get(url):
#pylint: disable=unused-argument
#pylint: disable=no-self-argument
#pylint: disable=no-self-use
"""
Creates a FakeResponse with multiple anchor tags as the text
Args: url - Unused variable to match the requests get() function
Return: A FakeResponse with text for testing
"""
response = FakeResponse()
text = "<a href='https://www.example.com'></a>"
text += "<a href='www.example.org'></a>"
text += "<a href='tel:1234567890'></a>"
text += "<a href='mailto:example@example.com'></a>"
text += "<a href='/dir/page.html'></a>"
text += "<a href='#id'></a>"
text += "<a href='javascript:void(0);'></a>"
response.text = text
return response
class TestGetLinksFromUrl(unittest.TestCase):
"""
Tests for the get_links_from_url() function
"""
def test_valid_response_text(self):
"""
Checks if a valid response is scraped correctly
"""
url = "https://www.example.com"
links = utils.get_links_from_url(url, FakeResponse)
expected_links = [
"www.example.com",
"www.example.org",
"tel:1234567890",
"mailto:example@example.com",
"/dir/page.html",
"#id",
"javascript:void(0);",
]
self.assertEqual(links, expected_links)
def test_request_handler_lacks_get_function(self):
"""
Checks that an AttributeError is raised if request_handler
lacks a get() function
"""
url = "https://www.example.com"
with self.assertRaises(AttributeError):
utils.get_links_from_url(url, True)
def test_none_for_url(self):
"""
Checks that a MissingSchema exception is raised if None is used
as the url
"""
with self.assertRaises(requests.exceptions.MissingSchema):
utils.get_links_from_url(None)
def test_empty_string(self):
"""
Checks that a MissingSchema exception is raised if an empty string
is used for the url
"""
with self.assertRaises(requests.exceptions.MissingSchema):
utils.get_links_from_url("")
def test_url_without_protocol(self):
"""
Checks that a MissingSchema exception is raised if the url is missing
the protocol
"""
url = "www.example.com"
with self.assertRaises(requests.exceptions.MissingSchema):
utils.get_links_from_url(url)
class TestGetStyleForLink(unittest.TestCase):
"""
Tests for the get_style_for_link() function
"""
def test_absolute_href(self):
"""
Checks for a valid return for an absolute url
"""
url = "www.example.com"
expected = ""
actual = utils.get_style_for_link(url)
self.assertEqual(expected, actual)
def test_phone_href(self):
"""
Checks for a valid return for a phone url
"""
url = "tel:1234567890"
expected = " style='color: #faa;'"
actual = utils.get_style_for_link(url)
self.assertEqual(expected, actual)
def test_email_href(self):
"""
Checks for a valid return for an absolute url
"""
url = "mailto:example@example.com"
expected = " style='color: #f9f;'"
actual = utils.get_style_for_link(url)
self.assertEqual(expected, actual)
def test_relative_href(self):
"""
Checks for a valid return for an absolute url
"""
url = "/dir/page.html"
expected = " style='color: goldenrod;'"
actual = utils.get_style_for_link(url)
self.assertEqual(expected, actual)
def test_id_href(self):
"""
Checks for a valid return for an absolute url
"""
url = "#id"
expected = " style='color: #9f9;'"
actual = utils.get_style_for_link(url)
self.assertEqual(expected, actual)
def test_javascript_href(self):
"""
Checks for a valid return for an absolute url
"""
url = "javascript:void(0);"
expected = " style='color: #aaf;'"
actual = utils.get_style_for_link(url)
self.assertEqual(expected, actual)