Skip to content

Commit 2cec592

Browse files
authored
Merge pull request #7 from omidcodes/007-add-a-sample-unit-test
add tests
2 parents af0b8e5 + 520c0ec commit 2cec592

8 files changed

Lines changed: 51 additions & 1 deletion

File tree

pytest.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# pytest.ini tells Pytest how to use Django:
2+
[pytest]
3+
DJANGO_SETTINGS_MODULE = taskflow_api.settings
4+
python_files = tests.py test_*.py *_tests.py
5+
# adopts Speeds up tests by reusing the test DB & Measures coverage
6+
addopts = --reuse-db --cov=. --cov-report=term-missing

requirements.txt

38 Bytes
Binary file not shown.

taskflow_api/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,6 @@
124124

125125
CELERY_ACCEPT_CONTENT = ["json"]
126126
CELERY_TASK_SERIALIZER = "json"
127+
128+
129+
SWAGGER_USE_COMPAT_RENDERERS = False

tasks/tests.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

tasks/tests/__init__.py

Whitespace-only changes.

tasks/tests/test_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from rest_framework.test import APIClient
3+
from django.urls import reverse
4+
from datetime import date
5+
6+
@pytest.mark.django_db
7+
def test_create_task_api():
8+
client = APIClient()
9+
url = reverse("task-list") # Adjust if you use a different route name
10+
response = client.post(url, {
11+
"title": "API Task",
12+
"due_date": date.today(),
13+
}, format="json")
14+
assert response.status_code == 201
15+
assert response.data["title"] == "API Task"

tasks/tests/test_celery.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
from tasks.models import Task
3+
from tasks.tasks import log_task_action
4+
from datetime import date
5+
import os
6+
7+
@pytest.mark.django_db
8+
def test_log_task_action(tmp_path, settings):
9+
# Ensure log directory exists
10+
os.makedirs("logs", exist_ok=True)
11+
task = Task.objects.create(title="CeleryTest", due_date=date.today())
12+
log_task_action(task.id, "created")
13+
14+
# Check the log file for correct entry
15+
with open("logs/task_activity.log") as f:
16+
log_lines = f.readlines()
17+
assert any("CeleryTest" in line for line in log_lines)

tasks/tests/test_models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
from tasks.models import Task
3+
from datetime import date
4+
5+
# @pytest.mark.django_db allows DB access in your test.
6+
@pytest.mark.django_db
7+
def test_task_creation():
8+
task = Task.objects.create(title="Testing", due_date=date.today())
9+
assert task.id is not None
10+
assert task.title == "Testing"

0 commit comments

Comments
 (0)