Skip to content

Commit

Permalink
OP-2219: added tests for gql mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
sniedzielski committed Dec 10, 2024
1 parent 8382a8d commit c6b85d8
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 1 deletion.
36 changes: 36 additions & 0 deletions grievance_social_protection/tests/gql_payloads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
gql_mutation_create_ticket = """
mutation createTicket {
createTicket(input: {
category: "%s",
title: "%s",
resolution: "%s",
priority: "%s",
dateOfIncident: "%s",
channel: "%s",
flags: "%s",
clientMutationId: "%s"
}) {
clientMutationId
}
}
"""


gql_mutation_update_ticket = """
mutation updateTicket {
updateTicket(input: {
id: "%s",
category: "%s",
title: "%s",
resolution: "%s",
priority: "%s",
dateOfIncident: "%s",
channel: "%s",
flags: "%s",
status: %s,
clientMutationId: "%s"
}) {
clientMutationId
}
}
"""
129 changes: 129 additions & 0 deletions grievance_social_protection/tests/test_gql_ticket_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from django.test import TestCase
from core.models import MutationLog
from graphene import Schema
from graphene.test import Client
from core.test_helpers import create_test_interactive_user
from grievance_social_protection.models import Ticket
from grievance_social_protection.schema import Query, Mutation
from grievance_social_protection.tests.gql_payloads import gql_mutation_create_ticket


class GQLTicketCreateTestCase(TestCase):
class GQLContext:
def __init__(self, user):
self.user = user

user = None
eu = None

category = None
title = None
resolution = None
priority = None
dateOfIncident = None
channel = None
flags = None

@classmethod
def setUpClass(cls):
super(GQLTicketCreateTestCase, cls).setUpClass()
cls.user = create_test_interactive_user(username='user_authorized', roles=[7])

gql_schema = Schema(
query=Query,
mutation=Mutation
)

cls.category = "Default"
cls.title = "TestMutationCreate"
cls.resolution = "2,5"
cls.priority = "Medium"
cls.date_of_incident = "2024-11-20"
cls.channel = "Channel A"
cls.flags = "Default"

cls.gql_client = Client(gql_schema)
cls.gql_context = cls.GQLContext(cls.user)

def test_create_ticket_success(self):
mutation_id = "99g453h5g92h04gh88"
payload = gql_mutation_create_ticket % (
self.category,
self.title,
self.resolution,
self.gender_id,
self.date_of_incident,
self.channel,
self.flags,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertFalse(mutation_log.error)
tickets = Ticket.objects.filter(title=self.title)
self.assertEquals(tickets.count(), 1)
ticket = tickets.first()
self.assertEquals(ticket.title, self.title)
self.assertEquals(ticket.category, self.category)
self.assertEquals(ticket.resolution, self.resolution)
self.assertEquals(ticket.priority, self.priority)
self.assertEquals(ticket.date_of_incident, self.date_of_incident)
self.assertEquals(ticket.flags, self.flags)

def test_create_ticket_false_invalid_resolution_format(self):
mutation_id = "65g453h4g92h04gh98"
payload = gql_mutation_create_ticket % (
self.category,
self.title,
"kjdslkdjslk",
self.gender_id,
self.date_of_incident,
self.channel,
self.flags,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertTrue(mutation_log.error)
tickets = Ticket.objects.filter(title=self.title)
self.assertEquals(tickets.count(), 0)

def test_create_ticket_false_invalid_resolution_day_format(self):
mutation_id = "62g453h4g92h04gh90"
payload = gql_mutation_create_ticket % (
self.category,
self.title,
"99,3",
self.gender_id,
self.date_of_incident,
self.channel,
self.flags,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertTrue(mutation_log.error)
tickets = Ticket.objects.filter(title=self.title)
self.assertEquals(tickets.count(), 0)

def test_create_ticket_false_invalid_resolution_hour_format(self):
mutation_id = "15g453h4g92h04gh92"
payload = gql_mutation_create_ticket % (
self.category,
self.title,
"4,66",
self.gender_id,
self.date_of_incident,
self.channel,
self.flags,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertTrue(mutation_log.error)
tickets = Ticket.objects.filter(title=self.title)
self.assertEquals(tickets.count(), 0)
140 changes: 140 additions & 0 deletions grievance_social_protection/tests/test_gql_ticket_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
from django.test import TestCase
from core.models import MutationLog
from graphene import Schema
from graphene.test import Client
from core.test_helpers import create_test_interactive_user
from grievance_social_protection.models import Ticket
from grievance_social_protection.schema import Query, Mutation
from grievance_social_protection.tests.gql_payloads import gql_mutation_update_ticket
from grievance_social_protection.tests.test_helpers import create_ticket


class GQLTicketUpdateTestCase(TestCase):
class GQLContext:
def __init__(self, user):
self.user = user

user = None
eu = None

category = None
title = None
resolution = None
priority = None
dateOfIncident = None
channel = None
flags = None
status = None

@classmethod
def setUpClass(cls):
super(GQLTicketUpdateTestCase, cls).setUpClass()
cls.user = create_test_interactive_user(username='user_authorized', roles=[7])
cls.existing_ticket = create_ticket(cls.user.username)

gql_schema = Schema(
query=Query,
mutation=Mutation
)

cls.category = "Default"
cls.title = "TestMutationUpdate"
cls.resolution = "2,5"
cls.priority = "Medium"
cls.date_of_incident = "2024-11-20"
cls.channel = "Channel A"
cls.flags = "Default"
cls.status = "OPEN"

cls.gql_client = Client(gql_schema)
cls.gql_context = cls.GQLContext(cls.user)

def test_update_ticket_success(self):
mutation_id = "99g453h5g92h04xc66"
payload = gql_mutation_update_ticket % (
self.existing_ticket.id,
self.category,
self.title,
self.resolution,
self.gender_id,
self.date_of_incident,
self.channel,
self.flags,
self.status,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertFalse(mutation_log.error)
ticket = Ticket.objects.get(id=self.existing_ticket.id)
self.assertEquals(ticket.title, self.title)
self.assertEquals(ticket.category, self.category)
self.assertEquals(ticket.resolution, self.resolution)
self.assertEquals(ticket.priority, self.priority)
self.assertEquals(ticket.date_of_incident, self.date_of_incident)
self.assertEquals(ticket.flags, self.flags)
self.assertEquals(ticket.status, self.status)

def test_update_ticket_false_invalid_resolution_format(self):
mutation_id = "65g453h4g92h04yf43"
payload = gql_mutation_update_ticket % (
self.existing_ticket.id,
self.category,
self.title,
"kjdslkdjslk",
self.gender_id,
self.date_of_incident,
self.channel,
self.flags,
self.status,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertTrue(mutation_log.error)
ticket = Ticket.objects.get(id=self.existing_ticket.id)
self.assertNotEquals(ticket.title, self.title)

def test_update_ticket_false_invalid_resolution_day_format(self):
mutation_id = "65g453h4g92h0zx54"
payload = gql_mutation_update_ticket % (
self.existing_ticket.id,
self.category,
self.title,
"99,3",
self.gender_id,
self.date_of_incident,
self.channel,
self.flags,
self.status,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertTrue(mutation_log.error)
ticket = Ticket.objects.get(id=self.existing_ticket.id)
self.assertNotEquals(ticket.title, self.title)

def test_update_ticket_false_invalid_resolution_hour_format(self):
mutation_id = "65g453h4g92h04wl32"
payload = gql_mutation_update_ticket % (
self.existing_ticket.id,
self.category,
self.title,
"4,66",
self.gender_id,
self.date_of_incident,
self.channel,
self.flags,
self.status,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertTrue(mutation_log.error)
ticket = Ticket.objects.get(id=self.existing_ticket.id)
self.assertNotEquals(ticket.title, self.title)
2 changes: 1 addition & 1 deletion grievance_social_protection/validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def validate_resolution(data):
pattern = r"^(?P<days>[0-9]{1,2}),(?P<hours>[0-9]{1,2})$"
match = re.match(pattern, resolution)
if not match:
return {"message": _("validations.CommentValidation.validate_resolution.invalid_format")}
return {"message": _("validations.TicketValidation.validate_resolution.invalid_format")}
else:
days = int(match.group("days"))
hours = int(match.group("hours"))
Expand Down

0 comments on commit c6b85d8

Please sign in to comment.