first setup, travel works, bjornd api works
This commit is contained in:
53
tests/cache.py
Normal file
53
tests/cache.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
cache.py — import this before anything else in a test file to enable
|
||||
file-based caching of fetch_json and fetch_soup calls.
|
||||
|
||||
Cache is stored in tests/cache/ keyed by a hash of the URL + params.
|
||||
Delete the cache directory to bust it.
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import pickle
|
||||
from pathlib import Path
|
||||
|
||||
CACHE_DIR = Path(__file__).parent / "cache"
|
||||
CACHE_DIR.mkdir(exist_ok=True)
|
||||
|
||||
|
||||
def _key(url: str, params: dict[str,str] | None) -> str:
|
||||
raw = json.dumps({"url": url, "params": params or {}}, sort_keys=True)
|
||||
return hashlib.sha256(raw.encode()).hexdigest()
|
||||
|
||||
|
||||
def _patch():
|
||||
import adapters.api as api_mod
|
||||
import adapters.ssr as ssr_mod
|
||||
|
||||
_orig_fetch_json = api_mod.fetch_json
|
||||
_orig_fetch_soup = ssr_mod.fetch_soup
|
||||
|
||||
def cached_fetch_json(url, *, params: dict[str,str]|None=None, headers=None):
|
||||
path = CACHE_DIR / (_key(url, params) + ".json")
|
||||
if path.exists():
|
||||
print(f"[cache hit] {url}")
|
||||
return json.loads(path.read_text())
|
||||
result = _orig_fetch_json(url, params=params, headers=headers)
|
||||
path.write_text(json.dumps(result))
|
||||
return result
|
||||
|
||||
def cached_fetch_soup(url, *, params=None):
|
||||
path = CACHE_DIR / (_key(url, params) + ".pickle")
|
||||
if path.exists():
|
||||
print(f"[cache hit] {url}")
|
||||
return pickle.loads(path.read_bytes())
|
||||
result = _orig_fetch_soup(url, params=params)
|
||||
path.write_bytes(pickle.dumps(result))
|
||||
return result
|
||||
|
||||
api_mod.fetch_json = cached_fetch_json
|
||||
ssr_mod.fetch_soup = cached_fetch_soup
|
||||
print("[cache] fetch_json and fetch_soup patched")
|
||||
|
||||
|
||||
_patch()
|
||||
17
tests/test_adapters.py
Normal file
17
tests/test_adapters.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import sys
|
||||
sys.path.insert(0, "../src")
|
||||
|
||||
from cache import * # noqa: F401 — must be before adapter imports
|
||||
|
||||
from adapters import SCRAPERS
|
||||
|
||||
|
||||
# --- change this to test a different adapter ---
|
||||
ADAPTER = SCRAPERS['bjornd']
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Testing adapter: {ADAPTER.__name__}")
|
||||
listings = ADAPTER()
|
||||
print(f"Got {len(listings)} listings\n")
|
||||
for l in listings:
|
||||
print(f" {l.adres}, {l.stad} — €{l.prijs} — {l.url}")
|
||||
26
tests/test_email.py
Normal file
26
tests/test_email.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import sys
|
||||
sys.path.insert(0, "../src")
|
||||
|
||||
from huizenbot import notify_email, RawListing
|
||||
|
||||
TEST_LISTING = RawListing(
|
||||
url="https://example.com/test-woning",
|
||||
source_makelaar="test",
|
||||
adres="Teststraat 1",
|
||||
stad="Delft",
|
||||
postcode="2613AA",
|
||||
prijs=350000,
|
||||
hero_image_url=None,
|
||||
)
|
||||
|
||||
TEST_TRAVEL = {
|
||||
"fiets_mark": 20,
|
||||
"fiets_michelle": 35,
|
||||
"ov_mark": 30,
|
||||
"ov_michelle": 45,
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("=== Email ===")
|
||||
notify_email(TEST_LISTING, TEST_TRAVEL)
|
||||
print(" verstuurd (check je inbox)")
|
||||
0
tests/test_ha.py
Normal file
0
tests/test_ha.py
Normal file
24
tests/test_travel.py
Normal file
24
tests/test_travel.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import sys
|
||||
sys.path.insert(0, "../src")
|
||||
from huizenbot import fiets_minuten, ov_minuten, geocode
|
||||
from config import *
|
||||
|
||||
# --- change these to test different postcodes ---
|
||||
POSTCODE_FROM = "3028GE"
|
||||
POSTCODE_FROM_9292 = "rotterdam/" + POSTCODE_FROM
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("=== Geocode ===")
|
||||
origin = geocode(POSTCODE_FROM)
|
||||
dest = geocode(MARK_WERK_POSTCODE)
|
||||
print(f" {POSTCODE_FROM} → {origin}")
|
||||
print(f" {MARK_WERK_POSTCODE} → {dest}")
|
||||
if not origin or not dest:
|
||||
print("Geocode mislukt, stop.")
|
||||
sys.exit(1)
|
||||
|
||||
print("\n=== Fiets (OSRM) ===")
|
||||
print(f" {fiets_minuten(origin, dest)} minuten")
|
||||
|
||||
print("\n=== OV (9292) ===")
|
||||
print(f" {ov_minuten(POSTCODE_FROM_9292, MARK_WERK_9292)} minuten")
|
||||
Reference in New Issue
Block a user