def test_attribute(self): # Check we get an error when called with something other than a function. self.assertRaises(ProgrammingError, attribute, 'not a getter') self.assertRaises(ProgrammingError, attribute, 123) self.assertRaises(ProgrammingError, attribute, None) # Call the decorator with a function. getter = lambda: None p = attribute(getter) # Check we got a property object. self.assertIsInstance(p, property) # Check the property object has both setter and getter functions. self.assertTrue(p.fset) self.assertTrue(p.fget) # Pretend we decorated an object. entity_id = uuid4() o = VersionedEntity(originator_id=entity_id, originator_version=0) o.__dict__['_<lambda>'] = 'value1' # Call the property's getter function. value = p.fget(o) self.assertEqual(value, 'value1') # Call the property's setter function. p.fset(o, 'value2') # Check the attribute has changed. value = p.fget(o) self.assertEqual(value, 'value2') # Check the property's getter function isn't the getter function we passed in. self.assertNotEqual(p.fget, getter) # Define a class that uses the decorator. class Aaa(VersionedEntity): "An event sourced entity." def __init__(self, a, *args, **kwargs): super(Aaa, self).__init__(*args, **kwargs) self._a = a @attribute def a(self): "A mutable event sourced property." # Instantiate the class and check assigning to the property publishes an event and updates the object state. published_events = [] subscription = (lambda x: True, lambda x: published_events.append(x)) subscribe(*subscription) entity_id = uuid4() try: aaa = Aaa(originator_id=entity_id, originator_version=1, a=1) self.assertEqual(aaa.a, 1) aaa.a = 'value1' self.assertEqual(aaa.a, 'value1') finally: unsubscribe(*subscription) # Check an event was published. self.assertEqual(len(published_events), 1) # Check the published event was an AttributeChanged event, with the expected attribute values. published_event = published_events[0] self.assertIsInstance(published_event, AttributeChanged) self.assertEqual(published_event.name, '_a') self.assertEqual(published_event.value, 'value1') self.assertTrue(published_event.originator_version, 1) self.assertEqual(published_event.originator_id, entity_id)
def test_attribute(self): # Check we get an error when called with something other than a function. self.assertRaises(ProgrammingError, attribute, 'not a getter') self.assertRaises(ProgrammingError, attribute, 123) self.assertRaises(ProgrammingError, attribute, None) # Call the decorator with a function. getter = lambda: None p = attribute(getter) # Check we got a property object. self.assertIsInstance(p, property) # Check the property object has both setter and getter functions. self.assertTrue(p.fset) self.assertTrue(p.fget) # Pretend we decorated an object. entity_id = uuid4() o = VersionedEntity(id=entity_id, __version__=0) o.__dict__['_<lambda>'] = 'value1' # Call the property's getter function. value = p.fget(o) self.assertEqual(value, 'value1') # Call the property's setter function. p.fset(o, 'value2') # Check the attribute has changed. value = p.fget(o) self.assertEqual(value, 'value2') # Check the property's getter function isn't the getter function we passed in. self.assertNotEqual(p.fget, getter) # Define a class that uses the decorator. class Aaa(VersionedEntity): "An event sourced entity." def __init__(self, a, *args, **kwargs): super(Aaa, self).__init__(*args, **kwargs) self._a = a @attribute def a(self): "A mutable event sourced property." # Instantiate the class and check assigning to the property publishes an event and updates the object state. published_events = [] subscription = (lambda x: True, lambda x: published_events.append(x)) subscribe(*subscription) entity_id = uuid4() try: aaa = Aaa(id=entity_id, __version__=1, a=1) self.assertEqual(aaa.a, 1) aaa.a = 'value1' self.assertEqual(aaa.a, 'value1') finally: unsubscribe(*subscription) # Check an event was published. self.assertEqual(len(published_events), 1) # Check the published event was an AttributeChanged event, with the expected attribute values. published_event = published_events[0] self.assertIsInstance(published_event, AttributeChanged) self.assertEqual(published_event.name, '_a') self.assertEqual(published_event.value, 'value1') self.assertTrue(published_event.originator_version, 1) self.assertEqual(published_event.originator_id, entity_id)