diff options
Diffstat (limited to 'trunk/dimbola/copier_tests.py')
-rw-r--r-- | trunk/dimbola/copier_tests.py | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/trunk/dimbola/copier_tests.py b/trunk/dimbola/copier_tests.py new file mode 100644 index 0000000..73c8070 --- /dev/null +++ b/trunk/dimbola/copier_tests.py @@ -0,0 +1,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") |