def test_applied_attributes(): product = get_default_product() for spec in ATTR_SPECS: # This loop sets each attribute twice. That's okay. attr = Attribute.objects.get(identifier=spec["identifier"]) pa, _ = ProductAttribute.objects.get_or_create(product=product, attribute=attr) _populate_applied_attribute(pa) pa.save() if not attr.is_translated: product.set_attribute_value(attr.identifier, pa.value) assert product.get_attribute_value("bogomips") == 320, "integer attribute loaded neatly" product.set_attribute_value("bogomips", 480) assert product.get_attribute_value("bogomips") == 480, "integer attribute updated neatly" Product.cache_attributes_for_targets( applied_attr_cls=ProductAttribute, targets=[product], attribute_identifiers=[a["identifier"] for a in ATTR_SPECS], language=get_language() ) assert (get_language(), "bogomips",) in product._attr_cache, "integer attribute in cache" assert product.get_attribute_value("bogomips") == 480, "integer attribute value in cache" assert product.get_attribute_value("ba:gelmips", default="Britta") == "Britta", "non-existent attributes return default value" assert product._attr_cache[(get_language(), "ba:gelmips")] is NoSuchAttributeHere, "cache miss saved" attr_info = product.get_all_attribute_info(language=get_language(), visibility_mode=AttributeVisibility.SHOW_ON_PRODUCT_PAGE) assert set(attr_info.keys()) <= set(a["identifier"] for a in ATTR_SPECS), "get_all_attribute_info gets all attribute info"
def cache_product_things(request, products, language=None, attribute_identifiers=("author",)): # Cache necessary things for products. WARNING: This will cause queryset iteration. language = language or get_language() # TODO: Should we cache prices here? if attribute_identifiers: Product.cache_attributes_for_targets( ProductAttribute, products, attribute_identifiers=attribute_identifiers, language=language) products = cache_translations(products, (language,)) return products
def cache_product_things(request, products, language=None, attribute_identifiers=("author", )): # Cache necessary things for products. WARNING: This will cause queryset iteration. language = language or get_language() # TODO: Should we cache prices here? if attribute_identifiers: Product.cache_attributes_for_targets( ProductAttribute, products, attribute_identifiers=attribute_identifiers, language=language) products = cache_translations(products, (language, )) return products
def test_applied_attributes(): product = get_default_product() for spec in ATTR_SPECS: # This loop sets each attribute twice. That's okay. attr = Attribute.objects.get(identifier=spec["identifier"]) pa, _ = ProductAttribute.objects.get_or_create(product=product, attribute=attr) _populate_applied_attribute(pa) pa.save() if not attr.is_translated: product.set_attribute_value(attr.identifier, pa.value) assert product.get_attribute_value( "bogomips") == 320, "integer attribute loaded neatly" product.set_attribute_value("bogomips", 480) assert product.get_attribute_value( "bogomips") == 480, "integer attribute updated neatly" Product.cache_attributes_for_targets( applied_attr_cls=ProductAttribute, targets=[product], attribute_identifiers=[a["identifier"] for a in ATTR_SPECS], language=get_language()) assert ( get_language(), "bogomips", ) in product._attr_cache, "integer attribute in cache" assert product.get_attribute_value( "bogomips") == 480, "integer attribute value in cache" assert product.get_attribute_value( "ba:gelmips", default="Britta" ) == "Britta", "non-existent attributes return default value" assert product._attr_cache[(get_language(), "ba:gelmips" )] is NoSuchAttributeHere, "cache miss saved" attr_info = product.get_all_attribute_info( language=get_language(), visibility_mode=AttributeVisibility.SHOW_ON_PRODUCT_PAGE) assert set(attr_info.keys()) <= set( a["identifier"] for a in ATTR_SPECS), "get_all_attribute_info gets all attribute info"