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
|