From 4f7ec25bbe8456451f1d67f15dd36af7fbfb7343 Mon Sep 17 00:00:00 2001 From: marination Date: Fri, 21 Jan 2022 14:33:30 +0530 Subject: [PATCH] tests: Variant without web item price fetch and add to cart - Also, optionally get image from template web item for cart if variant has no image --- erpnext/e_commerce/shopping_cart/cart.py | 10 +++- .../shopping_cart/test_shopping_cart.py | 40 ++++++++++++- .../variant_selector/test_variant_selector.py | 60 ++++++++++++------- erpnext/e_commerce/variant_selector/utils.py | 8 ++- 4 files changed, 91 insertions(+), 27 deletions(-) diff --git a/erpnext/e_commerce/shopping_cart/cart.py b/erpnext/e_commerce/shopping_cart/cart.py index 9982ce80bee..a3f3c2dfec9 100644 --- a/erpnext/e_commerce/shopping_cart/cart.py +++ b/erpnext/e_commerce/shopping_cart/cart.py @@ -41,7 +41,7 @@ def get_cart_quotation(doc=None): if not doc.customer_address and addresses: update_cart_address("billing", addresses[0].name) - print("doc>>", doc, type(doc)) + return { "doc": decorate_quotation_doc(doc), "shipping_addresses": get_shipping_addresses(party), @@ -283,12 +283,16 @@ def decorate_quotation_doc(doc): variant_data = frappe.db.get_values( "Item", filters={"item_code": item_code}, - fieldname=["variant_of", "item_name"], + fieldname=["variant_of", "item_name", "image"], as_dict=True )[0] item_code = variant_data.variant_of - d.website_item_name = variant_data.item_name fields = fields[1:] + d.website_item_name = variant_data.item_name + + if variant_data.image: # get image from variant or template web item + d.thumbnail = variant_data.image + fields = fields[2:] d.update(frappe.db.get_value( "Website Item", diff --git a/erpnext/e_commerce/shopping_cart/test_shopping_cart.py b/erpnext/e_commerce/shopping_cart/test_shopping_cart.py index 26d49c1dc8b..19bf50bf3b2 100644 --- a/erpnext/e_commerce/shopping_cart/test_shopping_cart.py +++ b/erpnext/e_commerce/shopping_cart/test_shopping_cart.py @@ -9,8 +9,8 @@ from frappe.utils import add_months, nowdate from erpnext.accounts.doctype.tax_rule.tax_rule import ConflictingTaxRule from erpnext.e_commerce.doctype.website_item.website_item import make_website_item -from erpnext.e_commerce.shopping_cart.cart import _get_cart_quotation, get_party, update_cart -from erpnext.tests.utils import create_test_contact_and_address +from erpnext.e_commerce.shopping_cart.cart import _get_cart_quotation, get_cart_quotation, get_party, update_cart +from erpnext.tests.utils import create_test_contact_and_address, change_settings # test_dependencies = ['Payment Terms Template'] @@ -35,6 +35,7 @@ class TestShoppingCart(unittest.TestCase): make_website_item(frappe.get_cached_doc("Item", "_Test Item 2")) def tearDown(self): + frappe.db.rollback() frappe.set_user("Administrator") self.disable_shopping_cart() @@ -129,6 +130,41 @@ class TestShoppingCart(unittest.TestCase): self.remove_test_quotation(quotation) + @change_settings("E Commerce Settings",{ + "company": "_Test Company", + "enabled": 1, + "default_customer_group": "_Test Customer Group", + "price_list": "_Test Price List India", + "show_price": 1 + } + ) + def test_add_item_variant_without_web_item_to_cart(self): + "Test adding Variants having no Website Items in cart via Template Web Item." + from erpnext.controllers.item_variant import create_variant + from erpnext.e_commerce.doctype.website_item.website_item import make_website_item + from erpnext.stock.doctype.item.test_item import make_item + + template_item = make_item("Test-Tshirt-Temp", { + "has_variant": 1, + "variant_based_on": "Item Attribute", + "attributes": [ + {"attribute": "Test Size"}, + {"attribute": "Test Colour"} + ] + }) + variant = create_variant("Test-Tshirt-Temp", { + "Test Size": "Small", "Test Colour": "Red" + }) + variant.save() + make_website_item(template_item) # publish template not variant + + update_cart("Test-Tshirt-Temp-S-R", 1) + + cart = get_cart_quotation() # test if cart page gets data without errors + doc = cart.get("doc") + + self.assertEqual(doc.get("items")[0].item_name, "Test-Tshirt-Temp-S-R") + def create_tax_rule(self): tax_rule = frappe.get_test_records("Tax Rule")[0] try: diff --git a/erpnext/e_commerce/variant_selector/test_variant_selector.py b/erpnext/e_commerce/variant_selector/test_variant_selector.py index 31c86f1fd53..94e9e9b816d 100644 --- a/erpnext/e_commerce/variant_selector/test_variant_selector.py +++ b/erpnext/e_commerce/variant_selector/test_variant_selector.py @@ -4,23 +4,22 @@ import frappe from erpnext.controllers.item_variant import create_variant from erpnext.e_commerce.doctype.website_item.website_item import make_website_item +from erpnext.e_commerce.variant_selector.utils import get_next_attribute_and_values from erpnext.stock.doctype.item.test_item import make_item +from erpnext.tests.utils import ERPNextTestCase, change_settings test_dependencies = ["Item"] -class TestVariantSelector(unittest.TestCase): +class TestVariantSelector(ERPNextTestCase): - def setUp(self) -> None: - self.template_item = make_item("Test-Tshirt-Temp", { + @classmethod + def setUpClass(cls): + template_item = make_item("Test-Tshirt-Temp", { "has_variant": 1, "variant_based_on": "Item Attribute", "attributes": [ - { - "attribute": "Test Size" - }, - { - "attribute": "Test Colour" - } + {"attribute": "Test Size"}, + {"attribute": "Test Colour"} ] }) @@ -28,19 +27,16 @@ class TestVariantSelector(unittest.TestCase): for size in ("Large", "Medium",): for colour in ("Red", "Green",): variant = create_variant("Test-Tshirt-Temp", { - "Test Size": size, - "Test Colour": colour + "Test Size": size, "Test Colour": colour }) variant.save() variant = create_variant("Test-Tshirt-Temp", { - "Test Size": "Small", - "Test Colour": "Red" + "Test Size": "Small", "Test Colour": "Red" }) variant.save() - def tearDown(self): - frappe.db.rollback() + make_website_item(template_item) # publish template not variants def test_item_attributes(self): """ @@ -51,8 +47,6 @@ class TestVariantSelector(unittest.TestCase): """ from erpnext.e_commerce.variant_selector.utils import get_attributes_and_values - make_website_item(self.template_item) # publish template not variants - attr_data = get_attributes_and_values("Test-Tshirt-Temp") self.assertEqual(attr_data[0]["attribute"], "Test Size") @@ -72,7 +66,7 @@ class TestVariantSelector(unittest.TestCase): self.assertEqual(len(attr_data[0]["values"]), 2) # ['Medium', 'Large'] # teardown - small_variant.disabled = 1 + small_variant.disabled = 0 small_variant.save() def test_next_item_variant_values(self): @@ -84,8 +78,6 @@ class TestVariantSelector(unittest.TestCase): There is a ** Small-Red ** Tshirt. No other colour in this size. On selecting ** Small **, only ** Red ** should be selectable next. """ - from erpnext.e_commerce.variant_selector.utils import get_next_attribute_and_values - next_values = get_next_attribute_and_values("Test-Tshirt-Temp", selected_attributes={"Test Size": "Small"}) next_colours = next_values["valid_options_for_attributes"]["Test Colour"] filtered_items = next_values["filtered_items"] @@ -94,3 +86,31 @@ class TestVariantSelector(unittest.TestCase): self.assertEqual(next_colours.pop(), "Red") self.assertEqual(len(filtered_items), 1) self.assertEqual(filtered_items.pop(), "Test-Tshirt-Temp-S-R") + + @change_settings("E Commerce Settings",{ + "company": "_Test Company", + "enabled": 1, + "default_customer_group": "_Test Customer Group", + "price_list": "_Test Price List India", + "show_price": 1 + } + ) + def test_exact_match_with_price(self): + """ + Test price fetching and matching of variant without Website Item + """ + from erpnext.e_commerce.doctype.website_item.test_website_item import ( + make_web_item_price, + ) + + make_web_item_price(item_code="Test-Tshirt-Temp-S-R", price_list_rate=100) + next_values = get_next_attribute_and_values( + "Test-Tshirt-Temp", + selected_attributes={"Test Size": "Small", "Test Colour": "Red"} + ) + price_info = next_values["product_info"]["price"] + + self.assertEqual(next_values["exact_match"][0],"Test-Tshirt-Temp-S-R") + self.assertEqual(next_values["exact_match"][0],"Test-Tshirt-Temp-S-R") + self.assertEqual(price_info["price_list_rate"], 100.0) + self.assertEqual(price_info["formatted_price_sales_uom"], "₹ 100.00") \ No newline at end of file diff --git a/erpnext/e_commerce/variant_selector/utils.py b/erpnext/e_commerce/variant_selector/utils.py index bccc57b7320..4b525d08c21 100644 --- a/erpnext/e_commerce/variant_selector/utils.py +++ b/erpnext/e_commerce/variant_selector/utils.py @@ -1,7 +1,10 @@ import frappe from frappe.utils import cint -from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import get_shopping_cart_settings +from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import ( + get_shopping_cart_settings +) +from erpnext.e_commerce.shopping_cart.cart import _set_price_list from erpnext.e_commerce.variant_selector.item_variants_cache import ItemVariantsCacheManager from erpnext.utilities.product import get_price @@ -202,9 +205,10 @@ def get_item_variant_price_dict(item_code, cart_settings): # Show Price if logged in. # If not logged in, check if price is hidden for guest. if not is_guest or not cart_settings.hide_price_for_guest: + price_list = _set_price_list(cart_settings, None) price = get_price( item_code, - cart_settings.price_list, #TODO + price_list, cart_settings.default_customer_group, cart_settings.company )