aboutsummaryrefslogtreecommitdiffstats
path: root/unittest/exif.py
blob: 72fb089fed6a2b4590b0af142c32fb03ffc3ea98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-

# ******************************************************************************
#
# Copyright (C) 2009 Olivier Tilloy <olivier@tilloy.net>
#
# This file is part of the pyexiv2 distribution.
#
# pyexiv2 is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# pyexiv2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyexiv2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
#
# Author: Olivier Tilloy <olivier@tilloy.net>
#
# ******************************************************************************

import unittest
from pyexiv2 import ExifTag, ExifValueError
import datetime


class TestExifTag(unittest.TestCase):

    def test_convert_to_python_ascii(self):
        xtype = 'Ascii'
        # Valid values: datetimes
        self.assertEqual(ExifTag._convert_to_python('2009-03-01 12:46:51', xtype),
                         datetime.datetime(2009, 03, 01, 12, 46, 51))
        self.assertEqual(ExifTag._convert_to_python('2009:03:01 12:46:51', xtype),
                         datetime.datetime(2009, 03, 01, 12, 46, 51))
        self.assertEqual(ExifTag._convert_to_python('2009-03-01T12:46:51Z', xtype),
                         datetime.datetime(2009, 03, 01, 12, 46, 51))
        # Valid values: strings
        self.assertEqual(ExifTag._convert_to_python('Some text.', xtype), u'Some text.')
        self.assertEqual(ExifTag._convert_to_python('Some text with exotic chàräctérʐ.', xtype),
                         u'Some text with exotic chàräctérʐ.')
        # Invalid values: datetimes
        self.assertEqual(ExifTag._convert_to_python('2009-13-01 12:46:51', xtype),
                         u'2009-13-01 12:46:51')
        self.assertEqual(ExifTag._convert_to_python('2009-12-01', xtype),
                         u'2009-12-01')

    def test_convert_to_string_ascii(self):
        xtype = 'Ascii'
        # Valid values: datetimes
        self.assertEqual(ExifTag._convert_to_string(datetime.datetime(2009, 03, 01, 12, 54, 28), xtype),
                         '2009-03-01 12:54:28')
        self.assertEqual(ExifTag._convert_to_string(datetime.date(2009, 03, 01), xtype),
                         '2009-03-01 00:00:00')
        # Valid values: strings
        self.assertEqual(ExifTag._convert_to_string(u'Some text', xtype), 'Some text')
        self.assertEqual(ExifTag._convert_to_string(u'Some text with exotic chàräctérʐ.', xtype),
                         'Some text with exotic chàräctérʐ.')
        self.assertEqual(ExifTag._convert_to_string('Some text with exotic chàräctérʐ.', xtype),
                         'Some text with exotic chàräctérʐ.')
        # Invalid values
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, None, xtype)

    def test_convert_to_python_short(self):
        xtype = 'Short'
        # Valid values
        self.assertEqual(ExifTag._convert_to_python('23', xtype), 23)
        self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628)
        # Invalid values
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype)

    def test_convert_to_string_short(self):
        xtype = 'Short'
        # Valid values
        self.assertEqual(ExifTag._convert_to_string(123, xtype), '123')
        # Invalid values
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, -57, xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 'invalid', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 3.14, xtype)

    def test_convert_to_python_long(self):
        xtype = 'Long'
        # Valid values
        self.assertEqual(ExifTag._convert_to_python('23', xtype), 23)
        self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628)
        # Invalid values
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype)

    def test_convert_to_string_long(self):
        xtype = 'Long'
        # Valid values
        self.assertEqual(ExifTag._convert_to_string(123, xtype), '123')
        self.assertEqual(ExifTag._convert_to_string(678024, xtype), '678024')
        # Invalid values
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, -57, xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 'invalid', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 3.14, xtype)

    def test_convert_to_python_slong(self):
        xtype = 'SLong'
        # Valid values
        self.assertEqual(ExifTag._convert_to_python('23', xtype), 23)
        self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628)
        self.assertEqual(ExifTag._convert_to_python('-437', xtype), -437)
        # Invalid values
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype)

    def test_convert_to_string_slong(self):
        xtype = 'SLong'
        # Valid values
        self.assertEqual(ExifTag._convert_to_string(123, xtype), '123')
        self.assertEqual(ExifTag._convert_to_string(678024, xtype), '678024')
        self.assertEqual(ExifTag._convert_to_string(-437, xtype), '-437')
        # Invalid values
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 'invalid', xtype)
        self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 3.14, xtype)