Use list.sort's keyword parameters
[joel/kofoto.git] / src / test / shelftests.py
index 9befb28..4c77fa5 100755 (executable)
@@ -6,15 +6,41 @@ import shutil
 import sys
 import threading
 import unittest
-import Image as PILImage
 
 if __name__ == "__main__":
     cwd = os.getcwd()
     libdir = unicode(os.path.realpath(
-        os.path.join(os.path.dirname(sys.argv[0]), "..", "lib")))
+        os.path.join(os.path.dirname(sys.argv[0]), "..", "packages")))
     os.chdir(libdir)
     sys.path.insert(0, libdir)
-from kofoto.shelf import *
+from kofoto.shelf import \
+    Shelf, \
+    computeImageHash, \
+    makeValidTag, \
+    verifyValidAlbumTag, \
+    verifyValidCategoryTag
+from kofoto.albumtype import AlbumType
+from kofoto.imageversiontype import ImageVersionType
+from kofoto.shelfexceptions import \
+    AlbumDoesNotExistError, \
+    AlbumExistsError, \
+    BadAlbumTagError, \
+    BadCategoryTagError, \
+    CategoriesAlreadyConnectedError, \
+    CategoryDoesNotExistError, \
+    CategoryExistsError, \
+    CategoryLoopError, \
+    CategoryPresentError, \
+    FailedWritingError, \
+    ImageDoesNotExistError, \
+    ImageVersionDoesNotExistError, \
+    ImageVersionExistsError, \
+    ShelfLockedError, \
+    ShelfNotFoundError, \
+    UndeletableAlbumError, \
+    UndeletableAlbumError, \
+    UnsettableChildrenError, \
+    UnsupportedShelfError
 
 PICDIR = unicode(os.path.realpath(
     os.path.join("..", "reference_pictures", "working")))
@@ -22,7 +48,6 @@ PICDIR = unicode(os.path.realpath(
 ######################################################################
 
 db = "shelf.tmp"
-codeset = "latin1"
 
 def removeTmpDb():
     for x in [db, db + "-journal"]:
@@ -36,7 +61,7 @@ class LockerThread(threading.Thread):
         self.ltContinue = ltContinue
 
     def run(self):
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         s.begin()
         self.mContinue.set()
@@ -104,7 +129,7 @@ class TestNegativeShelfOpens(unittest.TestCase):
 
     def test_NonexistingShelf(self):
         try:
-            s = Shelf(db, codeset)
+            s = Shelf(db)
             s.begin()
         except ShelfNotFoundError:
             pass
@@ -115,7 +140,7 @@ class TestNegativeShelfOpens(unittest.TestCase):
     def test_BadShelf(self):
         file(db, "w") # Create empty file.
         try:
-            s = Shelf(db, codeset)
+            s = Shelf(db)
             s.begin()
         except UnsupportedShelfError:
             pass
@@ -130,7 +155,7 @@ class TestNegativeShelfOpens(unittest.TestCase):
         mContinue.wait()
         try:
             try:
-                s = Shelf(db, codeset)
+                s = Shelf(db)
                 s.begin()
             except ShelfLockedError:
                 pass
@@ -145,13 +170,13 @@ class TestShelfCreation(unittest.TestCase):
         removeTmpDb()
 
     def test_CreateShelf1(self):
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         assert os.path.exists(db)
 
     def test_CreateShelf2(self):
         file(db, "w") # Create empty file.
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         try:
             s.create()
         except FailedWritingError:
@@ -164,12 +189,12 @@ class TestShelfMemoryLeakage(unittest.TestCase):
         removeTmpDb()
 
     def test_MemoryLeak1(self):
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         assert gc.collect() == 0
 
     def test_MemoryLeak2(self):
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         s.begin()
         s.getObject(0)
@@ -177,7 +202,7 @@ class TestShelfMemoryLeakage(unittest.TestCase):
         assert gc.collect() == 0
 
     def test_MemoryLeak3(self):
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         s.begin()
         s.getObject(0)
@@ -189,19 +214,19 @@ class TestShelfTransactions(unittest.TestCase):
         removeTmpDb()
 
     def test_commit(self):
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         s.begin()
         s.createAlbum(u"foo")
         assert s.getAlbumByTag(u"foo")
         s.commit()
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.begin()
         assert s.getAlbumByTag(u"foo")
         s.rollback()
 
     def test_rollback(self):
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         s.begin()
         s.createAlbum(u"foo")
@@ -215,7 +240,7 @@ class TestShelfTransactions(unittest.TestCase):
             assert False
 
     def test_isModified(self):
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         s.begin()
         assert not s.isModified()
@@ -230,7 +255,7 @@ class TestShelfTransactions(unittest.TestCase):
         res = [False]
         def f(x):
             res[0] = True
-        s = Shelf(db, codeset)
+        s = Shelf(db)
         s.create()
         s.begin()
         s.registerModificationCallback(f)
@@ -248,7 +273,7 @@ class TestShelfTransactions(unittest.TestCase):
 
 class TestShelfFixture(unittest.TestCase):
     def setUp(self):
-        self.shelf = Shelf(db, codeset)
+        self.shelf = Shelf(db)
         self.shelf.create()
         self.shelf.begin()
         root = self.shelf.getRootAlbum()
@@ -363,8 +388,16 @@ class TestShelfMethods(TestShelfFixture):
         assert len(imageversions) == 11
 
     def test_getImageVersionsInDirectory(self):
+        self.shelf.flushImageVersionCache()
+
         imageversions = list(self.shelf.getImageVersionsInDirectory(u"."))
         assert len(imageversions) == 0
+
+        # Image versions not in cache.
+        imageversions = list(self.shelf.getImageVersionsInDirectory(PICDIR))
+        assert len(imageversions) == 11
+
+        # Image versions in cache.
         imageversions = list(self.shelf.getImageVersionsInDirectory(PICDIR))
         assert len(imageversions) == 11
 
@@ -522,8 +555,7 @@ class TestShelfMethods(TestShelfFixture):
             assert False
 
     def test_getAllAttributeNames(self):
-        attrnames = list(self.shelf.getAllAttributeNames())
-        attrnames.sort()
+        attrnames = sorted(self.shelf.getAllAttributeNames())
         assert attrnames == [
             "cameramake", "cameramodel", "captured", "description",
             "digitalzoom", "exposurebias", "exposureprogram", "exposuretime",
@@ -560,12 +592,12 @@ class TestShelfMethods(TestShelfFixture):
             assert False
 
     def test_getRootCategories(self):
-        categories = list(self.shelf.getRootCategories())
+        categories = sorted(
+            self.shelf.getRootCategories(), key=lambda x: x.getTag())
         cat_a = self.shelf.getCategoryByTag(u"a")
         cat_events = self.shelf.getCategoryByTag(u"events")
         cat_locations = self.shelf.getCategoryByTag(u"locations")
         cat_people = self.shelf.getCategoryByTag(u"people")
-        categories.sort(lambda x, y: cmp(x.getTag(), y.getTag()))
         assert categories == [cat_a, cat_events, cat_locations, cat_people], \
                categories
 
@@ -584,8 +616,7 @@ class TestCategory(TestShelfFixture):
         cat_a.setDescription(u"foo")
         assert cat_a.getDescription() == "foo"
 
-        a_children = list(cat_a.getChildren())
-        a_children.sort(lambda x, y: cmp(x.getId(), y.getId()))
+        a_children = sorted(cat_a.getChildren(), key=lambda x: x.getId())
         assert a_children == [cat_b, cat_c]
         b_children = list(cat_b.getChildren())
         assert b_children == [cat_d]
@@ -596,8 +627,7 @@ class TestCategory(TestShelfFixture):
         assert a_parents == []
         b_parents = list(cat_b.getParents())
         assert b_parents == [cat_a]
-        d_parents = list(cat_d.getParents())
-        d_parents.sort(lambda x, y: cmp(x.getTag(), y.getTag()))
+        d_parents = sorted(cat_d.getParents(), key=lambda x: x.getTag())
         assert d_parents == [cat_b, cat_c]
 
         assert not cat_a.isChildOf(cat_a)
@@ -650,8 +680,7 @@ class TestObject(TestShelfFixture):
         root = self.shelf.getRootAlbum()
         alpha = self.shelf.getAlbumByTag(u"alpha")
         beta = self.shelf.getAlbumByTag(u"beta")
-        parents = list(beta.getParents())
-        parents.sort(lambda x, y: cmp(x.getTag(), y.getTag()))
+        parents = sorted(beta.getParents(), key=lambda x: x.getTag())
         assert parents == [alpha, root]
 
     def test_getAttribute(self):
@@ -670,8 +699,7 @@ class TestObject(TestShelfFixture):
 
     def test_getAttributeNames(self):
         orphans = self.shelf.getAlbumByTag(u"orphans")
-        names = list(orphans.getAttributeNames())
-        names.sort()
+        names = sorted(orphans.getAttributeNames())
         assert names == ["description", "title"]
 
     def test_setAttribute(self):
@@ -748,8 +776,7 @@ class TestAlbum(TestShelfFixture):
     def test_getAlbumParents(self):
         root = self.shelf.getRootAlbum()
         alpha = self.shelf.getAlbumByTag(u"alpha")
-        parents = list(alpha.getAlbumParents())
-        parents.sort(lambda x, y: cmp(x.getTag(), y.getTag()))
+        parents = sorted(alpha.getAlbumParents(), key=lambda x: x.getTag())
         assert parents == [root]
 
     def test_isAlbum(self):
@@ -803,9 +830,8 @@ class TestImage(TestShelfFixture):
         imageversion2 = self.shelf.getImageVersionByLocation(
             os.path.join(PICDIR, "Canon_Digital_IXUS.jpg"))
         imageversion2.setImage(image)
-        imageversions = list(image.getImageVersions())
-        imageversions.sort(lambda x, y: cmp(x.getHash(), y.getHash()))
-        assert list(image.getImageVersions()) == [imageversion, imageversion2]
+        imageversions = set(image.getImageVersions())
+        assert set(image.getImageVersions()) == imageversions
         self.shelf.deleteImageVersion(imageversion.getId())
         self.shelf.deleteImageVersion(imageversion2.getId())
         assert list(image.getImageVersions()) == []
@@ -821,6 +847,14 @@ class TestImage(TestShelfFixture):
         assert image.getPrimaryVersion() == imageversion
         imageversion2.makePrimary()
         assert image.getPrimaryVersion() == imageversion2
+
+        newImage = self.shelf.createImage()
+        lastImageVersion = list(image.getImageVersions())[-1]
+        lastImageVersion.setImage(newImage)
+        assert image.getPrimaryVersion() != lastImageVersion
+        assert newImage.getPrimaryVersion() == lastImageVersion
+        lastImageVersion.setImage(image)
+
         self.shelf.deleteImageVersion(imageversion2.getId())
         assert image.getPrimaryVersion() == imageversion
         self.shelf.deleteImageVersion(imageversion.getId())
@@ -838,8 +872,8 @@ class TestImageVersion(TestShelfFixture):
         imageversion.setType(ImageVersionType.Original)
         assert imageversion.getType() == ImageVersionType.Original
 
-    # ImageVersion.makePrimary tested in TestImage.test_getPrimaryversion.
-    # ImageVersion.setImage tested in TestImage.test_getPrimaryversion.
+    # ImageVersion.makePrimary tested in TestImage.test_getPrimaryVersion.
+    # ImageVersion.setImage tested in TestImage.test_getPrimaryVersion.
     # ImageVersion.setType tested in TestImageVersion.test_getType.
     # ImageVersion.setComment tested in TestImageVersion.test_getComment.
 
@@ -883,12 +917,11 @@ class TestImageVersion(TestShelfFixture):
             newimageversion = self.shelf.createImageVersion(
                 newimage, newpath, ImageVersionType.Original)
             oldmtime = imageversion.getModificationTime()
-            pilimg = PILImage.open(newpath)
-            pilimg.thumbnail((100, 100))
-            pilimg.save(newpath, "PNG")
+            f = open(newpath, "a")
+            f.write("foo")
+            f.close()
             newimageversion.contentChanged()
-            assert newimageversion.getHash() == "d55a9cc74371c09d484b163c71497cab"
-            assert newimageversion.getSize() == (56, 100)
+            assert newimageversion.getHash() == "b27312d9739c0edfd115f824be244b75"
             assert newimageversion.getModificationTime() > oldmtime
         finally:
             try: