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'))

[((245, 259), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (257, 259), False, 'from datetime import date, datetime, timedelta\n'), ((413, 438), 'yolk.utils.guess_timezone', 'utils.guess_timezone', (['now'], {}), '(now)\n', (433, 438), False, 'from yolk import utils\n'), ((517, 545), 'yolk.utils.guess_timezone', 'utils.guess_timezone', (['utcnow'], {}), '(utcnow)\n', (537, 545), False, 'from yolk import utils\n'), ((1246, 1267), 'yolk.utils.clean', 'utils.clean', (['combined'], {}), '(combined)\n', (1257, 1267), False, 'from yolk import utils\n'), ((1712, 1729), 'yolk.utils.clean', 'utils.clean', (['item'], {}), '(item)\n', (1723, 1729), False, 'from yolk import utils\n'), ((1778, 1823), 'yolk.utils.clean', 'utils.clean', (["{'fn': lambda x: x, 'number': 4}"], {}), "({'fn': lambda x: x, 'number': 4})\n", (1789, 1823), False, 'from yolk import utils\n'), ((326, 345), 'yolk.utils.is_naive', 'utils.is_naive', (['now'], {}), '(now)\n', (340, 345), False, 'from yolk import utils\n'), ((372, 394), 'yolk.utils.is_naive', 'utils.is_naive', (['utcnow'], {}), '(utcnow)\n', (386, 394), False, 'from yolk import utils\n'), ((464, 485), 'yolk.utils.is_naive', 'utils.is_naive', (['fixed'], {}), '(fixed)\n', (478, 485), False, 'from yolk import utils\n'), ((668, 687), 'decimal.Decimal', 'Decimal', (['"""0.142857"""'], {}), "('0.142857')\n", (675, 687), False, 'from decimal import Decimal\n'), ((712, 724), 'six.u', 'six.u', (['"""woo"""'], {}), "('woo')\n", (717, 724), False, 'import six\n'), ((746, 760), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (758, 760), False, 'from datetime import date, datetime, timedelta\n'), ((1041, 1067), 'datetime.timedelta', 'timedelta', ([], {'microseconds': '(20)'}), '(microseconds=20)\n', (1050, 1067), False, 'from datetime import date, datetime, timedelta\n'), ((1417, 1433), 'datetime.date', 'date', (['(1980)', '(1)', '(1)'], {}), '(1980, 1, 1)\n', (1421, 1433), False, 'from datetime import date, datetime, timedelta\n'), ((1463, 1480), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (1478, 1480), False, 'from datetime import date, datetime, timedelta\n'), ((1534, 1562), 'yolk.utils.clean', 'utils.clean', (['dict_with_dates'], {}), '(dict_with_dates)\n', (1545, 1562), False, 'from yolk import utils\n'), ((2054, 2103), 'yolk.utils.remove_trailing_slash', 'utils.remove_trailing_slash', (['"""http://segment.io/"""'], {}), "('http://segment.io/')\n", (2081, 2103), False, 'from yolk import utils\n'), ((2176, 2224), 'yolk.utils.remove_trailing_slash', 'utils.remove_trailing_slash', (['"""http://segment.io"""'], {}), "('http://segment.io')\n", (2203, 2224), False, 'from yolk import utils\n'), ((293, 300), 'dateutil.tz.tzutc', 'tzutc', ([], {}), '()\n', (298, 300), False, 'from dateutil.tz import tzutc\n')]