# Path: baroque/datastructures/counters.py
# class EventCounter:
#     """A counter of events."""
# 
#     def __init__(self):
#         self.events_count = 0
#         self.events_count_by_type = dict()
# 
#     def increment_counting(self, event):
#         """Counts an event
# 
#         Args:
#             event (:obj:`baroque.entities.event.Event`): the event to be counted
# 
#         """
#         assert isinstance(event, Event)
#         self.events_count += 1
#         t = type(event.type)
#         if t in self.events_count_by_type:
#             self.events_count_by_type[t] += 1
#         else:
#             self.events_count_by_type[t] = 1
# 
#     def count_all(self):
#         """Tells how many events have been counted globally
# 
#         Returns:
#             int
# 
#         """
#         return self.events_count
# 
#     def count(self, eventtype):
#         """Tells how many events have been counted of the specified type
# 
#         Args:
#             eventtype (:obj:`baroque.entities.eventtype.EventType`): the type of events to be counted
# 
#         Returns:
#             int
# 
#         """
#         return self.events_count_by_type.get(type(eventtype), 0)
# 
# 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: 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)


import pytest
from baroque.datastructures.counters import EventCounter
from baroque.entities.event import Event
from baroque.defaults.eventtypes import GenericEventType, MetricEventType


def test_events_counting():
    eventtype1 = GenericEventType()
    eventtype2 =MetricEventType()

    c = EventCounter()
    assert c.count_all() == 0
    assert c.count(eventtype1) == 0
    assert c.count(eventtype2) == 0

    c.increment_counting(Event(eventtype1))
    assert c.count_all() == 1
    assert c.count(eventtype1) == 1
    assert c.count(eventtype2) == 0

    c.increment_counting(Event(eventtype2))
    assert c.count_all() == 2
    assert c.count(eventtype1) == 1
    assert c.count(eventtype2) == 1


def test_increment_counting():

# Path: baroque/datastructures/counters.py
# class EventCounter:
#     """A counter of events."""
# 
#     def __init__(self):
#         self.events_count = 0
#         self.events_count_by_type = dict()
# 
#     def increment_counting(self, event):
#         """Counts an event
# 
#         Args:
#             event (:obj:`baroque.entities.event.Event`): the event to be counted
# 
#         """
#         assert isinstance(event, Event)
#         self.events_count += 1
#         t = type(event.type)
#         if t in self.events_count_by_type:
#             self.events_count_by_type[t] += 1
#         else:
#             self.events_count_by_type[t] = 1
# 
#     def count_all(self):
#         """Tells how many events have been counted globally
# 
#         Returns:
#             int
# 
#         """
#         return self.events_count
# 
#     def count(self, eventtype):
#         """Tells how many events have been counted of the specified type
# 
#         Args:
#             eventtype (:obj:`baroque.entities.eventtype.EventType`): the type of events to be counted
# 
#         Returns:
#             int
# 
#         """
#         return self.events_count_by_type.get(type(eventtype), 0)
# 
# 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: 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)


# Path: tests/datastructures/test_eventcounter.py
import pytest
from baroque.datastructures.counters import EventCounter
from baroque.entities.event import Event
from baroque.defaults.eventtypes import GenericEventType, MetricEventType



def test_events_counting():
    eventtype1 = GenericEventType()
    eventtype2 =MetricEventType()

    c = EventCounter()
    assert c.count_all() == 0
    assert c.count(eventtype1) == 0
    assert c.count(eventtype2) == 0

    c.increment_counting(Event(eventtype1))
    assert c.count_all() == 1
    assert c.count(eventtype1) == 1
    assert c.count(eventtype2) == 0

    c.increment_counting(Event(eventtype2))
    assert c.count_all() == 2
    assert c.count(eventtype1) == 1
    assert c.count(eventtype2) == 1


def test_increment_counting():

    c = EventCounter()