#!/usr/bin/env python3 """Tests pour le store AliExpress.""" import pytest from pathlib import Path from pricewatch.app.stores.aliexpress.store import AliexpressStore class TestAliexpressStore: """Tests pour AliexpressStore.""" @pytest.fixture def store(self): """Fixture du store AliExpress.""" return AliexpressStore() # ========== Tests de match() ========== def test_match_aliexpress_com_product(self, store): """URL aliexpress.com/item/ reconnue comme produit.""" url = "https://www.aliexpress.com/item/1005007187023722.html" score = store.match(url) assert score == 0.9 def test_match_aliexpress_fr_product(self, store): """URL fr.aliexpress.com/item/ reconnue comme produit.""" url = "https://fr.aliexpress.com/item/1005007187023722.html" score = store.match(url) assert score == 0.9 def test_match_aliexpress_non_product(self, store): """URL aliexpress.com mais pas /item/ → score réduit.""" url = "https://www.aliexpress.com/category/electronics" score = store.match(url) assert score == 0.5 def test_match_other_site(self, store): """Autres sites non reconnus.""" urls = [ "https://www.amazon.fr/dp/ASIN", "https://www.cdiscount.com/f-123-abc.html", "", None, ] for url in urls: if url is not None: score = store.match(url) assert score == 0.0 def test_match_case_insensitive(self, store): """Match insensible à la casse.""" url = "https://FR.ALIEXPRESS.COM/ITEM/1234567890.HTML" score = store.match(url) assert score == 0.9 # ========== Tests de canonicalize() ========== def test_canonicalize_remove_query_params(self, store): """Canonicalize retire les paramètres de query.""" url = "https://fr.aliexpress.com/item/1005007187023722.html?spm=a2g0o.detail.0.0" canonical = store.canonicalize(url) assert canonical == "https://fr.aliexpress.com/item/1005007187023722.html" def test_canonicalize_remove_fragment(self, store): """Canonicalize retire le fragment (#).""" url = "https://fr.aliexpress.com/item/1005007187023722.html#reviews" canonical = store.canonicalize(url) assert canonical == "https://fr.aliexpress.com/item/1005007187023722.html" def test_canonicalize_keep_item_path(self, store): """Canonicalize garde le chemin /item/{ID}.html.""" url = "https://fr.aliexpress.com/item/1005007187023722.html" canonical = store.canonicalize(url) assert canonical == "https://fr.aliexpress.com/item/1005007187023722.html" def test_canonicalize_empty_url(self, store): """Canonicalize avec URL vide retourne la même.""" assert store.canonicalize("") == "" assert store.canonicalize(None) is None # ========== Tests de extract_reference() ========== def test_extract_reference_standard_format(self, store): """Extraction du SKU depuis format standard /item/{ID}.html.""" url = "https://fr.aliexpress.com/item/1005007187023722.html" ref = store.extract_reference(url) assert ref == "1005007187023722" def test_extract_reference_with_query_params(self, store): """Extraction du SKU ignore les paramètres de query.""" url = "https://fr.aliexpress.com/item/1005007187023722.html?param=value" ref = store.extract_reference(url) assert ref == "1005007187023722" def test_extract_reference_different_domain(self, store): """Extraction du SKU fonctionne avec différents domaines.""" url = "https://www.aliexpress.com/item/9876543210987.html" ref = store.extract_reference(url) assert ref == "9876543210987" def test_extract_reference_invalid_url(self, store): """Extraction du SKU depuis URL invalide retourne None.""" urls = [ "https://www.aliexpress.com/category/electronics", "https://www.aliexpress.com/", "", None, ] for url in urls: ref = store.extract_reference(url) assert ref is None # ========== Tests de parse() ========== def test_parse_basic_html_with_title(self, store): """Parse HTML basique avec h1.""" html = """
Empty content
" url = "https://fr.aliexpress.com/item/1234567890.html" snapshot = store.parse(html, url) assert snapshot.title is None assert snapshot.price is None assert not snapshot.is_complete() assert snapshot.debug.status == "partial" def test_parse_small_html_warning(self, store): """Parse avec HTML petit génère un warning.""" html = "