Ejemplo n.º 1
0
    def test_validation(self):
        # no data - not valid
        form = AdvancedSearchForm(data={})
        self.assertFalse(form.is_valid(),
            'advanced search form is not valid when no fields are specified')
        self.assertTrue(form.non_field_errors(),
            'a non-field error is displayed when no search terms are entered')
        # any one field - valid
        form = AdvancedSearchForm(data={'keywords': 'foo'})
        self.assertTrue(form.is_valid(),
            'advanced search form is valid when only keywords are specified')
        form = AdvancedSearchForm(data={'subject': 'bar'})
        self.assertTrue(form.is_valid(),
            'advanced search form is valid when only subject is specified')
        # FIXME: this could break due to caching when testing & running a dev site at the same time
        # adjust the test to clear the cache or use a separate cache for tests

        # grab a valid choice from the current options in the form:
        repo = form.fields['repository'].choices[0][0]  # id value of first option tuple
        form = AdvancedSearchForm(data={'repository': repo})
        self.assertTrue(form.is_valid(),
            'advanced search form is valid when only repository is specified')