Hello, everybody.
I have the following model
class Paciente(models.Model):
cedula = models.CharField(max_length=8, validators=[validar_cedula])
nombre = models.CharField(max_length=50)
apellido = models.CharField(max_length=50)
sexo = models.CharField(max_length=1, choices=GENERO_CHOICES, default="M")
fecha_de_nacimiento = models.DateField(validators=[validar_fecha_de_nacimiento])
def __str__(self):
return f"{self.nombre} {self.apellido}"
and its view:
class PacienteCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
model = models.Paciente
fields = ["cedula", "nombre", "apellido", "sexo", "fecha_de_nacimiento"]
success_message = '''Paciente registrado satisfactoriamente.'''
permissions = ["can_add_paciente"]
def get_success_url(self):
return reverse_lazy('paciente_list')
the url:
path("paciente/add/", views.PacienteCreateView.as_view(), name="paciente_add"),
and I wrote some test for the view:
class PacienteCreateViewTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.view_url = reverse('paciente_add')
cls.redirect_url = reverse("paciente_list")
cls.redirect_url_not_logged = "/accounts/login/?next=/consultorio/paciente/add/"
cls.user = CustomUser.objects.create_user(
email="doctor@ejemplo.com",
first_name="doctor",
last_name="ejemplo",
sexo="F",
password="nucote77",
)
cls.doctor = DoctorFactory(user=cls.user)
cls.consultorio = ConsultorioFactory(doctor=cls.doctor)
cls.doctor_data = {
"username": "doctor@ejemplo.com",
"password": "nucote77"
}
cls.paciente_data = {
"cedula": "3242918",
"nombre": "Carmen",
"apelido": "García",
"sexo": "F",
"fecha_de_nacimiento": "1947-03-29"
}
def setUp(self):
self.client.login(**self.doctor_data)
self.response = self.client.get(self.view_url)
def test_url_exists_at_correct_location(self):
self.assertEqual(self.view_url, "/consultorio/paciente/add/")
def test_url_available_by_name(self):
self.assertEqual(self.response.status_code, 200)
def test_signup_template(self):
self.assertTemplateUsed(self.response, 'consultorio/paciente_form.html')
self.assertTemplateNotUsed(self.response, 'plantilla,html')
self.assertContains(self.response, '<p>Agregar Nuevo Paciente</p>')
self.assertNotContains(self.response, 'Hi there')
def test_view_name(self):
view = resolve(self.view_url)
self.assertEqual(view.func.__name__, PacienteCreateView.as_view().__name__)
def test_post_sucess(self):
response = self.client.post(self.view_url, data=self.paciente_data, follow=True)
print(response.redirect_chain)
self.assertEquals(response.status_code, 302)
self.assertRedirects(response, self.redirect_url)
def test_redirects_if_user_not_logged(self):
self.client.logout()
response = self.client.get(self.view_url)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, self.redirect_url_not_logged)
and it throws the following error:
Found 6 test(s).
System check identified no issues (0 silenced).
F.....
======================================================================
FAIL: test_post_sucess (consultorio.test.test_views.PacienteCreateViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\goyo\Documents\django\proyecto_consultorio\consultorio\test\test_views.py", line 119, in test_post_sucess
self.assertEquals(response.status_code, 302)
AssertionError: 200 != 302
----------------------------------------------------------------------
Ran 6 tests in 4.471s
FAILED (failures=1)
Destroying test database for alias 'default'...
[]
I don’t know why this is happening, I have another model with a almost equal view and test and it doesn’t fail at all. Tkanks in advance for your valuable help.