Example #1
0
    def test_delete_product(self):
        self.client.login(username='******', password='******')
        products = []
        test_product = Product(id="1",
                               product_name='testproduct',
                               description='this is a test description',
                               price='13.99',
                               image='test.jpg',
                               rating='8')
        test_product.delete()

        page = self.client.get("/products/", {"products": products})

        self.assertEqual(page.status_code, 200)
        self.assertTemplateUsed(page, "products.html")
Example #2
0
class CartTests(TestCase):
    """To test validation of the Cart model """
    def setUp(self):
        self.user = get_user_model().objects.create_user(
            username='******',
            password='******',
            email='*****@*****.**')
        self.user.save()
        self.cart = Cart(id='1', user=self.user, created=datetime.now)
        self.product = Product(name="TestProduct",
                               description="TestProduct description",
                               price="100.99",
                               image="images/test.jpg",
                               rating="0.0")
        self.product.save()
        self.cart_item = CartItem(id='1',
                                  cart=self.cart,
                                  product=self.product,
                                  quantity='1')

    def tearDown(self):
        self.product.delete()
        self.cart_item.delete()
        self.cart.delete()
        self.user.delete()

    def test_str(self):
        self.assertEqual(self.cart.user, self.user)
        self.assertEqual(self.cart_item.cart, self.cart)

    def test_cart_item_quantity_blank(self):
        self.cart_item.quantity = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "value must be an integer."):
            self.cart_item.full_clean()

    def test_cart_item_product_blank(self):
        with self.assertRaises(ValueError):
            self.cart_item.product = ''
class OrderTests(TestCase):
    """To test validation of the Order model """
    def setUp(self):
        self.order = Order(username="******",
                           full_name="Test User",
                           phone_number="0871234567",
                           country="Ireland",
                           postcode="D1",
                           town_or_city="Dublin",
                           street_address1="123 Street",
                           street_address2="Rathgar",
                           county="Dublin",
                           date="2020-02-04")
        self.order.save()
        self.product = Product(name="TestProduct",
                               description="TestProduct description",
                               price="100.99",
                               image="images/test.jpg",
                               rating="0.0")
        self.product.save()
        self.order_line_item = OrderLineItem(order=self.order,
                                             product=self.product,
                                             quantity="0")
        self.order_line_item.save()

    def tearDown(self):
        self.order_line_item.delete()
        self.order.delete()
        self.product.delete()

    def test_str(self):
        self.assertEqual(self.order.full_name, 'Test User')
        self.assertEqual(self.order.date, '2020-02-04')
        self.assertEqual(self.order_line_item.product, self.product)
        self.assertEqual(self.order_line_item.quantity, '0')

    def test_username_blank(self):
        self.order.username = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "This field cannot be blank."):
            self.order.full_clean()

    def test_username_max(self):
        self.order.username = '******'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 20 characters"):
            self.order.full_clean()

    def test_fullname_blank(self):
        self.order.full_name = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "This field cannot be blank."):
            self.order.full_clean()

    def test_fullname_max(self):
        self.order.full_name = 'abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyz'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 50 characters"):
            self.order.full_clean()

    def test_phone_blank(self):
        self.order.phone_number = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "This field cannot be blank."):
            self.order.full_clean()

    def test_phone_max(self):
        self.order.phone_number = '123456789012345678901'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 20 characters"):
            self.order.full_clean()

    def test_country_blank(self):
        self.order.country = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "This field cannot be blank."):
            self.order.full_clean()

    def test_country_max(self):
        self.order.country = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmno'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 40 characters"):
            self.order.full_clean()

    #Postcode can be blank
    def test_postcode_blank(self):
        self.order.postcode = ''
        self.assertEqual(self.order.postcode, '')

    def test_postcode_max(self):
        self.order.postcode = 'abcdefghijklmnopqrstu'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 20 characters"):
            self.order.full_clean()

    def test_town_or_city_blank(self):
        self.order.town_or_city = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "This field cannot be blank."):
            self.order.full_clean()

    def test_town_or_city_max(self):
        self.order.town_or_city = 'abcdefghijklmnopqrstuvwxyabcdefghijklmnop'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 40 characters"):
            self.order.full_clean()

    def test_street_address1_blank(self):
        self.order.street_address1 = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "This field cannot be blank."):
            self.order.full_clean()

    def test_street_address1_max(self):
        self.order.street_address1 = 'abcdefghijklmnopqrstuvwxyabcdefghijklmnop'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 40 characters"):
            self.order.full_clean()

    def test_street_address2_blank(self):
        self.order.street_address2 = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "This field cannot be blank."):
            self.order.full_clean()

    def test_street_address2_max(self):
        self.order.street_address2 = 'abcdefghijklmnopqrstuvwxyabcdefghijklmnop'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 40 characters"):
            self.order.full_clean()

    def test_county_blank(self):
        self.order.county = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "This field cannot be blank."):
            self.order.full_clean()

    def test_county_max(self):
        self.order.county = 'abcdefghijklmnopqrstuvwxyabcdefghijklmnop'
        with self.assertRaisesRegexp(
                ValidationError,
                "Ensure this value has at most 40 characters"):
            self.order.full_clean()

    def test_order_quantity_blank(self):
        self.order_line_item.quantity = ''
        with self.assertRaisesRegexp(ValidationError,
                                     "value must be an integer."):
            self.order_line_item.full_clean()

    def test_order_product_blank(self):
        with self.assertRaises(ValueError):
            self.order_line_item.product = ''
Example #4
0
class IndexViewTests(TestCase):
    
    def setUp(self):
        patcher = mock.patch("research.views.SearchForm")
        self.addCleanup(patcher.stop)
        mock_form_class = patcher.start()
        self.mock_form = mock_form_class.return_value
        # a category 
        self.category = Category(name="jus de fruits")
        self.category.save()
        self.product1 = Product(
            name="Pur jus d'orange sans pulpe", 
            image_url="https://static.openfoodfacts.org/images/products/350/211/000/9449/front_fr.80.400.jpg",
            url="https://world.openfoodfacts.org/product/3502110009449/pur-jus-d-orange-sans-pulpe-tropicana", 
            nutriscore=3, packaging="carton")
        self.product2 = Product(
            name="Jus d'orange pas bon pas bio pas cher", 
            image_url="fake url",
            url="fake url", 
            nutriscore=5, packaging="osef")
        self.product1.save()
        self.product2.save()
        self.product1.category.add(self.category)
        self.product2.category.add(self.category)
        self.product1.save()
        self.product2.save()

    def tearDown(self):
        self.category.delete()
        self.product1.delete()
        self.product2.delete()

    def test_get_access_page(self):
        """ Tests whether the function returns a web page """
        response = self.client.get('http://127.0.0.1:8000/p10/index')
        self.assertEqual(response.status_code, 200)
        
    def test_index_post_form_is_not_valid(self):
        """ Tests whether the function loads an error page if form is not valid """
        self.mock_form.is_valid.return_value = False
        response = self.client.post(reverse("research:index"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Problème dans le formulaire")
    
    def test_index_post_find_result(self):
        """ Tests whether the function gets the input data, find a prod, subs and load aresult page """
        self.mock_form.is_valid.return_value = True
        self.mock_form.cleaned_data = {"simple_search" : "Pur jus d'orange sans pulpe"}
        response = self.client.post(reverse("research:index"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Vous pouvez remplacer cet aliment par ... ! ")


    def test_index_post_do_not_find_result(self):
        """ Tests whether the function gets the input data, but do not find anything """
        self.mock_form.is_valid.return_value = True
        self.mock_form.cleaned_data = {"simple_search" : "123"}
        response = self.client.post(reverse("research:index"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Pas de résultats dans la base actuellement")

    def test_index_post_no_result_but_category(self):
        """ Tests whether the function gets the input data,  do not find anything but propose new categories """
        self.mock_form.is_valid.return_value = True
        self.mock_form.cleaned_data = {"simple_search" : "pas sans pulpe"}
        response = self.client.post(reverse("research:index"), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Pas de résultats parfaitement identiques ")
Example #5
0
 def delelte(self, request, pk):
     Product = self.get_object(pk)
     Product.delete()
     return Response(status=status.HTTP_204_NO_CONTENT)