|
1 | 1 | (ns webscraping.core-test |
2 | 2 | (:require [clojure.test :refer :all] |
3 | 3 | [clojure.pprint :as pp] |
4 | | - [webscraping.core :refer :all])) |
| 4 | + [clojure.string :as str] ;; Added for string checks |
| 5 | + [webscraping.core :refer :all]) |
| 6 | + (:import (org.jsoup Jsoup))) ;; Added Jsoup import |
5 | 7 |
|
6 | | -(deftest a-test |
7 | | - (testing |
8 | | - "Fetch my personal website and check number key/values in results" |
| 8 | +(deftest mark-watson-website-test |
| 9 | + (testing "Fetch Mark Watson's website and verify basic anchor extraction" |
9 | 10 | (let [page-data (fetch-web-page-data "https://markwatson.com")] |
10 | | - (pp/pprint page-data)))) |
| 11 | + (is (string? (:page-text page-data))) |
| 12 | + (is (not (str/blank? (:page-text page-data)))) |
| 13 | + (is (vector? (:anchors page-data))) |
| 14 | + (is (pos? (count (:anchors page-data)))) |
| 15 | + (let [anchors (:anchors page-data)] |
| 16 | + (is (some #(str/includes? (:text %) "Read My Blog on Blogspot") anchors) |
| 17 | + "Expected to find an anchor with 'Read My Blog on Blogspot' in its text") |
| 18 | + (is (some #(= (:uri %) "https://mark-watson.blogspot.com/") anchors) |
| 19 | + "Expected to find an anchor linking to https://mark-watson.blogspot.com/") |
| 20 | + |
| 21 | + (is (some #(str/includes? (str/lower-case (:text %)) "clojure") anchors) |
| 22 | + "Expected to find an anchor with 'Clojure' (case-insensitive) in its text") |
| 23 | + (is (some #(= (:uri %) "https://leanpub.com/clojureai") anchors) |
| 24 | + "Expected to find an anchor linking to Leanpub Clojure AI book") |
| 25 | + |
| 26 | + ;; Add a check for one more reasonably stable link, e.g., "My Books" |
| 27 | + (is (some #(and (str/includes? (:text %) "My Books") |
| 28 | + (= (:uri %) "https://markwatson.com#books")) |
| 29 | + anchors) |
| 30 | + "Expected to find an anchor 'My Books' linking to '#books'"))))) |
| 31 | + |
| 32 | +(deftest no-anchors-test |
| 33 | + (testing "Page with no anchor tags" |
| 34 | + (let [html-doc (Jsoup/parse "<html><body><p>No links here.</p></body></html>") |
| 35 | + anchors (get-html-anchors html-doc)] |
| 36 | + (is (empty? anchors) "Expected no anchors from HTML with no links")))) |
| 37 | + |
| 38 | +(deftest relative-and-absolute-uris-test |
| 39 | + (testing "Anchor URI resolution for relative and absolute paths" |
| 40 | + (let [base-uri "http://example.com/docs/" |
| 41 | + html-content "<html><body><a href=\"/page1\">Page 1</a> <a href=\"http://domain.com/page2\">Page 2</a> <a href=\"../page3\">Page 3</a> <a href=\"sub/page4\">Page 4</a><a href=\"page5.html\">Page 5</a></body></html>" |
| 42 | + html-doc (. Jsoup parse html-content base-uri) |
| 43 | + anchors (get-html-anchors html-doc) |
| 44 | + uris (set (map :uri anchors))] |
| 45 | + (is (contains? uris "http://example.com/page1") "Relative /page1 should resolve to http://example.com/page1") |
| 46 | + (is (contains? uris "http://domain.com/page2") "Absolute http://domain.com/page2 should remain unchanged") |
| 47 | + (is (contains? uris "http://example.com/page3") "Relative ../page3 should resolve to http://example.com/page3") |
| 48 | + (is (contains? uris "http://example.com/docs/sub/page4") "Relative sub/page4 should resolve to http://example.com/docs/sub/page4") |
| 49 | + (is (contains? uris "http://example.com/docs/page5.html") "Relative page5.html should resolve to http://example.com/docs/page5.html")))) |
0 commit comments