コード例 #1
0
    def test_list_products_by_tags(self):
        owner = sample_user()
        store1 = sample_store(owner)
        product1 = sample_product(owner, store1, title='Product A')
        product1.tags.add('fabric', 'disney')
        product2 = sample_product(owner, store1, title='Product B')
        product2.tags.add('fabric')
        product3 = sample_product(owner, store1, title='Product C')
        product3.tags.add('target')

        url = product_url(store1.slug)
        res = self.client.get(url, {'tags': 'fabric, disney'})

        serializer = serializers.ProductSerializer(product1)
        serializer2 = serializers.ProductSerializer(product3)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertIn(serializer.data, res.data)
        self.assertNotIn(serializer2.data, res.data)
コード例 #2
0
    def test_get_products_by_collection_any(self):
        owner = sample_user()
        store = sample_store(owner)
        collection = sample_collection(owner, store, type=Collection.ANY)
        sample_condition(collection, field_val='Disney')
        sample_condition(collection, field_val='TsumTsum')
        product1 = sample_product(owner, store, title='Product A')
        product1.tags.add('TsumTsum', 'disney')
        product2 = sample_product(owner, store, title='Product B')
        product2.tags.add('TsumTsum')
        product3 = sample_product(owner, store, title='Product C')
        product3.tags.add('Should Not Be here')

        url = product_by_collection_url(store.slug, collection.id)
        res = self.client.get(url)

        serializer_in1 = serializers.ProductSerializer(product1)
        serializer_in2 = serializers.ProductSerializer(product2)
        serializer_notin1 = serializers.ProductSerializer(product3)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertIn(serializer_in1.data, res.data)
        self.assertIn(serializer_in2.data, res.data)
        self.assertNotIn(serializer_notin1.data, res.data)
コード例 #3
0
    def test_get_products_by_collection_all(self):
        owner = sample_user()
        store = sample_store(owner)
        collection = sample_collection(owner, store, type=Collection.ALL)
        sample_condition(collection, field_val='Disney')
        sample_condition(collection, field_val='TsumTsum')
        product1 = sample_product(owner, store, title="Product A")
        product1.tags.add("Disney", "TsumTsum")
        product2 = sample_product(owner, store, title="Product B")
        product2.tags.add("Disney")
        product3 = sample_product(owner, store, title="Product C")
        product3.tags.add("green")

        url = product_by_collection_url(store.slug, collection.id)
        res = self.client.get(url)

        serializer_in1 = serializers.ProductSerializer(product1)
        serializer_in2 = serializers.ProductSerializer(product2)
        serializer_notin1 = serializers.ProductSerializer(product3)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertIn(serializer_in1.data, res.data)
        self.assertIn(serializer_in2.data, res.data)
        self.assertNotIn(serializer_notin1.data, res.data)
コード例 #4
0
    def test_list_all_products(self):
        owner = sample_user()
        owner2 = sample_user(email='*****@*****.**')
        store1 = sample_store(owner)
        store2 = sample_store(owner2, title='House of Nails')

        product1 = sample_product(owner, store1)
        sample_product(owner2, store2)

        url = product_url(store1.slug)
        res = self.client.get(url)
        serializer = serializers.ProductSerializer(product1)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertIn(serializer.data, res.data)
コード例 #5
0
    def test_get_products_with_type(self):
        owner = sample_user()
        store = sample_store(owner)
        product_type = ProductType.objects.create(name='Quilting Cotton',
                                                  store=store,
                                                  user=owner)
        product1 = sample_product(owner,
                                  store,
                                  title='Product A',
                                  type=product_type)

        url = detail_url(store.slug, product1.id)
        res = self.client.get(url)

        serializer = serializers.ProductSerializer(product1)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(res.data, serializer.data)
コード例 #6
0
    def test_get_products_by_collection_complex(self):
        owner = sample_user()
        store = sample_store(owner)
        collection = sample_collection(owner, store, type=Collection.ALL)
        pt = ProductType.objects.create(name='Quilting Cotton',
                                        store=store,
                                        user=owner)
        pt2 = ProductType.objects.create(name='Zippers',
                                         store=store,
                                         user=owner)
        pt3 = ProductType.objects.create(name='Thread',
                                         store=store,
                                         user=owner)
        product1 = sample_product(owner, store, title="Product A", type=pt)
        product1.tags.add("Disney", "TsumTsum")
        product2 = sample_product(owner, store, title="Product B", type=pt2)
        product3 = sample_product(owner, store, title="Product C", type=pt3)
        sample_condition(collection,
                         field_val=pt.name,
                         field_reference=Condition.PRODUCT_TYPE)
        sample_condition(collection,
                         field_val=pt2.name,
                         field_reference=Condition.PRODUCT_TYPE)
        sample_condition(collection, field_val='Disney')
        url = product_by_collection_url(store.slug, collection.id)
        res = self.client.get(url)

        serializer1 = serializers.ProductSerializer(product1)
        serializer2 = serializers.ProductSerializer(product2)
        serializer3 = serializers.ProductSerializer(product3)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertIn(serializer1.data, res.data)
        self.assertNotIn(serializer2.data, res.data)
        self.assertNotIn(serializer3.data, res.data)

        collection.type = Collection.ANY
        collection.save()
        collection.refresh_from_db()

        res = self.client.get(url)

        serializer1 = serializers.ProductSerializer(product1)
        serializer2 = serializers.ProductSerializer(product2)
        serializer3 = serializers.ProductSerializer(product3)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertIn(serializer1.data, res.data)
        self.assertIn(serializer2.data, res.data)
        self.assertNotIn(serializer3.data, res.data)
コード例 #7
0
    def product_list(self, request, store, pk):
        collection = Collection.objects.get(pk=pk, store__slug=store)
        products = collection.get_products()
        serializer = serializers.ProductSerializer(products, many=True)

        return Response(serializer.data, status=status.HTTP_200_OK)