From ba11e633646fc64f6ee0511ba5d0ca4440da4ffe Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 16 Nov 2016 19:11:55 -0400 Subject: [PATCH] Improve transformation hashing algorithm. --- mayan/apps/converter/classes.py | 12 ++++++------ mayan/apps/converter/tests/test_classes.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/mayan/apps/converter/classes.py b/mayan/apps/converter/classes.py index 1183a90327..794b4db4c1 100644 --- a/mayan/apps/converter/classes.py +++ b/mayan/apps/converter/classes.py @@ -233,11 +233,11 @@ class BaseTransformation(object): def combine(transformations): result = None - for transformation in transformations: + for index, transformation in enumerate(transformations): if not result: - result = BaseTransformation.decode_hash(transformation.cache_hash()) + result = hash((BaseTransformation.decode_hash(transformation.cache_hash()), index)) else: - result ^= BaseTransformation.decode_hash(transformation.cache_hash()) + result ^= hash((BaseTransformation.decode_hash(transformation.cache_hash()), index)) return BaseTransformation.encode_hash(result) @@ -267,8 +267,8 @@ class BaseTransformation(object): def cache_hash(self): result = unicode.__hash__(self.name) - for key, value in self.kwargs.items(): - result ^= unicode.__hash__(key) ^ str.__hash__(str(value)) + for index, (key, value) in enumerate(self.kwargs.items()): + result ^= hash((key, index)) ^ hash((value, index)) return BaseTransformation.encode_hash(result) @@ -314,7 +314,7 @@ class TransformationRotate(BaseTransformation): self.degrees %= 360 - if self.degress == 0: + if self.degrees == 0: return self.image return self.image.rotate( diff --git a/mayan/apps/converter/tests/test_classes.py b/mayan/apps/converter/tests/test_classes.py index aece4b2733..9290c3ba58 100644 --- a/mayan/apps/converter/tests/test_classes.py +++ b/mayan/apps/converter/tests/test_classes.py @@ -21,6 +21,24 @@ TRANSFORMATION_ZOOM_CACHE_HASH = '47840c3658dc399a' class TransformationTestCase(TestCase): + def test_cache_uniqness(self): + transformation_1 = TransformationResize(width=640, height=640) + + transformation_2 = TransformationResize(width=800, height=800) + + self.assertNotEqual( + transformation_1.cache_hash(), transformation_2.cache_hash() + ) + + def test_cache_combining_uniqness(self): + transformation_1 = TransformationZoom(percent=100) + transformation_2 = TransformationResize(width=800, height=800) + + self.assertNotEqual( + BaseTransformation.combine((transformation_1, transformation_2)), + BaseTransformation.combine((transformation_2, transformation_1)), + ) + def test_resize_cache_hashing(self): # Test if the hash is being generated correctly transformation = TransformationResize(