def test_instance_fallback_order(self): class Sloth(object): def __init__(self): self.id = 'id' self.uuid = 'uuid' self.__cache_key__ = lambda: 'cache_key' sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.cache_key') del sloth.__cache_key__ self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.uuid') del sloth.uuid self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.id') del sloth.id with self.assertRaises(UniqueRepresentationNotFound) as e: get_unique_representation(sloth) self.assertEquals( str(e.exception), "Object of type <class 'tests.test_object_representations.Sloth'> " "has not declared an unique representation")
def test_datetime_derivate_object(self): self.assertEquals( get_unique_representation(datetime.datetime(2015, 1, 2, 3, 4, 5)), '2015-01-02T03:04:05') self.assertEquals(get_unique_representation(datetime.date(2015, 1, 2)), '2015-01-02') self.assertEquals(get_unique_representation(datetime.time(3, 4, 5)), '03:04:05')
def test_object_with_id_attribute(self): class Sloth(object): id = '1' sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.1') sloth.id = '2' self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.2')
def test_object_with_uuid_attribute(self): class Sloth(object): uuid = '123' sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.123') sloth.uuid = '456' self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.456')
def test_object_without_unique_representation(self): class Sloth(object): pass sloth = Sloth() with self.assertRaises(UniqueRepresentationNotFound) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), "Object of type <class 'tests.test_object_representations.Sloth'> " "has not declared an unique representation")
def test_object_without_unique_representation(self): class Sloth(object): pass sloth = Sloth() with self.assertRaises(UniqueRepresentationNotFound) as e: get_unique_representation(sloth) self.assertEquals( str(e.exception), "Object of type <class 'tests.test_object_representations.Sloth'> " "has not declared an unique representation")
def test_object_with_uuid_attribute(self): class Sloth(object): uuid = '123' sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.123') sloth.uuid = '456' self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.456')
def test_object_with_id_attribute(self): class Sloth(object): id = '1' sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.1') sloth.id = '2' self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.2')
def test_object_with_explicit_cache_key_method(self): class Sloth(object): lazy = 2 def __cache_key__(self): return str(self.lazy * 2) sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.4') sloth.lazy = 10 self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.20')
def test_object_with_explicit_cache_key_method(self): class Sloth(object): lazy = 2 def __cache_key__(self): return str(self.lazy * 2) sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.4') sloth.lazy = 10 self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.20')
def test_custom_representation_not_returning_string(self): class SlothBadlyRepresented(object): pass class SlothUniqueRepresentation(UniqueRepresentation): def get_unique_representation(self, obj): if isinstance(obj, SlothBadlyRepresented): return 42 sloth = SlothBadlyRepresented() with self.assertRaises(InvalidTypeForUniqueRepresentation) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), "<class 'tests.test_object_representations.SlothUniqueRepresentation'> " "returned non-string unique representation for " "<class 'tests.test_object_representations.SlothBadlyRepresented'> instance: 42")
def test_object_with_cache_key_declared_returning_something_different_to_string( self): class Sloth(object): def __init__(self): self.__cache_key__ = lambda: 'cache_key' sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.cache_key') sloth.__cache_key__ = lambda: False with self.assertRaises(InvalidTypeForUniqueRepresentation) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), 'obj.__cache_key__() must return a string') sloth.__cache_key__ = lambda: 42 with self.assertRaises(InvalidTypeForUniqueRepresentation) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), 'obj.__cache_key__() must return a string') sloth.__cache_key__ = lambda: None with self.assertRaises(InvalidTypeForUniqueRepresentation) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), 'obj.__cache_key__() must return a string')
def test_custom_representation_not_returning_string(self): class SlothBadlyRepresented(object): pass class SlothUniqueRepresentation(UniqueRepresentation): def get_unique_representation(self, obj): if isinstance(obj, SlothBadlyRepresented): return 42 sloth = SlothBadlyRepresented() with self.assertRaises(InvalidTypeForUniqueRepresentation) as e: get_unique_representation(sloth) self.assertEquals( str(e.exception), "<class 'tests.test_object_representations.SlothUniqueRepresentation'> " "returned non-string unique representation for " "<class 'tests.test_object_representations.SlothBadlyRepresented'> instance: 42" )
def test_custom_representation(self): class SlothRepresented(object): pass class SlothUniqueRepresentation(UniqueRepresentation): def get_unique_representation(self, obj): if isinstance(obj, SlothRepresented): return 'custom-representation-for-sloth' sloth = SlothRepresented() self.assertEquals(get_unique_representation(sloth), 'custom-representation-for-sloth')
def _create_cache_key(self): sanitized_keys = [self.func_full_qualified_name] for key in self.keys: parts = key.split('.') current_object = self.function_arguments[parts[0]] str_attr = '.'.join(parts[1:]) target_object = depth_getattr(current_object, str_attr) sanitized_keys.append(get_unique_representation(target_object)) self.cache_key = '//'.join(sanitized_keys)
def _create_cache_key(self): sanitized_keys = [self.func_full_qualified_name] for key in self.keys: parts = key.split('.') current_object = self.function_arguments[parts[0]] str_attr = '.'.join(parts[1:]) target_object = depth_getattr(current_object, str_attr) sanitized_keys.append(get_unique_representation(target_object)) self.cache_key = '//'.join(sanitized_keys)
def test_custom_representation(self): class SlothRepresented(object): pass class SlothUniqueRepresentation(UniqueRepresentation): def get_unique_representation(self, obj): if isinstance(obj, SlothRepresented): return 'custom-representation-for-sloth' sloth = SlothRepresented() self.assertEquals(get_unique_representation(sloth), 'custom-representation-for-sloth')
def test_instance_fallback_order(self): class Sloth(object): def __init__(self): self.id = 'id' self.uuid = 'uuid' self.__cache_key__ = lambda: 'cache_key' sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.cache_key') del sloth.__cache_key__ self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.uuid') del sloth.uuid self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.id') del sloth.id with self.assertRaises(UniqueRepresentationNotFound) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), "Object of type <class 'tests.test_object_representations.Sloth'> " "has not declared an unique representation")
def test_object_with_cache_key_declared_returning_something_different_to_string(self): class Sloth(object): def __init__(self): self.__cache_key__ = lambda: 'cache_key' sloth = Sloth() self.assertEquals(get_unique_representation(sloth), 'tests.test_object_representations.Sloth.cache_key') sloth.__cache_key__ = lambda: False with self.assertRaises(InvalidTypeForUniqueRepresentation) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), 'obj.__cache_key__() must return a string') sloth.__cache_key__ = lambda: 42 with self.assertRaises(InvalidTypeForUniqueRepresentation) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), 'obj.__cache_key__() must return a string') sloth.__cache_key__ = lambda: None with self.assertRaises(InvalidTypeForUniqueRepresentation) as e: get_unique_representation(sloth) self.assertEquals(str(e.exception), 'obj.__cache_key__() must return a string')
def test_primitive_objects(self): self.assertEquals(get_unique_representation(1), repr(1)) self.assertEquals(get_unique_representation('sloth'), repr('sloth')) self.assertEquals(get_unique_representation(False), repr(False)) self.assertEquals(get_unique_representation(3.14159), repr(3.14159))
def test_None(self): self.assertEquals(get_unique_representation(None), repr(None))
def test_decimal(self): self.assertEquals(get_unique_representation(Decimal(1.618033989)), '1.618033989')
def test_max_lenght(self): # we strive to keep cache_key as a kind of readable thing. # However, when key is too long we need to shorten that - in order to avoid memcached key lenght restrictions. self.assertEquals(len(get_unique_representation(range(200))), 32)
def test_datetime_derivate_object(self): self.assertEquals(get_unique_representation(datetime.datetime(2015, 1, 2, 3, 4, 5)), '2015-01-02T03:04:05') self.assertEquals(get_unique_representation(datetime.date(2015, 1, 2)), '2015-01-02') self.assertEquals(get_unique_representation(datetime.time(3, 4, 5)), '03:04:05')
def test_decimal(self): self.assertEquals(get_unique_representation(Decimal(1.618033989)), '1.618033989')
def test_None(self): self.assertEquals(get_unique_representation(None), repr(None))
def test_primitive_objects(self): self.assertEquals(get_unique_representation(1), repr(1)) self.assertEquals(get_unique_representation('sloth'), repr('sloth')) self.assertEquals(get_unique_representation(False), repr(False)) self.assertEquals(get_unique_representation(3.14159), repr(3.14159))
def test_max_lenght(self): # we strive to keep cache_key as a kind of readable thing. # However, when key is too long we need to shorten that - in order to avoid memcached key lenght restrictions. self.assertEquals(len(get_unique_representation(range(200))), 32)