Add Task CRUD API tests

This commit is contained in:
sosokker 2023-10-30 15:46:55 +07:00
parent 8025746e93
commit 82ac48f3de
4 changed files with 140 additions and 0 deletions

View File

@ -23,6 +23,7 @@ class TaskCreateView(CreateAPIView):
class TaskRetrieveView(RetrieveAPIView):
queryset = Task.objects.all()
serializer_class = TaskGeneralSerializer
permission_classes = [IsAuthenticated]
class TaskUpdateView(RetrieveUpdateAPIView):

View File

View File

@ -0,0 +1,73 @@
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from .utils import create_test_user, login_user
from ..models import Task
class TaskCreateViewTests(APITestCase):
def setUp(self):
self.user = create_test_user()
self.client = login_user(self.user)
self.url = reverse("add-task")
def test_create_valid_task(self):
"""
Test creating a valid task using the API.
"""
data = {
'title': 'Test Task',
'type': 'habit',
'exp': 10,
'attribute': 'str',
'priority': 1.5,
'difficulty': 1,
'user': self.user.id,
}
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Task.objects.count(), 1)
self.assertEqual(Task.objects.get().title, 'Test Task')
def test_create_invalid_task(self):
"""
Test creating an invalid task using the API.
"""
data = {
'type': 'invalid', # Invalid task type
}
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Task.objects.count(), 0) # No task should be created
def test_missing_required_fields(self):
"""
Test creating a task with missing required fields using the API.
"""
data = {
'title': 'Incomplete Task',
'type': 'habit',
}
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Task.objects.count(), 0) # No task should be created
def test_invalid_user_id(self):
"""
Test creating a task with an invalid user ID using the API.
"""
data = {
'title': 'Test Task',
'type': 'habit',
'exp': 10,
'priority': 1.5,
'difficulty': 1,
'user': 999, # Invalid user ID
}
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Task.objects.count(), 0) # No task should be created

View File

@ -0,0 +1,66 @@
from rest_framework.test import APIClient
from users.models import CustomUser
from ..models import Task
def create_test_user(email="testusertestuser@example.com", username="testusertestuser",
first_name="Test", password="testpassword",):
"""create predifined user for testing"""
return CustomUser.objects.create_user(
email=email,
username=username,
first_name=first_name,
password=password,
)
def login_user(user):
"""Login a user to API client."""
client = APIClient()
client.force_authenticate(user=user)
return client
def create_task_json(user, **kwargs):
"""Create task JSON data to use with the API."""
defaults = {
"title": "Test Task",
"type": "habit",
"notes": "This is a test task created via the API.",
"exp": 10,
"priority": 1.5,
"difficulty": 1,
"attribute": "str",
"challenge": False,
"reminders": False,
"fromSystem": False,
"creation_date": None,
"last_update": None,
}
task_attributes = {**defaults, **kwargs}
task_attributes["user"] = user
return task_attributes
def create_test_task(user, **kwargs):
"""Create a test task and associate it with the given user."""
defaults = {
'title': "Test Task",
'task_type': 'habit',
'notes': "This is a test task created via the API.",
'exp': 10,
'priority': 1.5,
'difficulty': 1,
'attribute': 'str',
'challenge': False,
'reminders': False,
'fromSystem': False,
}
task_attributes = {**defaults, **kwargs}
return Task.objects.create(user=user, **task_attributes)