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

Task CRUD tests #1490

Merged
merged 4 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
108 changes: 73 additions & 35 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ def terms(self, create, extracted, **kwargs):
class ContactBaseFactory(TimeStampBaseFactory):
"""Contact Base Factory."""

is_active = True
is_external = False
company = FuzzyText()
contact_type = FuzzyChoice(["one", "two"])
email = Sequence(lambda n: f"user{n}@example.com")
company = FuzzyText()
is_active = Faker().pybool()
is_external = Faker().pybool()
notes = FuzzyText()
owner = "kevin@example.com"
owner = Sequence(lambda n: f"user{n}@example.com")

@post_generation
def incident_priorities(self, create, extracted, **kwargs):
Expand Down Expand Up @@ -347,12 +347,12 @@ class Meta:
class IndividualContactFactory(ContactBaseFactory):
"""Individual Contact Factory."""

name = Sequence(lambda n: f"Joe{n}")
mobile_phone = "111-111-1111"
name = Sequence(lambda n: f"Joe{n}")
office_phone = "111-111-1111"
title = FuzzyText()
weblink = FuzzyText()
project = SubFactory(ProjectFactory)
title = FuzzyText()
weblink = Sequence(lambda n: f"https://www.example.com/{n}")

class Meta:
"""Factory Configuration."""
Expand Down Expand Up @@ -566,20 +566,64 @@ def incident(self, create, extracted, **kwargs):
self.incident_id = extracted.id


class IncidentFactory(BaseFactory):
"""Incident Factory."""

id = Sequence(lambda n: f"1{n}")
title = FuzzyText()
description = FuzzyText()
status = FuzzyChoice(["Active", "Stable", "Closed"])
project = SubFactory(ProjectFactory)

class Meta:
"""Factory Configuration."""

model = Incident

@post_generation
def participants(self, create, extracted, **kwargs):
if not create:
return

if extracted:
for participant in extracted:
self.participants.append(participant)


class TaskFactory(ResourceBaseFactory):
"""Task Factory."""

resolved_at = FuzzyDateTime(datetime(2020, 1, 1, tzinfo=UTC))
last_reminder_at = FuzzyDateTime(datetime(2020, 1, 1, tzinfo=UTC))
creator = "[email protected]"
assignees = "[email protected]"
description = FuzzyText()
last_reminder_at = FuzzyDateTime(datetime(2020, 1, 1, tzinfo=UTC))
priority = FuzzyChoice(["Low", "Medium", "High"])
reminders = Faker().pybool()
resolve_by = FuzzyDateTime(datetime(2020, 1, 2, tzinfo=UTC))
resolved_at = FuzzyDateTime(datetime(2020, 1, 1, tzinfo=UTC))
source = FuzzyChoice(["Incident", "Post Incident Review"])
status = FuzzyChoice(["Open", "Resolved"])
incident = SubFactory(IncidentFactory)

class Meta:
"""Factory Configuration."""

model = Task

@post_generation
def creator(self, create, extracted, **kwargs):
if not create:
return

if extracted:
self.creator_id = extracted.id

@post_generation
def owner(self, create, extracted, **kwargs):
if not create:
return

if extracted:
self.owner_id = extracted.id

@post_generation
def incident(self, create, extracted, **kwargs):
if not create:
Expand All @@ -588,6 +632,24 @@ def incident(self, create, extracted, **kwargs):
if extracted:
self.incident_id = extracted.id

@post_generation
def assignees(self, create, extracted, **kwargs):
if not create:
return

if extracted:
for assignee in extracted:
self.assignees.append(assignee)

@post_generation
def tickets(self, create, extracted, **kwargs):
if not create:
return

if extracted:
for ticket in extracted:
self.tickets.append(ticket)


class TeamContactFactory(ContactBaseFactory):
"""Team Contact Factory."""
Expand Down Expand Up @@ -646,30 +708,6 @@ class Meta:
model = Ticket


class IncidentFactory(BaseFactory):
"""Incident Factory."""

id = Sequence(lambda n: f"1{n}")
title = FuzzyText()
description = FuzzyText()
status = FuzzyChoice(["Active", "Stable", "Closed"])
project = SubFactory(ProjectFactory)

class Meta:
"""Factory Configuration."""

model = Incident

@post_generation
def participants(self, create, extracted, **kwargs):
if not create:
return

if extracted:
for participant in extracted:
self.participants.append(participant)


class EventFactory(BaseFactory):
"""Event Factory."""

Expand Down
49 changes: 49 additions & 0 deletions tests/task/test_task_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest


def test_get(session, task):
from dispatch.task.service import get

t_task = get(db_session=session, task_id=task.id)
assert t_task.id == task.id


@pytest.mark.skip # NOTE: refactor create function in the task service module before enabling this test
def test_create(
session, incident, incident_type, incident_priority, participant, individual_contact
):
from dispatch.task.service import create
from dispatch.task.models import TaskCreate

incident.incident_type = incident_type
incident.incident_priority = incident_priority
participant.individual = individual_contact

task_in = TaskCreate(incident=incident, owner=participant)
task = create(db_session=session, task_in=task_in)
assert task


@pytest.mark.skip
def test_update(session, task):
from dispatch.task.service import update
from dispatch.task.models import TaskUpdate

description = "Updated description"

task_in = TaskUpdate(
description=description,
)
task = update(
db_session=session,
task=task,
task_in=task_in,
)
assert task.description == description


def test_delete(session, task):
from dispatch.task.service import delete, get

delete(db_session=session, task_id=task.id)
assert not get(db_session=session, task_id=task.id)