from unittest.mock import patch

def test_parse_molit_rent_fields():
    from app.listing_collector import _parse_molit_rent
    raw = {"aptNm": "OO아파트", "excluUseAr": "42.5", "deposit": "43,000",
           "monthlyRent": "0", "dealYear": "2026", "dealMonth": "5", "floor": "5"}
    d = _parse_molit_rent(raw, "아파트")
    assert d["complex_name"] == "OO아파트" and d["area"] == 42.5
    assert d["deposit"] == 43000 and d["deal_type"] == "전세" and d["deal_ym"] == "202605"

def test_parse_naver_article_fields():
    from app.listing_collector import _parse_naver_article
    raw = {"articleNo": "A123", "tradeTypeName": "전세", "dealOrWarrantPrc": "2억 9,000",
           "areaName": "42", "area2": "42.5", "floorInfo": "5/15", "articleName": "OO아파트",
           "buildingName": "OO", "cortarNo": "1159010100"}
    d = _parse_naver_article(raw)
    assert d["article_no"] == "A123" and d["deal_type"] == "전세" and d["deposit"] == 29000
    assert d["source"] == "naver"

def test_collect_naver_block_sets_naver_ok_false(monkeypatch):
    """네이버 403 → 매물수집 skip + naver_ok=False, 실거래는 계속."""
    from app import listing_collector as lc
    monkeypatch.setattr(lc, "_fetch_molit_all", lambda: 2)      # 실거래 2건 저장됨 가정
    def boom(*a, **k):
        import requests
        raise requests.RequestException("403")
    monkeypatch.setattr(lc, "_fetch_naver_all", boom)
    r = lc.collect_listings()
    assert r["naver_ok"] is False   # 안전마진(실거래)은 유지
