Here’s a Python command to create a function to open a support ticket, along with corresponding test cases. The function will allow you to input details for the ticket and store it in a list.
Python Command
class Ticket:
"""
A class to represent a support ticket.
Attributes:
title (str): The title of the ticket.
description (str): A detailed description of the ticket.
status (str): The current status of the ticket.
"""
def __init__(self, title, description):
"""
Initializes a new ticket with the title and description.
Args:
title (str): Title of the ticket.
description (str): Description of the ticket.
"""
self.title = title
self.description = description
self.status = 'Open'
def __repr__(self):
return f'Ticket(title={self.title}, status={self.status})'
class TicketSystem:
"""
A class to manage a ticketing system.
Attributes:
tickets (list): A list of active tickets.
"""
def __init__(self):
"""Initializes the TicketSystem with an empty ticket list."""
self.tickets = []
def open_ticket(self, title, description):
"""
Opens a new support ticket and adds it to the ticket list.
Args:
title (str): Title of the ticket.
description (str): Description of the ticket.
Returns:
Ticket: The created ticket object.
"""
new_ticket = Ticket(title, description)
self.tickets.append(new_ticket)
return new_ticket
def get_tickets(self):
"""
Returns the list of current tickets.
Returns:
list: A list of current tickets.
"""
return self.tickets
Test Cases
Here are some test cases for the TicketSystem class to validate its functionality. The tests will use the unittest framework.
import unittest
class TestTicketSystem(unittest.TestCase):
def setUp(self):
"""Sets up a TicketSystem instance for testing."""
self.ticket_system = TicketSystem()
def test_open_ticket(self):
"""Test if a new ticket is opened correctly."""
title = "Sample Ticket"
description = "This is a test ticket."
ticket = self.ticket_system.open_ticket(title, description)
self.assertEqual(ticket.title, title)
self.assertEqual(ticket.description, description)
self.assertEqual(ticket.status, 'Open')
self.assertIn(ticket, self.ticket_system.tickets)
def test_get_tickets(self):
"""Test if the current tickets can be retrieved."""
self.ticket_system.open_ticket("First Ticket", "First ticket description.")
self.ticket_system.open_ticket("Second Ticket", "Second ticket description.")
tickets = self.ticket_system.get_tickets()
self.assertEqual(len(tickets), 2)
self.assertEqual(tickets[0].title, "First Ticket")
self.assertEqual(tickets[1].title, "Second Ticket")
if __name__ == '__main__':
unittest.main()
Explanation
- Classes: The
Ticket class encapsulates attributes and methods related to a support ticket, whereas the TicketSystem class manages a collection of tickets.
- Functions: The
open_ticket method creates and stores a new ticket in the list.
- Testing: The test cases ensure that tickets are created correctly and can be retrieved from the system.