write smoke_tests/test_render_scraper_script.py
This commit is contained in:
@@ -1,18 +1,23 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import ast
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def test_render_scraper_script_has_no_numeric_underscore_literals() -> None:
|
def _load_agent_module():
|
||||||
agent_path = Path(__file__).resolve().parents[1] / "agent.py"
|
agent_path = Path(__file__).resolve().parents[1] / "agent.py"
|
||||||
spec = importlib.util.spec_from_file_location("agent", agent_path)
|
spec = importlib.util.spec_from_file_location("agent", agent_path)
|
||||||
assert spec is not None and spec.loader is not None
|
assert spec is not None and spec.loader is not None
|
||||||
module = importlib.util.module_from_spec(spec)
|
module = importlib.util.module_from_spec(spec)
|
||||||
spec.loader.exec_module(module)
|
spec.loader.exec_module(module)
|
||||||
|
return module
|
||||||
|
|
||||||
script = module._render_scraper_script(
|
|
||||||
|
def _render_script() -> str:
|
||||||
|
module = _load_agent_module()
|
||||||
|
return module._render_scraper_script(
|
||||||
{
|
{
|
||||||
"url": "https://example.com/",
|
"url": "https://example.com/",
|
||||||
"selectors": {"title": "title"},
|
"selectors": {"title": "title"},
|
||||||
@@ -28,12 +33,27 @@ def test_render_scraper_script_has_no_numeric_underscore_literals() -> None:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_render_scraper_script_parses_as_python_35() -> None:
|
||||||
|
script = _render_script()
|
||||||
|
ast.parse(script, feature_version=(3, 5))
|
||||||
|
|
||||||
|
|
||||||
|
def test_render_scraper_script_has_no_forbidden_modern_syntax_markers() -> None:
|
||||||
|
script = _render_script()
|
||||||
|
|
||||||
numeric_underscore_literal = re.compile(r"(?<![A-Za-z0-9_])\d+(?:_\d+)+(?![A-Za-z0-9_])")
|
numeric_underscore_literal = re.compile(r"(?<![A-Za-z0-9_])\d+(?:_\d+)+(?![A-Za-z0-9_])")
|
||||||
|
f_string_prefix = re.compile(r"(?<![A-Za-z0-9_])(?:[fF]|[rR][fF]|[fF][rR])(['\"])")
|
||||||
|
|
||||||
assert numeric_underscore_literal.findall(script) == []
|
assert numeric_underscore_literal.findall(script) == []
|
||||||
|
assert f_string_prefix.findall(script) == []
|
||||||
|
assert "2_000_000" not in script
|
||||||
assert "response.read(2000000)" in script
|
assert "response.read(2000000)" in script
|
||||||
assert "response.read(2_000_000)" not in script
|
assert ":=" not in script
|
||||||
|
assert "pathlib" not in script
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_render_scraper_script_has_no_numeric_underscore_literals()
|
test_render_scraper_script_parses_as_python_35()
|
||||||
print("smoke ok: no numeric underscore literals; response.read(2000000) present")
|
test_render_scraper_script_has_no_forbidden_modern_syntax_markers()
|
||||||
|
print("smoke ok: rendered scraper script parses as Python 3.5 and contains no forbidden markers")
|
||||||
|
|||||||
Reference in New Issue
Block a user