-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
222 lines (199 loc) · 7.5 KB
/
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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from collections import OrderedDict
from unittest import TestCase
class TestEncoder(TestCase):
"""
Encoder Tests
"""
def test_check_simple_encoder(self):
"""
Check: Is Storage with 1 entry generates a valid sitemap ?
"""
from backends.encoders import SitemapEncoder
from backends.storage import UrlStorage
storage = UrlStorage(None)
storage.register_url("http://example.com/", {
"loc": "http://example.com/"
})
dumped_result = SitemapEncoder(None).dumps(storage, indent=0)
expected = '<?xml version="1.0" encoding="UTF-8"?>' \
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' \
'<url>' \
'<loc>http://example.com/</loc>' \
'</url>' \
'</urlset>'
self.assertEqual(expected, dumped_result)
def test_check_location_override(self):
"""
Check: Does location overloading while registering works ?
"""
from backends.encoders import SitemapEncoder
from backends.storage import UrlStorage
storage = UrlStorage(None)
storage.register_url("http://example.com/", {
"loc": "http://example.com/index.html"
})
dumped_result = SitemapEncoder(None).dumps(storage, indent=0)
expected = '<?xml version="1.0" encoding="UTF-8"?>' \
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' \
'<url>' \
'<loc>http://example.com/index.html</loc>' \
'</url>' \
'</urlset>'
self.assertEqual(expected, dumped_result)
def test_check_no_location(self):
"""
Check: Does default location is the same as url ?
"""
from backends.encoders import SitemapEncoder
from backends.storage import UrlStorage
storage = UrlStorage(None)
storage.register_url("http://example.com/")
dumped_result = SitemapEncoder(None).dumps(storage, indent=0)
expected = '<?xml version="1.0" encoding="UTF-8"?>' \
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' \
'<url>' \
'<loc>http://example.com/</loc>' \
'</url>' \
'</urlset>'
self.assertEqual(expected, dumped_result)
def test_check_encode_with_props(self):
"""
Check: Does different props registered correctly ?
"""
from backends.encoders import SitemapEncoder
from backends.storage import UrlStorage
storage = UrlStorage(None)
storage.register_url("http://example.com/", OrderedDict(
(
("priority", 1),
("loc", "http://example.com/"),
)
))
dumped_result = SitemapEncoder(None).dumps(storage, indent=0)
expected = '<?xml version="1.0" encoding="UTF-8"?>' \
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' \
'<url>' \
'<priority>1</priority>' \
'<loc>http://example.com/</loc>' \
'</url>' \
'</urlset>'
self.assertEqual(expected, dumped_result)
class TestStorage(TestCase):
"""
Storage Tests
"""
def test_check_storage_add(self):
"""
Check: Can add multiple addresses ?
"""
from backends.storage import UrlStorage
storage = UrlStorage(None)
storage.register_url("http://example.com/")
storage.register_url("http://example.com/aa.html")
storage.register_url("http://example.com/bb.html")
self.assertEqual(3, len(list(storage.urls.values())))
def test_check_storage_remove(self):
"""
Check: Can remove addresses ?
"""
from backends.storage import UrlStorage
storage = UrlStorage(None)
storage.register_url("http://example.com/")
storage.register_url("http://example.com/aa.html")
storage.unregister_url("http://example.com/aa.html")
self.assertEqual(1, len(list(storage.urls.values())))
def test_check_storage_remove_nonexisting(self):
"""
Check: Can remove non-existing addresses ?
"""
from backends.storage import UrlStorage
storage = UrlStorage(None)
storage.register_url("http://example.com/")
storage.register_url("http://example.com/aa.html")
try:
storage.unregister_url("http://example.com/bb.html")
except KeyError:
pass
else:
assert "No exception raised"
def test_iter_all_links(self):
"""
Check: Can iterate all addresses right?
"""
from backends.storage import UrlStorage
storage = UrlStorage(None)
links = [
"http://example.com/",
"http://example.com/aa.html",
"http://example.com/bb.html",
"http://example.com/cc/dd.html",
]
for link in links:
storage.register_url(link)
for link in storage:
links.pop(links.index(link))
self.assertEqual([], links)
class TestParser(TestCase):
"""
Parser Tests
"""
def test_parse_links(self):
"""
Check: Can parse a absolute url?
"""
from main import WebCrawler
base_url = "http://example.com/"
html = '<html>' \
'<a href="http://example.com/aa.html"></a>' \
'</html>'
crawler = WebCrawler()
links = crawler.parse_links(base_url, html)
self.assertEqual(links, ['http://example.com/aa.html'])
def test_parse_relative_links(self):
"""
Check: Can convert a relative link in to absolute link correctly?
"""
from main import WebCrawler
base_url = "http://example.com/"
html = '<html>' \
'<a href="/aa.html"></a>' \
'</html>'
crawler = WebCrawler()
links = crawler.parse_links(base_url, html)
self.assertEqual(links, ['http://example.com/aa.html'])
def test_parse_inner_root_links(self):
"""
Check: Can convert a relative root link in to absolute link correctly?
"""
from main import WebCrawler
base_url = "http://example.com/aa/bb/vv"
html = '<html>' \
'<a href="/aa.html"></a>' \
'</html>'
crawler = WebCrawler()
links = crawler.parse_links(base_url, html)
self.assertEqual(links, ['http://example.com/aa.html'])
def test_parse_inner_parent_links(self):
"""
Check: Can convert a relative parent link in to absolute link correctly?
"""
from main import WebCrawler
base_url = "http://example.com/aa/bb/"
html = '<html>' \
'<a href="../cc.html"></a>' \
'</html>'
crawler = WebCrawler()
links = crawler.parse_links(base_url, html)
self.assertEqual(links, ['http://example.com/aa/cc.html'])
def test_parse_external_links(self):
"""
Check: Can keep a full external link?
"""
from main import WebCrawler
base_url = "http://example.com/aa/bb/"
html = '<html>' \
'<a href="http://google.com/cc.html"></a>' \
'</html>'
crawler = WebCrawler()
links = crawler.parse_links(base_url, html)
self.assertEqual(links, ["http://google.com/cc.html"])