Hi,

I've found two similar (and potentially linked) weird problems in the
test framework while trying to understand why the perfectly looking
tests in #14608 were failing. Before I open some tickets I'd like to
hear if anybody has any clue about what's going on or if I'm missing
anything.

1) assertFieldOutput calls assertRaisesRegexp and passes the expected
error messages as a unicode string (in django/trunk/tests/
regressiontests/forms/localflavor/utils.py#L40):

    self.assertRaisesRegexp(ValidationError, unicode(errors),
                required.clean, input
            )

... then further down the track in assertRaisesRegexp, that string is
compiled as a regular expression (in django/trunk/django/utils/
unittest/case.py#L991):

    expected_regexp = re.compile(expected_regexp)

Now, the issue here is that the error message in the patch for #14608
contains some '-' characters, and that the errors list is cast to
unicode wrapping it with '[' and ']' characters:

    class INLocalFlavorTests(LocalFlavorTestCase):
        def test_INPhoneNumberField(self):
            error_format = [u'Phone numbers must be in 02X-7X or
03X-6X or 04X-5X format.']
        ...

and:

    unicode(errors) == u"[u'Phone numbers must be in 02X-7X or 03X-6X
or 04X-5X format.']"

Therefore, when assertRaisesRegexp compiles the unicode string it
interprets "X-7", "X-6" and "X-5" as ranges, and then throws a 'bad
character range' exception making the tests fail. If we remove those
'-' characters from the expected error message, then the tests will
pass.

I find it really weird that the error message is assumed to be a
compilable regular expression...

2) While debugging the problem above I've found another (possibly
linked) weirdness. Basically the error message that is passed to
assertRaisesRegexp is not always matched as you would expect. Here's a
simple test case I've made:

class AssertRaisesRegexp(TestCase):

    def test_error_message_match(self):
        def func():
            raise ValidationError("No no no!")
        self.assertRaisesRegexp(ValidationError, "No no no!", func) #
This passes - all good.
        self.assertRaises(AssertionError, self.assertRaisesRegexp,
ValidationError, "Yes yes yes!", func) # This passes, as expected.
        self.assertRaises(AssertionError, self.assertRaisesRegexp,
ValidationError, "[Yes yes yes]", func) # This fails, why?!

You'll notice that the only difference between the last two lines are
the use of '[' and ']' in the last line. So, when wrapping the error
message with '[' and ']', the error message is purely ignored and the
test just passes. This, by the way, means that pretty much all the
tests in 'regressiontests.forms.localflavor' using 'assertFieldOutput'
are currently passing without really being sure that the provided
error messages are correct.

I'm not sure how to properly fix this or where the core issue is. Am I
missing something or is it worth opening a ticket?

Thank you!

Julien

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to