Skip to content

Commit

Permalink
🧪 test: adicionando o teste de requisição do endpoint "listar_licitac…
Browse files Browse the repository at this point in the history
…oes"
  • Loading branch information
thaleseuflauzino committed Aug 12, 2024
1 parent fcb50ec commit 463054f
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions backend/server/app/tests.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APIClient
from rest_framework import status
from .models import Orgao
from rest_framework.test import APITestCase
from app.models import Orgao, Licitacao
from datetime import datetime
class LicitacaoTests(APITestCase):

class URLTests(TestCase):
def setUp(self):
# Configura o cliente de teste da API
self.client = APIClient()
# Criar múltiplos objetos Orgao para testar a paginação e a busca
for i in range(15):
Orgao.objects.create(id=i+1, nome=f'Orgao Teste {i+1}')
# Criar um objeto Orgao específico para o teste de nome_orgaos_por_id

self.orgao = Orgao.objects.create(id=16, nome='Orgao Teste 16')

for i in range(15):
Licitacao.objects.create(
tipo='Tipo Teste',
data=(datetime.now()).strftime('%d/%m/%Y'),
objeto=f'Objeto Teste {i+1}',
idorgao=self.orgao,
valores=[1000 * (i+1)]
)

def test_nome_orgaos_por_id(self):
# Faz uma requisição GET para a URL nome_orgaos_por_id com o ID do orgao criado
Expand All @@ -36,4 +42,24 @@ def test_listar_orgaos_busca(self):
# Verifica se o status da resposta é 200 OK
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Verifica se pelo menos um dos resultados contém 'Teste 1' no nome
self.assertTrue(any('Teste 1' in orgao['nome'] for orgao in response.data['results']))
self.assertTrue(any('Teste 1' in orgao['nome'] for orgao in response.data['results']))

def test_listar_licitacoes_paginacao(self):
response = self.client.get(reverse('listar_licitacoes'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data['results']), 10)

def test_listar_licitacoes_filtro_search(self):
response = self.client.get(reverse('listar_licitacoes'), {'search': 'Teste 1'})
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_listar_licitacoes_filtro_orgao(self):
response = self.client.get(reverse('listar_licitacoes'), {'idorgao': self.orgao.id})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(all(licitacao['idorgao'] == self.orgao.id for licitacao in response.data['results']))

def test_listar_licitacoes_ordenar_por_valor(self):
response = self.client.get(reverse('listar_licitacoes'), {'ordering': 'valores'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
valores = [licitacao['valores'][0] for licitacao in response.data['results'] if licitacao['valores']]
self.assertEqual(valores, sorted(valores))

0 comments on commit 463054f

Please sign in to comment.