from unittest import TestCase

from distribution import utils


class UtilsTestCase(TestCase):

    def test_validate_uri(self):

        valid = [
            ('http://news.nationalgeographic.com/2017/05/killer-whale-attacks-blue-whale-monterey-drone-video/',
             '2017/05/killer-whale-attacks-blue-whale-monterey-drone-video/'),
            ('http://news-staging.nationalgeographic.com/amp-test-may-24/',
             'amp-test-may-24/')
        ]

        invalid = [
            ('http://news.nationalgeographic.com/2017/05/killer-whale-attacks-blue-whale-monterey-drone-video/',
             '05/killer-whale-attacks-blue-whale-monterey-drone-video/'),
            ('http://news.nationalgeographic.com/2017/05/killer-whale-attacks-blue-whale-monterey-drone-video/',
             'killer-whale-attacks-blue-whale-monterey-drone-video/'),
            ('http://news.nationalgeographic.com/2017/05/killer-whale-attacks-blue-whale-monterey-drone-video/',
             '2017/05/'),
            ('http://news.nationalgeographic.com/2017/05/killer-whale-attacks-blue-whale-monterey-drone-video/',
             '2017/'),
        ]

        for obj_url, query in valid:
            self.assertTrue(utils.validate_uri_format(obj_url, query))

        for obj_url, query in invalid:
            self.assertFalse(utils.validate_uri_format(obj_url, query))

    def test_validate_hours_day_format(self):
        valid = ['1h', '23h', '15h', '1d', '100d', '48d']
        invalid = ['-3h', '0h', '24h', '15ddh', '15horas', '-3d', '0d',
                   '101d', '-4z', 'jflksajfklds', '543', '56h', '433d', '5dd']

        for s in valid:
            self.assertEqual(utils.validate_hours_day_format(s), s)

        for s in invalid:
            with self.assertRaises(ValueError):
                utils.validate_hours_day_format(s)

    def test_validate_rfc3339_date(self):
        valid = ['2014-02-05T20:43:33Z', '2017-05-01T20:43:33Z']
        invalid = ['2014-2-05T20:43:33Z', '2014-2-05T20:4:33Z',
                   '2013-01-06', '20:40:30Z', '2014-02-05T25:43:33Z',
                   '2012-32-05T20:43:33Z', '2017-02-05T24:43:33Z',
                   '2014-02-30T21:43:33Z', '2014-02-05T24:00:00Z']

        for s in valid:
            self.assertEqual(utils.validate_rfc3339_date(s), s)

        for s in invalid:
            with self.assertRaises(ValueError):
                utils.validate_rfc3339_date(s)

    def test_get_worst_status(self):
        self.assertEqual(utils.get_worst_status([]), 'green')
        self.assertEqual(utils.get_worst_status(['green']), 'green')
        self.assertEqual(utils.get_worst_status(['green', 'green']), 'green')
        self.assertEqual(utils.get_worst_status(['yellow', 'green']), 'yellow')
        self.assertEqual(utils.get_worst_status(['green', 'red']), 'red')
        self.assertEqual(utils.get_worst_status(['red', 'green', 'red', 'yellow']), 'red')
        self.assertEqual(utils.get_worst_status(['invalid']), 'green')
        self.assertEqual(utils.get_worst_status(['invalid', 'green']), 'green')
        self.assertEqual(utils.get_worst_status(['green', 'invalid', 'yellow']), 'yellow')

    def test_remove_invalid_xml_chars(self):
        invalid_string = u'This is a bad character\u0001 and has to be removed.'
        expected_string = u'This is a bad character and has to be removed.'
        parsed_string = utils.invalid_xml_remove(invalid_string)

        self.assertEqual(parsed_string, expected_string)

        invalid_string = u'A lot of invalid chars\u0001\u0002 and this is valid \u0009.'
        expected_string = u'A lot of invalid chars and this is valid \u0009.'

        parsed_string = utils.invalid_xml_remove(invalid_string)
        self.assertEqual(parsed_string, expected_string)

    def test_validate_domain_wrong_string_separators(self):
        wrong_separators_strings = [
            'History Travel',
            'gs:uk gs:mx gs:pt'
            'Travel, History'
            'Travel , History'
        ]

        for wrong_string in wrong_separators_strings:
            self.assertRaises(
                ValueError,
                utils.validate_domain_string,
                wrong_string
            )

    def test_validate_domain_wrong_string_domain(self):
        wrong_domain_strings = [
            'History!,Travel',
            'gs;uk,gs:mx,gs:pt'
            'Travel OR History9'
            'Trav&l OR History'
            'Travel%History'
        ]

        for wrong_string in wrong_domain_strings:
            self.assertRaises(
                ValueError,
                utils.validate_domain_string,
                wrong_string
            )

    def test_validate_domain_valid_string(self):
        valid_domain_strings = [
            'Travel',
            'History OR Travel',
            'gs:uk OR gs:mx OR gs:pt',
            'gs:uk or gs:mx or gs:pt',
            'Travel,History'
        ]

        for valid_string in valid_domain_strings:
            self.assertEqual(valid_string, utils.validate_domain_string(valid_string))
