# Path: baroque/datastructures/registries.py
# class EventTypesRegistry:
#     """Interface adapter to an event bag."""
#     def __init__(self):
#         self.registered_types = set()
# 
#     def register(self, eventtype):
#         """Adds an event type to this registry.
# 
#         Args:
#             eventtype (:obj:`baroque.entities.eventtypes.EventType` instance or `type` object): the event type to be added
# 
#         Raises:
#             `AssertionError`: when argument is not an :obj:`baroque.entities.eventtypes.EventType` instance or a `type` object
# 
#         """
#         if type(eventtype) == type:
#             eventtype = eventtype()
#         assert isinstance(eventtype, EventType)
#         self.registered_types.add(type(eventtype))
# 
#     def count(self):
#         """Tells how many event types are registered on this registry
# 
#         Returns:
#             int
# 
#         """
#         return len(self.registered_types)
# 
#     def remove(self, eventtype):
#         """Removes an event type from this registry
# 
#         Args:
#             eventtype (:obj:`baroque.entities.eventtypes.EventType` instance or `type` object): the event type to be removed
# 
#         Raises:
#             `AssertionError`: when argument is not an :obj:`baroque.entities.eventtypes.EventType` instance or a `type` object
# 
#         """
#         if type(eventtype) == type:
#             eventtype = eventtype()
#         assert isinstance(eventtype, EventType)
#         self.registered_types.discard(type(eventtype))
# 
#     def remove_all(self):
#         """Removes all event types from this registry."""
#         self.registered_types = set()
# 
#     # --- magic methods ---
# 
#     def __contains__(self, eventtype):
#         return type(eventtype) in self.registered_types
# 
#     def __len__(self):
#         return len(self.registered_types)
# 
#     def __repr__(self):
#         return '<{}.{} - event types: {}>'.format(
#             __name__,
#             self.__class__.__name__,
#             ', '.join([t.__name__ for t in self.registered_types]) or 'None')
# 
# Path: baroque/defaults/eventtypes.py
# class GenericEventType(EventType):
#     """Describes generic events with a free-form content.
# 
#     Args:
#         owner (str, optional): ID of the owner of this event type.
# 
#     """
#     def __init__(self, owner=None):
#         EventType.__init__(self,
#                            '''{
#                              "$schema": "http://json-schema.org/draft-04/schema#"
#                            }''',
#                            description='Generic event',
#                            owner=owner)
# 
# class MetricEventType(EventType):
#     """Describes events carrying metric data.
#     Suitable i.e. to track values about measured physical quantities.
#     The metric name and value are conveyed in the event payload.
# 
#     Args:
#         owner (str, optional): ID of the owner of this event type.
# 
#     """
#     def __init__(self, owner=None):
#         EventType.__init__(
#             self,
#             '''
#             {
#               "$schema": "http://json-schema.org/draft-04/schema#",
#               "type": "object",
#               "properties": {
#                 "payload": {
#                   "type": "object",
#                   "properties": {
#                     "metric": {
#                       "type": ["string", "number"]
#                     },
#                     "value": {
#                       "type": ["string", "number"]
#                     },
#                     "timestamp": {
#                       "type": ["string", "number"]
#                     },
#                     "meta": {
#                       "type": "object",
#                       "properties": {}
#                     }
#                   },
#                   "required": [
#                     "metric",
#                     "value",
#                     "timestamp"
#                   ]
#                 }
#               },
#               "required": [
#                 "payload"
#               ]
#             }
#             ''',
#             description='Metric events',
#             owner=owner)
# 
# class DataOperationEventType(EventType):
#     """Describes events cast when some kind of operation is done on a
#     piece of data.
#     Suitable i.e. to track CRUD operations on DB tables or whole datastores.
#     Details about the impacted data entity and the operation are
#     conveyed in the event payload.
# 
#     Args:
#         owner (str, optional): ID of the owner of this event type.
# 
#     """
#     def __init__(self, owner=None):
#         EventType.__init__(
#             self,
#             '''
#             {
#               "$schema": "http://json-schema.org/draft-04/schema#",
#               "type": "object",
#               "properties": {
#                 "payload": {
#                   "type": "object",
#                   "properties": {
#                     "datum": {
#                       "type": "object",
#                       "properties": {}
#                     },
#                     "operation": {
#                       "type": "string"
#                     },
#                     "timestamp": {
#                       "type": ["number", "string"]
#                     },
#                     "meta": {
#                       "type": "object",
#                       "properties": {}
#                     }
#                   },
#                   "required": [
#                     "datum",
#                     "operation",
#                     "timestamp"
#                   ]
#                 }
#               },
#               "required": [
#                 "payload"
#               ]
#             }
#             ''',
#             description='Data operation events',
#             owner=owner)


import pytest
from baroque.datastructures.registries import EventTypesRegistry
from baroque.defaults.eventtypes import GenericEventType, MetricEventType, \
    DataOperationEventType


def test_register():
    reg = EventTypesRegistry()
    assert reg.count() == 0
    reg.register(GenericEventType())  # with a concrete instance
    assert reg.count() == 1
    reg.register(GenericEventType)   # with a type object
    assert reg.count() == 1
    reg.register(MetricEventType())
    assert reg.count() == 2
    with pytest.raises(AssertionError):
        reg.register('not-a-reactor')
        pytest.fail()


def test_remove():
    reg = EventTypesRegistry()
    reg.register(GenericEventType())

# Path: baroque/datastructures/registries.py
# class EventTypesRegistry:
#     """Interface adapter to an event bag."""
#     def __init__(self):
#         self.registered_types = set()
# 
#     def register(self, eventtype):
#         """Adds an event type to this registry.
# 
#         Args:
#             eventtype (:obj:`baroque.entities.eventtypes.EventType` instance or `type` object): the event type to be added
# 
#         Raises:
#             `AssertionError`: when argument is not an :obj:`baroque.entities.eventtypes.EventType` instance or a `type` object
# 
#         """
#         if type(eventtype) == type:
#             eventtype = eventtype()
#         assert isinstance(eventtype, EventType)
#         self.registered_types.add(type(eventtype))
# 
#     def count(self):
#         """Tells how many event types are registered on this registry
# 
#         Returns:
#             int
# 
#         """
#         return len(self.registered_types)
# 
#     def remove(self, eventtype):
#         """Removes an event type from this registry
# 
#         Args:
#             eventtype (:obj:`baroque.entities.eventtypes.EventType` instance or `type` object): the event type to be removed
# 
#         Raises:
#             `AssertionError`: when argument is not an :obj:`baroque.entities.eventtypes.EventType` instance or a `type` object
# 
#         """
#         if type(eventtype) == type:
#             eventtype = eventtype()
#         assert isinstance(eventtype, EventType)
#         self.registered_types.discard(type(eventtype))
# 
#     def remove_all(self):
#         """Removes all event types from this registry."""
#         self.registered_types = set()
# 
#     # --- magic methods ---
# 
#     def __contains__(self, eventtype):
#         return type(eventtype) in self.registered_types
# 
#     def __len__(self):
#         return len(self.registered_types)
# 
#     def __repr__(self):
#         return '<{}.{} - event types: {}>'.format(
#             __name__,
#             self.__class__.__name__,
#             ', '.join([t.__name__ for t in self.registered_types]) or 'None')
# 
# Path: baroque/defaults/eventtypes.py
# class GenericEventType(EventType):
#     """Describes generic events with a free-form content.
# 
#     Args:
#         owner (str, optional): ID of the owner of this event type.
# 
#     """
#     def __init__(self, owner=None):
#         EventType.__init__(self,
#                            '''{
#                              "$schema": "http://json-schema.org/draft-04/schema#"
#                            }''',
#                            description='Generic event',
#                            owner=owner)
# 
# class MetricEventType(EventType):
#     """Describes events carrying metric data.
#     Suitable i.e. to track values about measured physical quantities.
#     The metric name and value are conveyed in the event payload.
# 
#     Args:
#         owner (str, optional): ID of the owner of this event type.
# 
#     """
#     def __init__(self, owner=None):
#         EventType.__init__(
#             self,
#             '''
#             {
#               "$schema": "http://json-schema.org/draft-04/schema#",
#               "type": "object",
#               "properties": {
#                 "payload": {
#                   "type": "object",
#                   "properties": {
#                     "metric": {
#                       "type": ["string", "number"]
#                     },
#                     "value": {
#                       "type": ["string", "number"]
#                     },
#                     "timestamp": {
#                       "type": ["string", "number"]
#                     },
#                     "meta": {
#                       "type": "object",
#                       "properties": {}
#                     }
#                   },
#                   "required": [
#                     "metric",
#                     "value",
#                     "timestamp"
#                   ]
#                 }
#               },
#               "required": [
#                 "payload"
#               ]
#             }
#             ''',
#             description='Metric events',
#             owner=owner)
# 
# class DataOperationEventType(EventType):
#     """Describes events cast when some kind of operation is done on a
#     piece of data.
#     Suitable i.e. to track CRUD operations on DB tables or whole datastores.
#     Details about the impacted data entity and the operation are
#     conveyed in the event payload.
# 
#     Args:
#         owner (str, optional): ID of the owner of this event type.
# 
#     """
#     def __init__(self, owner=None):
#         EventType.__init__(
#             self,
#             '''
#             {
#               "$schema": "http://json-schema.org/draft-04/schema#",
#               "type": "object",
#               "properties": {
#                 "payload": {
#                   "type": "object",
#                   "properties": {
#                     "datum": {
#                       "type": "object",
#                       "properties": {}
#                     },
#                     "operation": {
#                       "type": "string"
#                     },
#                     "timestamp": {
#                       "type": ["number", "string"]
#                     },
#                     "meta": {
#                       "type": "object",
#                       "properties": {}
#                     }
#                   },
#                   "required": [
#                     "datum",
#                     "operation",
#                     "timestamp"
#                   ]
#                 }
#               },
#               "required": [
#                 "payload"
#               ]
#             }
#             ''',
#             description='Data operation events',
#             owner=owner)


# Path: tests/datastructures/test_eventtypesregistry.py
import pytest
from baroque.datastructures.registries import EventTypesRegistry
from baroque.defaults.eventtypes import GenericEventType, MetricEventType, \
    DataOperationEventType



def test_register():
    reg = EventTypesRegistry()
    assert reg.count() == 0
    reg.register(GenericEventType())  # with a concrete instance
    assert reg.count() == 1
    reg.register(GenericEventType)   # with a type object
    assert reg.count() == 1
    reg.register(MetricEventType())
    assert reg.count() == 2
    with pytest.raises(AssertionError):
        reg.register('not-a-reactor')
        pytest.fail()


def test_remove():
    reg = EventTypesRegistry()
    reg.register(GenericEventType())

    reg.register(MetricEventType())