# -*- coding: utf-8 -*-
#
# This file is part of reprints released under the AGPLv3 license.
# See the NOTICE for more information.

# future compatibilty
from __future__ import absolute_import

# standard
import unittest

# local
from reprints.errors import UnsupportImageFormatError, IllegalFilenameError
from reprints import filename


class TestFilename(unittest.TestCase):

    def test_parse_filename(self):
        id, size, format = filename.parse_filename('name/100x200.jpg')
        self.assertEqual(id, 'name')
        self.assertEqual(size, (100, 200))
        self.assertEqual(format, 'JPEG')

        id, size, format = filename.parse_filename('name_9999/100x200.jpg')
        self.assertEqual(id, 'name_9999')
        self.assertEqual(size, (100, 200))
        self.assertEqual(format, 'JPEG')

        id, size, format = filename.parse_filename('name/x200.png')
        self.assertEqual(id, 'name')
        self.assertEqual(size, ('', 200))
        self.assertEqual(format, 'PNG')

        id, size, format = filename.parse_filename('name/100x.webp')
        self.assertEqual(id, 'name')
        self.assertEqual(size, (100, ''))
        self.assertEqual(format, 'WEBP')

        self.assertRaises(UnsupportImageFormatError, filename.parse_filename, 'name/100x200.txt')
        self.assertRaises(IllegalFilenameError, filename.parse_filename, '100x200.jpg')
        self.assertRaises(IllegalFilenameError, filename.parse_filename, 'name.jpg')

    def test_get_filename(self):
        self.assertEqual(filename.get_filename('name', (100, 200), 'JPEG'),
                         'name/100x200.jpg')
        self.assertEqual(filename.get_filename('name', (100, None), 'PNG'),
                         'name/100x.png')
        self.assertEqual(filename.get_filename('name', (None, 200), 'WEBP'),
                         'name/x200.webp')

if __name__ == "__main__":
    unittest.main()
