from datetime import date, datetime, timedelta
from decimal import Decimal
import unittest

from dateutil.tz import tzutc
import six

from yolk import utils


class TestUtils(unittest.TestCase):

    def test_timezone_utils(self):
        now = datetime.now()
        utcnow = datetime.now(tz=tzutc())
        self.assertTrue(utils.is_naive(now))
        self.assertFalse(utils.is_naive(utcnow))

        fixed = utils.guess_timezone(now)
        self.assertFalse(utils.is_naive(fixed))

        shouldnt_be_edited = utils.guess_timezone(utcnow)
        self.assertEqual(utcnow, shouldnt_be_edited)

    def test_clean(self):
        simple = {
            'decimal': Decimal('0.142857'),
            'unicode': six.u('woo'),
            'date': datetime.now(),
            'long': 200000000,
            'integer': 1,
            'float': 2.0,
            'bool': True,
            'str': 'woo',
            'none': None
        }

        complicated = {
            'exception': Exception('This should show up'),
            'timedelta': timedelta(microseconds=20),
            'list': [1, 2, 3]
        }

        combined = dict(simple.items())
        combined.update(complicated.items())

        pre_clean_keys = combined.keys()

        utils.clean(combined)
        self.assertEqual(combined.keys(), pre_clean_keys)

    def test_clean_with_dates(self):
        dict_with_dates = {
            'birthdate': date(1980, 1, 1),
            'registration': datetime.utcnow(),
        }
        self.assertEqual(dict_with_dates, utils.clean(dict_with_dates))

    @classmethod
    def test_bytes(cls):
        if six.PY3:
            item = bytes(10)
        else:
            item = bytearray(10)

        utils.clean(item)

    def test_clean_fn(self):
        cleaned = utils.clean({'fn': lambda x: x, 'number': 4})
        self.assertEqual(cleaned['number'], 4)
        if 'fn' in cleaned:
            self.assertEqual(cleaned['fn'], None)

    def test_remove_slash(self):
        self.assertEqual('http://segment.io',
                         utils.remove_trailing_slash('http://segment.io/'))
        self.assertEqual('http://segment.io',
                         utils.remove_trailing_slash('http://segment.io'))

[((14, 14, 14, 28), 'datetime.datetime.now', 'datetime.now', ({}, {}), '()', False, 'from datetime import date, datetime, timedelta\n'), ((19, 16, 19, 41), 'yolk.utils.guess_timezone', 'utils.guess_timezone', ({(19, 37, 19, 40): 'now'}, {}), '(now)', False, 'from yolk import utils\n'), ((22, 29, 22, 57), 'yolk.utils.guess_timezone', 'utils.guess_timezone', ({(22, 50, 22, 56): 'utcnow'}, {}), '(utcnow)', False, 'from yolk import utils\n'), ((49, 8, 49, 29), 'yolk.utils.clean', 'utils.clean', ({(49, 20, 49, 28): 'combined'}, {}), '(combined)', False, 'from yolk import utils\n'), ((66, 8, 66, 25), 'yolk.utils.clean', 'utils.clean', ({(66, 20, 66, 24): 'item'}, {}), '(item)', False, 'from yolk import utils\n'), ((69, 18, 69, 63), 'yolk.utils.clean', 'utils.clean', ({(69, 30, 69, 62): "{'fn': lambda x: x, 'number': 4}"}, {}), "({'fn': lambda x: x, 'number': 4})", False, 'from yolk import utils\n'), ((16, 24, 16, 43), 'yolk.utils.is_naive', 'utils.is_naive', ({(16, 39, 16, 42): 'now'}, {}), '(now)', False, 'from yolk import utils\n'), ((17, 25, 17, 47), 'yolk.utils.is_naive', 'utils.is_naive', ({(17, 40, 17, 46): 'utcnow'}, {}), '(utcnow)', False, 'from yolk import utils\n'), ((20, 25, 20, 46), 'yolk.utils.is_naive', 'utils.is_naive', ({(20, 40, 20, 45): 'fixed'}, {}), '(fixed)', False, 'from yolk import utils\n'), ((27, 23, 27, 42), 'decimal.Decimal', 'Decimal', ({(27, 31, 27, 41): '"""0.142857"""'}, {}), "('0.142857')", False, 'from decimal import Decimal\n'), ((28, 23, 28, 35), 'six.u', 'six.u', ({(28, 29, 28, 34): '"""woo"""'}, {}), "('woo')", False, 'import six\n'), ((29, 20, 29, 34), 'datetime.datetime.now', 'datetime.now', ({}, {}), '()', False, 'from datetime import date, datetime, timedelta\n'), ((40, 25, 40, 51), 'datetime.timedelta', 'timedelta', (), '', False, 'from datetime import date, datetime, timedelta\n'), ((54, 25, 54, 41), 'datetime.date', 'date', ({(54, 30, 54, 34): '(1980)', (54, 36, 54, 37): '(1)', (54, 39, 54, 40): '(1)'}, {}), '(1980, 1, 1)', False, 'from datetime import date, datetime, timedelta\n'), ((55, 28, 55, 45), 'datetime.datetime.utcnow', 'datetime.utcnow', ({}, {}), '()', False, 'from datetime import date, datetime, timedelta\n'), ((57, 42, 57, 70), 'yolk.utils.clean', 'utils.clean', ({(57, 54, 57, 69): 'dict_with_dates'}, {}), '(dict_with_dates)', False, 'from yolk import utils\n'), ((76, 25, 76, 74), 'yolk.utils.remove_trailing_slash', 'utils.remove_trailing_slash', ({(76, 53, 76, 73): '"""http://segment.io/"""'}, {}), "('http://segment.io/')", False, 'from yolk import utils\n'), ((78, 25, 78, 73), 'yolk.utils.remove_trailing_slash', 'utils.remove_trailing_slash', ({(78, 53, 78, 72): '"""http://segment.io"""'}, {}), "('http://segment.io')", False, 'from yolk import utils\n'), ((15, 33, 15, 40), 'dateutil.tz.tzutc', 'tzutc', ({}, {}), '()', False, 'from dateutil.tz import tzutc\n')]