Spaces:
Running
Running
File size: 8,782 Bytes
94ec243 | 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | import re
import pytest
from unittest.mock import Mock
from scrapling import Selector, Selectors
from scrapling.core.custom_types import TextHandler, TextHandlers
from scrapling.core.storage import SQLiteStorageSystem
class TestSelectorAdvancedFeatures:
"""Test advanced Selector features like adaptive matching"""
def test_adaptive_initialization_with_storage(self):
"""Test adaptive initialization with custom storage"""
html = "<html><body><p>Test</p></body></html>"
# Use the actual SQLiteStorageSystem for this test
selector = Selector(
content=html,
adaptive=True,
storage=SQLiteStorageSystem,
storage_args={"storage_file": ":memory:", "url": "https://example.com"}
)
assert selector._Selector__adaptive_enabled is True
assert selector._storage is not None
def test_adaptive_initialization_with_default_storage_args(self):
"""Test adaptive initialization with default storage args"""
html = "<html><body><p>Test</p></body></html>"
url = "https://example.com"
# Test that adaptive mode uses default storage when no explicit args provided
selector = Selector(
content=html,
url=url,
adaptive=True
)
# Should create storage with default args
assert selector._storage is not None
def test_adaptive_with_existing_storage(self):
"""Test adaptive initialization with existing storage object"""
html = "<html><body><p>Test</p></body></html>"
mock_storage = Mock()
selector = Selector(
content=html,
adaptive=True,
_storage=mock_storage
)
assert selector._storage is mock_storage
class TestAdvancedSelectors:
"""Test advanced selector functionality"""
@pytest.fixture
def complex_html(self):
return """
<html>
<body>
<div class="container" data-test='{"key": "value"}'>
<p>First paragraph</p>
<!-- Comment -->
<p>Second paragraph</p>
<![CDATA[Some CDATA content]]>
<div class="nested">
<span id="special">Special content</span>
<span>Regular content</span>
</div>
<table>
<tr><td>Cell 1</td><td>Cell 2</td></tr>
<tr><td>Cell 3</td><td>Cell 4</td></tr>
</table>
</div>
</body>
</html>
"""
def test_comment_and_cdata_handling(self, complex_html):
"""Test handling of comments and CDATA"""
# With comments/CDATA kept
page = Selector(
complex_html,
keep_comments=True,
keep_cdata=True
)
content = page.body
assert "Comment" in content
assert "CDATA" in content
# Without comments/CDATA
page = Selector(
complex_html,
keep_comments=False,
keep_cdata=False
)
content = page.html_content
assert "Comment" not in content
def test_advanced_xpath_variables(self, complex_html):
"""Test XPath with variables"""
page = Selector(complex_html)
# Using XPath variables
cells = page.xpath(
"//td[text()=$cell_text]",
cell_text="Cell 1"
)
assert len(cells) == 1
assert cells[0].text == "Cell 1"
def test_pseudo_elements(self, complex_html):
"""Test CSS pseudo-elements"""
page = Selector(complex_html)
# ::text pseudo-element
texts = page.css("p::text")
assert len(texts) == 2
assert isinstance(texts[0], Selector)
assert isinstance(texts[0].get(), TextHandler)
# ::attr() pseudo-element
attrs = page.css("div::attr(class)")
assert "container" in attrs.getall()
def test_complex_attribute_operations(self, complex_html):
"""Test complex attribute handling"""
page = Selector(complex_html)
container = page.css(".container")[0]
# JSON in attributes
data = container.attrib["data-test"].json()
assert data["key"] == "value"
# Attribute searching
matches = list(container.attrib.search_values("container"))
assert len(matches) == 1
def test_url_joining(self):
"""Test URL joining functionality"""
page = Selector("<html></html>", url="https://example.com/page")
# Relative URL
assert page.urljoin("../other") == "https://example.com/other"
assert page.urljoin("/absolute") == "https://example.com/absolute"
assert page.urljoin("relative") == "https://example.com/relative"
def test_find_operations_edge_cases(self, complex_html):
"""Test edge cases in find operations"""
page = Selector(complex_html)
# Multiple argument types
_ = page.find_all(
"span",
["div"],
{"class": "nested"},
lambda e: e.text != ""
)
# Regex pattern matching
pattern = re.compile(r"Cell \d+")
cells = page.find_all(pattern)
assert len(cells) == 4
def test_text_operations_edge_cases(self, complex_html):
"""Test text operation edge cases"""
page = Selector(complex_html)
# get_all_text with a custom separator
text = page.get_all_text(separator=" | ", strip=True)
assert " | " in text
# Ignore specific tags
text = page.get_all_text(ignore_tags=("table",))
assert "Cell" not in text
# With empty values
text = page.get_all_text(valid_values=False)
assert text != ""
class TestTextHandlerAdvanced:
"""Test advanced TextHandler functionality"""
def test_text_handler_operations(self):
"""Test various TextHandler operations"""
text = TextHandler(" Hello World ")
# All string methods should return TextHandler
assert isinstance(text.strip(), TextHandler)
assert isinstance(text.upper(), TextHandler)
assert isinstance(text.lower(), TextHandler)
assert isinstance(text.replace("World", "Python"), TextHandler)
# Custom methods
assert text.clean() == "Hello World"
# Sorting
text2 = TextHandler("dcba")
assert text2.sort() == "abcd"
def test_text_handler_regex(self):
"""Test regex operations on TextHandler"""
text = TextHandler("Price: $10.99, Sale: $8.99")
# Basic regex
prices = text.re(r"\$[\d.]+")
assert len(prices) == 2
assert prices[0] == "$10.99"
# Case insensitive
text2 = TextHandler("HELLO hello HeLLo")
matches = text2.re(r"hello", case_sensitive=False)
assert len(matches) == 3
# Clean match
text3 = TextHandler(" He l lo ")
matches = text3.re(r"He l lo", clean_match=True, case_sensitive=False)
assert len(matches) == 1
def test_text_handlers_operations(self):
"""Test TextHandlers list operations"""
handlers = TextHandlers([
TextHandler("First"),
TextHandler("Second"),
TextHandler("Third")
])
# Slicing should return TextHandlers
assert isinstance(handlers[0:2], TextHandlers)
# Get methods
assert handlers.get() == "First"
assert handlers.get("default") == "First"
assert TextHandlers([]).get("default") == "default"
class TestSelectorsAdvanced:
"""Test advanced Selectors functionality"""
def test_selectors_filtering(self):
"""Test filtering operations on Selectors"""
html = """
<div>
<p class="highlight">Important</p>
<p>Regular</p>
<p class="highlight">Also important</p>
</div>
"""
page = Selector(html)
paragraphs = page.css("p")
# Filter by class
highlighted = paragraphs.filter(lambda p: p.has_class("highlight"))
assert len(highlighted) == 2
# Search for a specific element
found = paragraphs.search(lambda p: p.text == "Regular")
assert found is not None
assert found.text == "Regular"
def test_selectors_properties(self):
"""Test Selectors properties"""
html = "<div><p>1</p><p>2</p><p>3</p></div>"
page = Selector(html)
paragraphs = page.css("p")
assert paragraphs.first.text == "1"
assert paragraphs.last.text == "3"
assert paragraphs.length == 3
|