def test_attributes_to_hstore(product, color_attribute): color_value = color_attribute.values.first() # test transforming slugs of existing attributes to IDs input_data = [{"slug": color_attribute.slug, "value": color_value.slug}] attrs_qs = product.product_type.product_attributes.all() ids = attributes_to_hstore(input_data, attrs_qs) assert str(color_attribute.pk) in ids assert ids[str(color_attribute.pk)] == str(color_value.pk) # test creating a new attribute value input_data = [{"slug": color_attribute.slug, "value": "Space Grey"}] ids = attributes_to_hstore(input_data, attrs_qs) new_value = AttributeValue.objects.get(slug="space-grey") assert str(color_attribute.pk) in ids assert ids[str(color_attribute.pk)] == str(new_value.pk) # test passing an attribute that doesn't belong to this product raises # an error input_data = [{"slug": "not-an-attribute", "value": "not-a-value"}] with pytest.raises(ValueError): attributes_to_hstore(input_data, attrs_qs)
def test_attributes_to_hstore(product, color_attribute): color_value = color_attribute.values.first() # test transforming slugs of existing attributes to IDs input_data = [{"slug": color_attribute.slug, "value": color_value.slug}] attrs_qs = product.product_type.product_attributes.all() ids = attributes_to_hstore(input_data, attrs_qs) assert str(color_attribute.pk) in ids assert ids[str(color_attribute.pk)] == str(color_value.pk) # test creating a new attribute value input_data = [{"slug": color_attribute.slug, "value": "Space Grey"}] ids = attributes_to_hstore(input_data, attrs_qs) new_value = AttributeValue.objects.get(slug="space-grey") assert str(color_attribute.pk) in ids assert ids[str(color_attribute.pk)] == str(new_value.pk) # test passing an attribute that doesn't belong to this product raises # an error input_data = [{"slug": "not-an-attribute", "value": "not-a-value"}] with pytest.raises(ValueError): attributes_to_hstore(input_data, attrs_qs)
def test_attributes_to_hstore_duplicated_slug(product, color_attribute, size_attribute): # It's possible to have a value with the same slug but for a different attribute. # Ensure that `attributes_to_hstore` works in that case. color_value = color_attribute.values.first() # Create a fake duplicated value. AttributeValue.objects.create( slug=color_value.slug, name="Duplicated value", attribute=size_attribute ) input_data = [{"slug": color_attribute.slug, "value": color_value.slug}] attrs_qs = product.product_type.product_attributes.all() ids = attributes_to_hstore(input_data, attrs_qs) assert str(color_attribute.pk) in ids assert ids[str(color_attribute.pk)] == str(color_value.pk)
def test_attributes_to_hstore_duplicated_slug(product, color_attribute, size_attribute): # It's possible to have a value with the same slug but for a different attribute. # Ensure that `attributes_to_hstore` works in that case. color_value = color_attribute.values.first() # Create a fake duplicated value. AttributeValue.objects.create(slug=color_value.slug, name="Duplicated value", attribute=size_attribute) input_data = [{"slug": color_attribute.slug, "value": color_value.slug}] attrs_qs = product.product_type.product_attributes.all() ids = attributes_to_hstore(input_data, attrs_qs) assert str(color_attribute.pk) in ids assert ids[str(color_attribute.pk)] == str(color_value.pk)