-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_crawl.py
More file actions
134 lines (106 loc) · 4.81 KB
/
test_crawl.py
File metadata and controls
134 lines (106 loc) · 4.81 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
import unittest
from crawl import (
normalize_url,
get_h1_from_html,
get_first_paragraph_from_html,
get_urls_from_html,
get_images_from_html,
extract_page_data
)
class TestCrawl(unittest.TestCase):
# ---------- normalize_url ----------
def test_normalize_url_basic(self):
self.assertEqual(normalize_url("https://blog.boot.dev/path"), "blog.boot.dev/path")
def test_normalize_url_trailing_slash(self):
self.assertEqual(normalize_url("https://blog.boot.dev/path/"), "blog.boot.dev/path")
def test_normalize_url_no_scheme(self):
self.assertEqual(normalize_url("blog.boot.dev/path"), "blog.boot.dev/path")
# ---------- get_h1_from_html ----------
def test_get_h1_basic(self):
html = "<html><body><h1>Title</h1></body></html>"
self.assertEqual(get_h1_from_html(html), "Title")
def test_get_h1_missing(self):
html = "<html><body></body></html>"
self.assertEqual(get_h1_from_html(html), "")
def test_get_h1_nested(self):
html = "<html><body><div><h1>Nested</h1></div></body></html>"
self.assertEqual(get_h1_from_html(html), "Nested")
# ---------- get_first_paragraph_from_html ----------
def test_get_first_paragraph_basic(self):
html = "<html><body><p>First paragraph</p></body></html>"
self.assertEqual(get_first_paragraph_from_html(html), "First paragraph")
def test_get_first_paragraph_main_priority(self):
html = "<html><body><p>Outside</p><main><p>Main</p></main></body></html>"
self.assertEqual(get_first_paragraph_from_html(html), "Main")
def test_get_first_paragraph_multiple_p_no_main(self):
html = "<html><body><p>First</p><p>Second</p></body></html>"
self.assertEqual(get_first_paragraph_from_html(html), "First")
# ---------- get_urls_from_html ----------
def test_get_urls_absolute(self):
html = '<html><body><a href="https://blog.boot.dev"></a></body></html>'
result = get_urls_from_html(html, "https://blog.boot.dev")
self.assertEqual(result, ["https://blog.boot.dev"])
def test_get_urls_relative(self):
html = '<html><body><a href="/about"></a></body></html>'
result = get_urls_from_html(html, "https://blog.boot.dev")
self.assertEqual(result, ["https://blog.boot.dev/about"])
def test_get_urls_missing_href(self):
html = "<html><body><a>Missing href</a></body></html>"
result = get_urls_from_html(html, "https://blog.boot.dev")
self.assertEqual(result, [])
# ---------- get_images_from_html ----------
def test_get_images_relative(self):
html = '<html><body><img src="/logo.png"></body></html>'
result = get_images_from_html(html, "https://blog.boot.dev")
self.assertEqual(result, ["https://blog.boot.dev/logo.png"])
def test_get_images_absolute(self):
html = '<html><body><img src="https://cdn.site/img.png"></body></html>'
result = get_images_from_html(html, "https://blog.boot.dev")
self.assertEqual(result, ["https://cdn.site/img.png"])
def test_get_images_missing_src_alt(self):
html = "<html><body><img></body></html>"
result = get_images_from_html(html, "https://blog.boot.dev")
self.assertEqual(result, [])
# ---------- extract_page_data ----------
def test_extract_page_data_basic(self):
html = """
<html><body>
<h1>Test Title</h1>
<p>This is the first paragraph.</p>
<a href="/link1">Link 1</a>
<img src="/image1.jpg" alt="Image 1">
</body></html>
"""
url = "https://blog.boot.dev"
expected = {
"url": url,
"h1": "Test Title",
"first_paragraph": "This is the first paragraph.",
"outgoing_links": ["https://blog.boot.dev/link1"],
"image_urls": ["https://blog.boot.dev/image1.jpg"]
}
self.assertEqual(extract_page_data(html, url), expected)
def test_extract_page_data_missing(self):
html = "<html><body></body></html>"
url = "https://example.com"
expected = {
"url": url,
"h1": "",
"first_paragraph": "",
"outgoing_links": [],
"image_urls": []
}
self.assertEqual(extract_page_data(html, url), expected)
def test_extract_page_data_relative_links_images(self):
html = "<html><body><a href='/link2'></a><img src='/img2.png'></body></html>"
url = "https://example.com"
expected = {
"url": url,
"h1": "",
"first_paragraph": "",
"outgoing_links": ["https://example.com/link2"],
"image_urls": ["https://example.com/img2.png"]
}
self.assertEqual(extract_page_data(html, url), expected)
if __name__ == "__main__":
unittest.main()