def test_after_insert_modify_delete(self): from cocktail.persistence import datastore from woost.models import (Item, User, InsertTrigger, ModifyTrigger, DeleteTrigger) response_log = [] insert_trigger = self.make_trigger(InsertTrigger, response_log, execution_point="after", batch_execution=False) modify_trigger = self.make_trigger(ModifyTrigger, response_log, execution_point="after", batch_execution=False) delete_trigger = self.make_trigger(DeleteTrigger, response_log, execution_point="after", batch_execution=False) # Create and insert an item item = Item() item.qname = "foo" item.insert() # Modify it item.qname = "bar" item.global_id = "foobar" # Delete it item.delete() # Commit the transaction; this should execute all the scheduled # responses datastore.commit() assert len(response_log) == 4 response = response_log.pop(0) assert response["trigger"] is insert_trigger assert response["items"] == [item] assert response["user"] is self.user assert not response["batch"] for member in (Item.qname, Item.global_id): response = response_log.pop(0) assert response["trigger"] is modify_trigger assert response["items"] == [item] assert response["context"]["member"] is member assert response["user"] is self.user assert not response["batch"] response = response_log[0] assert response["trigger"] is delete_trigger assert response["items"] == [item] assert response["user"] is self.user assert not response["batch"]
def test_after_modify_batched(self): from cocktail.persistence import datastore from woost.models import ModifyTrigger, Item, User # Declare the trigger trigger, response_log = self.make_trigger(ModifyTrigger, execution_point="after", batch_execution=True) # Create two items and initialize them. This shouldn't trigger any # response, since modifications happen before items are inserted. item1 = Item() item1.qname = "foo" item1.insert() item2 = Item() item2.global_id = "x1" item2.insert() datastore.commit() assert not response_log # Modify the inserted items, but abort the transaction. Again, this # shouldn't trigger any response. item1.qname = "bar" item2.global_id = "x2" datastore.abort() assert not response_log # Modify the inserted items and commit the transaction. This should # trigger the response just once. item1.qname = "spam" item2.global_id = "x3" datastore.commit() assert len(response_log) == 1 response = response_log[0] assert response["trigger"] is trigger assert response["items"] == set([item1, item2]) assert response["context"]["modified_members"] == { item1: set([(Item.qname, None)]), item2: set([(Item.global_id, None)]) } assert response["user"] is self.user assert response["batch"]
def test_before_modify(self): from woost.models import ModifyTrigger, Item, User # Declare the trigger trigger, response_log = self.make_trigger(ModifyTrigger, execution_point="before", batch_execution=False) # Create an item and initialize it. This shouldn't trigger any # response, since modifications happen before the item is inserted. item = Item() item.qname = "foo" item.insert() assert not response_log # Modify the inserted item two times. This should trigger the response # twice. item.qname = "bar" assert len(response_log) == 1 item.global_id = "foobar" assert len(response_log) == 2 response = response_log[0] assert response["trigger"] is trigger assert response["items"] == [item] assert response["context"]["member"] is Item.qname assert response["user"] is self.user assert not response["batch"] response = response_log[1] assert response["trigger"] is trigger assert response["items"] == [item] assert response["context"]["member"] is Item.global_id assert response["user"] is self.user assert not response["batch"]
def test_after_modify(self): from cocktail.persistence import datastore from woost.models import ModifyTrigger, Item # Declare the trigger trigger, response_log = self.make_trigger(ModifyTrigger, execution_point="after", batch_execution=False) # Create an item and initialize it. This shouldn't trigger any # response, since modifications happen before the item is inserted. item = Item() item.qname = "foo" item.insert() datastore.commit() assert not response_log # Modify the inserted item, but abort the transaction. Again, this # shouldn't trigger any response. item.qname = "bar" datastore.abort() assert not response_log # Modify the inserted item and commit the transaction. This should # trigger the response. item.qname = "spam" datastore.commit() assert len(response_log) == 1 response = response_log[0] assert response["trigger"] is trigger assert response["items"] == [item] assert response["context"]["member"] is Item.qname assert response["user"] is self.user assert not response["batch"]
def test_modify_batched_order(self): from cocktail.persistence import datastore from woost.models import ModifyTrigger, Item, User trigger, response_log = self.make_trigger( ModifyTrigger, execution_point="after", batch_execution=True, matching_members=["woost.models.item.Item.global_id"]) # Modifying a member that is not covered by the trigger should alter # the context passed to responses, even if it is modified before the # member that actions the response item = Item() item.insert() item.qname = "foo" item.global_id = "x1" datastore.commit() response = response_log[0] assert response["context"]["modified_members"] == { item: set([(Item.qname, None), (Item.global_id, None)]) }
def test_before_create_insert_modify_delete(self): from woost.models import (Item, CreateTrigger, InsertTrigger, ModifyTrigger, DeleteTrigger) response_log = [] create_trigger = self.make_trigger(CreateTrigger, response_log, execution_point="before", batch_execution=False) insert_trigger = self.make_trigger(InsertTrigger, response_log, execution_point="before", batch_execution=False) modify_trigger = self.make_trigger(ModifyTrigger, response_log, execution_point="before", batch_execution=False) delete_trigger = self.make_trigger(DeleteTrigger, response_log, execution_point="before", batch_execution=False) # Create and insert an item item = Item(qname="foo") item.insert() assert len(response_log) == 2 response = response_log.pop() assert response["trigger"] is insert_trigger assert response["items"] == [item] assert response["user"] is self.user assert not response["batch"] response = response_log.pop() assert response["trigger"] is create_trigger assert response["items"] == [item] assert response["user"] is self.user assert response["context"]["values"]["qname"] == "foo" assert not response["batch"] # Modify the item item.qname = "bar" assert len(response_log) == 1 response = response_log.pop() assert response["trigger"] is modify_trigger assert response["items"] == [item] assert response["context"]["member"] is Item.qname assert response["user"] is self.user assert not response["batch"] # Delete the item item.delete() assert len(response_log) == 1 response = response_log.pop() assert response["trigger"] is delete_trigger assert response["items"] == [item] assert response["user"] is self.user assert not response["batch"]