Skip to content

Commit

Permalink
tests: Variant without web item price fetch and add to cart
Browse files Browse the repository at this point in the history
- Also, optionally get image from template web item for cart if variant has no image
  • Loading branch information
marination committed Jan 21, 2022
1 parent 7d130af commit c1dd3d7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 27 deletions.
10 changes: 7 additions & 3 deletions erpnext/e_commerce/shopping_cart/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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),
Expand Down Expand Up @@ -284,12 +284,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",
Expand Down
40 changes: 38 additions & 2 deletions erpnext/e_commerce/shopping_cart/test_shopping_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

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


class TestShoppingCart(unittest.TestCase):
Expand All @@ -34,6 +34,7 @@ def setUp(self):
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()

Expand Down Expand Up @@ -128,6 +129,41 @@ def test_tax_rule(self):

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:
Expand Down
60 changes: 40 additions & 20 deletions erpnext/e_commerce/variant_selector/test_variant_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,39 @@

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"}
]
})

# create L-R, L-G, M-R, M-G and S-R
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):
"""
Expand All @@ -51,8 +47,6 @@ def test_item_attributes(self):
"""
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")
Expand All @@ -72,7 +66,7 @@ def test_item_attributes(self):
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):
Expand All @@ -84,8 +78,6 @@ def test_next_item_variant_values(self):
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"]
Expand All @@ -94,3 +86,31 @@ def test_next_item_variant_values(self):
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")
8 changes: 6 additions & 2 deletions erpnext/e_commerce/variant_selector/utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit c1dd3d7

Please sign in to comment.