graphcore-kernel 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (343) hide show
  1. graphcore/__init__.py +5 -0
  2. graphcore/agent_frontend/__init__.py +8 -0
  3. graphcore/agent_frontend/api.py +69 -0
  4. graphcore/agent_frontend/base/__init__.py +11 -0
  5. graphcore/agent_frontend/base/views.py +167 -0
  6. graphcore/agent_frontend/client.py +31 -0
  7. graphcore/agent_frontend/context.py +157 -0
  8. graphcore/agent_frontend/errors.py +12 -0
  9. graphcore/agent_frontend/loading.py +54 -0
  10. graphcore/agent_frontend/registry.py +42 -0
  11. graphcore/agent_frontend/render.py +207 -0
  12. graphcore/agent_frontend/server.py +103 -0
  13. graphcore/agent_frontend/signatures.py +35 -0
  14. graphcore/agent_frontend/testing.py +45 -0
  15. graphcore/agent_frontend/tests/__init__.py +0 -0
  16. graphcore/agent_frontend/tests/fakes.py +114 -0
  17. graphcore/agent_frontend/tests/test_frontend.py +447 -0
  18. graphcore/agent_frontend/tests/test_loading.py +83 -0
  19. graphcore/agent_frontend/tests/test_server.py +109 -0
  20. graphcore/agent_frontend/tools.py +137 -0
  21. graphcore/api/__init__.py +4 -0
  22. graphcore/api/graph_api.py +378 -0
  23. graphcore/api/predicate_codec.py +81 -0
  24. graphcore/api/tests/__init__.py +0 -0
  25. graphcore/api/tests/test_changed_entities.py +109 -0
  26. graphcore/api/tests/test_graph_api.py +290 -0
  27. graphcore/api/tests/test_graph_api_access.py +94 -0
  28. graphcore/api/tests/test_graph_api_grants.py +90 -0
  29. graphcore/api/tests/test_predicate_codec.py +83 -0
  30. graphcore/api/tests/test_subtree_query.py +79 -0
  31. graphcore/apps.py +71 -0
  32. graphcore/auth/__init__.py +1 -0
  33. graphcore/auth/authn/README.md +178 -0
  34. graphcore/auth/authn/__init__.py +3 -0
  35. graphcore/auth/authn/authenticator.py +120 -0
  36. graphcore/auth/authn/onboarding.py +105 -0
  37. graphcore/auth/authn/principals.py +20 -0
  38. graphcore/auth/authn/resource_metadata.py +43 -0
  39. graphcore/auth/authn/service_accounts.py +96 -0
  40. graphcore/auth/authn/testing.py +96 -0
  41. graphcore/auth/authn/tests/__init__.py +0 -0
  42. graphcore/auth/authn/tests/test_authenticator.py +145 -0
  43. graphcore/auth/authn/tests/test_onboarding.py +145 -0
  44. graphcore/auth/authn/tests/test_service_accounts.py +180 -0
  45. graphcore/auth/authn/users.py +59 -0
  46. graphcore/auth/authz/__init__.py +0 -0
  47. graphcore/auth/authz/access_port.py +120 -0
  48. graphcore/auth/authz/adapter/__init__.py +0 -0
  49. graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
  50. graphcore/auth/authz/adapter/tests/__init__.py +0 -0
  51. graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
  52. graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
  53. graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
  54. graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
  55. graphcore/auth/authz/defaults.py +26 -0
  56. graphcore/auth/authz/kernel_policy.py +191 -0
  57. graphcore/auth/authz/kernel_writes.py +31 -0
  58. graphcore/auth/authz/lookups.py +25 -0
  59. graphcore/auth/authz/policy.py +403 -0
  60. graphcore/auth/authz/tests/__init__.py +0 -0
  61. graphcore/auth/authz/tests/test_can_contain.py +150 -0
  62. graphcore/auth/authz/tests/test_contained_by.py +167 -0
  63. graphcore/auth/authz/tests/test_creation.py +391 -0
  64. graphcore/auth/authz/tests/test_delete_permission.py +82 -0
  65. graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
  66. graphcore/auth/authz/tests/test_integration.py +637 -0
  67. graphcore/auth/authz/tests/test_invites.py +184 -0
  68. graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
  69. graphcore/auth/authz/tests/test_lockout.py +132 -0
  70. graphcore/auth/authz/tests/test_policy.py +329 -0
  71. graphcore/auth/authz/tests/test_primitives.py +73 -0
  72. graphcore/auth/authz/tests/test_principal_creation.py +62 -0
  73. graphcore/bootstrap/__init__.py +0 -0
  74. graphcore/bootstrap/deployment.py +67 -0
  75. graphcore/bootstrap/signals.py +16 -0
  76. graphcore/bootstrap/tests/__init__.py +0 -0
  77. graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
  78. graphcore/broadcast/__init__.py +4 -0
  79. graphcore/broadcast/broadcaster.py +201 -0
  80. graphcore/broadcast/channels.py +56 -0
  81. graphcore/broadcast/tests/__init__.py +0 -0
  82. graphcore/broadcast/tests/test_broadcaster.py +444 -0
  83. graphcore/celery.py +14 -0
  84. graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
  85. graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
  86. graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
  87. graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
  88. graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
  89. graphcore/docs/RUNTIME.md +90 -0
  90. graphcore/docs/SCHEDULING_TDD.md +244 -0
  91. graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
  92. graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
  93. graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
  94. graphcore/docs/__init__.py +0 -0
  95. graphcore/docs/doc_topics.py +97 -0
  96. graphcore/docs/docs_discovery.py +64 -0
  97. graphcore/docs/public_api.py +56 -0
  98. graphcore/domain/__init__.py +0 -0
  99. graphcore/domain/entity/Entity.py +204 -0
  100. graphcore/domain/entity/__init__.py +0 -0
  101. graphcore/domain/entity/deployment/Deployment.py +25 -0
  102. graphcore/domain/entity/deployment/__init__.py +0 -0
  103. graphcore/domain/entity/grant/Grant.py +31 -0
  104. graphcore/domain/entity/grant/__init__.py +0 -0
  105. graphcore/domain/entity/group/Group.py +48 -0
  106. graphcore/domain/entity/group/README.md +88 -0
  107. graphcore/domain/entity/group/__init__.py +0 -0
  108. graphcore/domain/entity/invite/Invite.py +31 -0
  109. graphcore/domain/entity/invite/__init__.py +0 -0
  110. graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
  111. graphcore/domain/entity/request_template/__init__.py +0 -0
  112. graphcore/domain/entity/request_template/tests/__init__.py +0 -0
  113. graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
  114. graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
  115. graphcore/domain/entity/service_account/__init__.py +0 -0
  116. graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
  117. graphcore/domain/entity/strategy_request/__init__.py +0 -0
  118. graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
  119. graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
  120. graphcore/domain/entity/tests/__init__.py +0 -0
  121. graphcore/domain/entity/tests/test_entity.py +227 -0
  122. graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
  123. graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
  124. graphcore/domain/entity/user/User.py +39 -0
  125. graphcore/domain/entity/user/__init__.py +0 -0
  126. graphcore/domain/entity/user/tests/__init__.py +0 -0
  127. graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
  128. graphcore/domain/entity/view/View.py +37 -0
  129. graphcore/domain/entity/view/__init__.py +0 -0
  130. graphcore/domain/entity/view/position.py +78 -0
  131. graphcore/domain/entity/view/tests/__init__.py +0 -0
  132. graphcore/domain/entity/view/tests/test_position.py +71 -0
  133. graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
  134. graphcore/domain/strategy/BaseStrategy.py +759 -0
  135. graphcore/domain/strategy/DeploymentStrategies.py +55 -0
  136. graphcore/domain/strategy/GrantStrategies.py +189 -0
  137. graphcore/domain/strategy/GroupStrategies.py +193 -0
  138. graphcore/domain/strategy/InviteStrategies.py +84 -0
  139. graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
  140. graphcore/domain/strategy/ViewStrategies.py +219 -0
  141. graphcore/domain/strategy/__init__.py +0 -0
  142. graphcore/domain/strategy/runner.py +84 -0
  143. graphcore/domain/strategy/session.py +56 -0
  144. graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
  145. graphcore/domain/strategy/tests/__init__.py +0 -0
  146. graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
  147. graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
  148. graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
  149. graphcore/domain/strategy/tests/test_runner.py +220 -0
  150. graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
  151. graphcore/domain/strategy/tests/test_utils.py +95 -0
  152. graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
  153. graphcore/errors.py +145 -0
  154. graphcore/execution/README.md +97 -0
  155. graphcore/execution/StrategyExecutorService.py +333 -0
  156. graphcore/execution/__init__.py +0 -0
  157. graphcore/execution/dispatcher.py +19 -0
  158. graphcore/execution/environments/__init__.py +0 -0
  159. graphcore/execution/environments/celery.py +59 -0
  160. graphcore/execution/environments/in_process.py +55 -0
  161. graphcore/execution/local_session.py +117 -0
  162. graphcore/execution/redis_run_events.py +74 -0
  163. graphcore/execution/requests.py +22 -0
  164. graphcore/execution/run_events.py +85 -0
  165. graphcore/execution/strategy_context.py +54 -0
  166. graphcore/execution/tasks.py +34 -0
  167. graphcore/execution/tests/__init__.py +5 -0
  168. graphcore/execution/tests/contract.py +171 -0
  169. graphcore/execution/tests/support.py +310 -0
  170. graphcore/execution/tests/test_dispatchers.py +76 -0
  171. graphcore/execution/tests/test_executor.py +368 -0
  172. graphcore/execution/tests/test_local_session.py +95 -0
  173. graphcore/execution/tests/test_reconcile_runs.py +134 -0
  174. graphcore/execution/tests/test_run_events.py +106 -0
  175. graphcore/execution/tests/test_strategy_context.py +62 -0
  176. graphcore/gateway/__init__.py +1 -0
  177. graphcore/gateway/remote.py +35 -0
  178. graphcore/gateway/tests/__init__.py +0 -0
  179. graphcore/gateway/tests/test_remote.py +30 -0
  180. graphcore/graph/EntityService.py +710 -0
  181. graphcore/graph/__init__.py +0 -0
  182. graphcore/graph/entity_cache.py +152 -0
  183. graphcore/graph/tests/__init__.py +4 -0
  184. graphcore/graph/tests/test_EntityService.py +908 -0
  185. graphcore/graph/tests/test_EntityService_list.py +170 -0
  186. graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
  187. graphcore/graph/tests/test_EntityService_referrers.py +236 -0
  188. graphcore/graph/tests/test_EntityService_routing.py +119 -0
  189. graphcore/graph/tests/test_dependent_on.py +141 -0
  190. graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
  191. graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
  192. graphcore/graph/tests/test_entity_cache.py +159 -0
  193. graphcore/graph/tests/test_null_entity_cache.py +12 -0
  194. graphcore/management/__init__.py +0 -0
  195. graphcore/management/commands/__init__.py +0 -0
  196. graphcore/management/commands/dev_reset.py +18 -0
  197. graphcore/management/commands/graphcore_createadmin.py +65 -0
  198. graphcore/management/commands/graphcore_policy.py +59 -0
  199. graphcore/management/commands/graphcore_status.py +98 -0
  200. graphcore/management/tests/__init__.py +0 -0
  201. graphcore/management/tests/test_createadmin.py +78 -0
  202. graphcore/management/tests/test_policy_command.py +46 -0
  203. graphcore/management/tests/test_status_command.py +85 -0
  204. graphcore/migrations/0001_initial.py +186 -0
  205. graphcore/migrations/0002_invite.py +26 -0
  206. graphcore/migrations/__init__.py +0 -0
  207. graphcore/models.py +11 -0
  208. graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
  209. graphcore/persistence/__init__.py +6 -0
  210. graphcore/persistence/django/__init__.py +0 -0
  211. graphcore/persistence/django/backends.py +43 -0
  212. graphcore/persistence/django/bindings.py +28 -0
  213. graphcore/persistence/django/cache.py +38 -0
  214. graphcore/persistence/django/compiler.py +214 -0
  215. graphcore/persistence/django/identity.py +18 -0
  216. graphcore/persistence/django/models.py +252 -0
  217. graphcore/persistence/django/repository.py +525 -0
  218. graphcore/persistence/django/tests/__init__.py +0 -0
  219. graphcore/persistence/django/tests/test_backends.py +68 -0
  220. graphcore/persistence/django/tests/test_compiler.py +269 -0
  221. graphcore/persistence/django/tests/test_repository.py +472 -0
  222. graphcore/persistence/django/tests/test_shared_column.py +120 -0
  223. graphcore/persistence/django/tests/test_threads.py +50 -0
  224. graphcore/persistence/django/tests/test_update_shape.py +175 -0
  225. graphcore/persistence/django/threads.py +38 -0
  226. graphcore/persistence/repository.py +172 -0
  227. graphcore/persistence/tests/__init__.py +0 -0
  228. graphcore/persistence/tests/contract.py +688 -0
  229. graphcore/persistence/tests/evaluate.py +51 -0
  230. graphcore/persistence/tests/fixtures.py +32 -0
  231. graphcore/persistence/tests/memory.py +263 -0
  232. graphcore/persistence/tests/test_memory_repository.py +10 -0
  233. graphcore/persistence/tests/test_query_types.py +127 -0
  234. graphcore/public/__init__.py +9 -0
  235. graphcore/public/api.py +9 -0
  236. graphcore/public/apps.py +10 -0
  237. graphcore/public/auth/__init__.py +1 -0
  238. graphcore/public/auth/grants.py +7 -0
  239. graphcore/public/auth/onboarding.py +8 -0
  240. graphcore/public/auth/perms.py +7 -0
  241. graphcore/public/auth/policy.py +10 -0
  242. graphcore/public/auth/roles.py +8 -0
  243. graphcore/public/auth/subjects.py +7 -0
  244. graphcore/public/entities/__init__.py +32 -0
  245. graphcore/public/errors.py +19 -0
  246. graphcore/public/persistence/__init__.py +44 -0
  247. graphcore/public/persistence/django.py +24 -0
  248. graphcore/public/scheduling.py +23 -0
  249. graphcore/public/strategies/__init__.py +85 -0
  250. graphcore/public/test_settings.py +16 -0
  251. graphcore/public/testing.py +47 -0
  252. graphcore/registry/EntityDirectory.py +246 -0
  253. graphcore/registry/README.md +147 -0
  254. graphcore/registry/StrategyDirectory.py +308 -0
  255. graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
  256. graphcore/registry/__init__.py +0 -0
  257. graphcore/registry/app_discovery.py +108 -0
  258. graphcore/registry/decorators.py +77 -0
  259. graphcore/registry/discovery.py +111 -0
  260. graphcore/registry/registry.py +79 -0
  261. graphcore/registry/tests/__init__.py +0 -0
  262. graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
  263. graphcore/registry/tests/test_app_config.py +36 -0
  264. graphcore/registry/tests/test_discovery.py +147 -0
  265. graphcore/registry/tests/test_entity_directory_storage.py +73 -0
  266. graphcore/registry/tests/test_import_package.py +87 -0
  267. graphcore/registry/tests/test_principal_flag.py +67 -0
  268. graphcore/registry/tests/test_strategy_directory.py +29 -0
  269. graphcore/routes/__init__.py +116 -0
  270. graphcore/routes/tests/__init__.py +0 -0
  271. graphcore/routes/tests/test_routes.py +90 -0
  272. graphcore/runtime.py +243 -0
  273. graphcore/scheduling/__init__.py +0 -0
  274. graphcore/scheduling/beat_scheduler.py +164 -0
  275. graphcore/scheduling/beat_tasks.py +63 -0
  276. graphcore/scheduling/scheduler.py +37 -0
  277. graphcore/scheduling/tests/__init__.py +0 -0
  278. graphcore/scheduling/tests/contract.py +136 -0
  279. graphcore/scheduling/tests/memory.py +42 -0
  280. graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
  281. graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
  282. graphcore/scheduling/tests/test_ticks.py +143 -0
  283. graphcore/tasks.py +9 -0
  284. graphcore/test_settings.py +78 -0
  285. graphcore/testing/__init__.py +0 -0
  286. graphcore/testing/case.py +112 -0
  287. graphcore/testing/runner.py +32 -0
  288. graphcore/testing/settings.py +26 -0
  289. graphcore/testing/tests/__init__.py +0 -0
  290. graphcore/testing/tests/agent_view_fixture.py +19 -0
  291. graphcore/testing/tests/test_case_and_runner.py +219 -0
  292. graphcore/testing/tests/test_settings_for_apps.py +50 -0
  293. graphcore/tests/__init__.py +0 -0
  294. graphcore/tests/ids.py +14 -0
  295. graphcore/tests/public_api_manifest.py +102 -0
  296. graphcore/tests/runs.py +7 -0
  297. graphcore/tests/test_app_discovery.py +90 -0
  298. graphcore/tests/test_composition_boundaries.py +231 -0
  299. graphcore/tests/test_docs_discovery.py +108 -0
  300. graphcore/tests/test_no_print.py +70 -0
  301. graphcore/tests/test_packaging.py +64 -0
  302. graphcore/tests/test_public_api_boundaries.py +229 -0
  303. graphcore/tests/test_public_api_contract.py +62 -0
  304. graphcore/tests/test_public_api_docs.py +28 -0
  305. graphcore/tests/test_runtime.py +42 -0
  306. graphcore/tests/test_shared_type_boundaries.py +128 -0
  307. graphcore/tests/test_shared_types.py +144 -0
  308. graphcore/tests/test_test_settings.py +45 -0
  309. graphcore/transport/__init__.py +0 -0
  310. graphcore/transport/agent_asgi.py +68 -0
  311. graphcore/transport/agent_client.py +85 -0
  312. graphcore/transport/channel_layer.py +68 -0
  313. graphcore/transport/consumers.py +507 -0
  314. graphcore/transport/routing.py +6 -0
  315. graphcore/transport/tests/__init__.py +0 -0
  316. graphcore/transport/tests/test_agent_client.py +31 -0
  317. graphcore/transport/tests/test_api_views.py +733 -0
  318. graphcore/transport/tests/test_channel_layer.py +96 -0
  319. graphcore/transport/tests/test_consumers.py +963 -0
  320. graphcore/transport/tests/test_resource_metadata.py +67 -0
  321. graphcore/transport/urls.py +29 -0
  322. graphcore/transport/views.py +358 -0
  323. graphcore/types/__init__.py +4 -0
  324. graphcore/types/access.py +114 -0
  325. graphcore/types/api.py +76 -0
  326. graphcore/types/entities.py +100 -0
  327. graphcore/types/entity_id.py +85 -0
  328. graphcore/types/execution.py +41 -0
  329. graphcore/types/identity.py +20 -0
  330. graphcore/types/onboarding.py +20 -0
  331. graphcore/types/query.py +116 -0
  332. graphcore/types/schedules.py +91 -0
  333. graphcore/types/strategy.py +73 -0
  334. graphcore/types/tests/__init__.py +0 -0
  335. graphcore/types/tests/test_entity_id.py +50 -0
  336. graphcore/urls.py +5 -0
  337. graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
  338. graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
  339. graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
  340. graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
  341. graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
  342. graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
  343. graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,870 @@
1
+ """
2
+ Tests for all strategies defined in BaseStrategy.py
3
+
4
+ This test file covers base strategies including:
5
+ - CreateEntity
6
+ - SetAttributes
7
+ - RemoveAttribute
8
+ - ReparentChild
9
+ - ReorderChild
10
+ - DeleteEntity
11
+ - FindAndReplace
12
+ """
13
+
14
+ import json
15
+
16
+ from django.test import SimpleTestCase
17
+
18
+ from graphcore.domain.strategy.tests.BaseStrategyTest import BaseStrategyTest
19
+ from graphcore.domain.entity.Entity import Entity
20
+ from graphcore.errors import InvalidRequest, NotFound, StrategyFailed
21
+ from graphcore.execution.strategy_context import kernel_internal_context
22
+ from graphcore.domain.strategy.BaseStrategy import (
23
+ CreateEntity,
24
+ SetAttributes,
25
+ RemoveAttribute,
26
+ ReparentChild,
27
+ ReorderChild,
28
+ DeleteEntity,
29
+ FindAndReplace
30
+ )
31
+ from graphcore.domain.strategy.RequestTemplateStrategies import ManageRequestTemplate
32
+ from graphcore.domain.strategy.ViewStrategies import CreateView, SetViewState
33
+ from graphcore.tests.ids import fixture_id
34
+ from graphcore.types.entity_id import new_entity_id
35
+ from graphcore.types.strategy import StrategyRequestPayload
36
+
37
+
38
+
39
+ class CreateEntityStrategyTest(BaseStrategyTest):
40
+ """Tests for CreateEntity"""
41
+
42
+ async def test_create_entity_new(self):
43
+ """Test creating a new entity with generated UUID"""
44
+ parent = await self.create_test_entity(name="Parent")
45
+
46
+ request = self.create_strategy_request(
47
+ 'CreateEntity',
48
+ parent.entity_id,
49
+ {
50
+ 'entity_type_id': 'graphcore.Entity'
51
+ }
52
+ )
53
+
54
+ result = await self.execute_strategy(request)
55
+
56
+ self.assert_strategy_success(result)
57
+ self.assertIn('child_entity', result.ret_val)
58
+
59
+ child_id = result.ret_val['child_entity']
60
+ self.assertIsNotNone(child_id)
61
+
62
+ # Fetch the created child entity
63
+ child = await self.entity_service.get_entity(child_id, self.test_principal)
64
+
65
+ # Verify child was added to parent
66
+ reloaded_parent = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
67
+ self.assert_entity_has_child(reloaded_parent, child.entity_id)
68
+
69
+ async def test_create_entity_with_initial_attributes(self):
70
+ """Test creating entity with initial attributes"""
71
+ parent = await self.create_test_entity(name="Parent")
72
+
73
+ request = self.create_strategy_request(
74
+ 'CreateEntity',
75
+ parent.entity_id,
76
+ {
77
+ 'entity_type_id': 'graphcore.Entity',
78
+ 'initial_attributes': {
79
+ 'name': 'Child Entity',
80
+ 'value': 100
81
+ }
82
+ }
83
+ )
84
+
85
+ result = await self.execute_strategy(request)
86
+
87
+ self.assert_strategy_success(result)
88
+ child_id = result.ret_val['child_entity']
89
+
90
+ # Fetch the created child entity
91
+ child = await self.entity_service.get_entity(child_id, self.test_principal)
92
+
93
+ self.assert_entity_has_attribute(child, 'name', 'Child Entity')
94
+ self.assert_entity_has_attribute(child, 'value', 100)
95
+
96
+ async def test_create_entity_invalid_entity_type_id(self):
97
+ """Test that invalid entity_type_id raises error"""
98
+ parent = await self.create_test_entity(name="Parent")
99
+
100
+ request = self.create_strategy_request(
101
+ 'CreateEntity',
102
+ parent.entity_id,
103
+ {
104
+ 'entity_type_id': 'invalid.Unknown'
105
+ }
106
+ )
107
+
108
+ with self.assertRaises(StrategyFailed) as context:
109
+ await self.execute_strategy(request)
110
+
111
+ self.assertIn("Unknown entity_type_id", str(context.exception))
112
+
113
+ async def test_create_entity_adds_parent_relationship(self):
114
+ """Test that child has parent relationship established"""
115
+ parent = await self.create_test_entity(name="Parent")
116
+
117
+ request = self.create_strategy_request(
118
+ 'CreateEntity',
119
+ parent.entity_id,
120
+ {
121
+ 'entity_type_id': 'graphcore.Entity'
122
+ }
123
+ )
124
+
125
+ result = await self.execute_strategy(request)
126
+ child_id = result.ret_val['child_entity']
127
+
128
+ # Fetch the created child entity
129
+ child = await self.entity_service.get_entity(child_id, self.test_principal)
130
+
131
+ # Child should have parent in parent_ids
132
+ self.assertIn(parent.entity_id, child.parent_ids)
133
+
134
+
135
+ class RequestConstructorPayloadTest(BaseStrategyTest):
136
+ """Nothing above the executor constructs a StrategyRequest."""
137
+
138
+ async def test_request_constructor_returns_an_unminted_payload(self):
139
+ payload = SetAttributes.request_constructor(
140
+ target_entity_id='gc://b/e', attribute_map={'name': 'x'},
141
+ )
142
+
143
+ self.assertIsInstance(payload, StrategyRequestPayload)
144
+ self.assertEqual(payload.strategy_name, 'SetAttributes')
145
+ self.assertEqual(payload.target_entity_id, 'gc://b/e')
146
+ self.assertEqual(payload.param_config, {'attribute_map': {'name': 'x'}})
147
+ self.assertFalse(hasattr(payload, 'entity_id'))
148
+
149
+ async def test_every_built_in_request_constructor_returns_a_payload(self):
150
+ for payload in (
151
+ CreateEntity.request_constructor(
152
+ target_entity_id='gc://b/e', entity_type_id='graphcore.Entity',
153
+ ),
154
+ SetViewState.request_constructor(None, state={}, client='web', address='/'),
155
+ CreateView.request_constructor(None),
156
+ RemoveAttribute.request_constructor('gc://b/e', attribute_name='a'),
157
+ FindAndReplace.request_constructor(
158
+ 'gc://b/e', attribute_name='a', find_string='x', replace_string='y',
159
+ ),
160
+ ManageRequestTemplate.request_constructor('gc://b/e', action='pause'),
161
+ ):
162
+ with self.subTest(strategy=payload.strategy_name):
163
+ self.assertIsInstance(payload, StrategyRequestPayload)
164
+
165
+
166
+ class SetAttributesStrategyTest(BaseStrategyTest):
167
+ """Tests for SetAttributes"""
168
+
169
+ async def test_set_single_attribute(self):
170
+ """Test setting a single attribute"""
171
+ entity = await self.create_test_entity(name="Original")
172
+
173
+ request = self.create_strategy_request(
174
+ 'SetAttributes',
175
+ entity.entity_id,
176
+ {
177
+ 'attribute_map': {'name': 'Updated'}
178
+ }
179
+ )
180
+
181
+ result = await self.execute_strategy(request)
182
+
183
+ self.assert_strategy_success(result)
184
+
185
+ reloaded = await self.entity_service.get_entity(entity.entity_id, self.test_principal)
186
+ self.assert_entity_has_attribute(reloaded, 'name', 'Updated')
187
+
188
+ async def test_set_multiple_attributes(self):
189
+ """Test setting multiple attributes"""
190
+ entity = await self.create_test_entity()
191
+
192
+ request = self.create_strategy_request(
193
+ 'SetAttributes',
194
+ entity.entity_id,
195
+ {
196
+ 'attribute_map': {
197
+ 'name': 'Test Entity',
198
+ 'value': 42,
199
+ 'active': True,
200
+ 'tags': ['test', 'example']
201
+ }
202
+ }
203
+ )
204
+
205
+ result = await self.execute_strategy(request)
206
+
207
+ self.assert_strategy_success(result)
208
+
209
+ reloaded = await self.entity_service.get_entity(entity.entity_id, self.test_principal)
210
+ self.assert_entity_has_attribute(reloaded, 'name', 'Test Entity')
211
+ self.assert_entity_has_attribute(reloaded, 'value', 42)
212
+ self.assert_entity_has_attribute(reloaded, 'active', True)
213
+ self.assert_entity_has_attribute(reloaded, 'tags', ['test', 'example'])
214
+
215
+ async def test_set_attributes_overwrites_existing(self):
216
+ """Test that SetAttributes overwrites existing attributes"""
217
+ entity = await self.create_test_entity(name="Original", value=10)
218
+
219
+ request = self.create_strategy_request(
220
+ 'SetAttributes',
221
+ entity.entity_id,
222
+ {
223
+ 'attribute_map': {
224
+ 'name': 'Overwritten',
225
+ 'value': 20
226
+ }
227
+ }
228
+ )
229
+
230
+ result = await self.execute_strategy(request)
231
+
232
+ reloaded = await self.entity_service.get_entity(entity.entity_id, self.test_principal)
233
+ self.assert_entity_has_attribute(reloaded, 'name', 'Overwritten')
234
+ self.assert_entity_has_attribute(reloaded, 'value', 20)
235
+
236
+ async def test_set_attributes_missing_attribute_map_fails(self):
237
+ """Test that missing attribute_map causes verify_executable to fail"""
238
+ entity = await self.create_test_entity()
239
+
240
+ request = self.create_strategy_request(
241
+ 'SetAttributes',
242
+ entity.entity_id,
243
+ {} # Missing attribute_map
244
+ )
245
+
246
+ # Should fail during execution due to verify_executable check
247
+ with self.assertRaises(Exception):
248
+ await self.execute_strategy(request)
249
+
250
+
251
+ async def test_obsolete_framework_write_is_rejected(self):
252
+ entity = await self.create_test_entity()
253
+ request = self.create_strategy_request('SetAttributes', entity.entity_id, {
254
+ 'attribute_map': {}, 'framework_field_map': {'owner': 'other'},
255
+ })
256
+ with self.assertRaises(InvalidRequest):
257
+ await self.execute_strategy(request)
258
+
259
+
260
+ class RemoveAttributeStrategyTest(BaseStrategyTest):
261
+ """Tests for RemoveAttribute"""
262
+
263
+ async def test_remove_existing_attribute(self):
264
+ """Test removing an existing attribute"""
265
+ entity = await self.create_test_entity(name="Test", value=42)
266
+
267
+ request = self.create_strategy_request(
268
+ 'RemoveAttribute',
269
+ entity.entity_id,
270
+ {
271
+ 'attribute_name': 'value'
272
+ }
273
+ )
274
+
275
+ result = await self.execute_strategy(request)
276
+
277
+ self.assert_strategy_success(result)
278
+
279
+ reloaded = await self.entity_service.get_entity(entity.entity_id, self.test_principal)
280
+ self.assertFalse(reloaded.has_attribute('value'))
281
+ self.assertTrue(reloaded.has_attribute('name')) # Other attributes remain
282
+
283
+ async def test_remove_nonexistent_attribute(self):
284
+ """Test removing attribute that doesn't exist doesn't fail"""
285
+ entity = await self.create_test_entity(name="Test")
286
+
287
+ request = self.create_strategy_request(
288
+ 'RemoveAttribute',
289
+ entity.entity_id,
290
+ {
291
+ 'attribute_name': 'nonexistent'
292
+ }
293
+ )
294
+
295
+ result = await self.execute_strategy(request)
296
+
297
+ # Should succeed without error
298
+ self.assert_strategy_success(result)
299
+
300
+ async def test_remove_attribute_missing_param(self):
301
+ """Test that missing attribute_name parameter fails"""
302
+ entity = await self.create_test_entity()
303
+
304
+ request = self.create_strategy_request(
305
+ 'RemoveAttribute',
306
+ entity.entity_id,
307
+ {} # Missing attribute_name
308
+ )
309
+
310
+ with self.assertRaises(Exception):
311
+ await self.execute_strategy(request)
312
+
313
+
314
+ class ReparentChildStrategyTest(BaseStrategyTest):
315
+ """Tests for ReparentChild"""
316
+
317
+ async def test_reparent_establishes_relationship(self):
318
+ parent = await self.create_test_entity(name="Parent")
319
+ child = await self.create_test_entity(name="Child")
320
+
321
+ request = self.create_strategy_request(
322
+ 'ReparentChild',
323
+ child.entity_id,
324
+ {'parent_id': parent.entity_id},
325
+ )
326
+
327
+ result = await self.execute_strategy(request)
328
+
329
+ self.assert_strategy_success(result)
330
+
331
+ reloaded_parent = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
332
+ reloaded_child = await self.entity_service.get_entity(child.entity_id, self.test_principal)
333
+ self.assertEqual(list(reloaded_parent.children_ids), [child.entity_id])
334
+ self.assertEqual(reloaded_child.parent_id, parent.entity_id)
335
+
336
+ async def test_reparent_missing_parent_id_key_fails(self):
337
+ child = await self.create_test_entity(name="Child")
338
+
339
+ request = self.create_strategy_request('ReparentChild', child.entity_id, {})
340
+
341
+ with self.assertRaises(InvalidRequest) as context:
342
+ await self.execute_strategy(request)
343
+
344
+ self.assertIn("Invalid param_config for ReparentChild", str(context.exception))
345
+
346
+ async def test_reparent_nonexistent_parent_fails(self):
347
+ child = await self.create_test_entity(name="Child")
348
+ nonexistent_uuid = fixture_id(self.entity_service.backend_id)
349
+
350
+ request = self.create_strategy_request(
351
+ 'ReparentChild', child.entity_id, {'parent_id': nonexistent_uuid},
352
+ )
353
+
354
+ with self.assertRaises(Exception):
355
+ await self.execute_strategy(request)
356
+
357
+ async def test_detach_sets_parent_to_null(self):
358
+ parent = await self.create_test_entity(name="Parent")
359
+ child = await self.create_test_entity(name="Child")
360
+ parent_entity = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
361
+ child_entity = await self.entity_service.get_entity(child.entity_id, self.test_principal)
362
+ child_entity.set_parent(parent_entity.entity_id)
363
+ await self.save_test_entity(child_entity)
364
+
365
+ request = self.create_strategy_request(
366
+ 'ReparentChild', child.entity_id, {'parent_id': None},
367
+ )
368
+
369
+ result = await self.execute_strategy(request)
370
+
371
+ self.assert_strategy_success(result)
372
+ reloaded_parent = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
373
+ reloaded_child = await self.entity_service.get_entity(child.entity_id, self.test_principal)
374
+ self.assertEqual(list(reloaded_parent.children_ids), [])
375
+ self.assertIsNone(reloaded_child.parent_id)
376
+
377
+ async def test_detach_when_already_orphan_is_noop(self):
378
+ child = await self.create_test_entity(name="Child")
379
+
380
+ request = self.create_strategy_request(
381
+ 'ReparentChild', child.entity_id, {'parent_id': None},
382
+ )
383
+
384
+ result = await self.execute_strategy(request)
385
+
386
+ self.assert_strategy_success(result)
387
+ reloaded_child = await self.entity_service.get_entity(child.entity_id, self.test_principal)
388
+ self.assertIsNone(reloaded_child.parent_id)
389
+
390
+ async def test_reparent_between_two_parents_moves_relationship(self):
391
+ parent_a = await self.create_test_entity(name="Parent A")
392
+ parent_b = await self.create_test_entity(name="Parent B")
393
+ child = await self.create_test_entity(name="Child")
394
+ parent_a_entity = await self.entity_service.get_entity(parent_a.entity_id, self.test_principal)
395
+ child_entity = await self.entity_service.get_entity(child.entity_id, self.test_principal)
396
+ child_entity.set_parent(parent_a_entity.entity_id)
397
+ await self.save_test_entity(parent_a_entity)
398
+ await self.save_test_entity(child_entity)
399
+
400
+ request = self.create_strategy_request(
401
+ 'ReparentChild', child.entity_id, {'parent_id': parent_b.entity_id},
402
+ )
403
+
404
+ result = await self.execute_strategy(request)
405
+
406
+ self.assert_strategy_success(result)
407
+ reloaded_parent_a = await self.entity_service.get_entity(parent_a.entity_id, self.test_principal)
408
+ reloaded_parent_b = await self.entity_service.get_entity(parent_b.entity_id, self.test_principal)
409
+ self.assertEqual(list(reloaded_parent_a.children_ids), [])
410
+ self.assertEqual(list(reloaded_parent_b.children_ids), [child.entity_id])
411
+
412
+ async def test_idempotent_reexecution_does_not_duplicate_relationships(self):
413
+ parent = await self.create_test_entity(name="Parent")
414
+ child = await self.create_test_entity(name="Child")
415
+
416
+ request = self.create_strategy_request(
417
+ 'ReparentChild', child.entity_id, {'parent_id': parent.entity_id},
418
+ )
419
+ await self.execute_strategy(request)
420
+ await self.execute_strategy(request)
421
+
422
+ reloaded_parent = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
423
+ self.assertEqual(list(reloaded_parent.children_ids), [child.entity_id])
424
+
425
+
426
+ class ReorderChildStrategyTest(BaseStrategyTest):
427
+ """Tests for ReorderChild"""
428
+
429
+ async def _make_siblings(self, *names):
430
+ parent = await self.create_test_entity(name="Parent")
431
+ parent_entity = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
432
+ children = []
433
+ for name in names:
434
+ child = await self.create_test_entity(name=name)
435
+ child_entity = await self.entity_service.get_entity(child.entity_id, self.test_principal)
436
+ child_entity.set_parent(parent_entity.entity_id)
437
+ await self.save_test_entity(child_entity)
438
+ children.append(child)
439
+ return parent, children
440
+
441
+ async def _reloaded_order(self, parent_id):
442
+ reloaded_parent = await self.entity_service.get_entity(parent_id, self.test_principal)
443
+ return list(reloaded_parent.children_ids)
444
+
445
+ async def test_move_before_reorders_siblings(self):
446
+ parent, (child_a, child_b, child_c) = await self._make_siblings("A", "B", "C")
447
+
448
+ request = self.create_strategy_request(
449
+ 'ReorderChild', child_c.entity_id, {'before_child_id': child_a.entity_id},
450
+ )
451
+
452
+ result = await self.execute_strategy(request)
453
+
454
+ self.assert_strategy_success(result)
455
+ self.assertEqual(
456
+ await self._reloaded_order(parent.entity_id),
457
+ [child_c.entity_id, child_a.entity_id, child_b.entity_id],
458
+ )
459
+
460
+ async def test_move_after_reorders_siblings(self):
461
+ parent, (child_a, child_b, child_c) = await self._make_siblings("A", "B", "C")
462
+
463
+ request = self.create_strategy_request(
464
+ 'ReorderChild', child_a.entity_id, {'after_child_id': child_c.entity_id},
465
+ )
466
+
467
+ result = await self.execute_strategy(request)
468
+
469
+ self.assert_strategy_success(result)
470
+ self.assertEqual(
471
+ await self._reloaded_order(parent.entity_id),
472
+ [child_b.entity_id, child_c.entity_id, child_a.entity_id],
473
+ )
474
+
475
+ async def test_move_to_start(self):
476
+ parent, (child_a, child_b, child_c) = await self._make_siblings("A", "B", "C")
477
+
478
+ request = self.create_strategy_request(
479
+ 'ReorderChild', child_c.entity_id, {'to_start': True},
480
+ )
481
+
482
+ result = await self.execute_strategy(request)
483
+
484
+ self.assert_strategy_success(result)
485
+ self.assertEqual(
486
+ await self._reloaded_order(parent.entity_id),
487
+ [child_c.entity_id, child_a.entity_id, child_b.entity_id],
488
+ )
489
+
490
+ async def test_move_to_end(self):
491
+ parent, (child_a, child_b, child_c) = await self._make_siblings("A", "B", "C")
492
+
493
+ request = self.create_strategy_request(
494
+ 'ReorderChild', child_a.entity_id, {'to_end': True},
495
+ )
496
+
497
+ result = await self.execute_strategy(request)
498
+
499
+ self.assert_strategy_success(result)
500
+ self.assertEqual(
501
+ await self._reloaded_order(parent.entity_id),
502
+ [child_b.entity_id, child_c.entity_id, child_a.entity_id],
503
+ )
504
+
505
+ async def test_rebalances_when_positions_exhausted(self):
506
+ parent, (child_a, child_b) = await self._make_siblings("A", "B")
507
+ # Force the midpoint between A and B down to nothing so the next
508
+ # move must trigger the O(n) rebalance path.
509
+ entity_a = await self.entity_service.get_entity(child_a.entity_id, self.test_principal)
510
+ entity_b = await self.entity_service.get_entity(child_b.entity_id, self.test_principal)
511
+ entity_a.sibling_position = 0.0
512
+ entity_b.sibling_position = 1e-10
513
+ await self.save_test_entity(entity_a)
514
+ await self.save_test_entity(entity_b)
515
+
516
+ request = self.create_strategy_request(
517
+ 'ReorderChild', child_b.entity_id, {'before_child_id': child_a.entity_id},
518
+ )
519
+
520
+ result = await self.execute_strategy(request)
521
+
522
+ self.assert_strategy_success(result)
523
+ self.assertEqual(
524
+ await self._reloaded_order(parent.entity_id),
525
+ [child_b.entity_id, child_a.entity_id],
526
+ )
527
+
528
+ async def test_reorder_without_parent_fails(self):
529
+ child = await self.create_test_entity(name="Orphan")
530
+
531
+ request = self.create_strategy_request(
532
+ 'ReorderChild', child.entity_id, {'to_start': True},
533
+ )
534
+
535
+ with self.assertRaises(StrategyFailed):
536
+ await self.execute_strategy(request)
537
+
538
+ async def test_invalid_anchor_fails(self):
539
+ parent, (child_a,) = await self._make_siblings("A")
540
+ stray = await self.create_test_entity(name="Stray")
541
+
542
+ request = self.create_strategy_request(
543
+ 'ReorderChild', child_a.entity_id, {'before_child_id': stray.entity_id},
544
+ )
545
+
546
+ with self.assertRaises(StrategyFailed) as context:
547
+ await self.execute_strategy(request)
548
+
549
+ self.assertIn("is not a sibling", str(context.exception))
550
+
551
+
552
+ class DeleteEntityStrategyTest(BaseStrategyTest):
553
+ """Tests for DeleteEntity"""
554
+
555
+ async def _link_entities(self, parent, *children):
556
+ for child in children:
557
+ child.set_parent(parent.entity_id)
558
+ await self.save_test_entity(parent)
559
+ for child in children:
560
+ await self.save_test_entity(child)
561
+
562
+ async def _delete_entity(self, entity):
563
+ request = self.create_strategy_request(
564
+ 'DeleteEntity',
565
+ entity.entity_id
566
+ )
567
+ result = await self.execute_strategy(request)
568
+ self.assert_strategy_success(result)
569
+ return result
570
+
571
+ async def _assert_entity_deleted(self, entity):
572
+ with self.assertRaises(NotFound):
573
+ await self.entity_service.get_entity(entity.entity_id, self.test_principal)
574
+
575
+ async def _assert_entity_exists(self, entity):
576
+ return await self.entity_service.get_entity(entity.entity_id, self.test_principal)
577
+
578
+ async def test_delete_entity_marks_as_deleted(self):
579
+ """Test that DeleteEntity marks entity as deleted and removes it"""
580
+ entity = await self.create_test_entity(name="To Delete")
581
+
582
+ request = self.create_strategy_request(
583
+ 'DeleteEntity',
584
+ entity.entity_id
585
+ )
586
+
587
+ result = await self.execute_strategy(request)
588
+
589
+ self.assert_strategy_success(result)
590
+ self.assertEqual(result.ret_val['entity_id'], entity.entity_id)
591
+
592
+ # Verify entity is actually deleted from database (cannot be fetched)
593
+ with self.assertRaises(NotFound) as context:
594
+ await self.entity_service.get_entity(entity.entity_id, self.test_principal)
595
+ self.assertIn('not found', str(context.exception))
596
+
597
+ async def test_delete_entity_removes_from_its_parent(self):
598
+ """Deleting a child removes it from its (single) parent's projection."""
599
+ parent = await self.create_test_entity(name="Parent")
600
+ child = await self.create_test_entity(name="Child")
601
+
602
+ p = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
603
+ c = await self.entity_service.get_entity(child.entity_id, self.test_principal)
604
+ c.set_parent(p.entity_id)
605
+ await self.save_test_entity(c)
606
+
607
+ request = self.create_strategy_request(
608
+ 'DeleteEntity',
609
+ child.entity_id
610
+ )
611
+
612
+ await self.execute_strategy(request)
613
+
614
+ reloaded_p = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
615
+ self.assertNotIn(child.entity_id, reloaded_p.children_ids)
616
+
617
+ async def test_delete_entity_recursively_deletes_orphaned_children(self):
618
+ """Test that deleting an entity deletes children that have no other parents"""
619
+ parent = await self.create_test_entity(name="Parent")
620
+ child = await self.create_test_entity(name="Child")
621
+
622
+ # Add child to parent
623
+ p = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
624
+ c = await self.entity_service.get_entity(child.entity_id, self.test_principal)
625
+ c.set_parent(p.entity_id)
626
+ await self.save_test_entity(c)
627
+
628
+ # Remove parent
629
+ request = self.create_strategy_request(
630
+ 'DeleteEntity',
631
+ parent.entity_id
632
+ )
633
+
634
+ result = await self.execute_strategy(request)
635
+
636
+ # Child should also be marked for deletion since it has no other parents
637
+ # (Implementation may vary - this tests the recursive deletion logic)
638
+ self.assert_strategy_success(result)
639
+
640
+ async def test_delete_entity_deletes_direct_orphan_child(self):
641
+ root = await self.create_test_entity(name="Root")
642
+ child = await self.create_test_entity(name="Child")
643
+ await self._link_entities(root, child)
644
+
645
+ await self._delete_entity(root)
646
+
647
+ await self._assert_entity_deleted(root)
648
+ await self._assert_entity_deleted(child)
649
+
650
+ async def test_delete_entity_does_not_affect_reparented_child(self):
651
+ """Single-parent model: re-parenting a child before its old parent
652
+ is deleted means the child is no longer in that subtree, so the
653
+ cascade never reaches it."""
654
+ root = await self.create_test_entity(name="Root")
655
+ new_parent = await self.create_test_entity(name="New Parent")
656
+ child = await self.create_test_entity(name="Child")
657
+ await self._link_entities(root, child)
658
+ # Re-parent: child now lives under new_parent instead of root.
659
+ await self._link_entities(new_parent, child)
660
+
661
+ await self._delete_entity(root)
662
+
663
+ await self._assert_entity_deleted(root)
664
+ reloaded_child = await self._assert_entity_exists(child)
665
+ reloaded_new_parent = await self._assert_entity_exists(new_parent)
666
+ self.assertIn(child.entity_id, reloaded_new_parent.children_ids)
667
+ self.assertEqual(reloaded_child.parent_ids, (new_parent.entity_id,))
668
+
669
+ async def test_delete_entity_deletes_deep_orphan_chain(self):
670
+ root = await self.create_test_entity(name="Root")
671
+ child = await self.create_test_entity(name="Child")
672
+ grandchild = await self.create_test_entity(name="Grandchild")
673
+ great_grandchild = await self.create_test_entity(name="Great Grandchild")
674
+ await self._link_entities(root, child)
675
+ await self._link_entities(child, grandchild)
676
+ await self._link_entities(grandchild, great_grandchild)
677
+
678
+ await self._delete_entity(root)
679
+
680
+ await self._assert_entity_deleted(root)
681
+ await self._assert_entity_deleted(child)
682
+ await self._assert_entity_deleted(grandchild)
683
+ await self._assert_entity_deleted(great_grandchild)
684
+
685
+ async def test_delete_entity_does_not_affect_sibling_subtree(self):
686
+ """Single-parent model: two independent subtrees under the same
687
+ root are only linked through that root - deleting one child's
688
+ subtree leaves the other, and the root's own surviving children,
689
+ untouched."""
690
+ root = await self.create_test_entity(name="Root")
691
+ child_a = await self.create_test_entity(name="Child A")
692
+ child_b = await self.create_test_entity(name="Child B")
693
+ grandchild_a = await self.create_test_entity(name="Grandchild A")
694
+ await self._link_entities(root, child_a, child_b)
695
+ await self._link_entities(child_a, grandchild_a)
696
+
697
+ await self._delete_entity(child_a)
698
+
699
+ await self._assert_entity_deleted(child_a)
700
+ await self._assert_entity_deleted(grandchild_a)
701
+ reloaded_root = await self._assert_entity_exists(root)
702
+ await self._assert_entity_exists(child_b)
703
+ self.assertNotIn(child_a.entity_id, reloaded_root.children_ids)
704
+ self.assertIn(child_b.entity_id, reloaded_root.children_ids)
705
+
706
+ async def test_delete_entity_with_deep_hierarchy_no_concurrent_modification(self):
707
+ """DeleteEntity handles a multi-level single-parent chain
708
+ (root -> child -> grandchild1/grandchild2) without concurrent
709
+ modification errors, deleting the whole closure."""
710
+ root = await self.create_test_entity(name="Root")
711
+ child = await self.create_test_entity(name="Child")
712
+ grandchild1 = await self.create_test_entity(name="Grandchild1")
713
+ grandchild2 = await self.create_test_entity(name="Grandchild2")
714
+ await self._link_entities(root, child)
715
+ await self._link_entities(child, grandchild1, grandchild2)
716
+
717
+ t_reloaded = await self.entity_service.get_entity(child.entity_id, self.test_principal)
718
+ self.assertEqual(t_reloaded.parent_ids, (root.entity_id,))
719
+ self.assertEqual(len(t_reloaded.children_ids), 2)
720
+
721
+ await self._delete_entity(root)
722
+
723
+ await self._assert_entity_deleted(root)
724
+ await self._assert_entity_deleted(child)
725
+ await self._assert_entity_deleted(grandchild1)
726
+ await self._assert_entity_deleted(grandchild2)
727
+
728
+
729
+ class DeletedStrategySurfaceTest(BaseStrategyTest):
730
+ """Step B4: AssignAttributes and its fabricated 'FRUAD' nested
731
+ request are gone; neither name can be constructed or dispatched."""
732
+
733
+ async def test_assign_attributes_strategy_is_not_registered(self):
734
+ self.assertIsNone(self.registry.strategies.by_name('AssignAttributes'))
735
+
736
+ async def test_fruad_strategy_is_not_registered(self):
737
+ self.assertIsNone(self.registry.strategies.by_name('FRUAD'))
738
+
739
+ async def test_serialize_has_no_source_key(self):
740
+ """Strategy.serialize() used to ship inspect.getsource() output;
741
+ it must carry name/schema/description only."""
742
+ serialized = SetAttributes.serialize()
743
+ self.assertEqual(serialized['name'], 'SetAttributes')
744
+ self.assertIn('schema', serialized)
745
+ self.assertIn('description', serialized)
746
+ self.assertNotIn('source', serialized)
747
+
748
+
749
+ class FindAndReplaceStrategyTest(BaseStrategyTest):
750
+ """Tests for FindAndReplace"""
751
+
752
+ async def test_find_and_replace_all_occurrences(self):
753
+ """Test replacing all occurrences of a substring"""
754
+ entity = await self.create_test_entity(
755
+ text="The quick brown fox jumps over the lazy dog. The fox is quick."
756
+ )
757
+
758
+ request = self.create_strategy_request(
759
+ 'FindAndReplace',
760
+ entity.entity_id,
761
+ {
762
+ 'attribute_name': 'text',
763
+ 'find_string': 'fox',
764
+ 'replace_string': 'cat',
765
+ 'replace_all': True
766
+ }
767
+ )
768
+
769
+ result = await self.execute_strategy(request)
770
+
771
+ self.assert_strategy_success(result)
772
+ self.assertEqual(result.ret_val['replacements_made'], 2)
773
+ self.assertEqual(set(result.ret_val.keys()), {'replacements_made'})
774
+
775
+ reloaded = await self.entity_service.get_entity(entity.entity_id, self.test_principal)
776
+ text = reloaded.get_attribute('text')
777
+ self.assertNotIn('fox', text)
778
+ self.assertIn('cat', text)
779
+ self.assertEqual(text.count('cat'), 2)
780
+
781
+ async def test_find_and_replace_first_occurrence_only(self):
782
+ """Test replacing only the first occurrence"""
783
+ entity = await self.create_test_entity(
784
+ text="test test test"
785
+ )
786
+
787
+ request = self.create_strategy_request(
788
+ 'FindAndReplace',
789
+ entity.entity_id,
790
+ {
791
+ 'attribute_name': 'text',
792
+ 'find_string': 'test',
793
+ 'replace_string': 'replaced',
794
+ 'replace_all': False
795
+ }
796
+ )
797
+
798
+ result = await self.execute_strategy(request)
799
+
800
+ self.assert_strategy_success(result)
801
+ self.assertEqual(result.ret_val['replacements_made'], 1)
802
+ self.assertEqual(set(result.ret_val.keys()), {'replacements_made'})
803
+
804
+ reloaded = await self.entity_service.get_entity(entity.entity_id, self.test_principal)
805
+ text = reloaded.get_attribute('text')
806
+ self.assertEqual(text, "replaced test test")
807
+
808
+ async def test_find_and_replace_missing_attribute_fails(self):
809
+ """Test that operating on nonexistent attribute fails"""
810
+ entity = await self.create_test_entity(name="Test")
811
+
812
+ request = self.create_strategy_request(
813
+ 'FindAndReplace',
814
+ entity.entity_id,
815
+ {
816
+ 'attribute_name': 'nonexistent',
817
+ 'find_string': 'old',
818
+ 'replace_string': 'new'
819
+ }
820
+ )
821
+
822
+ with self.assertRaises(StrategyFailed) as context:
823
+ await self.execute_strategy(request)
824
+
825
+ self.assertIn("does not have attribute", str(context.exception))
826
+
827
+ async def test_find_and_replace_non_string_attribute_fails(self):
828
+ """Test that operating on non-string attribute fails"""
829
+ entity = await self.create_test_entity(value=42)
830
+
831
+ request = self.create_strategy_request(
832
+ 'FindAndReplace',
833
+ entity.entity_id,
834
+ {
835
+ 'attribute_name': 'value',
836
+ 'find_string': '4',
837
+ 'replace_string': '5'
838
+ }
839
+ )
840
+
841
+ with self.assertRaises(StrategyFailed) as context:
842
+ await self.execute_strategy(request)
843
+
844
+ self.assertIn("must be a string", str(context.exception))
845
+
846
+
847
+
848
+
849
+ class StrategySerializeTest(SimpleTestCase):
850
+ def test_serialize_is_json_serializable(self):
851
+ info = CreateEntity.serialize()
852
+ self.assertEqual(json.loads(json.dumps(info))['name'], 'CreateEntity')
853
+
854
+
855
+ class ChildCreationProjectionTests(BaseStrategyTest):
856
+ async def test_create_strategies_do_not_save_the_parent(self):
857
+ for strategy, config, result_key in (
858
+ ('CreateEntity', {'entity_type_id': 'graphcore.Entity', 'initial_attributes': {'name': 'bag'}}, 'child_entity'),
859
+ ('CreateGroup', {'name': 'Typed group'}, 'group_entity_id'),
860
+ ):
861
+ with self.subTest(strategy=strategy):
862
+ parent = await self.create_test_entity()
863
+ version = parent.version
864
+ request = self.create_strategy_request(strategy, parent.entity_id, config)
865
+ result = await self.execute_strategy(request)
866
+ child_id = result.ret_val[result_key]
867
+ loaded = await self.entity_service.get_entity(parent.entity_id, self.test_principal)
868
+ self.assertEqual(loaded.version, version)
869
+ self.assertIn(child_id, loaded.children_ids)
870
+ self.assertEqual((await self.entity_service.repository.load(child_id)).parent_id, parent.entity_id)