|
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | 4 | import json |
| 5 | +import sys |
| 6 | +from unittest.mock import patch |
5 | 7 |
|
6 | 8 | import pandas as pd |
7 | 9 |
|
8 | 10 | from website_profiling.reporting.optional_audits import ( |
9 | 11 | amp_audit_issues, |
10 | 12 | apply_optional_audits, |
| 13 | + html_validation_issues, |
11 | 14 | pagination_issues, |
| 15 | + spell_check_issues, |
12 | 16 | ) |
13 | 17 |
|
14 | 18 |
|
@@ -38,12 +42,37 @@ def test_amp_audit_missing_canonical(): |
38 | 42 | assert len(issues) == 1 |
39 | 43 |
|
40 | 44 |
|
41 | | -def test_apply_optional_audits_spell_skipped_without_package(): |
| 45 | +def test_spell_check_issues_missing_pyspellchecker(): |
| 46 | + df = pd.DataFrame([ |
| 47 | + { |
| 48 | + "url": "https://example.com/typo", |
| 49 | + "status": "200", |
| 50 | + "content_excerpt": "This sentense has many misspelled wrds that should trigger heuristics.", |
| 51 | + }, |
| 52 | + ]) |
| 53 | + with patch.dict(sys.modules, {"spellchecker": None}): |
| 54 | + issues, skip = spell_check_issues(df) |
| 55 | + assert issues == [] |
| 56 | + assert skip and "pyspellchecker" in skip |
| 57 | + |
| 58 | + |
| 59 | +def test_html_validation_issues_missing_html5lib(): |
| 60 | + html = "<html><head><title>A</title></head><body>" + ("x" * 120) + "</body></html>" |
| 61 | + df = pd.DataFrame([{"url": "https://example.com", "html": html}]) |
| 62 | + with patch.dict(sys.modules, {"html5lib": None}): |
| 63 | + issues, use_parser = html_validation_issues(df) |
| 64 | + assert use_parser is False |
| 65 | + assert isinstance(issues, list) |
| 66 | + |
| 67 | + |
| 68 | +def test_apply_optional_audits_spell_skipped_without_package(capsys): |
42 | 69 | categories = [ |
43 | 70 | {"id": "technical_seo", "name": "Technical", "issues": [], "recommendations": []}, |
44 | 71 | {"id": "intelligence", "name": "Content", "issues": [], "recommendations": []}, |
45 | 72 | {"id": "html_accessibility", "name": "A11y", "issues": [], "recommendations": []}, |
46 | 73 | ] |
47 | 74 | df = pd.DataFrame([{"url": "https://example.com", "status": "200", "page_analysis": "{}"}]) |
48 | | - meta = apply_optional_audits(categories, df, {"enable_spell_check": "true"}) |
49 | | - assert isinstance(meta, dict) |
| 75 | + with patch.dict(sys.modules, {"spellchecker": None}): |
| 76 | + meta = apply_optional_audits(categories, df, {"enable_spell_check": "true"}) |
| 77 | + assert meta["spell_check_skipped"] == "pyspellchecker not installed (pip install -r requirements.txt)" |
| 78 | + assert "pyspellchecker" in capsys.readouterr().err |
0 commit comments