from django.conf import settings
from django.utils.functional import cached_property
from regulus_oscar.apps.catalogue.abstract_models import AbstractProduct
class Product(AbstractProduct):
def __str__(self):
"""
Appends the variant-identifier product attribute values to the string
representation of child products, for use in multiple places, including
on the front end and dashboard, and in emails to customers and the
merchant.
"""
if self.is_child and self.variant_attribute_summaries:
# The parent "__str__" method appends a summary of all the product
# attribute values, whereas we only want to include a summary of the
# variant-identifier ones
return f"{ self.title if self.title else self.parent.title } ({ self.variant_attribute_summaries })"
else:
return super().__str__()
def get_title(self):
"""
Appends the variant-identifier product attribute values to the title of
child products, for use in multiple places, including on the front end
and dashboard, and in emails to customers and the merchant.
"""
title = super().get_title()
if self.is_child and self.variant_attribute_summaries:
title = f"{ title } ({ self.variant_attribute_summaries })"
return title
get_title.short_description = "Title"
@cached_property
def variant_attribute_summaries(self):
"""
Returns a string of a product's variant-identifier ("Colour" and "Size")
product attribute values.
"""
return ", ".join(
[
product_attribute_value.summary()
for product_attribute_value in self.get_attribute_values().filter(
attribute__name__in=settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES
)
]
)
from regulus_oscar.apps.catalogue.models import * # noqa isort:skip
上面是我的models.py文件,在前端方面一切都按预期工作,但是当我运行下面的测试时,我有一些错误,我相信它们是由@cached_property引起的。我如何解决这个问题以进行测试并将其部署到生产环境而不会出现问题?我需要@cached_property属性,以避免每次都调用数据库,因为这是一个非常大的项目,而这将导致站点速度显著降低
import pytest
from regulus_oscar.test.factories import (
ProductAttributeFactory,
ProductAttributeValueFactory,
ProductClassFactory,
ProductFactory,
)
@pytest.mark.django_db
def test_product_get_title_without_variant_attribute_summaries_for_child_products(settings):
settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
product_class = ProductClassFactory(name="Foo")
parent_product = ProductFactory(structure="parent", product_class=product_class)
child_product = ProductFactory(structure="child", parent=parent_product, title = 'Foobar')
assert child_product.get_title() == 'Foobar'
@pytest.mark.django_db
def test_variant_attribute_summaries_returns_string_when_variant_attributes_exist_for_child_products(settings):
settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
product_class = ProductClassFactory(name="Foo")
colour_attribute = ProductAttributeFactory(product_class=product_class, name="Colour", code="colour", type="color")
size_attribute = ProductAttributeFactory(product_class=product_class, name="Size", code="size", type="text")
parent_product = ProductFactory(structure="parent", product_class=product_class)
child_product = ProductFactory(structure="child", parent=parent_product, title='Foobar')
red = ProductAttributeValueFactory(attribute=colour_attribute, product=child_product, value_text="#ff0000")
small = ProductAttributeValueFactory(attribute=size_attribute, product=child_product, value_text="Small")
attribute_summary = child_product.variant_attribute_summaries
assert attribute_summary == 'Colour: #ff0000, Size: Small'
@pytest.mark.django_db
def test_variant_attribute_summaries_returns_empty_string_when_variant_attributes_do_not_exist_for_child_product(settings):
settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
product_class = ProductClassFactory(name="Foo")
parent_product = ProductFactory(structure="parent", product_class=product_class)
child_product = ProductFactory(structure="child", parent=parent_product)
attribute_summary = child_product.variant_attribute_summaries
assert attribute_summary == ''
@pytest.mark.django_db
def test_variant_attribute_summaries_returns_string_when_variant_attributes_exist_for_parent_products(settings):
settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
product_class = ProductClassFactory(name="Foo")
colour_attribute = ProductAttributeFactory(product_class=product_class, name="Colour", code="colour", type="color")
size_attribute = ProductAttributeFactory(product_class=product_class, name="Size", code="size", type="text")
parent_product = ProductFactory(structure="parent", product_class=product_class, title="Parent Product")
red = ProductAttributeValueFactory(attribute=colour_attribute, product=parent_product, value_text="#ff0000")
small = ProductAttributeValueFactory(attribute=size_attribute, product=parent_product, value_text="Small")
assert parent_product.variant_attribute_summaries == 'Colour: #ff0000, Size: Small'
@pytest.mark.django_db
def test_variant_attribute_summaries_returns_empty_string_when_variant_attributes_do_not_exist_for_parent_products(settings):
settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
product_class = ProductClassFactory(name="Foo")
parent_product = ProductFactory(structure="parent", product_class=product_class)
attribute_summary = parent_product.variant_attribute_summaries
assert attribute_summary == ''
@pytest.mark.django_db
def test_product_get_title_without_variant_attribute_summaries_for_parent_products(settings):
settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
product_class = ProductClassFactory(name="Foo")
parent_product = ProductFactory(structure="parent", product_class=product_class, title = 'Foobar')
assert parent_product.get_title() == 'Foobar'
@pytest.mark.django_db
def test_str__for_product_without_variant_attribute_summaries_for_parent_products(settings):
settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
product_class = ProductClassFactory(name="Foo")
colour_attribute = ProductAttributeFactory(product_class=product_class, name="Colour", code="colour", type="color")
size_attribute = ProductAttributeFactory(product_class=product_class, name="Size", code="size", type="text")
parent_product = ProductFactory(structure="parent", product_class=product_class, title="Parent Product")
red = ProductAttributeValueFactory(attribute=colour_attribute, product=parent_product, value_text="#ff0000")
small = ProductAttributeValueFactory(attribute=size_attribute, product=parent_product, value_text="Small")
assert str(parent_product) == 'Parent Product'
@pytest.mark.django_db
def test_get_title_returns_string_when_variant_attributes_exist_for_child_products(settings):
settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
product_class = ProductClassFactory(name="Foo")
colour_attribute = ProductAttributeFactory(product_class=product_class, name="Colour", code="colour", type="color")
size_attribute = ProductAttributeFactory(product_class=product_class, name="Size", code="size", type="text")
parent_product = ProductFactory(structure="parent", product_class=product_class)
child_product = ProductFactory(structure="child", parent=parent_product, title = 'Foobar')
red = ProductAttributeValueFactory(attribute=colour_attribute, product=child_product, value_text="#ff0000")
small = ProductAttributeValueFactory(attribute=size_attribute, product=child_product, value_text="Small")
assert child_product.get_title() == 'Foobar (Colour: #ff0000, Size: Small)'
@pytest.mark.django_db
def test_settings_variant_attribute_names_can_be_overriden_trough_setting(settings):
list_of_settings = settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES = [
"Colour",
"Size",
]
list_of_settings[0] = 'testColour'
list_of_settings[1] = 'testSize'
product_class = ProductClassFactory(name="Foo")
colour_attribute = ProductAttributeFactory(product_class=product_class, name="testColour", code="colour", type="color")
size_attribute = ProductAttributeFactory(product_class=product_class, name="testSize", code="size", type="text")
parent_product = ProductFactory(structure="parent", product_class=product_class, title="Parent Product")
red = ProductAttributeValueFactory(attribute=colour_attribute, product=parent_product, value_text="#ff0000")
small = ProductAttributeValueFactory(attribute=size_attribute, product=parent_product, value_text="Small")
assert parent_product.variant_attribute_summaries == 'testColour: #ff0000, testSize: Small'
下面是我在运行测试时得到的错误
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================================================================================== short test summary info ===========================================================================================
FAILED tests/catalogue/test_models.py::test_variant_attribute_summaries_returns_string_when_variant_attributes_exist_for_child_products - AssertionError: assert '' == 'Colour: #ff0000, Size: Small'
FAILED tests/catalogue/test_models.py::test_get_title_returns_string_when_variant_attributes_exist_for_child_products - AssertionError: assert 'Foobar' == 'Foobar (Colo... Size: Small)'
FAILED tests/catalogue/test_models.py::test_settings_variant_attribute_names_can_be_overriden_trough_setting - AssertionError: assert '' == 'testColour: ...stSize: Small'
代码在前端按预期工作,当cached-property被删除时,它工作得很好,但我需要该属性用于生产目的,我可以做些什么来允许测试运行而不会出现问题?谢谢。
1条答案
按热度按时间ulydmbyx1#
在测试中将
_variant_attribute_summaries
更改为variant_attribute_summaries
,因为您在模型中定义了_variant_attribute_summaries
,而没有前导下划线...