Эх сурвалжийг харах

fix category and product parsers, added tests and mocks

sxoinas12 2 жил өмнө
parent
commit
87b0da95c8

+ 18 - 18
telecaster/telecaster/parsers/PrestaShopParser.py

@@ -1,28 +1,28 @@
 import xml.etree.ElementTree as ElementTree
 
+from .parse_xml_to_json import parse_xml_to_json
+
 
 class PrestaShopParser:
     def __init__(self):
         pass
 
     @classmethod
-    # WIP, parser is not finished yet
-    def categories_helper_parser(self, xmlCategories):
-        response = {}
-        for child in list(xmlCategories):
-            if len(list(child)) > 0:
-                if child.tag == 'category':
-                    self.categories_helper_parser(child)
-                else:
-                    response[child.tag] = self.categories_helper_parser(child)
-            else:
-                response[child.tag] = child.text or ''
-
-        return response
-
     # parses xml categories response to a dictionary
+    def parse_categories(self, xml_categories_content):
+        xml_categories = ElementTree.fromstring(xml_categories_content)
+        categories = list(xml_categories)[0]
+        result = {}
+        for child in categories:
+            child_ojb = parse_xml_to_json(child)
+            result[child_ojb['id']] = child_ojb
+        # Alternatively if we wanna convert to Array check use the below
+        # result = [parse_xml_to_json(child) for child in categories]
+        return result
+
     @classmethod
-    def parse_categories(self, xmlCategoriesContent):
-        xmlCategories = ElementTree.fromstring(xmlCategoriesContent)
-        categories = self.categories_helper_parser(xmlCategories)
-        return categories
+    def parse_products(self, xml_products_content):
+        xml_products = ElementTree.fromstring(xml_products_content)
+        products = list(xml_products)[0]
+        result = [parse_xml_to_json(child) for child in products]
+        return result

+ 0 - 1
telecaster/telecaster/parsers/parse_xml_to_json.py

@@ -1,6 +1,5 @@
 def parse_xml_to_json(xml):
     response = {}
-
     for child in list(xml):
         if len(list(child)) > 0:
             response[child.tag] = parse_xml_to_json(child)

+ 1 - 1
telecaster/telecaster/parsers/skroutz_parser.py

@@ -1,2 +1,2 @@
-class SkroutzParser(url):
+class SkroutzParser:
     pass

+ 17 - 0
telecaster/telecaster/tests/parsers/mock_categories_response.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
+<categories>
+<category>
+	<id><![CDATA[1]]></id>
+	<name><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[Root]]></language></name>
+</category>
+<category>
+	<id><![CDATA[2]]></id>
+	<name><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[Αρχική]]></language></name>
+</category>
+<category>
+	<id><![CDATA[8]]></id>
+	<name><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[Home Accessories]]></language></name>
+</category>
+</categories>
+</prestashop>

+ 66 - 0
telecaster/telecaster/tests/parsers/mock_products_response.xml

@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
+<products>
+<product>
+	<id><![CDATA[3962]]></id>
+	<id_category_default><![CDATA[0]]></id_category_default>
+	<id_default_image notFilterable="true"></id_default_image>
+	<manufacturer_name notFilterable="true"><![CDATA[Whitegg]]></manufacturer_name>
+	<quantity notFilterable="true"><![CDATA[0]]></quantity>
+	<ean13></ean13>
+	<delivery_in_stock><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[]]></language></delivery_in_stock>
+	<price><![CDATA[0.000000]]></price>
+	<wholesale_price><![CDATA[0.000000]]></wholesale_price>
+	<additional_shipping_cost><![CDATA[0.000000]]></additional_shipping_cost>
+	<name><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[]]></language></name>
+	<description><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[]]></language></description>
+</product>
+<product>
+	<id><![CDATA[4565]]></id>
+	<id_category_default><![CDATA[0]]></id_category_default>
+	<id_default_image xlink:href="https://cosmohome.gr/api/images/products/4565/16598" notFilterable="true"><![CDATA[16598]]></id_default_image>
+	<manufacturer_name notFilterable="true"><![CDATA[DAS HOME]]></manufacturer_name>
+	<quantity notFilterable="true"><![CDATA[0]]></quantity>
+	<ean13><![CDATA[5204850153960]]></ean13>
+	<delivery_in_stock><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[]]></language></delivery_in_stock>
+	<price><![CDATA[29.900000]]></price>
+	<wholesale_price><![CDATA[0.000000]]></wholesale_price>
+	<additional_shipping_cost><![CDATA[0.000000]]></additional_shipping_cost>
+	<name><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[537 ΤΡΑΠΕΖΟΜΑΝΤΗΛΟ]]></language></name>
+	<description><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[Ντύστε την τραπεζαρία σας με μονόχρωμα βαμβακερά τραπεζομάντηλα.
+ Διατίθεται σε διάσταση 140x240cm.
+ Σύνθεση: 100% Βαμβάκι.]]></language></description>
+</product>
+<product>
+	<id><![CDATA[5302]]></id>
+	<id_category_default><![CDATA[0]]></id_category_default>
+	<id_default_image notFilterable="true"></id_default_image>
+	<manufacturer_name notFilterable="true"><![CDATA[DAS MATTRESS]]></manufacturer_name>
+	<quantity notFilterable="true"><![CDATA[0]]></quantity>
+	<ean13><![CDATA[5204850173463]]></ean13>
+	<delivery_in_stock><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[]]></language></delivery_in_stock>
+	<price><![CDATA[235.000000]]></price>
+	<wholesale_price><![CDATA[0.000000]]></wholesale_price>
+	<additional_shipping_cost><![CDATA[0.000000]]></additional_shipping_cost>
+	<name><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[ΑΝΩΣΤΡΩΜΑ ΥΠΕΡΔΙΠΛΟ DAILY]]></language></name>
+	<description><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[Ανώστρωμα υπέρδιπλο της σειράς Daily.
+ Πάχος: 3cm. 
+Σύνθεση: Ύφασμα πλεκτό, antibacterial, Support Foam, Βάτα ακρυλική.]]></language></description>
+</product>
+<product>
+	<id><![CDATA[5303]]></id>
+	<id_category_default><![CDATA[0]]></id_category_default>
+	<id_default_image notFilterable="true"></id_default_image>
+	<manufacturer_name notFilterable="true"><![CDATA[DAS MATTRESS]]></manufacturer_name>
+	<quantity notFilterable="true"><![CDATA[0]]></quantity>
+	<ean13><![CDATA[5204850173470]]></ean13>
+	<delivery_in_stock><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[]]></language></delivery_in_stock>
+	<price><![CDATA[265.000000]]></price>
+	<wholesale_price><![CDATA[0.000000]]></wholesale_price>
+	<additional_shipping_cost><![CDATA[0.000000]]></additional_shipping_cost>
+	<name><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[ΑΝΩΣΤΡΩΜΑ KING SIZE DAILY]]></language></name>
+	<description><language id="2" xlink:href="https://cosmohome.gr/api/languages/2"><![CDATA[Ανώστρωμα king size της σειράς Daily.
+ Πάχος: 3cm. Σύνθεση: Ύφασμα πλεκτό, antibacterial, Support Foam, Βάτα ακρυλική.]]></language></description>
+</product>
+</products>
+</prestashop>

+ 46 - 0
telecaster/telecaster/tests/parsers/test_presta_shop_parser.py

@@ -0,0 +1,46 @@
+import sys
+
+import pytest
+import os
+from telecaster.telecaster.parsers.PrestaShopParser import PrestaShopParser
+
+
[email protected]
+def mock_categories_response():
+    with open(os.path.join(sys.path[0], 'mock_categories_response.xml'), 'r') as f:
+        return f.read()
+
+
[email protected]()
+def mock_products_response():
+    with open(os.path.join(sys.path[0], 'mock_products_response.xml'), 'r') as f:
+        return f.read()
+
+
+def test_categories(mock_categories_response):
+    categories = PrestaShopParser.parse_categories(mock_categories_response)
+    first_category = categories.get('1')
+
+    assert len(categories) == 3
+    assert list(first_category.keys()) == ['id', 'name']
+
+
+def test_parse_products(mock_products_response):
+    products = PrestaShopParser.parse_products(mock_products_response)
+    first_product = products[0]
+
+    assert len(products) == 4
+    assert list(first_product.keys()) == [
+        'id',
+        'id_category_default',
+        'id_default_image',
+        'manufacturer_name',
+        'quantity',
+        'ean13',
+        'delivery_in_stock',
+        'price',
+        'wholesale_price',
+        'additional_shipping_cost',
+        'name',
+        'description',
+    ]

+ 1 - 4
telecaster/telecaster/views/XmlGeneratorView.py

@@ -1,10 +1,7 @@
-import requests
-
 from rest_framework import views
 from rest_framework.request import Request
 
 from django.http import JsonResponse
-from ..parsers.parse_xml_to_json import parse_xml_to_json
 from ..Clients.PrestaShopClient import PrestaShopClient
 from ..parsers.PrestaShopParser import PrestaShopParser
 
@@ -25,5 +22,5 @@ class XmlGeneratorView(views.APIView):
         categories_response = prestashop_client.get_categories({'limit': 4})
         categories = PrestaShopParser.parse_categories(categories_response)
 
-        response_json = {}
+        response_json = {categories, products_response}
         return JsonResponse(response_json, safe=False)