Skip to content

Commit

Permalink
check for issue change
Browse files Browse the repository at this point in the history
  • Loading branch information
knightebsuku committed Feb 3, 2025
1 parent 2959d59 commit fd5596e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
57 changes: 53 additions & 4 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def test_create(self):
self.assertEqual(resp.json(), {
'id': task.id,
'project': {
'id': task.project.id,
'title': 'New Project',
'code': 'NP',
'created_at': self.project.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
Expand All @@ -215,7 +216,8 @@ def test_create(self):
'username': 'creator',
'email': ''
},
'subtasks': []
'subtasks': [],
'parent': None
})

def test_filter(self):
Expand Down Expand Up @@ -271,6 +273,21 @@ def test_exclude_archive(self):
self.assertEqual(resp.status_code, 200)
self.assertEqual(len(resp.json()), 1)

def test_update_task_issue(self):
task = TaskFactory(project=self.project, created_by=self.creator, assigned_to=self.assignee)
self.assertEqual(task.issue, Issue.EPIC)
data = {
'project_id': task.project.id,
'assigned_to_id': task.assigned_to.id,
'title': task.title,
'description': task.description,
'issue': Issue.SUBTASK.name,
}
url = reverse('task-detail', kwargs={'code': task.code})
resp = self.client.put(url, data, format='json')
self.assertEqual(resp.status_code, 400)
self.assertEqual(resp.json(), {'issue': ['Subtask needs a parent']})


class SubTasksTestCase(APITestCase):
url = reverse('task-list')
Expand Down Expand Up @@ -322,7 +339,6 @@ def test_subtask(self):
'title': 'First Subtask',
'description': 'Do stuff here',
'issue': 'SUBTASK',
'parent': self.task.id
}
resp = self.client.post(self.url, data, format='json')
self.assertEqual(resp.status_code, 201)
Expand All @@ -348,12 +364,14 @@ def test_subtask(self):
'email': ''
},
'project': {
'id': self.project.id,
'code': 'NP',
'created_at': self.project.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
'title': 'New Project',
'url': f'{self.test_server}{reverse("project-detail", kwargs={"pk": self.project.id})}'
},
'subtasks': []
'subtasks': [],
'parent': self.task.id
})

def test_subtask_list(self):
Expand All @@ -375,6 +393,7 @@ def test_subtask_list(self):
'issue': 'EPIC',
'modified_at': self.task.modified_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
'project': {
'id': self.project.id,
'code': 'NP',
'created_at': self.project.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
'title': 'New Project',
Expand All @@ -389,9 +408,39 @@ def test_subtask_list(self):
'title': 'First Task',
'url': f"{self.test_server}{reverse('task-detail', kwargs={'code': subtask.code})}"
}],
'title': 'First Task'}
'title': 'First Task',
'parent': None
}
])

def test_subtask_to_epic(self):
"""
Remove parent if subtask becomes epic
"""
subtask = TaskFactory(
project=self.project, issue=Issue.SUBTASK,
created_by=self.creator, assigned_to=self.assignee,
parent=self.task, code='NP-100'
)
data = {
'project_id': subtask.project.id,
'assigned_to_id': subtask.assigned_to.id,
'title': subtask.title,
'description': subtask.description,
'issue': Issue.EPIC,
'parent_id': None
}

url = reverse('task-detail', kwargs={'code': subtask.code})
resp = self.client.put(url, data, format='json')
self.assertEqual(resp.status_code, 200)
subtask.refresh_from_db()
self.assertEqual(subtask.parent, None)






class AdminFormTestCase(APITestCase):
def setUp(self) -> None:
Expand Down
12 changes: 8 additions & 4 deletions umsebenzi/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MinialProjectSerializer(serializers.ModelSerializer):

class Meta:
model = Project
fields = ('title', 'code', 'created_at', 'url')
fields = ('id', 'title', 'code', 'created_at', 'url')


class ProjectSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -65,7 +65,7 @@ class Meta:
class TaskSerializer(serializers.ModelSerializer):
project_id = serializers.PrimaryKeyRelatedField(write_only=True, queryset=Project.objects.all())
assigned_to_id = serializers.PrimaryKeyRelatedField(write_only=True, queryset=User.objects.all())
parent_id = serializers.PrimaryKeyRelatedField(write_only=True, required=False, queryset=Task.objects.all())
parent_id = serializers.PrimaryKeyRelatedField(write_only=True, required=False, queryset=Task.objects.all(), allow_null=True)
project = MinialProjectSerializer(read_only=True)
assigned_to = UserSerializer(read_only=True)
created_by = UserSerializer(read_only=True)
Expand All @@ -78,7 +78,7 @@ class Meta:
fields = (
'id', 'project_id', 'project', 'title', 'description', 'assigned_to',
'assigned_to_id', 'created_by', 'status', 'code', 'due_date',
'created_at', 'modified_at', 'parent_id', 'subtasks', 'issue'
'created_at', 'modified_at', 'parent_id', 'subtasks', 'issue', 'parent'
)
read_only_fields = ('code', 'created_at', 'modified_at')

Expand All @@ -92,10 +92,13 @@ def get_subtasks(self, obj: Task):
def validate(self, attrs):
issue = attrs.get('issue')
parent = attrs.get('parent_id')
if issue is Issue.EPIC and parent:
# breakpoint()
if issue == Issue.EPIC and parent:
raise serializers.ValidationError('Epic task cannot have parent')
if parent and parent.issue is Issue.SUBTASK and issue == Issue.SUBTASK:
raise serializers.ValidationError('Subtask cannot have subtask parent')
if issue == Issue.SUBTASK and not parent:
raise serializers.ValidationError({'issue': 'Subtask needs a parent'})
return attrs

def create(self, validated_data):
Expand All @@ -119,5 +122,6 @@ def update(self, instance: Task, validated_data):
instance.title = validated_data.get('title', instance.title)
instance.description = validated_data.get('description', instance.description)
instance.status = validated_data.get('status', instance.status)
instance.issue = validated_data.get('issue', instance.status)
instance.save()
return instance

0 comments on commit fd5596e

Please sign in to comment.