示例#1
0
    def test_order_form_valid_quantity_only(self):
        """Test the OrderForm is valid with only quantities supplied."""
        form = OrderForm({"magazine_count": "20", "book_count": "50"})

        self.assertTrue(form.is_valid())

        self.assertEqual(form.cleaned_data["magazine_count"], 20)
        self.assertEqual(form.cleaned_data["book_count"], 50)
        self.assertFalse(form.cleaned_data["send_confirmation"])
        self.assertEqual(form.cleaned_data["email"], "")
示例#2
0
 def test_email_domain_validation(self):
     """Test that only emails on the example.com domain are accepted."""
     form = OrderForm({
         "magazine_count": "80",
         "book_count": "50",
         "email": "*****@*****.**"
     })
     self.assertFalse(form.is_valid())
     self.assertEqual(
         form.errors["email"],
         ["The email address must be on the domain example.com."])
示例#3
0
    def test_order_form_quantity_exceeded(self):
        """Test the OrderForm has a non-field error when the totals exceed 100."""
        form = OrderForm({
            "magazine_count": "80",
            "book_count": "50",
            "send_confirmation": "on",
            "email": "*****@*****.**"
        })

        self.assertFalse(form.is_valid())

        self.assertEqual(form.non_field_errors(),
                         ["The total number of items must be 100 or less."])
示例#4
0
    def test_order_form_valid_all_fields(self):
        """Test the OrderForm with valid data."""
        form = OrderForm({
            "magazine_count": "20",
            "book_count": "50",
            "send_confirmation": "on",
            "email": "*****@*****.**"
        })

        self.assertTrue(form.is_valid())

        self.assertEqual(form.cleaned_data["magazine_count"], 20)
        self.assertEqual(form.cleaned_data["book_count"], 50)
        self.assertTrue(form.cleaned_data["send_confirmation"])

        # this also is a test of email cleaning (to lowercase)
        self.assertEqual(form.cleaned_data["email"], "*****@*****.**")
示例#5
0
    def test_email_required_on_send_confirmation(self):
        """
        The email should only be required if send_confirmation is on. Likewise send_confirmation must be on if email
        address is entered.
        """
        form = OrderForm({
            "magazine_count": "80",
            "book_count": "50",
            "email": "*****@*****.**"
        })

        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors["send_confirmation"],
            ["Please check this if you want to receive a confirmation email."])

        form = OrderForm({
            "magazine_count": "80",
            "book_count": "50",
            "send_confirmation": "on"
        })

        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors["email"], [
            "Please enter an email address to receive the confirmation message."
        ])