Ошибка утверждения Pytest в django rest framework - PullRequest
0 голосов
/ 01 мая 2020

моя модель вот так

class Appeal(models.Model):

    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
    other_category = models.CharField(max_length=500, null=True, blank=True)
    file = models.FileField(upload_to='appeals/', null=True)
    short_name = models.CharField(max_length=100)
    appeal_desc = models.TextField()
    state = models.IntegerField(choices=STATUS, default=NEW)
    dept = models.ForeignKey(Department, on_delete=models.SET_NULL, null=True)
    location = models.PointField(srid=4326, null=True)
    address = models.CharField(max_length=255)
    history = HistoricalRecords()
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)

Я собираюсь протестировать эту модель, но у меня не получается, я не знаю, где я делаю ошибку, Pytest выдает только ошибку, подобную этой

>       assert appeal_res.status_code == status.HTTP_201_CREATED
E       AssertionError: assert 400 == 201
E         +400
E         -201

весь мой тест здесь

class AppealTestCase(APITestCase):
    client = APIClient()

    def post_appeal(self, short_name, appeal_desc, address, location, category):
        url = reverse('appeal-create')
        data = {'short_name': short_name, 'appeal_desc': appeal_desc,
                'address': address, 'location': location, 'category_id': category}

        response = self.client.post(url, data, format='json')
        return response

    def create_user_set_token(self):
        user = User.objects.create_user(username='test', password='test123')
        token = Token.objects.create(user=user)
        self.client.credentials(HTTP_AUTHORIZATION='Token {0}'.format(token.key))

    def test_post_and_get_appeal(self):
        self.create_user_set_token()
        category = Category.objects.create(name='Test Category').id
        print(category)
        short_name = 'bla bla'
        desc = 'test desc'
        location = 'SRID=4326;POINT (1.406108835239464 17.66601562254118)'
        address = 'test address'
        appeal_res = self.post_appeal(short_name, desc, address, location, category)

        assert appeal_res.status_code == status.HTTP_201_CREATED

Вот что я пробовал до сих пор. Если кто-нибудь знает, пожалуйста, помогите мне в этом вопросе, с или без Category модель Я получаю ту же ошибку. Заранее спасибо!

1 Ответ

0 голосов
/ 01 мая 2020

[решено]

моя ошибка заключалась в объявлении FK в тестовом примере.

Исправлена ​​ошибка:

class AppealTestCase(APITestCase):
    client = APIClient()

    def post_appeal(self, category, short_name, appeal_desc, dept, location, address, ):
        url = reverse('appeal-create')
        print(url)
        data = {
            'category': category,
            'short_name': short_name,
            'appeal_desc': appeal_desc,
            'dept': dept,
            'location': location,
            'address': address
        }
        response = self.client.post(url, data, format='json')
        return response

    def create_user_and_set_token_credentials(self):
        user = User.objects.create_user(username='test', password='test123')
        token = Token.objects.create(user=user)
        self.client.credentials(HTTP_AUTHORIZATION='Token {0}'.format(token.key))

    def test_post_and_get_appeal(self):
        self.create_user_and_set_token_credentials()
        category = Category.objects.create(name='test')
        short_name = 'name short'
        appeal_desc = 'description'
        dept = Department.objects.create(name='test dept')
        location = 'SRID=4326;POINT (1.406108835239464 17.66601562254118)'
        address = 'test address'
        response = self.post_appeal(
            category.id,
            short_name,
            appeal_desc,
            dept.id,
            location,
            address
        )
        assert response.status_code == status.HTTP_201_CREATED
...