summaryrefslogtreecommitdiff
path: root/trunk/dimbola/copier_tests.py
blob: 73c8070ebd526934cf946a5481f98bea412a3294 (plain)
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
131
132
133
134
135
136
137
138
# Copyright (C) 2009  Lars Wirzenius <liw@liw.fi>
# 
# This program 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 3 of the License, or
# (at your option) any later version.
# 
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.


import datetime
import os
import tempfile
import unittest

import dimbola


class MockExiv2Image(dict):

    def __init__(self):
        dict.__init__(self)
        self["Exif.Image.DateTime"] = datetime.datetime(2009, 4, 11, 
                                                        8, 6, 38)

    def exifKeys(self):
        return self.keys()


class MockOptions:

    def __init__(self):
        self.rename = False
        self.template = ""
        self.input = "/foo"
        self.output = "/bar"
        self.verbose = False


class ImageDictTests(unittest.TestCase):

    def setUp(self):
        mock_image = MockExiv2Image()
        self.dict = dimbola.ImageDict("/foo/img_1234.cr2", mock_image, 42)

    def test_gets_input_suffix_when_one_exists(self):
        suffix = self.dict.get_input_suffix("/foo.bar/img_1234.cr2")
        self.assertEqual(suffix, ".cr2")

    def test_returns_empty_input_suffix_when_none_exists(self):
        suffix = self.dict.get_input_suffix("/foo/bar.foobar/yeehaa")
        self.assertEqual(suffix, "")
        
    def test_returns_camera_counter_from_filename(self):
        counter = self.dict.get_camera_counter("/foo/bar_1234.suffix5678")
        self.assertEqual(counter, "1234")        

    def test_returns_empty_string_when_no_camera_counter_in_filename(self):
        counter = self.dict.get_camera_counter("/foo/bar.suffix5678")
        self.assertEqual(counter, "")
        
    def test_sets_username_to_nonempty_string(self):
        self.assertNotEqual(self.dict["username"], "")
        
    def test_sets_suffix_to_input_filename_suffix(self):
        self.assertEqual(self.dict["suffix"], ".cr2")
        
    def test_sets_camera_counter_from_input_filename(self):
        self.assertEqual(self.dict["cameracounter"], "1234")
        
    def test_sets_counter_correctly(self):
        self.assertEqual(self.dict["counter"], 42)
        
    def test_sets_date_correctly(self):
        self.assertEqual(self.dict["date"], "2009-04-11")
        
    def test_sets_year_correctly(self):
        self.assertEqual(self.dict["year"], 2009)
        
    def test_sets_month_correctly(self):
        self.assertEqual(self.dict["month"], 04)
        
    def test_sets_day_correctly(self):
        self.assertEqual(self.dict["day"], 11)
        
    def test_sets_hour_correctly(self):
        self.assertEqual(self.dict["hour"], 8)
        
    def test_sets_min_correctly(self):
        self.assertEqual(self.dict["min"], 6)
        
    def test_sets_sec_correctly(self):
        self.assertEqual(self.dict["sec"], 38)

    def test_sets_exif_with_prefix(self):
        self.assert_("Exif.Image.DateTime" in self.dict)

    def test_sets_exif_without_prefix(self):
        self.assert_("DateTime" in self.dict)


class CopierTests(unittest.TestCase):

    def setUp(self):
        self.options = MockOptions()
        self.importer = dimbola.Copier()

    def test_initializes_counter_to_zero(self):
        self.assertEqual(self.importer.counter, 0)
        
    def test_increments_counter_when_copying_a_file(self):
        fd, tempname = tempfile.mkstemp()
        os.close(fd)
        os.remove(tempname)
        self.importer.output_name = lambda *args: tempname
        self.importer.copy_file('/dev/null', self.options)
        
    def test_has_nonempty_list_of_known_image_types(self):
        self.assert_(self.importer.known_image_types)
        
    def test_uses_basename_of_input_for_output_when_no_renaming(self):
        self.assertEqual(self.importer.output_name("/foo/img_1234.cr2",
                                                   self.options),
                         "/bar/img_1234.cr2")
                         
    def test_uses_template_when_renaming(self):
        self.options.template = "%(date)s-%(cameracounter)s%(suffix)s"
        self.options.rename = True
        self.importer.read_exif = lambda s: MockExiv2Image()
        self.assertEqual(self.importer.output_name("/foo/img_1234.cr2",
                                                   self.options),
                         "/bar/2009-04-11-1234.cr2")