Skip to content

Commit e772c41

Browse files
ci(fmt): black+isort [skip ci]
1 parent 66ca1d3 commit e772c41

File tree

6 files changed

+96
-29
lines changed

6 files changed

+96
-29
lines changed

scripts/to_xlsx.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import sys, pandas as pd
2-
from openpyxl.styles import Font, Alignment
1+
import sys
2+
3+
import pandas as pd
4+
from openpyxl.styles import Alignment, Font
35
from openpyxl.utils import get_column_letter
46

57

tests/test_crawl_run.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
1-
import httpx, pytest, respx
1+
import httpx
2+
import pytest
3+
import respx
4+
25
from main import crawl_one, run
36

7+
48
@pytest.mark.asyncio
59
@respx.mock
610
async def test_crawl_one_merges_contact(monkeypatch):
711
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="""
916
<a href="/contact">Contact</a>
1017
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="""
1325
<p>[email protected] 123 456 789</p>
14-
"""))
26+
""",
27+
)
28+
)
1529
async with httpx.AsyncClient() as client:
1630
out = await crawl_one("https://site.test/", client)
1731
assert "[email protected]" in out["emails"] and "[email protected]" in out["emails"]
1832
assert any("123" in p for p in out["phones"])
1933

34+
2035
@pytest.mark.asyncio
2136
@respx.mock
2237
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+
)
2646
# stub robots + strony
2747
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")
3155
results = await run("foo")
32-
assert {r["title"] for r in results} == {"A","B"}
56+
assert {r["title"] for r in results} == {"A", "B"}

tests/test_excel.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
from pathlib import Path
2+
23
import pandas as pd
4+
35
from main import save_results
46

7+
58
def test_save_results_creates_files(tmp_path, monkeypatch):
6-
root_out = tmp_path/"wyniki"
7-
data = [{"url":"https://x","title":"X","emails":["a@x"],"phones":["123"],"contact_url":None}]
9+
root_out = tmp_path / "wyniki"
10+
data = [
11+
{
12+
"url": "https://x",
13+
"title": "X",
14+
"emails": ["a@x"],
15+
"phones": ["123"],
16+
"contact_url": None,
17+
}
18+
]
819
from main import write_excel as real_write_excel
20+
921
xlsx_called = {}
22+
1023
def fake_write(csv, xlsx):
11-
xlsx_called["csv"] = csv; xlsx_called["xlsx"] = xlsx
24+
xlsx_called["csv"] = csv
25+
xlsx_called["xlsx"] = xlsx
1226
return real_write_excel(csv, xlsx)
27+
1328
monkeypatch.setattr("ginio.write_excel", fake_write)
1429
csv_p, xlsx_p = save_results(data, "20250101_000000", root_out)
1530
assert Path(csv_p).exists() and Path(xlsx_p).exists()
1631
df = pd.read_excel(xlsx_p)
17-
assert set(df.columns) >= {"url","title","emails","phones","contact_url"}
32+
assert set(df.columns) >= {"url", "title", "emails", "phones", "contact_url"}

tests/test_gui.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
11
from unittest.mock import patch
22

3+
34
def test_ensure_api_key_env(monkeypatch):
4-
monkeypatch.setenv("SERPAPI_KEY","secret")
5+
monkeypatch.setenv("SERPAPI_KEY", "secret")
56
from main import ensure_api_key
6-
assert ensure_api_key()=="secret"
7+
8+
assert ensure_api_key() == "secret"
9+
710

811
def test_ensure_api_key_prompt(tmp_path, monkeypatch):
912
monkeypatch.setenv("APPDATA", str(tmp_path))
1013
from main import ensure_api_key
14+
1115
with patch("ginio.simpledialog.askstring", return_value="abc"):
1216
key = ensure_api_key()
13-
assert key=="abc"
17+
assert key == "abc"
18+
1419

1520
def test_start_calls_run_without_threading(monkeypatch):
1621
import app_gui as g
22+
1723
g.build_ui()
18-
g.entry_query.delete(0,'end'); g.entry_query.insert(0,"kawa")
19-
monkeypatch.setenv("SERPAPI_KEY","x")
24+
g.entry_query.delete(0, "end")
25+
g.entry_query.insert(0, "kawa")
26+
monkeypatch.setenv("SERPAPI_KEY", "x")
27+
2028
class DummyThread:
21-
def __init__(self, target, daemon): self.target=target
22-
def start(self): self.target()
29+
def __init__(self, target, daemon):
30+
self.target = target
31+
32+
def start(self):
33+
self.target()
34+
2335
monkeypatch.setattr("ginio_gui.threading.Thread", DummyThread)
24-
monkeypatch.setattr("ginio_gui.run", lambda q: [{"url":"u","title":"t","emails":[],"phones":[],"contact_url":None}])
36+
monkeypatch.setattr(
37+
"ginio_gui.run",
38+
lambda q: [
39+
{"url": "u", "title": "t", "emails": [], "phones": [], "contact_url": None}
40+
],
41+
)
2542
with patch.object(g, "messagebox"):
2643
g.start()
2744
assert g.btn_start["state"] == "normal"

tests/test_net.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import httpx, pytest, respx
1+
import httpx
2+
import pytest
3+
import respx
4+
25
from main import fetch
36

7+
48
@pytest.mark.asyncio
59
@respx.mock
610
async def test_fetch_ok(monkeypatch):
711
monkeypatch.setattr("ginio.in_robots", lambda url: True)
8-
respx.get("https://x.test/ok").mock(return_value=httpx.Response(200, text="<h1>ok</h1>"))
12+
respx.get("https://x.test/ok").mock(
13+
return_value=httpx.Response(200, text="<h1>ok</h1>")
14+
)
915
async with httpx.AsyncClient() as client:
1016
html = await fetch("https://x.test/ok", client)
1117
assert "ok" in html
1218

19+
1320
@pytest.mark.asyncio
1421
@respx.mock
1522
async def test_fetch_respects_robots(monkeypatch):

tests/test_parse.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from main import parse_info, absolutize
1+
from main import absolutize, parse_info
2+
23

34
def test_absolutize():
45
assert absolutize("https://ex.com/dir/", "../a") == "https://ex.com/a"
56

7+
68
def test_parse_info_extracts_contact():
79
html = """
810
<html><head><title> ACME </title></head>

0 commit comments

Comments
 (0)