mirror of
https://github.com/guezoloic/millesima_projetS6.git
synced 2026-03-28 19:13:42 +00:00
modefication fonction prix() return None+tests
This commit is contained in:
111
main.py
111
main.py
@@ -35,90 +35,65 @@ class _ScraperData:
|
|||||||
return None
|
return None
|
||||||
return cast(dict[str, object], current_data.get("attributes"))
|
return cast(dict[str, object], current_data.get("attributes"))
|
||||||
|
|
||||||
def prix(self) -> float:
|
def prix(self) -> float | None:
|
||||||
"""
|
"""
|
||||||
Retourne le prix unitaire d'une bouteille (75cl).
|
Retourne le prix unitaire d'une bouteille (75cl).
|
||||||
|
|
||||||
Le JSON contient plusieurs formats de vente dans content["items"] :
|
Si aucun prix n'est disponible, retourne None.
|
||||||
- bouteille seule : nbunit = 1 et equivbtl = 1 -> prix direct
|
"""
|
||||||
- caisse de plusieurs bouteilles : nbunit > 1 -> on divise le prix total
|
|
||||||
- formats spéciaux (magnum etc.) : equivbtl > 1 -> même calcul
|
|
||||||
|
|
||||||
Formule générale :
|
content = self._getcontent()
|
||||||
prix_unitaire = offerPrice / (nbunit * equivbtl)
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
content = self._getcontent()
|
|
||||||
|
|
||||||
# si content n'existe pas -> erreur
|
|
||||||
if content is None:
|
if content is None:
|
||||||
raise ValueError("Contenu introuvable")
|
return None
|
||||||
|
|
||||||
# On récupère la liste des formats disponibles (bouteille, carton...)
|
|
||||||
items = content.get("items")
|
items = content.get("items")
|
||||||
|
|
||||||
# Vérification que items est bien une liste non vide
|
# Si aucun format disponible -> pas de prix
|
||||||
if not isinstance(items, list) or len(items) == 0:
|
if isinstance(items, list):
|
||||||
raise ValueError("Aucun prix disponible (items vide)")
|
|
||||||
|
|
||||||
# --------------------------
|
|
||||||
# CAS 1 : bouteille unitaire
|
|
||||||
# --------------------------
|
|
||||||
# On cherche un format où nbunit=1 et equivbtl=1 ->bouteille standard 75cl
|
|
||||||
for item in items:
|
|
||||||
|
|
||||||
if not isinstance(item, dict):
|
if len(items) == 0:
|
||||||
continue
|
return None
|
||||||
|
|
||||||
# On récupère les attributs du format
|
for item in items:
|
||||||
attrs = item.get("attributes", {})
|
|
||||||
|
|
||||||
# On récupère nbunit et equivbtl
|
if not isinstance(item, dict):
|
||||||
nbunit = attrs.get("nbunit", {}).get("value")
|
continue
|
||||||
equivbtl = attrs.get("equivbtl", {}).get("value")
|
|
||||||
|
|
||||||
# Si c'est une bouteille unitaire
|
attrs = item.get("attributes", {})
|
||||||
if nbunit == "1" and equivbtl == "1":
|
|
||||||
|
nbunit = attrs.get("nbunit", {}).get("value")
|
||||||
|
equivbtl = attrs.get("equivbtl", {}).get("value")
|
||||||
|
|
||||||
|
if nbunit == "1" and equivbtl == "1":
|
||||||
|
|
||||||
|
p = item.get("offerPrice")
|
||||||
|
|
||||||
|
if isinstance(p, (int, float)):
|
||||||
|
return float(p)
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
continue
|
||||||
|
|
||||||
p = item.get("offerPrice")
|
p = item.get("offerPrice")
|
||||||
|
attrs = item.get("attributes", {})
|
||||||
|
|
||||||
# Vérification que c'est bien un nombre
|
nbunit = attrs.get("nbunit", {}).get("value")
|
||||||
if isinstance(p, (int, float)):
|
equivbtl = attrs.get("equivbtl", {}).get("value")
|
||||||
return float(p)
|
|
||||||
|
|
||||||
# --------------------------
|
if isinstance(p, (int, float)) and nbunit and equivbtl:
|
||||||
# CAS 2 : caisse ou autre format
|
|
||||||
# --------------------------
|
|
||||||
# On calcule le prix unitaire à partir du prix total
|
|
||||||
for item in items:
|
|
||||||
|
|
||||||
if not isinstance(item, dict):
|
denom = float(nbunit) * float(equivbtl)
|
||||||
continue
|
|
||||||
|
|
||||||
p = item.get("offerPrice")
|
if denom > 0:
|
||||||
attrs = item.get("attributes", {})
|
|
||||||
|
|
||||||
nbunit = attrs.get("nbunit", {}).get("value")
|
prix_unitaire = float(p) / denom
|
||||||
equivbtl = attrs.get("equivbtl", {}).get("value")
|
|
||||||
|
|
||||||
# Vérification que toutes les valeurs existent
|
return round(prix_unitaire, 2)
|
||||||
if isinstance(p, (int, float)) and nbunit and equivbtl:
|
|
||||||
|
|
||||||
# Calcul du nombre total de bouteilles équivalentes
|
return None
|
||||||
denom = float(nbunit) * float(equivbtl)
|
|
||||||
|
|
||||||
# Évite division par zéro
|
|
||||||
if denom > 0:
|
|
||||||
|
|
||||||
# Calcul du prix unitaire
|
|
||||||
prix_unitaire = float(p) / denom
|
|
||||||
|
|
||||||
# Arrondi à 2 décimales
|
|
||||||
return round(prix_unitaire, 2)
|
|
||||||
|
|
||||||
# Si aucun prix trouvé
|
|
||||||
raise ValueError("Impossible de trouver le prix unitaire.")
|
|
||||||
|
|
||||||
def appellation(self) -> str | None:
|
def appellation(self) -> str | None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
@@ -181,12 +156,14 @@ class _ScraperData:
|
|||||||
parker = self.parker()
|
parker = self.parker()
|
||||||
robinson = self.robinson()
|
robinson = self.robinson()
|
||||||
suckling = self.suckling()
|
suckling = self.suckling()
|
||||||
prix = self.prix()
|
try:
|
||||||
|
prix = self.prix()
|
||||||
|
except ValueError:
|
||||||
|
prix = None
|
||||||
|
|
||||||
return f"{appellation},{parker},{robinson},{suckling},{prix}"
|
return f"{appellation},{parker},{robinson},{suckling},{prix}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Scraper:
|
class Scraper:
|
||||||
"""
|
"""
|
||||||
Scraper est une classe qui permet de gerer
|
Scraper est une classe qui permet de gerer
|
||||||
|
|||||||
20
test_main.py
20
test_main.py
@@ -225,22 +225,20 @@ def test_critiques(scraper: Scraper):
|
|||||||
assert contenu.suckling() == "93.5"
|
assert contenu.suckling() == "93.5"
|
||||||
assert contenu._getcritiques("test_ts") is None
|
assert contenu._getcritiques("test_ts") is None
|
||||||
|
|
||||||
|
|
||||||
def test_prix(scraper: Scraper):
|
def test_prix(scraper: Scraper):
|
||||||
vide = scraper.getjsondata("")
|
vide = scraper.getjsondata("")
|
||||||
poubelle = scraper.getjsondata("poubelle")
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
|
assert vide.prix() is None
|
||||||
# Cas vide : items == [] -> on ne peut pas calculer -> ValueError
|
assert poubelle.prix() is None
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_ = vide.prix()
|
|
||||||
|
|
||||||
# Cas poubelle : JSON incomplet -> _getcontent() None -> ValueError
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_ = poubelle.prix()
|
|
||||||
|
|
||||||
assert contenu.prix() == 65.0
|
assert contenu.prix() == 65.0
|
||||||
|
|
||||||
|
|
||||||
def test_informations(scraper: Scraper):
|
def test_informations(scraper: Scraper):
|
||||||
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
contenu = scraper.getjsondata("nino-negri-5-stelle-sfursat-2022.html")
|
||||||
assert contenu.informations() == "Sforzato di Valtellina,91,17,93.5,65.0"
|
assert contenu.informations() == "Sforzato di Valtellina,91,17,93.5,65.0"
|
||||||
|
vide = scraper.getjsondata("")
|
||||||
|
poubelle = scraper.getjsondata("poubelle")
|
||||||
|
assert vide.informations() == "None,None,None,None,None"
|
||||||
|
assert poubelle.informations() == "None,None,None,None,None"
|
||||||
|
|||||||
Reference in New Issue
Block a user