|
| 1 | +"""Tests for entity deletion functionality in ArkivModule.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from arkiv.client import Arkiv |
| 8 | +from arkiv.types import Annotations, CreateOp, DeleteOp, Operations |
| 9 | + |
| 10 | +from .utils import check_tx_hash |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class TestEntityDelete: |
| 16 | + """Test cases for delete_entity function.""" |
| 17 | + |
| 18 | + def test_delete_entity_simple(self, arkiv_client_http: Arkiv) -> None: |
| 19 | + """Test deleting a single entity.""" |
| 20 | + # First, create an entity to delete |
| 21 | + payload = b"Test entity for deletion" |
| 22 | + annotations: Annotations = Annotations({"type": "test", "purpose": "deletion"}) |
| 23 | + btl = 100 |
| 24 | + |
| 25 | + entity_key, _ = arkiv_client_http.arkiv.create_entity( |
| 26 | + payload=payload, annotations=annotations, btl=btl |
| 27 | + ) |
| 28 | + |
| 29 | + logger.info(f"Created entity {entity_key} for deletion test") |
| 30 | + |
| 31 | + # Verify the entity exists |
| 32 | + assert arkiv_client_http.arkiv.entity_exists(entity_key), ( |
| 33 | + "Entity should exist after creation" |
| 34 | + ) |
| 35 | + |
| 36 | + # Delete the entity |
| 37 | + delete_tx_hash = arkiv_client_http.arkiv.delete_entity(entity_key) |
| 38 | + |
| 39 | + label = "delete_entity" |
| 40 | + check_tx_hash(label, delete_tx_hash) |
| 41 | + logger.info(f"{label}: Deleted entity {entity_key}, tx_hash: {delete_tx_hash}") |
| 42 | + |
| 43 | + # Verify the entity no longer exists |
| 44 | + assert not arkiv_client_http.arkiv.entity_exists(entity_key), ( |
| 45 | + "Entity should not exist after deletion" |
| 46 | + ) |
| 47 | + |
| 48 | + logger.info(f"{label}: Entity deletion successful") |
| 49 | + |
| 50 | + def test_delete_multiple_entities_sequentially( |
| 51 | + self, arkiv_client_http: Arkiv |
| 52 | + ) -> None: |
| 53 | + """Test deleting multiple entities one by one.""" |
| 54 | + # Create multiple entities |
| 55 | + entity_keys = [] |
| 56 | + for i in range(3): |
| 57 | + payload = f"Entity {i} for sequential deletion".encode() |
| 58 | + annotations: Annotations = Annotations({"index": i, "batch": "sequential"}) |
| 59 | + entity_key, _ = arkiv_client_http.arkiv.create_entity( |
| 60 | + payload=payload, annotations=annotations, btl=150 |
| 61 | + ) |
| 62 | + entity_keys.append(entity_key) |
| 63 | + |
| 64 | + logger.info(f"Created {len(entity_keys)} entities for sequential deletion") |
| 65 | + |
| 66 | + # Verify all entities exist |
| 67 | + for entity_key in entity_keys: |
| 68 | + assert arkiv_client_http.arkiv.entity_exists(entity_key), ( |
| 69 | + f"Entity {entity_key} should exist before deletion" |
| 70 | + ) |
| 71 | + |
| 72 | + # Delete entities one by one |
| 73 | + for i, entity_key in enumerate(entity_keys): |
| 74 | + delete_tx_hash = arkiv_client_http.arkiv.delete_entity(entity_key) |
| 75 | + check_tx_hash(f"delete_entity_{i}", delete_tx_hash) |
| 76 | + logger.info(f"Deleted entity {i + 1}/{len(entity_keys)}: {entity_key}") |
| 77 | + |
| 78 | + # Verify this entity is deleted |
| 79 | + assert not arkiv_client_http.arkiv.entity_exists(entity_key), ( |
| 80 | + f"Entity {entity_key} should not exist after deletion" |
| 81 | + ) |
| 82 | + |
| 83 | + # Verify all entities are gone |
| 84 | + for entity_key in entity_keys: |
| 85 | + assert not arkiv_client_http.arkiv.entity_exists(entity_key), ( |
| 86 | + f"Entity {entity_key} should still be deleted" |
| 87 | + ) |
| 88 | + |
| 89 | + logger.info("Sequential deletion of multiple entities successful") |
| 90 | + |
| 91 | + def test_delete_entity_execute_bulk(self, arkiv_client_http: Arkiv) -> None: |
| 92 | + """Test deleting entities that were created in bulk.""" |
| 93 | + # Create entities in bulk |
| 94 | + create_ops = [ |
| 95 | + CreateOp( |
| 96 | + payload=f"Bulk entity {i}".encode(), |
| 97 | + annotations=Annotations({"batch": "bulk", "index": i}), |
| 98 | + btl=100, |
| 99 | + ) |
| 100 | + for i in range(3) |
| 101 | + ] |
| 102 | + |
| 103 | + entity_keys, _ = arkiv_client_http.arkiv.create_entities(create_ops) |
| 104 | + |
| 105 | + logger.info(f"Created {len(entity_keys)} entities in bulk") |
| 106 | + |
| 107 | + # Verify all exist |
| 108 | + for entity_key in entity_keys: |
| 109 | + assert arkiv_client_http.arkiv.entity_exists(entity_key), ( |
| 110 | + "Bulk-created entity should exist" |
| 111 | + ) |
| 112 | + |
| 113 | + # Bulk delete |
| 114 | + # Wrap in Operations container and execute |
| 115 | + delete_ops = [DeleteOp(entity_key=key) for key in entity_keys] |
| 116 | + operations = Operations(deletes=delete_ops) |
| 117 | + receipt = arkiv_client_http.arkiv.execute(operations) |
| 118 | + |
| 119 | + # Check transaction hash of bulk delete |
| 120 | + check_tx_hash("delete_bulk_entity", receipt.tx_hash) |
| 121 | + |
| 122 | + # Verify all deletes succeeded |
| 123 | + if len(receipt.deletes) != len(delete_ops): |
| 124 | + raise RuntimeError( |
| 125 | + f"Expected {len(delete_ops)} deletes in receipt, got {len(receipt.deletes)}" |
| 126 | + ) |
| 127 | + |
| 128 | + # Verify all are deleted |
| 129 | + for entity_key in entity_keys: |
| 130 | + assert not arkiv_client_http.arkiv.entity_exists(entity_key), ( |
| 131 | + "Bulk-created entity should be deleted" |
| 132 | + ) |
| 133 | + |
| 134 | + logger.info("Deletion of bulk-created entities successful") |
| 135 | + |
| 136 | + def test_delete_nonexistent_entity_behavior(self, arkiv_client_http: Arkiv) -> None: |
| 137 | + """Test that deleting a non-existent entity raises an exception.""" |
| 138 | + from eth_typing import HexStr |
| 139 | + from web3.exceptions import Web3RPCError |
| 140 | + |
| 141 | + from arkiv.types import EntityKey |
| 142 | + |
| 143 | + # Create a fake entity key (should not exist) |
| 144 | + fake_entity_key = EntityKey( |
| 145 | + HexStr("0x0000000000000000000000000000000000000000000000000000000000000001") |
| 146 | + ) |
| 147 | + |
| 148 | + # Verify it doesn't exist |
| 149 | + assert not arkiv_client_http.arkiv.entity_exists(fake_entity_key), ( |
| 150 | + "Fake entity should not exist" |
| 151 | + ) |
| 152 | + |
| 153 | + # Attempt to delete should raise a Web3RPCError |
| 154 | + with pytest.raises(Web3RPCError) as exc_info: |
| 155 | + logger.info( |
| 156 | + f"Attempting to delete non-existent entity {fake_entity_key} -> {exc_info}" |
| 157 | + ) |
| 158 | + arkiv_client_http.arkiv.delete_entity(fake_entity_key) |
| 159 | + |
| 160 | + # Verify the error message indicates entity not found |
| 161 | + error_message = str(exc_info.value) |
| 162 | + assert "entity" in error_message.lower(), "Error message should mention entity" |
| 163 | + assert "not found" in error_message.lower(), ( |
| 164 | + "Error message should indicate entity not found" |
| 165 | + ) |
| 166 | + |
| 167 | + logger.info( |
| 168 | + f"Delete of non-existent entity correctly raised {type(exc_info.value).__name__}" |
| 169 | + ) |
| 170 | + |
| 171 | + def test_delete_entity_twice(self, arkiv_client_http: Arkiv) -> None: |
| 172 | + """Test that deleting the same entity twice raises an exception.""" |
| 173 | + from web3.exceptions import Web3RPCError |
| 174 | + |
| 175 | + # Create an entity |
| 176 | + entity_key, _ = arkiv_client_http.arkiv.create_entity( |
| 177 | + payload=b"Entity to delete twice", btl=100 |
| 178 | + ) |
| 179 | + |
| 180 | + # First deletion |
| 181 | + delete_tx_hash_1 = arkiv_client_http.arkiv.delete_entity(entity_key) |
| 182 | + check_tx_hash("first_delete", delete_tx_hash_1) |
| 183 | + |
| 184 | + # Verify it's deleted |
| 185 | + assert not arkiv_client_http.arkiv.entity_exists(entity_key), ( |
| 186 | + "Entity should be deleted after first deletion" |
| 187 | + ) |
| 188 | + |
| 189 | + # Second deletion attempt should raise a Web3RPCError |
| 190 | + with pytest.raises(Web3RPCError) as exc_info: |
| 191 | + arkiv_client_http.arkiv.delete_entity(entity_key) |
| 192 | + |
| 193 | + # Verify the error message indicates entity not found |
| 194 | + error_message = str(exc_info.value) |
| 195 | + assert "entity" in error_message.lower(), "Error message should mention entity" |
| 196 | + assert "not found" in error_message.lower(), ( |
| 197 | + "Error message should indicate entity not found" |
| 198 | + ) |
| 199 | + |
| 200 | + logger.info( |
| 201 | + f"Second delete of same entity correctly raised {type(exc_info.value).__name__}" |
| 202 | + ) |
0 commit comments