Skip to content

Commit

Permalink
Check for changes in issues
Browse files Browse the repository at this point in the history
  • Loading branch information
knightebsuku committed Feb 3, 2025
1 parent fd5596e commit fbf71a2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
27 changes: 22 additions & 5 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,27 @@ def test_update_task_issue(self):
self.assertEqual(resp.status_code, 400)
self.assertEqual(resp.json(), {'issue': ['Subtask needs a parent']})

def test_epic_to_subtask(self):
"""
Block moving epic to subtask, if epic has subtasks
"""
parent = TaskFactory(project=self.project, created_by=self.creator, code='NP-3')
epic_task = TaskFactory(project=self.project, created_by=self.creator)
TaskFactory(project=self.project, issue=Issue.SUBTASK, parent=epic_task, code='NP-2', created_by=self.creator)

data = {
'project_id': epic_task.project.id,
'assigned_to_id': epic_task.assigned_to.id,
'title': epic_task.title,
'description': epic_task.description,
'issue': Issue.SUBTASK.name,
'parent_id': parent.id
}
url = reverse('task-detail', kwargs={'code': epic_task.code})
resp = self.client.put(url, data, format='json')
self.assertEqual(resp.status_code, 400)
self.assertEqual(resp.json(), {'issue': ['Subtask cannot have subtasks']})


class SubTasksTestCase(APITestCase):
url = reverse('task-list')
Expand All @@ -311,7 +332,7 @@ def test_epic_parent(self):
}
resp = self.client.post(self.url, data, format='json')
self.assertEqual(resp.status_code, 400)
self.assertEqual(resp.json(), {'non_field_errors': ['Epic task cannot have parent']})
self.assertEqual(resp.json(), {'issue': ['Epic task cannot have parent']})

def test_subtask_subtask(self):
subtask = TaskFactory(
Expand Down Expand Up @@ -438,10 +459,6 @@ def test_subtask_to_epic(self):
self.assertEqual(subtask.parent, None)






class AdminFormTestCase(APITestCase):
def setUp(self) -> None:
self.user = User.objects.create(username='creator', password='password')
Expand Down
12 changes: 9 additions & 3 deletions umsebenzi/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ def get_subtasks(self, obj: Task):
def validate(self, attrs):
issue = attrs.get('issue')
parent = attrs.get('parent_id')
# breakpoint()
if issue == Issue.EPIC and parent:
raise serializers.ValidationError('Epic task cannot have parent')
raise serializers.ValidationError({'issue': '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:
Expand All @@ -116,9 +115,16 @@ def create(self, validated_data):
)

def update(self, instance: Task, validated_data):
if Task.objects.filter(parent=instance).exists():
raise serializers.ValidationError({'issue': ['Subtask cannot have subtasks']})

issue = validated_data.get('issue')
if issue == Issue.EPIC:
instance.parent = None
else:
instance.parent = validated_data.get('parent_id', instance.parent)
instance.project = validated_data.get('project_id', instance.project)
instance.assigned_to = validated_data.get('assigned_to_id', instance.assigned_to)
instance.parent = validated_data.get('parent_id', instance.parent)
instance.title = validated_data.get('title', instance.title)
instance.description = validated_data.get('description', instance.description)
instance.status = validated_data.get('status', instance.status)
Expand Down

0 comments on commit fbf71a2

Please sign in to comment.