1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
from __future__ import annotations
from typing import cast
import pytest
from parsel import Selector
from parsel.selector import _NOT_SET
class TestJMESPath:
def test_json_has_html(self) -> None:
"""Sometimes the information is returned in a json wrapper"""
data = """
{
"content": [
{
"name": "A",
"value": "a"
},
{
"name": {
"age": 18
},
"value": "b"
},
{
"name": "C",
"value": "c"
},
{
"name": "<a>D</a>",
"value": "<div>d</div>"
}
],
"html": "<div><a>a<br>b</a>c</div><div><a>d</a>e<b>f</b></div>"
}
"""
sel = Selector(text=data)
assert (
sel.jmespath("html").get()
== "<div><a>a<br>b</a>c</div><div><a>d</a>e<b>f</b></div>"
)
assert sel.jmespath("html").xpath("//div/a/text()").getall() == ["a", "b", "d"]
assert sel.jmespath("html").css("div > b").getall() == ["<b>f</b>"]
assert cast("int", sel.jmespath("content").jmespath("name.age").get()) == 18
def test_html_has_json(self) -> None:
html_text = """
<div>
<h1>Information</h1>
<content>
{
"user": [
{
"name": "A",
"age": 18
},
{
"name": "B",
"age": 32
},
{
"name": "C",
"age": 22
},
{
"name": "D",
"age": 25
}
],
"total": 4,
"status": "ok"
}
</content>
</div>
"""
sel = Selector(text=html_text)
assert sel.xpath("//div/content/text()").jmespath("user[*].name").getall() == [
"A",
"B",
"C",
"D",
]
assert sel.xpath("//div/content").jmespath("user[*].name").getall() == [
"A",
"B",
"C",
"D",
]
assert cast("int", sel.xpath("//div/content").jmespath("total").get()) == 4
def test_jmestpath_with_re(self) -> None:
html_text = """
<div>
<h1>Information</h1>
<content>
{
"user": [
{
"name": "A",
"age": 18
},
{
"name": "B",
"age": 32
},
{
"name": "C",
"age": 22
},
{
"name": "D",
"age": 25
}
],
"total": 4,
"status": "ok"
}
</content>
</div>
"""
sel = Selector(text=html_text)
assert sel.xpath("//div/content/text()").jmespath("user[*].name").re(
r"(\w+)"
) == ["A", "B", "C", "D"]
assert sel.xpath("//div/content").jmespath("user[*].name").re(r"(\w+)") == [
"A",
"B",
"C",
"D",
]
with pytest.raises(TypeError):
sel.xpath("//div/content").jmespath("user[*].age").re(r"(\d+)")
assert sel.xpath("//div/content").jmespath("unavailable").re(r"(\d+)") == []
assert (
sel.xpath("//div/content").jmespath("unavailable").re_first(r"(\d+)")
is None
)
assert sel.xpath("//div/content").jmespath("user[*].age.to_string(@)").re(
r"(\d+)"
) == ["18", "32", "22", "25"]
def test_json_types(self) -> None:
for text, root in (
("{}", {}),
('{"a": "b"}', {"a": "b"}),
("[]", []),
('["a"]', ["a"]),
('""', ""),
("0", 0),
("1", 1),
("true", True),
("false", False),
("null", None),
):
selector = Selector(text=text, root=_NOT_SET)
assert selector.type == "json"
assert selector._text == text
assert selector.root == root
selector = Selector(text=None, root=root)
assert selector.type == "json"
assert selector._text is None
assert selector.root == root
|