Exemplo n.º 1
0
 def test_get_absolute_url(self):
     """
     Verify that the url returned by the method contains the product id
     """
     product = Products()
     product.id = 10
     url = product.get_absolute_url()
     self.assertIn("10", url)
    def test_check_contains_with_hit(self, mock_db):
        """
        Verify that the method returns the query set if a hit is found

        :param mock_db: mock the search against Products table
        """
        product1 = Products(name="test")
        mock_db.return_value = [product1]
        results = search_products.check_contains("test")
        self.assertEqual(len(results), 1)
        product2 = Products(name="test")
        mock_db.return_value = [product1, product2]
        results = search_products.check_contains("test")
        self.assertEqual(len(results), 2)
Exemplo n.º 3
0
    def test_details(self, mock_product, mock_nutrival):
        """
        Verify that the details page returns the following information on a
        product:
            the product object
            the nutritional values associated to this product
            the correct nutriscore image based on product rating

        :param mock_product: mock the query against Products table
        :param mock_nutrival: mock the search against NutritionalValues table
        """
        mock_product.return_value = MagicMock(
            side_effect=Products.objects.filter())
        mock_product.return_value.first.return_value = Products(rating="a")
        mock_nutrival.return_value = MagicMock(
            side_effect=Products.objects.filter())
        mock_nutrival.return_value.first.return_value = NutritionalValues()
        response = self.client.get("/details/1/")
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "search/base.html")
        self.assertTemplateUsed(response, "search/search_form.html")
        self.assertTemplateUsed(response, "search/details.html")
        self.assertIsInstance(response.context["product"], Products)
        self.assertIsInstance(response.context["nutrival"], NutritionalValues)
        self.assertIn("nutriscore-a", response.context["nutriscore"])
 def test_get_category(self):
     """
     A product's category is return when the product is passed to the
     method
     """
     category = Categories(name="test_category")
     product = Products(category=category)
     result = search_products.get_category(product)
     self.assertEqual(result.name, "test_category")
     self.assertIsInstance(result, Categories)
    def test_get_products(self, mock_db):
        """
        Verify that the method returns a list of products with the order_by
        method called with the rating parameter

        :param mock_db: mock the search against Products table
        """
        category = Categories(name="test")
        product = Products(name="test")
        mock_db.return_value = MagicMock(
            side_effect=Products.objects.filter(category=category)
        )
        mock_db.return_value.order_by.return_value = [product]
        result = search_products.get_products(category)
        self.assertEqual(len(result), 1)
        self.assertIsInstance(result, list)
        mock_db.assert_called_with(category=category)
        mock_db.return_value.order_by.assert_called_with("rating")
    def test_get_suggestions_check_name_hit(self, mock_search, mock_results):
        """
        Verify that if the check name method returns, get suggestions
        returns a tuple as well

        :param mock_search: mock of check_name method
        :param mock_results:  mock of get_products method
        """
        query_cat = Categories(name="test")
        query_result = Products(category=query_cat)
        mock_search.return_value = MagicMock(
            side_effect=Products.objects.filter(),
            return_value=query_result)
        mock_search.return_value.first.return_value = query_result
        mock_results.return_value = [query_result]
        result = search_products.get_suggestions("test")
        self.assertIsInstance(result, tuple)
        self.assertIsInstance(result[0], Products)
        self.assertIsInstance(result[1], list)
Exemplo n.º 7
0
    def handle(self, *args, **options):
        CLEAR_PRODUCTS = Products.objects.all()

        CLEAR_PRODUCTS.delete()

        for category in CATEGORIES:
            for nutriscore in NUTRISCORES:
                try:
                    payload = {
                        "action": "process",
                        "json": 1,
                        "tagtype_0": "categories",
                        "tag_contains_0": "contains",
                        "tag_0": category,
                        "tagtype_1": "nutrition_grades",
                        "tag_contains_1": "contains",
                        "tag_1": nutriscore,
                        "countries": "France",
                        "page_size": 500,
                        "sort_by": "unique_scans_n",
                    }
                    response = requests.get(
                        "https://world.openfoodfacts.org/cgi/search.pl",
                        params=payload,
                        headers={
                            "User-Agent": "Apptest - GNU/Linux - Version 0.1"
                        },
                    )
                    response = response.json()
                    response = response["products"]
                except HTTPError as err:
                    print(
                        "Openfoodfacts server might be busy or has crashed, or check your connection : {}\n"
                        .format(err))

                except ConnectionError as con_err:
                    print(
                        "Openfoodfacts server in not reachable, check your connexion {}"
                        .format(con_err))

                for item in response:
                    try:
                        # We want to check if the provided url is correct
                        url_validator = URLValidator()
                        url_validator(item["image_small_url"])
                        # Let's try to log this on DB
                        query = Products(
                            barcode=item["id"],
                            image=item["image_small_url"],
                            category=item["categories"],
                            name=item["product_name"],
                            nutriscore=item["nutrition_grades"],
                        )
                        query.save()
                    except KeyError:  #If an attribute is missing, pass
                        pass
                    except ValidationError:
                        query = Products(
                            barcode=item["id"],
                            image=None,
                            category=item["categories"],
                            name=item["product_name"],
                            nutriscore=item["nutrition_grades"],
                        )
                        query.save()

            print("Extraction of {} category, done.".format(category))
        print("Extraction DONE. Dabatbase up-to-date")