On Sat, Nov 22, 2008 at 6:52 AM, Florencio Cano <[EMAIL PROTECTED]>wrote:

> Hi!
> I'm testing a Django application where I'm using i18n.
> The problem is with a test that says that a word with an accent
> (spanish) is the same as a word without an accent. And although I have
> established my own error messages it shows other error message when
> using a word with accents.
> The problem is with the second test shown.
> I have these sentences in settings.py:
>
> TEST_DATABASE_CHARSET='utf8'
> TEST_DATABASE_COLLATION='utf8_spanish_ci'
>
> ------ code ------
>
> class Responsable(models.Model):
>    user = models.ForeignKey(User, unique=True)
>    nombre = models.CharField('Nombre', max_length=200, unique=True)
>    email = models.EmailField('Email', max_length=100, unique=True)
>    responsable_lopd = models.BooleanField('Responsable LOPD')
>
>    def __unicode__(self):
>        return self.nombre
>
>
> class ResponsableForm(forms.ModelForm):
>    def clean_nombre(self):
>        nombre = self.cleaned_data['nombre']
>        if nombre in [u.nombre for u in Responsable.objects.all()]:
>            raise forms.ValidationError('Ya existe un responsable con
> este nombre')
>        return nombre
>
>    class Meta:
>        model = Responsable
>

Why are doing your own uniqueness check for the nombre field?  You have
specified unique=True for nombre so the default ModelForm validation will
check uniqueness.  The ModelForm validation will (essentially) attempt to
get() a Responsable object with that nombre from the DB -- leaving it up to
the DB to find a match and return that one match, if it exists.  The check
as you have written it is much less efficient since it requires retrieving
all Responsable objects from the DB and then manipulating them in Python.
Plus, it will not see 'Jose Lopez' as a duplicate of 'José López' -- as far
as Python is concerned these are different strings.


>
>
> def test_agregar_responsable_nombre_duplicado(self):
>    respuesta = self.client.post('/responsables/agregar/',
>        {
>            'username':'pepe',
>            'password':'qwerty',
>            'confirma_password':'qwerty',
>            'nombre':'Pepe Lopez',
>            'email':'[EMAIL PROTECTED]<[EMAIL PROTECTED]>
> ',
>            'responsable_lopd':False
>        })
>
>
>    respuesta2 = self.client.post('/responsables/agregar/',
>        {
>            'username':'josele',
>            'password':'qwerty',
>            'confirma_password':'qwerty',
>            'nombre':'Pepe Lopez',
>            'email':'[EMAIL PROTECTED]<[EMAIL PROTECTED]>
> ',
>            'responsable_lopd':False
>     })
>
>    self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya
> existe un responsable con este nombre')
>
>
> def test_agregar_responsable_nombre_duplicado_tildes(self):
>    respuesta = self.client.post('/responsables/agregar/',
>        {
>            'username':'josele',
>            'password':'qwerty',
>            'confirma_password':'qwerty',
>            'nombre':'José López',
>            'email':'[EMAIL PROTECTED]<[EMAIL PROTECTED]>
> ',
>            'responsable_lopd':False
>     })
>
>    respuesta2 = self.client.post('/responsables/agregar/',
>        {
>            'username':'joselito',
>            'password':'qwerty',
>            'confirma_password':'qwerty',
>            'nombre':'Jose Lopez',
>            'email':'[EMAIL PROTECTED]<[EMAIL PROTECTED]>
> ',
>            'responsable_lopd':False
>     })
>
>    self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya
> existe un responsable con este nombre')
>
>
> FAIL: test_agregar_responsable_nombre_duplicado_tildes
> (gesfich.fichlopd.tests.RegistrarUsuariosTestCase)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>  File "/home/fcano/GesFichLOPD/gesfich/../gesfich/fichlopd/tests.py",
> line 228, in test_agregar_responsable_nombre_duplicado_tildes
>    self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya
> existe un responsable con este nombre')
>  File "/usr/lib/python2.5/site-packages/django/test/testcases.py",
> line 317, in assertFormError
>    repr(field_errors)))
> AssertionError: The field 'nombre' on form 'responsable_form' in
> context 0 does not contain the error 'Ya existe un responsable con
> este nombre' (actual errors: [u'Responsable with this Nombre already
> exists.'])
>

What you see here is the built-in ModelForm message reporting that the
nombre you have specified violates the unique constraint.  That is, the
check you provided in the field's clean did not find the duplicate, but the
DB did (given the utf8_spanish_ci collation you are using, which considers
the accented characters to be the same as the non-accented version).

Karen

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

Reply via email to