From 63374ee8418895f0e3d13582e07e3925f55ad175 Mon Sep 17 00:00:00 2001 From: felippejc Date: Fri, 7 Feb 2020 00:03:11 +0100 Subject: [PATCH 1/3] Defining the BaseModel objects --- model.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/model.py b/model.py index 04645e6..8f36029 100644 --- a/model.py +++ b/model.py @@ -4,11 +4,15 @@ class Image(BaseModel): url: str - # TODO, add the missing `attribute` for this class to pass the unittest + etag: str = None # The etag is required to pass the unittest but it's optional class Product(BaseModel): uid: str # unique id to identify product - # TODO, add the rest of Product information like: gender, images, product url and etc - # images: List[Image] - + gender: str + url: str + price: float + size: str + category: str + description: str + images: List[Image] From 9b38e63307d1ce0643a93868098065235e277741 Mon Sep 17 00:00:00 2001 From: felippejc Date: Fri, 7 Feb 2020 00:15:22 +0100 Subject: [PATCH 2/3] Adding parsing functionality --- parsing_client.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/parsing_client.py b/parsing_client.py index 47fbed6..58444ca 100644 --- a/parsing_client.py +++ b/parsing_client.py @@ -42,16 +42,29 @@ def __init__(self, filename): raise NotImplementedError def __len__(self): - #TODO, implement "how to get number of products" - pass + """Returns the number of products""" + if self.tree is not None: + root = self.tree.getroot() + return len(root) + return 0 def __getitem__(self, idx): - #TODO, implement the parsing product at `idx` + # TODO, implement the parsing product at `idx` # For instance, get the following information and others from the XML: # category = 'Papyon' # price = 329.00 # gender = "male" - pass + if self.tree is not None: + root = self.tree.getroot() + product_info = {element.tag: element.text for element in root[idx]} + return Product(uid=product_info.get('productid', ""), + gender=product_info.get('gender', ""), + url=product_info.get('url', ""), + price=float(product_info.get('price', 0)), + size=product_info.get('sizes', ""), + description=product_info.get('title', ""), + category=product_info.get('categories', ""), + images=[Image(url=product_info.get('image', ""))]) # auto generated doc from super class __len__.__doc__ = ClientParser.__len__.__doc__ From 1faf798384607d33e49e7b9c7ca890123426f5fc Mon Sep 17 00:00:00 2001 From: felippejc Date: Fri, 7 Feb 2020 00:16:16 +0100 Subject: [PATCH 3/3] Adjusting tests --- test/test_parsing.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/test/test_parsing.py b/test/test_parsing.py index b5a4e9c..bce4adc 100644 --- a/test/test_parsing.py +++ b/test/test_parsing.py @@ -1,4 +1,4 @@ -from model import Image, Product, BaseModel +from model import Image, Product from parsing_client import ClientAParser import pytest import unittest @@ -12,9 +12,9 @@ def setUp(self): @pytest.mark.order1 def test_image_type(self): - image_url = """https://images.asos-media.com/products/""" - """vestido-largo-y-plisado-de-dama-de-honor-en-rosa-exclusivo-de-tfnc/""" - """13955198-2?$XXL$&wid=513&fit=constrain""" + image_url = ("""https://images.asos-media.com/products/""" + """vestido-largo-y-plisado-de-dama-de-honor-en-rosa-exclusivo-de-tfnc/""" + """13955198-2?$XXL$&wid=513&fit=constrain""") for etag in ["xx", None]: image = Image(url=image_url, etag=etag) self.assertTrue(hasattr(image, "etag")) @@ -22,12 +22,12 @@ def test_image_type(self): @pytest.mark.order2 def test_product_type(self): - image_url = """https://images.asos-media.com/products/""" - """vestido-largo-y-plisado-de-dama-de-honor-en-rosa-exclusivo-de-tfnc/""" - """13955198-2?$XXL$&wid=513&fit=constrain""" - product_url = """https://www.asos.com/es/tfnc/""" - """vestido-largo-y-plisado-de-dama-de-honor-en-rosa-exclusivo-de-tfnc/""" - """prd/13955198?clr=rosa&colourWayId=16579390&SearchQuery=&cid=17245""" + image_url = ("""https://images.asos-media.com/products/""" + """vestido-largo-y-plisado-de-dama-de-honor-en-rosa-exclusivo-de-tfnc/""" + """13955198-2?$XXL$&wid=513&fit=constrain""") + product_url = ("""https://www.asos.com/es/tfnc/""" + """vestido-largo-y-plisado-de-dama-de-honor-en-rosa-exclusivo-de-tfnc/""" + """prd/13955198?clr=rosa&colourWayId=16579390&SearchQuery=&cid=17245""") description = "Vestido largo y plisado de dama de honor en rosa exclusivo de TFNC" images = [Image(url=image_url) for x in range(5)] # 5 fake images product = Product(uid="sku1000", images=images, gender="female", @@ -73,12 +73,11 @@ def test_parser_getitem(self): self.assertEqual(product.description, "Dsquared2 Siyah İpek Papyon") self.assertEqual(product.url, "https://www.beymen.com/p_dsquared2-siyah-ipek-papyon_303527") - - def tearDown(self): pass + if __name__ == '__main__': unittest.main()