|
1 | | -import httpx, pytest, respx |
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | +import respx |
| 4 | + |
2 | 5 | from main import crawl_one, run |
3 | 6 |
|
| 7 | + |
4 | 8 | @pytest.mark.asyncio |
5 | 9 | @respx.mock |
6 | 10 | async def test_crawl_one_merges_contact(monkeypatch): |
7 | 11 | monkeypatch.setattr("ginio.in_robots", lambda url: True) |
8 | | - respx.get("https://site.test/").mock(return_value=httpx.Response(200, text=""" |
| 12 | + respx.get("https://site.test/").mock( |
| 13 | + return_value=httpx.Response( |
| 14 | + 200, |
| 15 | + text=""" |
9 | 16 | <a href="/contact">Contact</a> |
10 | 17 | |
11 | | - """)) |
12 | | - respx.get("https://site.test/contact").mock(return_value=httpx.Response(200, text=""" |
| 18 | + """, |
| 19 | + ) |
| 20 | + ) |
| 21 | + respx.get("https://site.test/contact").mock( |
| 22 | + return_value=httpx.Response( |
| 23 | + 200, |
| 24 | + text=""" |
13 | 25 | <p>[email protected] 123 456 789</p> |
14 | | - """)) |
| 26 | + """, |
| 27 | + ) |
| 28 | + ) |
15 | 29 | async with httpx.AsyncClient() as client: |
16 | 30 | out = await crawl_one("https://site.test/", client) |
17 | 31 | assert "[email protected]" in out[ "emails"] and "[email protected]" in out[ "emails"] |
18 | 32 | assert any("123" in p for p in out["phones"]) |
19 | 33 |
|
| 34 | + |
20 | 35 | @pytest.mark.asyncio |
21 | 36 | @respx.mock |
22 | 37 | async def test_run_full(monkeypatch): |
23 | | - respx.get("https://serpapi.com/search").mock(return_value=httpx.Response( |
24 | | - 200, json={"organic_results":[{"link":"https://a.pl"}, {"link":"https://b.pl"}]} |
25 | | - )) |
| 38 | + respx.get("https://serpapi.com/search").mock( |
| 39 | + return_value=httpx.Response( |
| 40 | + 200, |
| 41 | + json={ |
| 42 | + "organic_results": [{"link": "https://a.pl"}, {"link": "https://b.pl"}] |
| 43 | + }, |
| 44 | + ) |
| 45 | + ) |
26 | 46 | # stub robots + strony |
27 | 47 | monkeypatch.setattr("ginio.in_robots", lambda url: True) |
28 | | - respx.get("https://a.pl").mock(return_value=httpx.Response(200, text="<title>A</title>")) |
29 | | - respx.get("https://b.pl").mock(return_value=httpx.Response(200, text="<title>B</title>")) |
30 | | - monkeypatch.setenv("SERPAPI_KEY","x") |
| 48 | + respx.get("https://a.pl").mock( |
| 49 | + return_value=httpx.Response(200, text="<title>A</title>") |
| 50 | + ) |
| 51 | + respx.get("https://b.pl").mock( |
| 52 | + return_value=httpx.Response(200, text="<title>B</title>") |
| 53 | + ) |
| 54 | + monkeypatch.setenv("SERPAPI_KEY", "x") |
31 | 55 | results = await run("foo") |
32 | | - assert {r["title"] for r in results} == {"A","B"} |
| 56 | + assert {r["title"] for r in results} == {"A", "B"} |
0 commit comments