Sławomir Kwiatkowski

by: Sławomir Kwiatkowski

2024/10/18

Django Rest Framework - API testing

Content description:
In this post I'll describe how to test a ModelViewSet based on the created Contract model.
I'll test the methods: GET, POST, PATCH and DELETE.
I'll check the correctness of the validators and permissions.

I create a Contract model.

The model consists of fields:

  • contract status (default: open),
  • contract number set by the customer,
  • contractor who is to perform the order,
  • planned delivery date and time,
  • warehouse to which the goods are to be delivered.

The delivery date contains a validator - it cannot be earlier than the order date.

2024/10/02

Django Rest Framework

Content description:
In this post I'll describe how to create a ModelViewSet based on the created Warehouse model. Then I'll describe how to create a serializer for the given model. I'll describe how to create custom permissions for the created ViewSet. Finally I'll make tests for the created API.

First, I make simple Warehouse model with foreign key - a client (the warehouse owner).

     
from django.db import models
from users.models import ContractUser


class Warehouse(models.Model):
    warehouse_name = models.CharField(max_length=15, unique=True)
    warehouse_info = models.TextField(max_length=100)
    client = models.ForeignKey(ContractUser, on_delete=models.CASCADE)

    def __str__(self):
        return self.warehouse_name