test: Variant Selector - Attribute Value fetching

This commit is contained in:
marination 2022-01-11 18:19:29 +05:30
parent 6c7b3026c9
commit 5a462db207
2 changed files with 82 additions and 6 deletions

View File

@ -2,7 +2,6 @@
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
import itertools
import json
import frappe

View File

@ -1,12 +1,89 @@
import unittest
# import frappe
import frappe
# from erpnext.e_commerce.product_data_engine.query import ProductQuery
# from erpnext.e_commerce.doctype.website_item.website_item import make_website_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
test_dependencies = ["Item"]
class TestVariantSelector(unittest.TestCase):
# TODO: Variant Selector Tests
pass
def setUp(self) -> None:
self.template_item = make_item("Test-Tshirt-Temp", {
"has_variant": 1,
"variant_based_on": "Item Attribute",
"attributes": [
{
"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
})
variant.save()
variant = create_variant("Test-Tshirt-Temp", {
"Test Size": "Small",
"Test Colour": "Red"
})
variant.save()
@classmethod
def tearDownClass(cls):
frappe.db.rollback()
def test_item_attributes(self):
"""
Test if the right attributes are fetched in the popup.
(Attributes must only come from active items)
Attribute selection must not be linked to Website Items.
"""
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")
self.assertEqual(attr_data[1]["attribute"], "Test Colour")
self.assertEqual(len(attr_data[0]["values"]), 3) # ['Small', 'Medium', 'Large']
self.assertEqual(len(attr_data[1]["values"]), 2) # ['Red', 'Green']
# disable small red tshirt, now there are no small tshirts.
# but there are some red tshirts
small_variant = frappe.get_doc("Item", "Test-Tshirt-Temp-S-R")
small_variant.disabled = 1
small_variant.save() # trigger cache rebuild
attr_data = get_attributes_and_values("Test-Tshirt-Temp")
# Only L and M attribute values must be fetched since S is disabled
self.assertEqual(len(attr_data[0]["values"]), 2) # ['Medium', 'Large']
# def test_next_item_variant_values(self):
# """
# Test if on selecting an attribute value, the next possible values
# are filtered accordingly.
# Values that dont apply should not be fetched.
# E.g.
# 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={
# "Colour": "Red"
# })
# print(next_values)