def test_product_attribute(admin_user): shop = get_default_shop() client = _get_client(admin_user) product1 = create_product("product1") attrib1 = create_random_product_attribute() attrib2 = create_random_product_attribute() product_type = get_default_product_type() product_type.attributes.add(attrib1) product_type.attributes.add(attrib2) product_attr1_data = {"attribute": attrib1.pk, "numeric_value": 0} product_attr2_data = {"attribute": attrib2.pk, "numeric_value": 1} response = client.post("/api/shuup/product/%d/add_attribute/" % product1.pk, content_type="application/json", data=json.dumps(product_attr1_data)) assert response.status_code == status.HTTP_201_CREATED response = client.post("/api/shuup/product/%d/add_attribute/" % product1.pk, content_type="application/json", data=json.dumps(product_attr2_data)) assert response.status_code == status.HTTP_201_CREATED attrs = ProductAttribute.objects.filter(product=product1) # get the first attr response = client.get("/api/shuup/product_attribute/%d/" % attrs[0].id) assert response.status_code == status.HTTP_200_OK data = json.loads(response.content.decode("utf-8")) assert data["product"] == product1.id assert data["attribute"] == attrib1.id assert Decimal(data["numeric_value"]) == Decimal(0) # get the 2nd attr response = client.get("/api/shuup/product_attribute/%d/" % attrs[1].id) assert response.status_code == status.HTTP_200_OK data = json.loads(response.content.decode("utf-8")) assert data["product"] == product1.id assert data["attribute"] == attrib2.id assert Decimal(data["numeric_value"]) == Decimal(1) # update the first attr - numeric_value=1, attr=2 attr_data = {"numeric_value": 1, "attribute": attrib2.pk} response = client.put("/api/shuup/product_attribute/%d/" % attrs[0].id, content_type="application/json", data=json.dumps(attr_data)) assert response.status_code == status.HTTP_200_OK response = client.get("/api/shuup/product_attribute/%d/" % attrs[0].id) assert response.status_code == status.HTTP_200_OK data = json.loads(response.content.decode("utf-8")) assert data["product"] == product1.id assert data["attribute"] == attrib2.id assert Decimal(data["numeric_value"]) == Decimal(1) # deletes the last attr response = client.delete("/api/shuup/product_attribute/%d/" % attrs[1].id) assert response.status_code == status.HTTP_204_NO_CONTENT assert ProductAttribute.objects.count() == 1
def test_product_type_api(admin_user): get_default_shop() client = APIClient() client.force_authenticate(user=admin_user) product_type_data = { "translations": { "en": { "name": "type 1" } }, "attributes": [ create_random_product_attribute().pk, create_random_product_attribute().pk, create_random_product_attribute().pk ] } response = client.post("/api/shuup/product_type/", content_type="application/json", data=json.dumps(product_type_data)) assert response.status_code == status.HTTP_201_CREATED product_type = ProductType.objects.first() assert product_type.name == product_type_data["translations"]["en"]["name"] assert set(product_type.attributes.all().values_list( "pk", flat=True)) >= set(product_type_data["attributes"]) product_type_data["translations"]["en"]["name"] = "name 2" product_type_data["attributes"] = [ create_random_product_attribute().pk, create_random_product_attribute().pk, create_random_product_attribute().pk ] response = client.put("/api/shuup/product_type/%d/" % product_type.id, content_type="application/json", data=json.dumps(product_type_data)) assert response.status_code == status.HTTP_200_OK product_type = ProductType.objects.first() assert product_type.name == product_type_data["translations"]["en"]["name"] assert set(product_type.attributes.all().values_list( "pk", flat=True)) >= set(product_type_data["attributes"]) response = client.get("/api/shuup/product_type/%d/" % product_type.id) assert response.status_code == status.HTTP_200_OK data = json.loads(response.content.decode("utf-8")) assert product_type.name == data["translations"]["en"]["name"] assert set(product_type.attributes.all().values_list( "pk", flat=True)) >= set(product_type_data["attributes"]) response = client.get("/api/shuup/product_type/") assert response.status_code == status.HTTP_200_OK data = json.loads(response.content.decode("utf-8")) assert product_type.name == data[0]["translations"]["en"]["name"] assert set(product_type.attributes.all().values_list( "pk", flat=True)) >= set(data[0]["attributes"]) # delete response = client.delete("/api/shuup/product_type/%d/" % product_type.id) assert response.status_code == status.HTTP_204_NO_CONTENT assert ProductType.objects.count() == 0 # create a product and relate it to a product type product_type = ProductType.objects.create(name="type") product = create_product("product with product_type", type=product_type) # deleting product type should set product type to null response = client.delete("/api/shuup/product_type/%d/" % product_type.id) assert response.status_code == 204 product.refresh_from_db() assert product.type is None
def mock_product_attribute(self, **kwargs): """ Create a random product attribute """ return create_random_product_attribute()
def test_product_type_api(admin_user): get_default_shop() client = APIClient() client.force_authenticate(user=admin_user) product_type_data = { "translations": { "en": { "name": "type 1" } }, "attributes": [ create_random_product_attribute().pk, create_random_product_attribute().pk, create_random_product_attribute().pk ] } response = client.post("/api/shuup/product_type/", content_type="application/json", data=json.dumps(product_type_data)) assert response.status_code == status.HTTP_201_CREATED product_type = ProductType.objects.first() assert product_type.name == product_type_data["translations"]["en"]["name"] assert set(product_type.attributes.all().values_list("pk", flat=True)) >= set(product_type_data["attributes"]) product_type_data["translations"]["en"]["name"] = "name 2" product_type_data["attributes"] = [ create_random_product_attribute().pk, create_random_product_attribute().pk, create_random_product_attribute().pk ] response = client.put("/api/shuup/product_type/%d/" % product_type.id, content_type="application/json", data=json.dumps(product_type_data)) assert response.status_code == status.HTTP_200_OK product_type = ProductType.objects.first() assert product_type.name == product_type_data["translations"]["en"]["name"] assert set(product_type.attributes.all().values_list("pk", flat=True)) >= set(product_type_data["attributes"]) response = client.get("/api/shuup/product_type/%d/" % product_type.id) assert response.status_code == status.HTTP_200_OK data = json.loads(response.content.decode("utf-8")) assert product_type.name == data["translations"]["en"]["name"] assert set(product_type.attributes.all().values_list("pk", flat=True)) >= set(product_type_data["attributes"]) response = client.get("/api/shuup/product_type/") assert response.status_code == status.HTTP_200_OK data = json.loads(response.content.decode("utf-8")) assert product_type.name == data[0]["translations"]["en"]["name"] assert set(product_type.attributes.all().values_list("pk", flat=True)) >= set(data[0]["attributes"]) # delete response = client.delete("/api/shuup/product_type/%d/" % product_type.id) assert response.status_code == status.HTTP_204_NO_CONTENT assert ProductType.objects.count() == 0 # create a product and relate it to a product type product_type = ProductType.objects.create(name="type") product = create_product("product with product_type", type=product_type) # shouldn't be possible to delete a product type with a related product response = client.delete("/api/shuup/product_type/%d/" % product_type.id) assert response.status_code == status.HTTP_400_BAD_REQUEST assert "This object can not be deleted because it is referenced by" in response.content.decode("utf-8")