Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create_task function #74

Merged
merged 8 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions tasks/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.db import models, transaction
from enumchoicefield import ChoiceEnum, EnumChoiceField
from users.models import User
from users.models import Role, User


class Status(ChoiceEnum):
Expand Down Expand Up @@ -53,6 +53,26 @@ def filter_by_symbol(cls, priority_filter):
print(priority_filter)
return cls.objects.filter(priority=priority_filter)

@classmethod
@transaction.atomic
def create_task(cls, title, assignee, created_by, priority, status, description):
if title == "":
raise ValueError("Title must contain at lease one character")
assigner_role = created_by.role
assigner_team = created_by.team
assigne_team = assignee.team
if assigne_team != assigner_team:
raise ValueError("Manager can assign tasks only for his own employees")
if assigner_role != Role.MANAGER:
orzionpour marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("User must be a manager to assign tasks")
task = Task.objects.create(title=title,
assignee=assignee,
created_by=created_by,
priority=priority,
status=status,
description=description)
return task


class Comment(models.Model):
appUser = models.ForeignKey(
Expand Down
145 changes: 145 additions & 0 deletions tasks/tests/test_create_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import pytest
from tasks.models import Priority, Status, Task
from users.models import User, Team, Role


@pytest.mark.django_db
class TestCreateTask:
"""
Add test team to DB
"""
@pytest.fixture
def team(self):
team = Team.objects.create(name="TestTeam",
description="This is a test team")
return team

"""
Add another test team to DB
"""
@pytest.fixture
def other_team(self):
team = Team.objects.create(name="TestOtherTeam",
description="This is a test team")
return team

"""
Add manager user to the DB
"""
@pytest.fixture
def manager(self, team):
manager = User.create_user(username="TestManager",
email="example@gmail.com",
password='xsdDS23',
first_name='Test',
last_name='Test',
role=Role.MANAGER,
team=team)
return manager

"""
Add an employee to the team in "team" fixture
"""
@pytest.fixture
def employee(self, team):
employee = User.create_user(username="TestEmployee",
email="example@gmail.com",
password='xsdDS23',
first_name='Test',
last_name='Test',
role=Role.EMPLOYEE,
team=team)
return employee

"""
Add an employee to the team in "other_team" fixture
"""
@pytest.fixture
def employee_other_team(self, other_team):
employee = User.create_user(username="TestEmployee",
email="example@gmail.com",
password='xsdDS23',
first_name='Test',
last_name='Test',
role=Role.EMPLOYEE,
team=other_team)
return employee

"""
Test add new task
"""
def test_add_task_to_db(self, manager, employee):
Task.create_task(title="TestTask",
assignee=employee,
created_by=manager,
priority=Priority.CRITICAL,
status=Status.BACKLOG,
description="This is a test task")
assert len(Task.objects.all()) == 1

"""
Test that a user who is not manager cannot assign task to other
teams employees.
"""
def test_assigned_by_non_manager(self, manager, employee):
assert employee.role != Role.MANAGER
with pytest.raises(Exception):
Task.create_task(title="TestTask",
assignee=manager,
created_by=employee,
priority=Priority.CRITICAL,
status=Status.BACKLOG,
description="This is a test task")
assert len(Task.objects.all()) == 0

"""
Test that title is required when creating a task.
"""
def test_no_title(self, manager, employee):
with pytest.raises(Exception):
Task.create_task(title="",
assignee=employee,
created_by=manager,
priority=Priority.CRITICAL,
status=Status.BACKLOG,
description="This is a test task")
assert len(Task.objects.all()) == 0

"""
Test that priority must be a valid enum value
"""
def test_invalid_priority(self, manager, employee):
with pytest.raises(Exception):
Task.create_task(title="TestTask",
assignee=employee,
created_by=manager,
priority='INVALID',
status=Status.BACKLOG,
description="This is a test task")
assert len(Task.objects.all()) == 0

"""
Test that status must be a valid enum value
"""
def test_invalid_status(self, manager, employee):
with pytest.raises(Exception):
Task.create_task(title="TestTask",
assignee=employee,
created_by=manager,
priority=Priority.CRITICAL,
status='INVALID',
description="This is a test task")
assert len(Task.objects.all()) == 0

"""
Test that manager cannot assign task to other teams employees
"""
def test_assign_other_team(self, manager, employee_other_team):
orzionpour marked this conversation as resolved.
Show resolved Hide resolved
with pytest.raises(Exception):
Task.create_task(title="TestTask",
assignee=employee_other_team,
created_by=manager,
priority=Priority.CRITICAL,
status=Status.BACKLOG,
description="This is a test task")
assert len(Task.objects.all()) == 0