- Add cian_state_parser.py: regex-based extractor for window._cianConfig push() entries, supports direct JSON, JSON.parse() escaped form, and optional demjson3 fallback - Extend ScrapedLot in base.py with 13 Cian-specific fields (living_area_m2, bedrooms_count, balconies_count, loggias_count, description_minhash, cadastral_number, building_cadastral_number, phones, is_homeowner, is_pro_seller, bargain_allowed, sale_type, metro_stations) - Add tests/test_cian_state_parser.py with 4 unit tests covering all parse strategies
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""Tests for cian_state_parser."""
|
|
import pytest
|
|
from app.services.scrapers.cian_state_parser import extract_state
|
|
|
|
|
|
def test_extract_state_simple_json():
|
|
html = '''
|
|
<html><body>
|
|
<script>
|
|
window._cianConfig['frontend-serp'] = window._cianConfig['frontend-serp'] || [];
|
|
window._cianConfig['frontend-serp'].push({
|
|
key: 'initialState',
|
|
value: {"results": {"offers": [{"id": 1}]}},
|
|
priority: 1
|
|
});
|
|
</script>
|
|
</body></html>
|
|
'''
|
|
state = extract_state(html, mfe='frontend-serp', key='initialState')
|
|
assert state is not None
|
|
assert state['results']['offers'][0]['id'] == 1
|
|
|
|
|
|
def test_extract_state_json_parse_form():
|
|
html = r'''
|
|
window._cianConfig['frontend-offer-card'].push({
|
|
key: 'defaultState',
|
|
value: JSON.parse("{\"offerData\":{\"offer\":{\"cianId\":123}}}"),
|
|
priority: 1
|
|
});
|
|
'''
|
|
state = extract_state(html, mfe='frontend-offer-card', key='defaultState')
|
|
assert state is not None
|
|
assert state['offerData']['offer']['cianId'] == 123
|
|
|
|
|
|
def test_extract_state_missing_returns_none():
|
|
html = "<html></html>"
|
|
assert extract_state(html, mfe='frontend-serp') is None
|
|
|
|
|
|
def test_extract_state_wrong_mfe_returns_none():
|
|
html = '''
|
|
window._cianConfig['other-mfe'].push({key: 'initialState', value: {}, priority: 1});
|
|
'''
|
|
assert extract_state(html, mfe='frontend-serp', key='initialState') is None
|