Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Guiiix committed Nov 27, 2024
1 parent 77fa1e5 commit eb1b98d
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 35 deletions.
79 changes: 59 additions & 20 deletions tests/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from psebpconnector.models import *

ADDRESSES = {
123456: Address(id=123456,
'fr': Address(id=123456,
id_customer=123456,
id_manufacturer=123,
id_supplier=0,
Expand Down Expand Up @@ -59,25 +59,63 @@
}

PRODUCTS = {
123456: Product(id=123456,
price=37.500000,
ean13='4573102667311',
name=[{'value':'Product 1'}],
wholesale_price='24.255000',
description='desc Product 1'),
654321: Product(id=654321,
price=65.833333,
ean13='987654321098',
name=[{'value':'Product 2'}],
wholesale_price='44.100000',
description='desc Product 2')
1: Product(id=1,
price=32.500000,
ean13='1111111111111',
name=[{'value':'Product 1'}],
wholesale_price='20.700000',
description='desc Product 1'),
2: Product(id=2,
price=37.500000,
ean13='4573102667311',
name=[{'value':'Product 2'}],
wholesale_price='24.255000',
description='desc Product 2'),
3: Product(id=3,
price=65.833333,
ean13='987654321098',
name=[{'value':'Product 3'}],
wholesale_price='44.100000',
description='desc Product 3')
}

SINGLE_ORDER_FR_ONE_PRODUCT = [
Order(
id=123456,
id_address_delivery='fr',
id_address_invoice='fr',
conversion_rate='1.000000',
payment='Ebay - FR - Creditcard',
total_discount=0,
total_paid='39.000000',
total_paid_real='78.000000',
total_products='32.500000',
total_products_wt='39.000000',
total_shipping='0.000000',
total_shipping_tax_incl='0.000000',
total_shipping_tax_excl='0.000000',
associations={'order_rows': [{'id': 99999,
'product_id': 1,
'product_attribute_id': 0,
'product_quantity': 1,
'product_name': 'Product 1',
'product_reference': '1111111111111',
'product_ean13': '1111111111111',
'product_isbn': '',
'product_upc': '',
'product_price': '32.500000',
'id_customization': 0,
'unit_price_tax_incl': '39.000000',
'unit_price_tax_excl': '32.500000'
}]}
)
]

SINGLE_ORDER_WITH_TWO_PRODUCTS_BAD_AMOUNT = [
Order(
id=123456,
id_address_delivery=123456,
id_address_invoice=123456,
id_address_delivery='fr',
id_address_invoice='fr',
id_cart=123456,
id_currency=1,
id_lang=1,
Expand All @@ -86,7 +124,7 @@
id_shop_group=1,
id_shop=1,
associations={'order_rows': [{'id': 123456,
'product_id': 123456,
'product_id': 2,
'product_attribute_id': 0,
'product_quantity': 1,
'product_name': 'Product 1',
Expand All @@ -100,7 +138,7 @@
'unit_price_tax_excl': '37.500000'
},
{'id': 999999,
'product_id': 654321,
'product_id': 3,
'product_attribute_id': 0,
'product_quantity': 1,
'product_name': 'Product 2',
Expand Down Expand Up @@ -134,10 +172,11 @@
)
]


SINGLE_ORDER_WITH_UNKNOWN_PAYMENT_METHOD = [
Order(
id=123456,
id_address_delivery=123456,
id_address_delivery='fr',
id_address_invoice=123456,
id_cart=123456,
id_currency=1,
Expand All @@ -147,7 +186,7 @@
id_shop_group=1,
id_shop=1,
associations={'order_rows': [{'id': 123456,
'product_id': 123456,
'product_id': 2,
'product_attribute_id': 0,
'product_quantity': 1,
'product_name': 'Product 1',
Expand All @@ -161,7 +200,7 @@
'unit_price_tax_excl': '37.500000'
},
{'id': 999999,
'product_id': 654321,
'product_id': 3,
'product_attribute_id': 0,
'product_quantity': 1,
'product_name': 'Product 2',
Expand Down
110 changes: 95 additions & 15 deletions tests/test_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,102 @@
SOFTWARE.
"""

import json
import pytest

from pathlib import Path
from psebpconnector.connector import Connector
from psebpconnector.models import *
from .datasets import *
from .fixtures import offline_connector
from psebpconnector.export_models import ExportOrderRow, ExportProduct

ORDER_1 = Order.from_dict(json.loads('{"order":{"id":549085,"id_address_delivery":967452,"id_address_invoice":967452,"id_cart":817776,"id_currency":1,"id_lang":1,"id_customer":532822,"id_carrier":103,"current_state":4,"module":"feedbiz","invoice_number":541557,"invoice_date":"2024-07-05 19:40:27","delivery_number":528463,"delivery_date":"2024-07-08 10:52:56","valid":"1","date_add":"2024-07-05 16:40:18","date_upd":"2024-10-23 11:09:16","shipping_number":"885000009059824","id_shop_group":1,"id_shop":1,"secure_key":"f6e7751671880b23ba68281089d71d35","payment":"Amazon - FR","total_discounts":"0.000000","total_discounts_tax_incl":"0.000000","total_discounts_tax_excl":"0.000000","total_paid":"20.230000","total_paid_tax_incl":"20.230000","total_paid_tax_excl":"16.860000","total_paid_real":"40.460000","total_products":"9.780000","total_products_wt":"11.730000","total_shipping":"8.500000","total_shipping_tax_incl":"8.500000","total_shipping_tax_excl":"7.080000","carrier_tax_rate":"20.000","total_wrapping":"0.000000","total_wrapping_tax_incl":"0.000000","total_wrapping_tax_excl":"0.000000","round_mode":2,"round_type":2,"conversion_rate":"1.000000","reference":"FMHGOYBGK","associations":{"order_rows":[{"id":983222,"product_id":52695,"product_attribute_id":0,"product_quantity":1,"product_name":"Porte Clé Capitaine Flam - Professeur Simon Gomme 8cm","product_reference":"4589504961513","product_ean13":"4589504961513","product_isbn":"","product_upc":"","product_price":"9.775000","id_customization":0,"unit_price_tax_incl":"11.730000","unit_price_tax_excl":"9.775000"}]}}}')['order'])
PRODUCT_1 = Product.from_dict(json.loads('{"product":{"id":59989,"id_manufacturer":538,"id_supplier":11,"id_category_default":30106305,"id_default_image":132903,"id_tax_rules_group":102,"position_in_category":51,"manufacturer_name":"Bandai Hobby","type":"simple","id_shop_default":1,"reference":"4573102616029","width":"0.000000","height":"0.000000","depth":"0.000000","weight":"1.500000","ean13":"4573102616029","state":1,"additional_delivery_times":1,"delivery_in_stock":[{"id":1,"value":null},{"id":2,"value":null}],"delivery_out_stock":[{"id":1,"value":null},{"id":2,"value":null}],"ecotax":"0.000000","minimal_quantity":1,"price":"24.166667","wholesale_price":"15.255000","unit_price":"0.000000","unit_price_ratio":"0.000000","additional_shipping_cost":"0.000000","active":"1","redirect_type":"404","available_for_order":"1","available_date":"2024-11-09","condition":"new","show_price":"1","indexed":"1","visibility":"both","date_add":"2021-02-27 11:18:25","date_upd":"2024-07-05 19:01:01","pack_stock_type":3,"meta_description":[{"id":1,"value":"- Maquette Gundam - 017 Wing Gundam Zero Endless Waltz Gunpla RG 1\/144- Armure Gundam pour le modèle Nepteight articulée à assembler- Système de montage SNA..."},{"id":2,"value":"- Maquette Gundam - 017 Wing Gundam Zero Endless Waltz Gunpla RG 1\/144- Armure Gundam pour le modèle Nepteight articulée à assembler- Système de montage SNA..."}],"meta_keywords":[{"id":1,"value":"4573102616029,Maquette Gundam - 017 Wing Gundam Zero Endless Waltz Gunpla RG 1\/144 13cm,RG,Bandai Hobby"},{"id":2,"value":"4573102616029,Maquette Gundam - 017 Wing Gundam Zero Endless Waltz Gunpla RG 1\/144 13cm,RG,Bandai Hobby"}],"meta_title":[{"id":1,"value":"Maquette Gundam - 017 Wing Gundam Zero Endless Waltz Gunpla RG 1\/14..."},{"id":2,"value":"Maquette Gundam - 017 Wing Gundam Zero Endless Waltz Gunpla RG 1\/14..."}],"link_rewrite":[{"id":1,"value":"maquette-gundam-017-wing-gundam-zero-endless-waltz-gunpla-rg-1-144-13cmrg"},{"id":2,"value":"maquette-gundam-017-wing-gundam-zero-endless-waltz-gunpla-rg-1-144-13cmrg"}],"name":[{"id":1,"value":"Maquette Gundam - 017 Wing Gundam Zero Endless Waltz Gunpla RG 1\/144 13cm"},{"id":2,"value":"Maquette Gundam - 017 Wing Gundam Zero Endless Waltz Gunpla RG 1\/144 13cm"}],"description":[{"id":1,"value":"<p>- Maquette Gundam - <span>017 Wing Gundam Zero Endless Waltz<\/span> Gunpla RG 1\/144<br \/><span>- Armure<span> Gundam pour le modèle Nepteight articulée à assembler<\/span><br \/><\/span>- <span>Système de montage <\/span><em>SNAPFIT<\/em><span> = ne nécessite ni colle, ni peinture<\/span><br \/>- Vendu sous boite carton<br \/>- Taille 13cm<\/p>"},{"id":2,"value":"<p>- Maquette Gundam - <span>017 Wing Gundam Zero Endless Waltz<\/span> Gunpla RG 1\/144<br \/><span>- Armure<span> Gundam pour le modèle Nepteight articulée à assembler<\/span><br \/><\/span>- <span>Système de montage <\/span><em>SNAPFIT<\/em><span> = ne nécessite ni colle, ni peinture<\/span><br \/>- Vendu sous boite carton<br \/>- Taille 13cm<\/p>"}],"description_short":[{"id":1,"value":""},{"id":2,"value":""}],"available_now":[{"id":1,"value":"En Stock"},{"id":2,"value":"Available"}],"available_later":[{"id":1,"value":"Rupture"},{"id":2,"value":"Out of Stock"}],"associations":{"categories":[{"id":30106305}],"images":[{"id":132903}],"tags":[{"id":57},{"id":105},{"id":106},{"id":107},{"id":700},{"id":701},{"id":737},{"id":739},{"id":771}],"stock_availables":[{"id":60251,"id_product_attribute":0}]}}}')['product'])
ADDRESS_1 = Address(id=967452, id_country=8)

def test_order(mocker):
mocker.patch('psebpconnector.webservice.Webservice.get_orders_to_export', return_value=[ORDER_1])
mocker.patch('psebpconnector.webservice.Webservice.get_address', return_value=ADDRESS_1)
mocker.patch('psebpconnector.webservice.Webservice.get_product', return_value=PRODUCT_1)
connector = Connector(Path(__file__).parent / 'samples/config/config_file_ok.ini')
connector.load_payment_method_mapping()
connector.load_vat_mapping()
connector.export_orders_and_products()
EXPORTED_ORDERS = []
EXPORTED_PRODUCTS = []

EXPECTED_RESULTS = [
(
# Simple case
SINGLE_ORDER_FR_ONE_PRODUCT,
(
[{
'document_use_original_number': 'N',
'line_quantity': '1',
'line_vat_rate': '20.000000',
'line_unit_price': '39.000000',
'document_payment_method': 'PAYPAL',
'document_currency_rate': '',
'line_vat_code': '36cab0de-3e5b-4bee-a556-8eabb1673e76'
}],
[{
'code': '1111111111111',
'name': 'Product 1',
'type': 'BIEN',
'price': '32.500000',
'ean': '1111111111111',
'wholesale_price': '20.700000'
}]
)
),
(
# Should export only one product
SINGLE_ORDER_FR_ONE_PRODUCT*2,
(
[{
'document_use_original_number': 'N',
'line_quantity': '1',
'line_vat_rate': '20.000000',
'line_unit_price': '39.000000',
'document_payment_method': 'PAYPAL',
'document_currency_rate': '',
'line_vat_code': '36cab0de-3e5b-4bee-a556-8eabb1673e76'
}]*2,
[{
'code': '1111111111111',
'name': 'Product 1',
'type': 'BIEN',
'price': '32.500000',
'ean': '1111111111111',
'wholesale_price': '20.700000'
}]
)
)
]

def _fake_write_csv_line(_, obj, __):
global EXPORTED_PRODUCTS
global EXPORTED_ORDERS

if isinstance(obj, ExportOrderRow):
EXPORTED_ORDERS.append(obj)
elif isinstance(obj, ExportProduct):
EXPORTED_PRODUCTS.append(obj)
else:
raise TypeError(type(obj))


@pytest.mark.parametrize("should", EXPECTED_RESULTS)
def test_order(offline_connector, mocker, should):
global EXPORTED_ORDERS
global EXPORTED_PRODUCTS

EXPORTED_ORDERS = []
EXPORTED_PRODUCTS = []

orders = should[0]
should_orders = should[1][0]
should_products = should[1][1]

mocker.patch("psebpconnector.webservice.Webservice.get_orders_to_export", return_value=orders)
mocker.patch('psebpconnector.connector.Connector._write_csv_line', new=_fake_write_csv_line)

assert offline_connector.run() == 0
assert len(should_orders) == len(EXPORTED_ORDERS)
assert len(should_products) == len(EXPORTED_PRODUCTS)

# Orders
for i in range(len(should_orders)):
for key, value in should_orders[i].items():
assert getattr(EXPORTED_ORDERS[i], key) == value, f"Wrong value for field {key}, order row n°{i+1}"

# Products
for i in range(len(should_products)):
for key, value in should_products[i].items():
assert getattr(EXPORTED_PRODUCTS[i], key) == value, f"Wrong value for field {key}, product n°{i + 1}"

0 comments on commit eb1b98d

Please sign in to comment.