def test_instructor_availability_not_required(self):
     """Test `instructor_availability` not required in some circumstances."""
     # Arrange
     data1 = {
         "preferred_dates": date.today() + timedelta(days=30),
         "other_preferred_dates": "",
         "instructor_availability": "1",
     }
     data2 = {
         "preferred_dates": "",
         "other_preferred_dates": "sometime in future",
         "instructor_availability": "1",
     }
     data3 = {
         "preferred_dates": date.today() + timedelta(days=61),
         "other_preferred_dates": "",
         "instructor_availability": "",
     }
     # Act
     form1 = WorkshopInquiryRequestBaseForm(data1)
     form2 = WorkshopInquiryRequestBaseForm(data2)
     form3 = WorkshopInquiryRequestBaseForm(data3)
     # Assert
     for form in (form1, form2, form3):
         self.assertNotIn("preferred_dates", form.errors)
         self.assertNotIn("other_preferred_dates", form.errors)
         self.assertNotIn("instructor_availability", form.errors)
    def test_dates_validation(self):
        """Ensure preferred dates validation."""
        # 1: both empty won't trigger error - the field is not required
        data = {
            "preferred_dates": "",
            "other_preferred_dates": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("preferred_dates", form.errors)
        self.assertNotIn("other_preferred_dates", form.errors)

        # 2: either one present will work
        data = {
            "preferred_dates": "{:%Y-%m-%d}".format(date.today()),
            "other_preferred_dates": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("preferred_dates", form.errors)
        self.assertNotIn("other_preferred_dates", form.errors)

        data = {
            "preferred_dates": "",
            "other_preferred_dates": "Next weekend",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("preferred_dates", form.errors)
        self.assertNotIn("other_preferred_dates", form.errors)

        # 3: preferred date from the past
        data = {
            "preferred_dates": "2000-01-01",
            "other_preferred_dates": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertIn("preferred_dates", form.errors)
        self.assertNotIn("other_preferred_dates", form.errors)

        # 4: preferred date wrong format
        data = {
            "preferred_dates": "{:%d-%m-%Y}".format(date.today()),
            "other_preferred_dates": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertIn("preferred_dates", form.errors)
        self.assertNotIn("other_preferred_dates", form.errors)
    def test_dates_validation(self):
        """Ensure preferred dates validation."""
        # 1: both empty won't trigger error - the field is not required
        data = {
            'preferred_dates': '',
            'other_preferred_dates': '',
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn('preferred_dates', form.errors)
        self.assertNotIn('other_preferred_dates', form.errors)

        # 2: either one present will work
        data = {
            'preferred_dates': '{:%Y-%m-%d}'.format(datetime.date.today()),
            'other_preferred_dates': '',
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn('preferred_dates', form.errors)
        self.assertNotIn('other_preferred_dates', form.errors)

        data = {
            'preferred_dates': '',
            'other_preferred_dates': 'Next weekend',
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn('preferred_dates', form.errors)
        self.assertNotIn('other_preferred_dates', form.errors)

        # 3: preferred date from the past
        data = {
            'preferred_dates': '2000-01-01',
            'other_preferred_dates': '',
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertIn('preferred_dates', form.errors)
        self.assertNotIn('other_preferred_dates', form.errors)

        # 4: preferred date wrong format
        data = {
            'preferred_dates': '{:%d-%m-%Y}'.format(datetime.date.today()),
            'other_preferred_dates': '',
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertIn('preferred_dates', form.errors)
        self.assertNotIn('other_preferred_dates', form.errors)
 def test_instructor_availability_required(self):
     """Test requiredness of `instructor_availability` depending on selected
     preferred dates."""
     # Arrange
     data1 = {
         "preferred_dates": date.today() + timedelta(days=30),
         "other_preferred_dates": "",
         "instructor_availability": "",
     }
     data2 = {
         "preferred_dates": "",
         "other_preferred_dates": "sometime in future",
         "instructor_availability": "",
     }
     # Act
     form1 = WorkshopInquiryRequestBaseForm(data1)
     form2 = WorkshopInquiryRequestBaseForm(data2)
     # Assert
     for form in (form1, form2):
         self.assertNotIn("preferred_dates", form.errors)
         self.assertNotIn("other_preferred_dates", form.errors)
         self.assertIn("instructor_availability", form.errors)
    def test_institution_validation(self):
        """Make sure institution data is present, and validation
        errors are triggered for various matrix of input data."""

        # 1: selected institution from the list
        data = {
            "institution": Organization.objects.first().pk,
            "institution_other_name": "",
            "institution_other_URL": "",
            "institution_department": "School of Wizardry",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("institution", form.errors)
        self.assertNotIn("institution_other_name", form.errors)
        self.assertNotIn("institution_other_URL", form.errors)
        self.assertNotIn("institution_department", form.errors)

        # 2: institution name manually entered
        data = {
            "institution": "",
            "institution_other_name": "Hogwarts",
            "institution_other_URL": "hogwarts.uk",
            "institution_department": "School of Wizardry",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("institution", form.errors)
        self.assertNotIn("institution_other_name", form.errors)
        self.assertNotIn("institution_other_URL", form.errors)
        self.assertNotIn("institution_department", form.errors)

        # 3: no institution and no department
        #    contrary to WorkshopRequestBaseForm, this form doesn't require
        #    institution
        data = {
            "institution": "",
            "institution_other_name": "",
            "institution_other_URL": "",
            "institution_department": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        # institution is NOT required
        self.assertNotIn("institution", form.errors)
        self.assertNotIn("institution_other_name", form.errors)
        self.assertNotIn("institution_other_URL", form.errors)
        self.assertNotIn("institution_department", form.errors)

        # 4: other name, but no other URL (+ no institution)
        data = {
            "institution": "",
            "institution_other_name": "Hogwarts",
            "institution_other_URL": "",
            "institution_department": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("institution", form.errors)
        self.assertIn("institution_other_name", form.errors)
        self.assertNotIn("institution_other_URL", form.errors)
        self.assertNotIn("institution_department", form.errors)

        # 5: other URL, but no other name (+ no institution)
        data = {
            "institution": "",
            "institution_other_name": "",
            "institution_other_URL": "hogwarts.uk",
            "institution_department": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("institution", form.errors)
        self.assertIn("institution_other_name", form.errors)
        self.assertNotIn("institution_other_URL", form.errors)
        self.assertNotIn("institution_department", form.errors)

        # 6: institution, other name, no other URL
        data = {
            "institution": Organization.objects.first().pk,
            "institution_other_name": "Hogwarts",
            "institution_other_URL": "",
            "institution_department": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("institution", form.errors)
        self.assertIn("institution_other_name", form.errors)
        self.assertNotIn("institution_other_URL", form.errors)
        self.assertNotIn("institution_department", form.errors)

        # 7: institution, other URL, no other name
        data = {
            "institution": Organization.objects.first().pk,
            "institution_other_name": "",
            "institution_other_URL": "hogwarts.uk",
            "institution_department": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("institution", form.errors)
        self.assertNotIn("institution_other_name", form.errors)
        self.assertIn("institution_other_URL", form.errors)
        self.assertNotIn("institution_department", form.errors)

        # 8: wrong URL format
        data = {
            "institution": "",
            "institution_other_name": "Hogwarts",
            "institution_other_URL": "wrong_url",
            "institution_department": "",
        }
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("institution", form.errors)
        self.assertIn("institution_other_name", form.errors)
        self.assertIn("institution_other_URL", form.errors)
        self.assertNotIn("institution_department", form.errors)
 def test_minimal_form(self):
     """Test if minimal form works."""
     data = self.minimal_data.copy()
     form = WorkshopInquiryRequestBaseForm(data)
     self.assertTrue(form.is_valid(), dict(form.errors))
    def test_dont_know_yet(self):
        """Make sure selecting 'Don't know yet' + other option in various
        fields yields errors."""
        # 1: routine data, domains, academic levels, computing levels and
        #    requested workshop types are not required
        data = self.minimal_data.copy()
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertNotIn("routine_data", form.errors)
        self.assertNotIn("domains", form.errors)
        self.assertNotIn("academic_levels", form.errors)
        self.assertNotIn("computing_levels", form.errors)
        self.assertNotIn("requested_workshop_types", form.errors)

        # 2: routine data
        self._test_dont_know_yet_option(
            Form=WorkshopInquiryRequestBaseForm,
            field="routine_data",
            value_normal=DataVariant.objects.filter(unknown=False).first().pk,
            value_dont_know_yet=DataVariant.objects.filter(unknown=True).first().pk,
        )
        # additionally test against `routine_data_other`
        data = self.minimal_data.copy()
        data["routine_data"] = [DataVariant.objects.filter(unknown=True).first().pk]
        data["routine_data_other"] = "Other routine data"
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertIn("routine_data", form.errors)

        # 3: domains
        self._test_dont_know_yet_option(
            Form=WorkshopInquiryRequestBaseForm,
            field="domains",
            value_normal=KnowledgeDomain.objects.exclude(name="Don't know yet")
            .first()
            .pk,
            value_dont_know_yet=KnowledgeDomain.objects.filter(name="Don't know yet")
            .first()
            .pk,
        )
        # additionally test against `domains_other`
        data = self.minimal_data.copy()
        data["domains"] = [
            KnowledgeDomain.objects.filter(name="Don't know yet").first().pk
        ]
        data["domains_other"] = "Other domains"
        form = WorkshopInquiryRequestBaseForm(data)
        self.assertIn("domains", form.errors)

        # 4: academic levels
        self._test_dont_know_yet_option(
            Form=WorkshopInquiryRequestBaseForm,
            field="academic_levels",
            value_normal=AcademicLevel.objects.exclude(name="Don't know yet")
            .first()
            .pk,
            value_dont_know_yet=AcademicLevel.objects.filter(name="Don't know yet")
            .first()
            .pk,
        )

        # 5: computing levels
        self._test_dont_know_yet_option(
            Form=WorkshopInquiryRequestBaseForm,
            field="computing_levels",
            value_normal=ComputingExperienceLevel.objects.exclude(name="Don't know yet")
            .first()
            .pk,
            value_dont_know_yet=ComputingExperienceLevel.objects.filter(
                name="Don't know yet"
            )
            .first()
            .pk,
        )

        # 6: requested workshop types
        base_curriculum = Curriculum.objects.default_order(
            allow_other=True, allow_unknown=True
        ).filter(active=True)
        self._test_dont_know_yet_option(
            Form=WorkshopInquiryRequestBaseForm,
            field="requested_workshop_types",
            value_normal=base_curriculum.filter(unknown=False).first().pk,
            value_dont_know_yet=base_curriculum.filter(unknown=True).first().pk,
        )