<repo_name>johnnncodes/DDD-and-Hexagonal-Architecture-in-Python<file_sep>/my_helpdesk/test_entities.py
import uuid as uuid_lib
import pytest

from my_helpdesk.entities import Ticket, Message, Customer
from my_helpdesk.exceptions import InvalidArgumentException
from my_helpdesk.value_objects import EmailAddress


class TestTicket:
    def test_uuid_is_being_set(self):
        uuid = uuid_lib.uuid4()
        title = "title"
        body = "body"
        ticket = Ticket(uuid=uuid, title=title, body=body)
        assert ticket.get_uuid() == uuid

    def test_title_is_being_set(self):
        uuid = uuid_lib.uuid4()
        title = "title"
        body = "body"
        ticket = Ticket(uuid=uuid, title=title, body=body)
        assert ticket.get_title() == title

    def test_body_is_being_set(self):
        uuid = uuid_lib.uuid4()
        title = "title"
        body = "body"
        ticket = Ticket(uuid=uuid, title=title, body=body)
        assert ticket.get_body() == body

    def test_add_message(self):
        uuid = uuid_lib.uuid4()
        title = "title"
        body = "body"
        ticket = Ticket(uuid=uuid, title=title, body=body)

        uuid2 = uuid_lib.uuid4()
        message = Message(
            uuid=uuid2,
            title="message title",
            body="message body"
        )

        ticket.add_message(message)

        assert len(ticket.get_messages()) == 1

    def test_get_customer(self):
        customer_uuid = uuid_lib.uuid4()
        email = EmailAddress("<EMAIL>")
        customer = Customer(uuid=customer_uuid, email=email)

        uuid = uuid_lib.uuid4()
        title = "title"
        body = "body"
        ticket = Ticket(uuid=uuid, title=title, body=body, customer=customer)

        assert ticket.get_customer() == customer

    def test_update_message(self):
        message_uuid = uuid_lib.uuid4()
        message = Message(
            uuid=message_uuid,
            title="message title",
            body="message body"
        )

        uuid = uuid_lib.uuid4()
        title = "title"
        body = "body"
        ticket = Ticket(uuid=uuid, title=title, body=body, messages=[message])

        new_title = "new title"
        message.update(title=new_title)

        ticket.update_message(message)

        assert ticket.get_messages()[0].get_title() == new_title

    def test_update(self):
        uuid = uuid_lib.uuid4()
        title = "title"
        body = "body"
        ticket = Ticket(uuid=uuid, title=title, body=body)

        new_title = "new title"
        ticket.update(title=new_title)

        assert ticket.get_title() == new_title

    def test_should_not_be_able_to_create_a_ticket_without_a_title(self):
        with pytest.raises(InvalidArgumentException):
                Ticket(body="body")

        def test_should_not_be_able_to_create_a_ticket_without_a_body(self):
            with pytest.raises(InvalidArgumentException):
                Ticket(title="title")
<file_sep>/my_helpdesk/entities.py
import uuid as uuid_lib

from my_helpdesk.exceptions import InvalidArgumentException
from my_helpdesk.value_objects import EmailAddress

class Customer(object):

    def __init__(self, uuid=None, email=None):
        if uuid is None:
            self.__uuid = uuid_lib.uuid4()
        else:
            self.__uuid = uuid

        if email is not None:
            if not isinstance(email, EmailAddress):
                raise Exception
            self.__email = email

    def get_uuid(self):
        return self.__uuid

    def get_email(self):
        return self.__email


class Ticket(object):

    def __init__(self, uuid=None, title=None, body=None, customer=None, messages=[]):
        if uuid is None:
            self.__uuid = uuid_lib.uuid4()
        else:
            self.__uuid = uuid

        if title is None:
            raise InvalidArgumentException

        if body is None:
            raise InvalidArgumentException

        self.__title = title
        self.__body = body
        self.__customer = customer
        self.__messages = messages

    def get_uuid(self):
        return self.__uuid

    def get_title(self):
        return self.__title

    def get_body(self):
        return self.__body

    def get_messages(self):
        return self.__messages

    def get_customer(self):
        return self.__customer

    def update(self, title=None, body=None):
        self.__title = title

    def add_message(self, message):
        self.__messages.append(message)

    def update_message(self, message):
        for m in self.__messages:
            if m.get_uuid() == message.get_uuid():
                self.__messages.remove(m)
                self.__messages.append(message)


class Message(object):

    def __init__(self, uuid=None, title=None, body=None):
        if uuid is None:
            self.__uuid = uuid_lib.uuid4()
        else:
            self.__uuid = uuid

        self.__title = title
        self.__body = body

    def get_uuid(self):
        return self.__uuid

    def get_title(self):
        return self.__title

    def get_body(self):
        return self.__body

    def update(self, title=None, body=None):
        if title is not None:
            self.__title = title

        if body is not None:
            self.__body = body
<file_sep>/my_helpdesk/value_objects.py
import re

from my_helpdesk.exceptions import InvalidArgumentException


class EmailAddress(object):

    __email_address = None

    def __init__(self, email_address):
        if re.match(r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"'r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', email_address, re.IGNORECASE):
            self.__email_address = email_address
        else:
            raise InvalidArgumentException

    def __repr__(self):
        return self.__email_address
