# 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/apirest/modelresource.py
# class MySerializer(Serializer):
# 
#     """
#     always return UTC datetimes in ISO 8601
#     """
# 
#     @classmethod
#     def format_datetime(cls, date):
#         if is_naive(date):
#             date = pytz.timezone(settings.TIME_ZONE).localize(date)
#         if not isinstance(date.tzinfo, pytz.UTC.__class__):
#             date = date.astimezone(pytz.UTC)
#         # XXX date set in the admin doesn't have microsecond, but the one set
#         #     with 'default=now' has, for consistency never output microsecond
#         return date.replace(microsecond=0).isoformat()
# 
#     def to_html(self, data, options=None):
#         if isinstance(data, dict) and 'fields' in data:
#             title = settings.TITLE_SCHEMA
#         else:
#             title = settings.TITLE_RESULT
#         return textwrap.dedent("""
#         <!DOCTYPE html>
#         <html>
#           <head>
#             <style>
#               body {
#                 font-family: Monaco,"DejaVu Sans Mono",
#                              "Courier New",monospace;
#               }
#               div {
#                 margin: 5px;
#                 padding: 5px;
#                 border: 1px solid #444;
#                 border-radius: 5px;
#                 background-color: #eee;
#               }
#               h1 {
#                 font-size: 1.4rem;
#               }
#               .command:before {
#                 font-size: 1.4rem;
#               }
#               pre {
#                 white-space: pre-wrap;
#               }
#             </style>
#             <title>%(title)s</title>
#           </head>
#           <body>
#             <div>
#               <h1 class="command">%(title)s</h1>
#               <pre>%(data)s</pre>
#             </div>
#           </body>
#         </html>
#         """) % {
#             'title': title,
#             'data': json.dumps(data, indent=4, sort_keys=True,
#                                default=my_handler)
#         }
# 
#     def serialize(self, bundle, format='application/json', options={}):
#         try:
#             return super(MySerializer, self).serialize(bundle, format, options)
#         except (ImproperlyConfigured, UnsupportedFormat):
#             raise ImmediateHttpResponse(
#                 HttpNotImplemented(settings.HTTP_NOT_IMPLEMENTED_ERROR)
#             )
#         except ImmediateHttpResponse:
#             raise
#         except Exception:
#             raise ImmediateHttpResponse(
#                 HttpApplicationError(settings.HTTP_APPLICATION_ERROR)
#             )


import datetime
import pytz
from django.conf import settings
from django.utils.timezone import is_naive, now
from baobab.utils.test import TestCase
from baobab.apirest.modelresource import MySerializer
class TestUTCDate(TestCase):
    url = '/api/services'

    def test_01_native_date(self):
        date = datetime.datetime(2014, 5, 9, 18, 00)
        tz = pytz.timezone(settings.TIME_ZONE)
        self.assertTrue(is_naive(date))
        api_date = MySerializer().format_datetime(date)
        date = date - tz.utcoffset(date)
        self.assertEqual(api_date, date.strftime('%Y-%m-%dT%H:%M:%S+00:00'))

    def test_02_default_tz(self):
        tz = pytz.timezone(settings.TIME_ZONE)
        date = datetime.datetime(2014, 5, 9, 18, 00, tzinfo=tz)
        self.assertFalse(is_naive(date))
        api_date = MySerializer().format_datetime(date)
        date = date - tz.utcoffset(date)
        #date = date.astimezone(pytz.UTC)
        self.assertEqual(api_date, date.strftime('%Y-%m-%dT%H:%M:%S+00:00'))

    def test_03_utc_tz(self):
        date = datetime.datetime(2014, 5, 9, 18, 00, tzinfo=pytz.UTC)
        self.assertFalse(is_naive(date))
        api_date = MySerializer().format_datetime(date)
        self.assertEqual(api_date, date.strftime('%Y-%m-%dT%H:%M:%S+00:00'))

    def test_04_another_tz(self):
        tz = pytz.timezone('US/Hawaii')
        date = datetime.datetime(2014, 5, 9, 18, 00, tzinfo=tz)
        self.assertFalse(is_naive(date))

# 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/apirest/modelresource.py
# class MySerializer(Serializer):
# 
#     """
#     always return UTC datetimes in ISO 8601
#     """
# 
#     @classmethod
#     def format_datetime(cls, date):
#         if is_naive(date):
#             date = pytz.timezone(settings.TIME_ZONE).localize(date)
#         if not isinstance(date.tzinfo, pytz.UTC.__class__):
#             date = date.astimezone(pytz.UTC)
#         # XXX date set in the admin doesn't have microsecond, but the one set
#         #     with 'default=now' has, for consistency never output microsecond
#         return date.replace(microsecond=0).isoformat()
# 
#     def to_html(self, data, options=None):
#         if isinstance(data, dict) and 'fields' in data:
#             title = settings.TITLE_SCHEMA
#         else:
#             title = settings.TITLE_RESULT
#         return textwrap.dedent("""
#         <!DOCTYPE html>
#         <html>
#           <head>
#             <style>
#               body {
#                 font-family: Monaco,"DejaVu Sans Mono",
#                              "Courier New",monospace;
#               }
#               div {
#                 margin: 5px;
#                 padding: 5px;
#                 border: 1px solid #444;
#                 border-radius: 5px;
#                 background-color: #eee;
#               }
#               h1 {
#                 font-size: 1.4rem;
#               }
#               .command:before {
#                 font-size: 1.4rem;
#               }
#               pre {
#                 white-space: pre-wrap;
#               }
#             </style>
#             <title>%(title)s</title>
#           </head>
#           <body>
#             <div>
#               <h1 class="command">%(title)s</h1>
#               <pre>%(data)s</pre>
#             </div>
#           </body>
#         </html>
#         """) % {
#             'title': title,
#             'data': json.dumps(data, indent=4, sort_keys=True,
#                                default=my_handler)
#         }
# 
#     def serialize(self, bundle, format='application/json', options={}):
#         try:
#             return super(MySerializer, self).serialize(bundle, format, options)
#         except (ImproperlyConfigured, UnsupportedFormat):
#             raise ImmediateHttpResponse(
#                 HttpNotImplemented(settings.HTTP_NOT_IMPLEMENTED_ERROR)
#             )
#         except ImmediateHttpResponse:
#             raise
#         except Exception:
#             raise ImmediateHttpResponse(
#                 HttpApplicationError(settings.HTTP_APPLICATION_ERROR)
#             )


# Path: baobab/apirest/tests/test_utc_date.py
import datetime
import pytz
from django.conf import settings
from django.utils.timezone import is_naive, now
from baobab.utils.test import TestCase
from baobab.apirest.modelresource import MySerializer

class TestUTCDate(TestCase):
    url = '/api/services'

    def test_01_native_date(self):
        date = datetime.datetime(2014, 5, 9, 18, 00)
        tz = pytz.timezone(settings.TIME_ZONE)
        self.assertTrue(is_naive(date))
        api_date = MySerializer().format_datetime(date)
        date = date - tz.utcoffset(date)
        self.assertEqual(api_date, date.strftime('%Y-%m-%dT%H:%M:%S+00:00'))

    def test_02_default_tz(self):
        tz = pytz.timezone(settings.TIME_ZONE)
        date = datetime.datetime(2014, 5, 9, 18, 00, tzinfo=tz)
        self.assertFalse(is_naive(date))
        api_date = MySerializer().format_datetime(date)
        date = date - tz.utcoffset(date)
        #date = date.astimezone(pytz.UTC)
        self.assertEqual(api_date, date.strftime('%Y-%m-%dT%H:%M:%S+00:00'))

    def test_03_utc_tz(self):
        date = datetime.datetime(2014, 5, 9, 18, 00, tzinfo=pytz.UTC)
        self.assertFalse(is_naive(date))
        api_date = MySerializer().format_datetime(date)
        self.assertEqual(api_date, date.strftime('%Y-%m-%dT%H:%M:%S+00:00'))

    def test_04_another_tz(self):
        tz = pytz.timezone('US/Hawaii')
        date = datetime.datetime(2014, 5, 9, 18, 00, tzinfo=tz)
        self.assertFalse(is_naive(date))

        api_date = MySerializer().format_datetime(date)