def test_permission_expression(self): from woost.models import (Item, Publishable, User, ReadPermission, PermissionExpression) self.everybody_role.permissions.append( ReadPermission(matching_items={ "type": "woost.models.publishable.Publishable" }, authorized=True)) i1 = Item() i2 = Item() d1 = Publishable() d2 = Publishable() i1.insert() i2.insert() d1.insert() d2.insert() results = set( Item.select( filters=[PermissionExpression(User(), ReadPermission)])) assert results == set([d1, d2])
def test_after_insert(self): from cocktail.persistence import datastore from woost.models import InsertTrigger, Item # Declare the trigger trigger, response_log = self.make_trigger(InsertTrigger, execution_point="after", batch_execution=False) # Insert a new item, but abort the transaction # (the trigger shouldn't be called) item = Item() item.insert() assert not response_log datastore.abort() assert not response_log # Insert a new item, and commit the transaction # (the trigger should be executed) item = Item() item.insert() datastore.commit() assert len(response_log) == 1 response = response_log[0] assert response["trigger"] is trigger assert response["items"] == [item] assert response["user"] is self.user assert not response["batch"]
def test_after_delete(self): from cocktail.persistence import datastore from woost.models import DeleteTrigger, Item # Declare the trigger trigger, response_log = self.make_trigger(DeleteTrigger, execution_point="after", batch_execution=False) # Create and insert an item item = Item() item.insert() datastore.commit() # Delete the item, but abort the transaction. This shouldn't trigger # the response. item.delete() datastore.abort() assert not response_log # Delete the inserted item and commit the transaction. This should # trigger the response. item.delete() datastore.commit() assert len(response_log) == 1 response = response_log[0] assert response["trigger"] is trigger assert response["items"] == [item] assert response["user"] is self.user assert not response["batch"]
def test_before_delete(self): from woost.models import DeleteTrigger, Item # Declare the trigger trigger, response_log = self.make_trigger(DeleteTrigger, execution_point="before", batch_execution=False) # Create and insert two items item1 = Item() item1.insert() item2 = Item() item2.insert() # Delete the items. This should trigger the response twice. item1.delete() assert len(response_log) == 1 item2.delete() assert len(response_log) == 2 response = response_log[0] assert response["trigger"] is trigger assert response["items"] == [item1] assert response["user"] is self.user assert not response["batch"] response = response_log[1] assert response["trigger"] is trigger assert response["items"] == [item2] assert response["user"] is self.user assert not response["batch"]
def test_delete(self): from datetime import datetime from woost.models import (Item, User, ChangeSet, changeset_context) author = User() author.insert() item = Item() item.insert() with changeset_context(author) as changeset: item.delete() assert list(ChangeSet.select()) == [changeset] assert changeset.author is author assert isinstance(changeset.date, datetime) assert changeset.changes.keys() == [item.id] change = changeset.changes[item.id] assert change.target is item assert change.action == "delete" assert change.changeset is changeset assert item.changes == [change] assert not item.id in Item.index
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_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_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_after_insert_batched(self): from cocktail.persistence import datastore from woost.models import InsertTrigger, Item # Declare the trigger trigger, response_log = self.make_trigger(InsertTrigger, execution_point="after", batch_execution=True) # Insert new items, but abort the transaction # (the trigger shouldn't be called) item1 = Item() item1.insert() assert not response_log item2 = Item() item2.insert() assert not response_log datastore.abort() assert not response_log # Create and insert two items, and commit the transaction. The response # should be triggered just once. item1 = Item() item1.insert() item2 = Item() item2.insert() datastore.commit() assert len(response_log) == 1 response = response_log[0] assert response["trigger"] is trigger assert response["items"] == set([item1, item2]) assert response["user"] is self.user assert response["batch"]
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"]