# Path: baroque/defaults/eventtypes.py
# class GenericEventType(EventType):
# class StateTransitionEventType(EventType):
# class DataOperationEventType(EventType):
# class MetricEventType(EventType):
#     def __init__(self, owner=None):
#     def __init__(self, owner=None):
#     def __init__(self, owner=None):
#     def __init__(self, owner=None):
# 
# Path: baroque/entities/eventtype.py
# class EventType:
#     """The type of an event, describing its semantics and content.
# 
#     Args:
#         jsonschema (str): the JSON schema string describing the content of the
#          events having this type
#         description (str, optional): the description of this event type
#         owner (str, optional): the owner of this event type
# 
#     """
# 
#     def __init__(self, jsonschema, description=None, owner=None):
#         assert jsonschema is not None
#         assert isinstance(jsonschema, str)
#         self.jsonschema = jsonschema
#         self.owner = owner
#         self.description = description
#         self.tags = set()
# 
#     @staticmethod
#     def validate(evt, evttype):
#         """Validates the content of an event against the JSON schema of its type.
# 
#         Args:
#             evt (:obj:`baroque.entities.event.Event`): the event to be validated
#             evttype (:obj:`baroque.entities.eventtype.EventType`): the type of
#             the event that needs to be validated
# 
#         Returns:
#             ``True`` if validation is OK, ``False`` otherwise
# 
#         """
#         try:
#             assert evt.type == evttype
#             check(loads(evt.json()), loads(evttype.jsonschema))
#             return True
#         except (AssertionError, ValidationError):
#             return False
# 
#     def json(self):
#         """Dumps this object to a JSON string.
# 
#         Returns:
#             str
# 
#         """
#         data = dict(jsonschema=self.jsonschema, owner=self.owner,
#                     description=self.description, tags=list(self.tags))
#         return dumps(data)
# 
#     def md5(self):
#         """Returns the MD5 hash of this object.
# 
#         Returns:
#             str
# 
#         """
#         m = hashlib.md5()
#         m.update(self.json().encode('utf-8'))
#         return m.hexdigest()
# 
#     def __repr__(self):
#         return '<{}.{} - description: {}>'.format(
#             __name__,
#             self.__class__.__name__,
#             self.description or 'None'
#         )
# 
# Path: baroque/entities/event.py
# class Event:
#     """An event that can be published.
# 
#     Args:
#         eventtype (:obj:`baroque.entities.eventtype.EventType` instance or `type` object): the type of the event
#         payload (dict, optional): the content of this event
#         description (str, optional): the description of this event
#         owner (str, optional): the owner of this event
# 
#     """
#     def __init__(self, eventtype, payload=None, description=None, owner=None):
#         assert eventtype is not None
#         if type(eventtype) == type:
#             eventtype = eventtype()
#         assert isinstance(eventtype, EventType)
#         if payload is not None:
#             assert isinstance(payload, dict)
#         self.id = str(uuid.uuid4())
#         self.type = eventtype
#         self.owner = owner
#         self.status = EventStatus.UNPUBLISHED
#         self.description = description
#         self.payload = payload
#         self.tags = set()
#         self.timestamp = None
#         self.touch()
# 
#     def set_published(self):
#         """Sets the status of this event to published."""
#         self.status = EventStatus.PUBLISHED
# 
#     def set_unpublished(self):
#         """Sets the status of this event to unpublished."""
#         self.status = EventStatus.UNPUBLISHED
# 
#     def touch(self):
#         """Sets the current time as timestamp of this event"""
#         self.timestamp = ts.utc_now()
# 
#     def json(self):
#         """Dumps this object to a JSON string.
# 
#         Returns:
#             str
# 
#         """
#         data = dict(id=self.id, type=self.type.json(),
#                     owner=self.owner, status=self.status,
#                     description=self.description, payload=self.payload,
#                     tags=list(self.tags), timestamp=ts.stringify(self.timestamp))
#         return dumps(data)
# 
#     def md5(self):
#         """Returns the MD5 hash of this object.
# 
#         Returns:
#             str
# 
#         """
#         m = hashlib.md5()
#         m.update(self.json().encode('utf-8'))
#         return m.hexdigest()
# 
#     def __repr__(self):
#         return '<{}.{} - type: {} - id: {}>'.format(
#             __name__,
#             self.__class__.__name__,
#             self.type.__class__.__name__,
#             self.id
#         )


from baroque.defaults import eventtypes
from baroque import Event, EventType


def test_generic_eventtype():
    et = eventtypes.GenericEventType()
    event = Event(et, payload=dict(test='value'))
    assert EventType.validate(event, et)

    # test printing
    print(et)


def test_state_transition_eventtype():
    # validation failure
    et = eventtypes.StateTransitionEventType()
    event = Event(et)
    assert not EventType.validate(event, et)

    # validation OK

# Path: baroque/defaults/eventtypes.py
# class GenericEventType(EventType):
# class StateTransitionEventType(EventType):
# class DataOperationEventType(EventType):
# class MetricEventType(EventType):
#     def __init__(self, owner=None):
#     def __init__(self, owner=None):
#     def __init__(self, owner=None):
#     def __init__(self, owner=None):
# 
# Path: baroque/entities/eventtype.py
# class EventType:
#     """The type of an event, describing its semantics and content.
# 
#     Args:
#         jsonschema (str): the JSON schema string describing the content of the
#          events having this type
#         description (str, optional): the description of this event type
#         owner (str, optional): the owner of this event type
# 
#     """
# 
#     def __init__(self, jsonschema, description=None, owner=None):
#         assert jsonschema is not None
#         assert isinstance(jsonschema, str)
#         self.jsonschema = jsonschema
#         self.owner = owner
#         self.description = description
#         self.tags = set()
# 
#     @staticmethod
#     def validate(evt, evttype):
#         """Validates the content of an event against the JSON schema of its type.
# 
#         Args:
#             evt (:obj:`baroque.entities.event.Event`): the event to be validated
#             evttype (:obj:`baroque.entities.eventtype.EventType`): the type of
#             the event that needs to be validated
# 
#         Returns:
#             ``True`` if validation is OK, ``False`` otherwise
# 
#         """
#         try:
#             assert evt.type == evttype
#             check(loads(evt.json()), loads(evttype.jsonschema))
#             return True
#         except (AssertionError, ValidationError):
#             return False
# 
#     def json(self):
#         """Dumps this object to a JSON string.
# 
#         Returns:
#             str
# 
#         """
#         data = dict(jsonschema=self.jsonschema, owner=self.owner,
#                     description=self.description, tags=list(self.tags))
#         return dumps(data)
# 
#     def md5(self):
#         """Returns the MD5 hash of this object.
# 
#         Returns:
#             str
# 
#         """
#         m = hashlib.md5()
#         m.update(self.json().encode('utf-8'))
#         return m.hexdigest()
# 
#     def __repr__(self):
#         return '<{}.{} - description: {}>'.format(
#             __name__,
#             self.__class__.__name__,
#             self.description or 'None'
#         )
# 
# Path: baroque/entities/event.py
# class Event:
#     """An event that can be published.
# 
#     Args:
#         eventtype (:obj:`baroque.entities.eventtype.EventType` instance or `type` object): the type of the event
#         payload (dict, optional): the content of this event
#         description (str, optional): the description of this event
#         owner (str, optional): the owner of this event
# 
#     """
#     def __init__(self, eventtype, payload=None, description=None, owner=None):
#         assert eventtype is not None
#         if type(eventtype) == type:
#             eventtype = eventtype()
#         assert isinstance(eventtype, EventType)
#         if payload is not None:
#             assert isinstance(payload, dict)
#         self.id = str(uuid.uuid4())
#         self.type = eventtype
#         self.owner = owner
#         self.status = EventStatus.UNPUBLISHED
#         self.description = description
#         self.payload = payload
#         self.tags = set()
#         self.timestamp = None
#         self.touch()
# 
#     def set_published(self):
#         """Sets the status of this event to published."""
#         self.status = EventStatus.PUBLISHED
# 
#     def set_unpublished(self):
#         """Sets the status of this event to unpublished."""
#         self.status = EventStatus.UNPUBLISHED
# 
#     def touch(self):
#         """Sets the current time as timestamp of this event"""
#         self.timestamp = ts.utc_now()
# 
#     def json(self):
#         """Dumps this object to a JSON string.
# 
#         Returns:
#             str
# 
#         """
#         data = dict(id=self.id, type=self.type.json(),
#                     owner=self.owner, status=self.status,
#                     description=self.description, payload=self.payload,
#                     tags=list(self.tags), timestamp=ts.stringify(self.timestamp))
#         return dumps(data)
# 
#     def md5(self):
#         """Returns the MD5 hash of this object.
# 
#         Returns:
#             str
# 
#         """
#         m = hashlib.md5()
#         m.update(self.json().encode('utf-8'))
#         return m.hexdigest()
# 
#     def __repr__(self):
#         return '<{}.{} - type: {} - id: {}>'.format(
#             __name__,
#             self.__class__.__name__,
#             self.type.__class__.__name__,
#             self.id
#         )


# Path: tests/defaults/test_eventtypes.py
from baroque.defaults import eventtypes
from baroque import Event, EventType



def test_generic_eventtype():
    et = eventtypes.GenericEventType()
    event = Event(et, payload=dict(test='value'))
    assert EventType.validate(event, et)

    # test printing
    print(et)


def test_state_transition_eventtype():
    # validation failure
    et = eventtypes.StateTransitionEventType()
    event = Event(et)
    assert not EventType.validate(event, et)

    # validation OK

    event = Event(et,