'가장 싼 값'과 '실제로 살 수 있는 가장 싼 값'은 다르다. 리뷰·평점이 전혀 없는 오퍼는 재고 없는 미끼가격일 수 있고, 그걸 최저가로 보고하면 사용자는 그 가격에 살 수 없다 — 조금 비싼 정답보다 나쁘다. 판단 근거를 수집해 둔다. - NormalizedProduct.rating / review_count 추가. 두 소스 모두 카드에 노출하는 값만 담아 교차 비교가 되게 했다. **없으면 None 유지** — '리뷰 0개'와 '리뷰 정보 없음'은 다른 뜻이다 - 네이버: product_grade 의 <strong>평점</strong><em>리뷰수</em>. 텍스트를 통째로 정규식 돌리면 '평점4.7473' 이 4.74/73 인지 4.7/473 인지 못 가르므로 노드로 분리해 읽는다. '1.7만' 같은 축약은 parse_ko_count 로 푼다(그대로 int() 하면 1 이 된다) - 쿠팡: 별점은 채워진 별 개수가 아니라 컨테이너 aria-label 에, 리뷰 수는 괄호 텍스트에 있다 - price_history.final_rating/final_review_count 추가(+마이그레이션) → "리뷰 0인 최저가가 몇 %인가"를 SQL 로 물을 수 있다. NULL 과 0 을 구분해야 해서 기본값을 두지 않았다 정렬 점검(사용자 제기): 두 소스 다 정렬 파라미터 없이 **랭킹/추천순**이다(픽스처 가격이 오름차순이 아님으로 확인). 가격순(sort=price_asc)은 차단 없이 동작하고 실측상 더 싼 후보를 찾지만(15,400→10,900), 리뷰·평점 없는 유령상품을 위로 끌어올려 미채택 — 추천순 유지. 신뢰 신호가 쌓이면 "리뷰 N 이상" 가드를 걸고 가격순을 켜는 선택지가 열린다. e2e: TR-1/TR-2 최저가에 평점 4.89·리뷰 7,314/102,000 이 함께 기록됨. 테스트 5건 추가, 228 passed.
178 lines
8.8 KiB
Python
178 lines
8.8 KiB
Python
"""네이버 모바일 쇼핑 파서 테스트 (저장 HTML — 네트워크 불필요).
|
|
|
|
fixtures/naver_msearch.html 은 2026-08-04 실측 응답에서 카드만 추린 축약본이다
|
|
(판매처 6 · 가격비교 6 · 광고 3 · 슈퍼적립 1 · 브랜드블록 2).
|
|
|
|
가격 함정 회귀 테스트를 합성 HTML 로 따로 둔다 — 픽스처는 네이버가 마크업을 바꾸면
|
|
같이 늙지만, 함정 자체(단위가격·배송비·정상가)는 계속 지켜야 하는 계약이라서다.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
from services.search.naver_shop.adapter import detect_block
|
|
from services.search.naver_shop.parser import parse_search_html
|
|
|
|
_FIXTURE = Path(__file__).parent / "fixtures" / "naver_msearch.html"
|
|
|
|
|
|
def _fixture() -> str:
|
|
return _FIXTURE.read_text()
|
|
|
|
|
|
def _card(inner: str, cls: str = "product_item_inner__9nwv0") -> str:
|
|
return f'<html><body><div class="{cls}">{inner}</div></body></html>'
|
|
|
|
|
|
# ── 픽스처 기반 ──────────────────────────────────────────────────────────
|
|
def test_parses_organic_cards_and_drops_ads():
|
|
ps = parse_search_html(_fixture())
|
|
assert len(ps) == 12 # 광고 3 + 슈퍼적립 1 은 제외됨
|
|
assert all(p.source == "naver" for p in ps)
|
|
assert all(p.price > 0 and p.name for p in ps)
|
|
|
|
|
|
def test_seller_and_catalog_cards_are_both_kept():
|
|
ps = parse_search_html(_fixture())
|
|
catalog = [p for p in ps if p.mall_name == "네이버"] # "최저 N원" = 가격비교
|
|
seller = [p for p in ps if p.mall_name and p.mall_name != "네이버"]
|
|
assert len(catalog) == 6 and len(seller) == 6
|
|
# 판매처 카드는 실제 몰명이 들어온다(빈 문자열/None 아님)
|
|
assert all(len(p.mall_name) > 1 for p in seller)
|
|
|
|
|
|
def test_shipping_is_captured():
|
|
"""옛 오픈API 는 배송비 필드가 없어 전 소스 None 이었다 — 크롤의 이점이라 회귀로 막는다."""
|
|
ps = parse_search_html(_fixture())
|
|
assert all(p.shipping_fee is not None for p in ps)
|
|
assert {p.shipping_type for p in ps} <= {"free", "paid"}
|
|
assert any(p.shipping_type == "free" and p.shipping_fee == 0 for p in ps)
|
|
assert any(p.shipping_type == "paid" and p.shipping_fee > 0 for p in ps)
|
|
|
|
|
|
def test_price_is_never_the_shipping_fee():
|
|
"""실측에서 실제로 밟은 버그: 가격 노드가 배송비까지 감싸 3,900원이 상품가로 잡혔다."""
|
|
ps = parse_search_html(_fixture())
|
|
assert not [p for p in ps if p.shipping_fee and p.price == p.shipping_fee]
|
|
|
|
|
|
def test_detail_url_present():
|
|
ps = parse_search_html(_fixture())
|
|
assert all(p.detail_url and p.detail_url.startswith("http") for p in ps)
|
|
|
|
|
|
# ── 가격 함정 회귀 (합성) ────────────────────────────────────────────────
|
|
def test_unit_price_is_not_mistaken_for_price():
|
|
"""'21,900원(100ml당 548원)' → 21900. 단위가격을 먼저 떼지 않으면 548 을 집는다."""
|
|
html = _card("""
|
|
<div class="product_info_tit__UOCqq">테스트 섬유유연제 4L</div>
|
|
<span class="product_price__O3ZGH">
|
|
<span class="product_immediate_price__m3YQU">21,900<span class="product_unit__seYZk">원</span>
|
|
<span class="product_unit_price__uLsbL">(100ml당 548원)</span>
|
|
</span>
|
|
</span>
|
|
<span class="product_mall__gUvbk">테스트몰</span>
|
|
<a class="product_btn_link__AhZaM" href="https://x.test/1">상품 바로가기</a>""")
|
|
(p,) = parse_search_html(html)
|
|
assert p.price == 21900
|
|
|
|
|
|
def test_delivery_fee_inside_price_node_is_excluded():
|
|
"""가격비교 카드 구조: 가격 노드가 배송비를 품는다. 상품가 7,790 / 배송비 3,900 로 갈려야 한다."""
|
|
html = _card("""
|
|
<div class="product_info_tit__UOCqq">커클랜드 드라이시트, 250매, 1개</div>
|
|
<span class="product_price__O3ZGH">
|
|
<span class="product_immediate_price__m3YQU">최저7,790<span class="product_unit__seYZk">원</span>
|
|
<span class="product_unit_price__uLsbL">(1개입당 31원)</span>
|
|
<span class="product_delivery_fee__agcRS">배송비3,900원</span>
|
|
</span>
|
|
</span>
|
|
<a class="product_btn_link__AhZaM" href="https://x.test/2">상품 바로가기</a>""")
|
|
(p,) = parse_search_html(html)
|
|
assert p.price == 7790
|
|
assert p.shipping_fee == 3900 and p.shipping_type == "paid"
|
|
assert p.mall_name == "네이버" # 판매처 표기가 없는 가격비교 카드
|
|
|
|
|
|
def test_list_price_and_discount_rate_are_ignored():
|
|
"""'정상가60,000원 할인율38% 36,840원' → 36840."""
|
|
html = _card("""
|
|
<div class="product_info_tit__UOCqq">다우니 미스티크 1L 6개</div>
|
|
<span class="product_price__O3ZGH">
|
|
<del class="product_org_price__Rc5Dx">정상가60,000원</del>
|
|
<span class="product_immediate_price__m3YQU">
|
|
<em class="product_rate__41yIl">38%</em>36,840<span class="product_unit__seYZk">원</span>
|
|
</span>
|
|
</span>
|
|
<span class="product_mall__gUvbk">공식몰</span>
|
|
<a class="product_btn_link__AhZaM" href="https://x.test/3">상품 바로가기</a>""")
|
|
(p,) = parse_search_html(html)
|
|
assert p.price == 36840
|
|
|
|
|
|
def test_coupon_price_is_not_used_as_price():
|
|
"""쿠폰할인가는 조건부(1인 1회·선착순)라 실구매가 하한으로 쓰면 최저가가 왜곡된다."""
|
|
html = _card("""
|
|
<div class="product_info_tit__UOCqq">테스트 상품</div>
|
|
<span class="product_price__O3ZGH">
|
|
<span class="product_immediate_price__m3YQU">21,900<span class="product_unit__seYZk">원</span></span>
|
|
<span class="product_discount_price__lL8iq">21,680원</span>
|
|
<span class="product_discount_title__arcCm">쿠폰할인가</span>
|
|
</span>
|
|
<span class="product_mall__gUvbk">테스트몰</span>
|
|
<a class="product_btn_link__AhZaM" href="https://x.test/4">상품 바로가기</a>""")
|
|
(p,) = parse_search_html(html)
|
|
assert p.price == 21900
|
|
|
|
|
|
def test_ad_card_family_is_dropped():
|
|
html = _card("""
|
|
<div class="adProduct_info_tit__x">광고 상품</div>
|
|
<span class="adProduct_price__DN4Al"><span class="adProduct_immediate_price__q">9,900원</span></span>
|
|
<a class="adProduct_link_ad__SqeqU">광고</a>""", cls="adProduct_item_inner__abc")
|
|
assert parse_search_html(html) == []
|
|
|
|
|
|
# ── 차단 판정 ────────────────────────────────────────────────────────────
|
|
def test_detect_block_flags_captcha_and_short_html():
|
|
assert detect_block("<html>wtm_captcha</html>", 0) == "wtm_captcha"
|
|
assert detect_block("<html>비정상적인 접근입니다</html>", 0) == "비정상적인 접근"
|
|
assert detect_block("<html>" + "x" * 5000 + "</html>", 0).startswith("short_html")
|
|
# 결과가 있으면 차단이 아니다 — 짧아도 통과
|
|
assert detect_block("<html>x</html>", 3) is None
|
|
|
|
|
|
def test_detect_block_passes_real_page():
|
|
assert detect_block(_fixture(), 12) is None
|
|
|
|
|
|
# ── 신뢰 신호 ────────────────────────────────────────────────────────────
|
|
def test_trust_signals_are_captured():
|
|
"""최저가는 '살 수 있는 가장 싼 값'이어야 한다 — 평점·리뷰가 그 판단 근거다."""
|
|
ps = parse_search_html(_fixture())
|
|
rated = [p for p in ps if p.review_count]
|
|
assert rated, "리뷰가 있는 오퍼가 하나도 안 잡히면 셀렉터가 드리프트한 것"
|
|
assert all(0 < p.rating <= 5 for p in rated)
|
|
|
|
|
|
def test_missing_trust_stays_none_not_zero():
|
|
"""리뷰 정보가 없는 것과 '리뷰 0개'는 다른 뜻이다 — 0 으로 채우면 구분이 사라진다."""
|
|
ps = parse_search_html(_fixture())
|
|
ghost = [p for p in ps if p.review_count is None]
|
|
assert ghost, "픽스처에 미검증 오퍼가 있어야 이 계약을 지킬 수 있다"
|
|
assert all(p.rating is None for p in ghost)
|
|
|
|
|
|
def test_korean_abbreviated_review_count():
|
|
"""'1.7만' 을 그대로 int() 하면 1 이 된다 — 만/천 단위를 풀어야 한다."""
|
|
html = _card("""
|
|
<div class="product_info_tit__UOCqq">테스트 상품</div>
|
|
<span class="product_price__O3ZGH">
|
|
<span class="product_immediate_price__m3YQU">10,000<span class="product_unit__seYZk">원</span></span>
|
|
</span>
|
|
<div class="product_info_count__J6ElA">
|
|
<span class="product_grade__eU8gY"><span class="blind">평점</span><strong>4.88</strong><em>1.7만</em></span>
|
|
</div>
|
|
<a class="product_btn_link__AhZaM" href="https://x.test/9">상품 바로가기</a>""")
|
|
(p,) = parse_search_html(html)
|
|
assert p.rating == 4.88 and p.review_count == 17000
|