# Path: baobab/utils/test.py
# class TestCase(DjangoTestCase):
# 
#     def setUp(self, *args, **kwargs):
#         super(TestCase, self).setUp(*args, **kwargs)
#         self._mock = []
# 
#         cls_path = 'baobab.socialnetwork.base.LOG'
#         self._mock.append(patch(cls_path, new_callable=MockLOG))
#         self.log = self._mock[0].start()
# 
#         cls_path = 'baobab.socialnetwork.base.SocialNetworkBase.__subclasses__'
#         self._mock.append(patch(cls_path, return_value=[MockSN, ]))
#         self._mock[1].start()
# 
#     def tearDown(self, *args, **kwargs):
#         super(TestCase, self).tearDown(*args, **kwargs)
#         for mock in self._mock:
#             mock.stop()
# 
# Path: baobab/utils/mock.py
# class MockSN(SocialNetworkBase):
#     name = 'MockSN'
# 
#     def __init__(self):
#         self._msg = []
# 
#     @classmethod
#     def is_configured(cls):
#         True
# 
#     @classmethod
#     def get_max_char(cls, default):
#         return 42
# 
#     def publish(self, msg, url):
#         self._msg.append((msg, url))
#         return len(self._msg)
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         pass
# 
# Path: baobab/backoffice/models.py
# class Event(models.Model):
#     objects = EventManager()  # override the default manager
# 
#     # Used for <pubDate> in RSS Feed
#     last_update = models.DateTimeField(default=now)
# 
#     date_start = models.DateTimeField(default=now)
#     date_end = models.DateTimeField(null=True, blank=True, default=None)
#     estimate_date_end = models.DateTimeField()
#     duration = models.PositiveIntegerField(help_text='Duration (in minutes)')
# 
#     # XXX to remove at the next release
#     title = models.CharField(max_length=255,
#                              help_text='A short description of the event')
# 
#     # XXX to remove at the next release
#     summary = models.TextField(null=True, blank=True, default=None)
#     services = models.ManyToManyField('Service', related_name='events',
#                                       blank=True)
#     MAINTENANCE = 0
#     INCIDENT = 1
#     CATEGORY_CHOICES = (
#         (MAINTENANCE, 'Maintenance'),
#         (INCIDENT, 'Incident'),
#     )
#     category = models.PositiveSmallIntegerField(choices=CATEGORY_CHOICES,
#                                                 help_text='Type of the Event')
#     msg = models.CharField('Social Network', max_length=255,
#                            null=True, blank=True, default=None)
# 
#     def __unicode__(self):
#         return self.title
# 
#     def save(self, *args, **kwargs):
#         if not self.id and self.date_end and self.date_end > now():
#             # create an event with a date_end in future: need to be converted
#             # to the estimate_date_end
#             delta = self.date_end - self.date_start
#             self.duration = delta.total_seconds() / 60
#             self.date_end = None
#         if self.date_end:
#             delta = self.date_end - self.date_start
#             self.duration = delta.total_seconds() / 60
#             if not self.estimate_date_end or \
#                self.date_start >= self.estimate_date_end:
#                 self.estimate_date_end = self.date_end
#             if self.date_end <= now():
#                 if self.eventlogs.exists() and \
#                    self.eventlogs.all()[0].date < self.date_end:
#                     self.last_update = self.date_end
#         else:
#             delta = timedelta(minutes=self.duration)
#             self.estimate_date_end = self.date_start + delta
#         super(Event, self).save(*args, **kwargs)
# 
#     class Meta:
#         # XXX avoid the admin to override the right order: see EventQuerySet
#         ordering = ['id', 'pk']
# 
# Path: baobab/socialnetwork/models.py
# class Event(models.Model):
# 
#     event = models.ForeignKey('backoffice.Event')
#     name = models.CharField(max_length=10)
#     sn_id = models.CharField(max_length=30)
# 
#     class Meta:
#         unique_together = ('event', 'name')


import datetime
from django.utils.timezone import now as tz_now
from baobab.utils.test import TestCase
from baobab.utils.mock import MockSN
from baobab.backoffice.models import Event
from baobab.socialnetwork.models import Event as SnEvent

        self.assertEqual(SnEvent.objects.count(), 1)
        sn_event = SnEvent.objects.get(id=1)
        self.assertEqual(sn_event.event, event)
        self.assertEqual(sn_event.name, MockSN.name)
        self.assertEqual(sn_event.sn_id, '1')

    def test_02_event_incident_past(self):
        self.assertEqual(SnEvent.objects.count(), 0)

        now = tz_now().replace(microsecond=0) - datetime.timedelta(minutes=20)
        end = now - datetime.timedelta(minutes=10)
        event = self._create_event(now, end, Event.INCIDENT)

        self.assertEqual(SnEvent.objects.count(), 1)
        sn_event = SnEvent.objects.get(id=1)
        self.assertEqual(sn_event.event, event)
        self.assertEqual(sn_event.name, MockSN.name)
        self.assertEqual(sn_event.sn_id, '1')

    def test_03_event_incident_futre(self):
        self.assertEqual(SnEvent.objects.count(), 0)

        now = tz_now().replace(microsecond=0) + datetime.timedelta(minutes=10)
        end = now + datetime.timedelta(minutes=10)
        event = self._create_event(now, end, Event.INCIDENT)

        self.assertEqual(SnEvent.objects.count(), 1)
        sn_event = SnEvent.objects.get(id=1)
        self.assertEqual(sn_event.event, event)

# Path: baobab/utils/test.py
# class TestCase(DjangoTestCase):
# 
#     def setUp(self, *args, **kwargs):
#         super(TestCase, self).setUp(*args, **kwargs)
#         self._mock = []
# 
#         cls_path = 'baobab.socialnetwork.base.LOG'
#         self._mock.append(patch(cls_path, new_callable=MockLOG))
#         self.log = self._mock[0].start()
# 
#         cls_path = 'baobab.socialnetwork.base.SocialNetworkBase.__subclasses__'
#         self._mock.append(patch(cls_path, return_value=[MockSN, ]))
#         self._mock[1].start()
# 
#     def tearDown(self, *args, **kwargs):
#         super(TestCase, self).tearDown(*args, **kwargs)
#         for mock in self._mock:
#             mock.stop()
# 
# Path: baobab/utils/mock.py
# class MockSN(SocialNetworkBase):
#     name = 'MockSN'
# 
#     def __init__(self):
#         self._msg = []
# 
#     @classmethod
#     def is_configured(cls):
#         True
# 
#     @classmethod
#     def get_max_char(cls, default):
#         return 42
# 
#     def publish(self, msg, url):
#         self._msg.append((msg, url))
#         return len(self._msg)
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         pass
# 
# Path: baobab/backoffice/models.py
# class Event(models.Model):
#     objects = EventManager()  # override the default manager
# 
#     # Used for <pubDate> in RSS Feed
#     last_update = models.DateTimeField(default=now)
# 
#     date_start = models.DateTimeField(default=now)
#     date_end = models.DateTimeField(null=True, blank=True, default=None)
#     estimate_date_end = models.DateTimeField()
#     duration = models.PositiveIntegerField(help_text='Duration (in minutes)')
# 
#     # XXX to remove at the next release
#     title = models.CharField(max_length=255,
#                              help_text='A short description of the event')
# 
#     # XXX to remove at the next release
#     summary = models.TextField(null=True, blank=True, default=None)
#     services = models.ManyToManyField('Service', related_name='events',
#                                       blank=True)
#     MAINTENANCE = 0
#     INCIDENT = 1
#     CATEGORY_CHOICES = (
#         (MAINTENANCE, 'Maintenance'),
#         (INCIDENT, 'Incident'),
#     )
#     category = models.PositiveSmallIntegerField(choices=CATEGORY_CHOICES,
#                                                 help_text='Type of the Event')
#     msg = models.CharField('Social Network', max_length=255,
#                            null=True, blank=True, default=None)
# 
#     def __unicode__(self):
#         return self.title
# 
#     def save(self, *args, **kwargs):
#         if not self.id and self.date_end and self.date_end > now():
#             # create an event with a date_end in future: need to be converted
#             # to the estimate_date_end
#             delta = self.date_end - self.date_start
#             self.duration = delta.total_seconds() / 60
#             self.date_end = None
#         if self.date_end:
#             delta = self.date_end - self.date_start
#             self.duration = delta.total_seconds() / 60
#             if not self.estimate_date_end or \
#                self.date_start >= self.estimate_date_end:
#                 self.estimate_date_end = self.date_end
#             if self.date_end <= now():
#                 if self.eventlogs.exists() and \
#                    self.eventlogs.all()[0].date < self.date_end:
#                     self.last_update = self.date_end
#         else:
#             delta = timedelta(minutes=self.duration)
#             self.estimate_date_end = self.date_start + delta
#         super(Event, self).save(*args, **kwargs)
# 
#     class Meta:
#         # XXX avoid the admin to override the right order: see EventQuerySet
#         ordering = ['id', 'pk']
# 
# Path: baobab/socialnetwork/models.py
# class Event(models.Model):
# 
#     event = models.ForeignKey('backoffice.Event')
#     name = models.CharField(max_length=10)
#     sn_id = models.CharField(max_length=30)
# 
#     class Meta:
#         unique_together = ('event', 'name')


# Path: baobab/backoffice/tests/test_event_trigger.py
import datetime
from django.utils.timezone import now as tz_now
from baobab.utils.test import TestCase
from baobab.utils.mock import MockSN
from baobab.backoffice.models import Event
from baobab.socialnetwork.models import Event as SnEvent


        self.assertEqual(SnEvent.objects.count(), 1)
        sn_event = SnEvent.objects.get(id=1)
        self.assertEqual(sn_event.event, event)
        self.assertEqual(sn_event.name, MockSN.name)
        self.assertEqual(sn_event.sn_id, '1')

    def test_02_event_incident_past(self):
        self.assertEqual(SnEvent.objects.count(), 0)

        now = tz_now().replace(microsecond=0) - datetime.timedelta(minutes=20)
        end = now - datetime.timedelta(minutes=10)
        event = self._create_event(now, end, Event.INCIDENT)

        self.assertEqual(SnEvent.objects.count(), 1)
        sn_event = SnEvent.objects.get(id=1)
        self.assertEqual(sn_event.event, event)
        self.assertEqual(sn_event.name, MockSN.name)
        self.assertEqual(sn_event.sn_id, '1')

    def test_03_event_incident_futre(self):
        self.assertEqual(SnEvent.objects.count(), 0)

        now = tz_now().replace(microsecond=0) + datetime.timedelta(minutes=10)
        end = now + datetime.timedelta(minutes=10)
        event = self._create_event(now, end, Event.INCIDENT)

        self.assertEqual(SnEvent.objects.count(), 1)
        sn_event = SnEvent.objects.get(id=1)
        self.assertEqual(sn_event.event, event)

        self.assertEqual(sn_event.name, MockSN.name)