Fundamentos de Django Rest Framework
Crea y escala APIs con Django REST Framework
Introducción a las APIs, REST y JSON
Instalación de Django y Django REST Framework
Integración de Django REST Framework en proyectos Django
Cómo crear modelos y serializadores en Django REST Framework
Implementar vistas basadas en funciones en Django REST Framework
Gestión de Vistas Basadas en Funciones en Django REST Framework
¿Cómo modificar y eliminar recursos con Django REST Framework?
Postman y cURL en Django REST Framework
Refactorizar las funciones a clases en Django REST Framework
Refactorizando vistas en Django REST Framework con vistas genéricas
Documentación de APIs con Django REST, Swagger y OpenAPI
Vistas y Endpoints
Vistas Personalizadas y ViewSets en Django REST Framework
Manejos de Acciones con ViewSet en Django REST Framework
Autenticación y Gestión de Roles en Django REST Framework
Manejo de Errores y Validaciones
Serializadores Avanzados
¿Cómo implementar serializadores anidados en Django Django REST Framework?
¿Cómo usar SerializerMethodField en Django REST Framework?
Endpoint Anidado para Appointments Usando @action
Testing y Desempeño
Pruebas Unitarias para Endpoints Anidados Usando APIClient
Throttling en Django REST Framework
You don't have access to this class
Keep learning! Join and start boosting your career
Unit tests are essential to ensure that our APIs work correctly without having to spend too many resources. Django REST Framework facilitates this process through the APIClient
class, which allows us to simulate requests and validate the results easily and efficiently. Next, we will learn how to create unit tests in a Django project using this tool.
To start creating tests in Django REST Framework, we need to work with the test.py
file, which is automatically generated when we create a project. In this file, we define our tests by inheriting from the TestCase
class, which provides all the functionality needed to run tests in Django.
Within the test class, we use the setUp
method to prepare common data that we will reuse in all our tests, such as creating a patient and a doctor. Here, we employ the Django ORM to manage the models easily.
The APIClient
client is essential for our tests as it simulates HTTP requests, allowing us to test our API responses without making real requests. This saves us time and resources. In addition, it is automatically configured to work with JSON data, simplifying testing.
We import the client using:
from rest_framework.test import APIClient
This allows us to perform operations such as GET
, POST
, PUT
, and more, directly from our tests. For example, to verify that a list of appointments returns a code 200, we simply write a test that uses the client to make a GET
request to the appointments URL.
Django REST Framework provides the status
module, which allows us to check the response codes in a simple way. In the tests, we use the self.assertEqual()
method to compare the status code returned by the API with the expected value:
from rest_framework import status self.assertEqual(response.status_code, status.HTTP_200_OK).
This assures us that the API code is working correctly as expected.
To obtain URLs dynamically in our tests, we use Django's reverse()
method, which allows us to construct URLs based on their names. This is especially useful when working with URLs that require parameters, such as IDs.
It is common for some views in the Django REST Framework to require authentication or special permissions. If our tests fail due to permissions, we can adjust the settings in the viewset, making sure that the tests run under the same conditions that real users would face. For example, if only doctors can see certain data, we must make sure that the user in the test has those permissions.
If a test fails, it is crucial to review the error returned and adjust the code as necessary. Sometimes, the failure may be due to errors in permissions or settings in the viewsets. By correcting these errors and re-running the test, we can validate that the adjustments made have fixed the problem.
Contributions 4
Questions 0
Want to see more contributions, questions and answers from the community?