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.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
"""The `EntityRepository` contract, run against every implementation.
|
|
2
|
+
|
|
3
|
+
Any backend that passes this behaves identically for `graph/` and
|
|
4
|
+
`auth/authz` - including the `ancestor_ids` invariant and the predicate
|
|
5
|
+
semantics `persistence/tests/evaluate.matches` defines. Published as
|
|
6
|
+
`graphcore.public.testing.EntityRepositoryContract` for third-party stores.
|
|
7
|
+
"""
|
|
8
|
+
from asgiref.sync import async_to_sync
|
|
9
|
+
|
|
10
|
+
from graphcore.domain.entity.Entity import Entity
|
|
11
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
12
|
+
from graphcore.domain.entity.group.Group import Group
|
|
13
|
+
from graphcore.domain.entity.user.User import User
|
|
14
|
+
from graphcore.errors import ConcurrentModificationError, DuplicateEntity, PreconditionFailed
|
|
15
|
+
from graphcore.persistence.repository import Precondition
|
|
16
|
+
from graphcore.types.entity_id import backend_of, new_entity_id
|
|
17
|
+
from graphcore.types.query import (
|
|
18
|
+
AllOf, ContainsAny, ContainsValue, Equal, EntityQuery, InValues, PageRequest,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class EntityRepositoryContract:
|
|
23
|
+
"""Mixin. Concrete cases supply `make_repository()`."""
|
|
24
|
+
|
|
25
|
+
def make_repository(self):
|
|
26
|
+
raise NotImplementedError
|
|
27
|
+
|
|
28
|
+
def setUp(self):
|
|
29
|
+
super().setUp()
|
|
30
|
+
self.repository = self.make_repository()
|
|
31
|
+
|
|
32
|
+
# --- store identity -------------------------------------------------
|
|
33
|
+
|
|
34
|
+
def test_backend_id_mints_identifiers_and_is_stable(self):
|
|
35
|
+
backend_id = self.repository.backend_id()
|
|
36
|
+
|
|
37
|
+
self.assertIsInstance(backend_id, str)
|
|
38
|
+
self.assertEqual(backend_of(new_entity_id(backend_id)), backend_id)
|
|
39
|
+
self.assertEqual(self.repository.backend_id(), backend_id)
|
|
40
|
+
|
|
41
|
+
# --- helpers --------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
def save(self, entity):
|
|
44
|
+
return async_to_sync(self.repository.save)(entity)
|
|
45
|
+
|
|
46
|
+
def new(self, entity_cls=Entity):
|
|
47
|
+
"""Unsaved, carrying this repository's own store identity."""
|
|
48
|
+
return entity_cls(new_entity_id(self.repository.backend_id()))
|
|
49
|
+
|
|
50
|
+
def create(self, entity_cls=Entity, parent=None, owner=None, **fields):
|
|
51
|
+
entity = self.new(entity_cls)
|
|
52
|
+
entity.owner = owner
|
|
53
|
+
if parent is not None:
|
|
54
|
+
entity.set_parent(parent.entity_id)
|
|
55
|
+
for name, value in fields.items():
|
|
56
|
+
setattr(entity, name, value)
|
|
57
|
+
self.save(entity)
|
|
58
|
+
return entity
|
|
59
|
+
|
|
60
|
+
def load(self, entity_id):
|
|
61
|
+
return async_to_sync(self.repository.load)(entity_id)
|
|
62
|
+
|
|
63
|
+
def list_ids(self, predicate):
|
|
64
|
+
return async_to_sync(self.repository.list_ids)(predicate)
|
|
65
|
+
|
|
66
|
+
def chain(self, depth):
|
|
67
|
+
"""A root and `depth` nested descendants, outermost first."""
|
|
68
|
+
entities = [self.create()]
|
|
69
|
+
for _ in range(depth):
|
|
70
|
+
entities.append(self.create(parent=entities[-1]))
|
|
71
|
+
return entities
|
|
72
|
+
|
|
73
|
+
# --- create / update ------------------------------------------------
|
|
74
|
+
|
|
75
|
+
def test_create_returns_a_creation_receipt(self):
|
|
76
|
+
entity = self.new()
|
|
77
|
+
receipt = self.save(entity)
|
|
78
|
+
|
|
79
|
+
self.assertTrue(receipt.created)
|
|
80
|
+
self.assertIsNone(receipt.previous)
|
|
81
|
+
self.assertEqual(entity.version, 1)
|
|
82
|
+
self.assertEqual(receipt.touched_parent_ids, frozenset())
|
|
83
|
+
|
|
84
|
+
def test_update_increments_version_and_carries_the_previous_entity(self):
|
|
85
|
+
entity = self.create()
|
|
86
|
+
entity.set_attribute('name', 'after')
|
|
87
|
+
|
|
88
|
+
receipt = self.save(entity)
|
|
89
|
+
|
|
90
|
+
self.assertFalse(receipt.created)
|
|
91
|
+
self.assertEqual(entity.version, 2)
|
|
92
|
+
self.assertFalse(receipt.previous.has_attribute('name'))
|
|
93
|
+
self.assertEqual(self.load(entity.entity_id).get_attribute('name'), 'after')
|
|
94
|
+
|
|
95
|
+
def test_stale_version_is_refused(self):
|
|
96
|
+
entity = self.create()
|
|
97
|
+
entity.version = 0
|
|
98
|
+
|
|
99
|
+
with self.assertRaises(ConcurrentModificationError):
|
|
100
|
+
self.save(entity)
|
|
101
|
+
|
|
102
|
+
def save_over(self, entity, previous):
|
|
103
|
+
return async_to_sync(self.repository.save)(entity, previous=previous)
|
|
104
|
+
|
|
105
|
+
def test_a_supplied_previous_saves_as_a_read_one_would(self):
|
|
106
|
+
parent = self.create()
|
|
107
|
+
entity = self.create(parent=parent)
|
|
108
|
+
previous = self.load(entity.entity_id)
|
|
109
|
+
entity.set_attribute('name', 'after')
|
|
110
|
+
|
|
111
|
+
receipt = self.save_over(entity, previous)
|
|
112
|
+
|
|
113
|
+
self.assertIs(receipt.previous, previous)
|
|
114
|
+
self.assertEqual(entity.version, 2)
|
|
115
|
+
stored = self.load(entity.entity_id)
|
|
116
|
+
self.assertEqual((stored.get_attribute('name'), stored.ancestor_ids), ('after', (parent.entity_id,)))
|
|
117
|
+
|
|
118
|
+
def test_a_previous_at_another_version_is_read_again(self):
|
|
119
|
+
entity = self.create()
|
|
120
|
+
stale = self.load(entity.entity_id)
|
|
121
|
+
entity.set_attribute('name', 'second')
|
|
122
|
+
self.save(entity)
|
|
123
|
+
entity.set_attribute('name', 'third')
|
|
124
|
+
|
|
125
|
+
receipt = self.save_over(entity, stale)
|
|
126
|
+
|
|
127
|
+
self.assertEqual(receipt.previous.get_attribute('name'), 'second')
|
|
128
|
+
self.assertEqual(self.load(entity.entity_id).get_attribute('name'), 'third')
|
|
129
|
+
|
|
130
|
+
def test_a_create_without_a_previous(self):
|
|
131
|
+
parent = self.create()
|
|
132
|
+
entity = self.new()
|
|
133
|
+
entity.set_parent(parent.entity_id)
|
|
134
|
+
|
|
135
|
+
receipt = self.save_over(entity, None)
|
|
136
|
+
|
|
137
|
+
self.assertTrue(receipt.created)
|
|
138
|
+
self.assertEqual(receipt.touched_parent_ids, frozenset({parent.entity_id}))
|
|
139
|
+
self.assertEqual(self.load(entity.entity_id).ancestor_ids, (parent.entity_id,))
|
|
140
|
+
|
|
141
|
+
def test_an_edit_keeps_the_ancestors_a_move_rewrote_since_its_previous(self):
|
|
142
|
+
old_root, new_root = self.create(), self.create()
|
|
143
|
+
middle = self.create(parent=old_root)
|
|
144
|
+
leaf = self.create(parent=middle)
|
|
145
|
+
previous = self.load(leaf.entity_id)
|
|
146
|
+
middle.set_parent(new_root.entity_id)
|
|
147
|
+
self.save(middle)
|
|
148
|
+
|
|
149
|
+
leaf.set_attribute('name', 'edited')
|
|
150
|
+
self.save_over(leaf, previous)
|
|
151
|
+
|
|
152
|
+
self.assertEqual(self.load(leaf.entity_id).ancestor_ids, (new_root.entity_id, middle.entity_id))
|
|
153
|
+
|
|
154
|
+
def _gate(self, name):
|
|
155
|
+
gate = self.create(Group, name=name)
|
|
156
|
+
return Precondition(gate.entity_id, AllOf((
|
|
157
|
+
Equal('entity_type_id', Group.get_entity_type_id()), Equal('name', 'open'),
|
|
158
|
+
)))
|
|
159
|
+
|
|
160
|
+
def test_a_write_whose_precondition_holds_takes_effect(self):
|
|
161
|
+
precondition = self._gate('open')
|
|
162
|
+
entity = self.new()
|
|
163
|
+
async_to_sync(self.repository.save)(entity, precondition)
|
|
164
|
+
self.assertIsNotNone(self.load(entity.entity_id))
|
|
165
|
+
|
|
166
|
+
async_to_sync(self.repository.delete_many)([entity.entity_id], precondition)
|
|
167
|
+
self.assertIsNone(self.load(entity.entity_id))
|
|
168
|
+
|
|
169
|
+
def test_a_write_whose_precondition_fails_writes_nothing(self):
|
|
170
|
+
precondition = self._gate('closed')
|
|
171
|
+
created = self.new()
|
|
172
|
+
with self.assertRaises(PreconditionFailed):
|
|
173
|
+
async_to_sync(self.repository.save)(created, precondition)
|
|
174
|
+
self.assertIsNone(self.load(created.entity_id))
|
|
175
|
+
|
|
176
|
+
kept = self.create(Group, name='before')
|
|
177
|
+
kept.name = 'after'
|
|
178
|
+
with self.assertRaises(PreconditionFailed):
|
|
179
|
+
async_to_sync(self.repository.save)(kept, precondition)
|
|
180
|
+
self.assertEqual(self.load(kept.entity_id).name, 'before')
|
|
181
|
+
|
|
182
|
+
with self.assertRaises(PreconditionFailed):
|
|
183
|
+
async_to_sync(self.repository.delete_many)([kept.entity_id], precondition)
|
|
184
|
+
self.assertIsNotNone(self.load(kept.entity_id))
|
|
185
|
+
|
|
186
|
+
def test_a_fresh_entity_minted_onto_an_existing_id_is_refused(self):
|
|
187
|
+
existing = self.create()
|
|
188
|
+
existing.set_attribute('name', 'kept')
|
|
189
|
+
self.save(existing)
|
|
190
|
+
duplicate = Entity(existing.entity_id)
|
|
191
|
+
|
|
192
|
+
with self.assertRaises(ConcurrentModificationError):
|
|
193
|
+
self.save(duplicate)
|
|
194
|
+
self.assertEqual(self.load(existing.entity_id).get_attribute('name'), 'kept')
|
|
195
|
+
|
|
196
|
+
def test_a_second_user_with_the_same_issuer_and_subject_is_refused(self):
|
|
197
|
+
first = self.create(User, issuer='https://idp.example', subject='alice')
|
|
198
|
+
|
|
199
|
+
with self.assertRaises(DuplicateEntity):
|
|
200
|
+
self.create(User, issuer='https://idp.example', subject='alice')
|
|
201
|
+
self.assertEqual(
|
|
202
|
+
self.list_ids(AllOf((
|
|
203
|
+
Equal('entity_type_id', User.get_entity_type_id()),
|
|
204
|
+
Equal('issuer', 'https://idp.example'), Equal('subject', 'alice'),
|
|
205
|
+
))),
|
|
206
|
+
{first.entity_id},
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
def test_users_sharing_only_an_issuer_or_a_subject_coexist(self):
|
|
210
|
+
self.create(User, issuer='https://idp.example', subject='alice')
|
|
211
|
+
self.create(User, issuer='https://idp.example', subject='bob')
|
|
212
|
+
self.create(User, issuer='https://other.example', subject='alice')
|
|
213
|
+
|
|
214
|
+
def test_creating_under_a_parent_touches_that_parent(self):
|
|
215
|
+
parent = self.create()
|
|
216
|
+
child = self.new()
|
|
217
|
+
child.set_parent(parent.entity_id)
|
|
218
|
+
|
|
219
|
+
self.assertEqual(self.save(child).touched_parent_ids, frozenset({parent.entity_id}))
|
|
220
|
+
|
|
221
|
+
def test_reparenting_touches_both_ends_of_the_move(self):
|
|
222
|
+
old_parent, new_parent = self.create(), self.create()
|
|
223
|
+
child = self.create(parent=old_parent)
|
|
224
|
+
child.set_parent(new_parent.entity_id)
|
|
225
|
+
|
|
226
|
+
self.assertEqual(
|
|
227
|
+
self.save(child).touched_parent_ids,
|
|
228
|
+
frozenset({old_parent.entity_id, new_parent.entity_id}),
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
def test_reordering_touches_only_the_parent(self):
|
|
232
|
+
parent = self.create()
|
|
233
|
+
child = self.create(parent=parent)
|
|
234
|
+
child.sibling_position = child.sibling_position + 1
|
|
235
|
+
|
|
236
|
+
self.assertEqual(self.save(child).touched_parent_ids, frozenset({parent.entity_id}))
|
|
237
|
+
|
|
238
|
+
def test_an_ordinary_edit_touches_nothing(self):
|
|
239
|
+
parent = self.create()
|
|
240
|
+
child = self.create(parent=parent)
|
|
241
|
+
child.set_attribute('name', 'edited')
|
|
242
|
+
|
|
243
|
+
self.assertEqual(self.save(child).touched_parent_ids, frozenset())
|
|
244
|
+
|
|
245
|
+
# --- ancestor_ids ---------------------------------------------------
|
|
246
|
+
|
|
247
|
+
def test_ancestors_are_the_chain_above_root_first(self):
|
|
248
|
+
root, middle, leaf = self.chain(2)
|
|
249
|
+
|
|
250
|
+
self.assertEqual(self.load(root.entity_id).ancestor_ids, ())
|
|
251
|
+
self.assertEqual(self.load(middle.entity_id).ancestor_ids, (root.entity_id,))
|
|
252
|
+
self.assertEqual(
|
|
253
|
+
self.load(leaf.entity_id).ancestor_ids, (root.entity_id, middle.entity_id),
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
def test_the_saved_entity_carries_its_new_ancestors(self):
|
|
257
|
+
root = self.create()
|
|
258
|
+
child = self.new()
|
|
259
|
+
child.set_parent(root.entity_id)
|
|
260
|
+
self.save(child)
|
|
261
|
+
|
|
262
|
+
self.assertEqual(child.ancestor_ids, (root.entity_id,))
|
|
263
|
+
|
|
264
|
+
def test_reparenting_rewrites_the_subtree_without_bumping_versions(self):
|
|
265
|
+
root, middle, leaf = self.chain(2)
|
|
266
|
+
new_root = self.create()
|
|
267
|
+
versions_before = {
|
|
268
|
+
eid: self.load(eid).version for eid in (middle.entity_id, leaf.entity_id)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
middle.set_parent(new_root.entity_id)
|
|
272
|
+
self.save(middle)
|
|
273
|
+
|
|
274
|
+
self.assertEqual(self.load(middle.entity_id).ancestor_ids, (new_root.entity_id,))
|
|
275
|
+
self.assertEqual(
|
|
276
|
+
self.load(leaf.entity_id).ancestor_ids, (new_root.entity_id, middle.entity_id),
|
|
277
|
+
)
|
|
278
|
+
self.assertEqual(self.load(leaf.entity_id).version, versions_before[leaf.entity_id])
|
|
279
|
+
self.assertEqual(self.load(middle.entity_id).version, versions_before[middle.entity_id] + 1)
|
|
280
|
+
self.assertNotIn(root.entity_id, self.load(leaf.entity_id).ancestor_ids)
|
|
281
|
+
|
|
282
|
+
def test_a_reparent_reports_the_subtree_it_rewrote(self):
|
|
283
|
+
"""Descendants change without a version bump, so nothing downstream
|
|
284
|
+
can notice from the row alone - the receipt is how a payload cache
|
|
285
|
+
learns to drop them."""
|
|
286
|
+
root, middle, leaf = self.chain(2)
|
|
287
|
+
new_root = self.create()
|
|
288
|
+
middle.set_parent(new_root.entity_id)
|
|
289
|
+
|
|
290
|
+
receipt = self.save(middle)
|
|
291
|
+
|
|
292
|
+
self.assertEqual(receipt.moved_descendant_ids, frozenset({leaf.entity_id}))
|
|
293
|
+
|
|
294
|
+
def test_an_ordinary_save_reports_no_moved_descendants(self):
|
|
295
|
+
root, middle, leaf = self.chain(2)
|
|
296
|
+
middle.set_attribute('name', 'edited')
|
|
297
|
+
|
|
298
|
+
self.assertEqual(self.save(middle).moved_descendant_ids, frozenset())
|
|
299
|
+
|
|
300
|
+
def test_descendant_ids_is_the_whole_subtree(self):
|
|
301
|
+
root, middle, leaf = self.chain(2)
|
|
302
|
+
sibling = self.create(parent=root)
|
|
303
|
+
|
|
304
|
+
self.assertEqual(
|
|
305
|
+
async_to_sync(self.repository.descendant_ids)(root.entity_id),
|
|
306
|
+
{middle.entity_id, leaf.entity_id, sibling.entity_id},
|
|
307
|
+
)
|
|
308
|
+
self.assertEqual(async_to_sync(self.repository.descendant_ids)(leaf.entity_id), set())
|
|
309
|
+
|
|
310
|
+
# --- reads ----------------------------------------------------------
|
|
311
|
+
|
|
312
|
+
def test_load_returns_none_for_an_unknown_id(self):
|
|
313
|
+
self.assertIsNone(self.load(self.new().entity_id))
|
|
314
|
+
|
|
315
|
+
def test_versions_reports_only_ids_that_exist(self):
|
|
316
|
+
entity = self.create()
|
|
317
|
+
missing = self.new().entity_id
|
|
318
|
+
|
|
319
|
+
self.assertEqual(
|
|
320
|
+
async_to_sync(self.repository.versions)([entity.entity_id, missing]),
|
|
321
|
+
{entity.entity_id: 1},
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
def test_children_are_projected_in_sibling_order(self):
|
|
325
|
+
parent = self.create()
|
|
326
|
+
first = self.create(parent=parent)
|
|
327
|
+
second = self.create(parent=parent)
|
|
328
|
+
first.sibling_position = second.sibling_position + 1
|
|
329
|
+
self.save(first)
|
|
330
|
+
|
|
331
|
+
self.assertEqual(
|
|
332
|
+
self.load(parent.entity_id).children_ids,
|
|
333
|
+
(second.entity_id, first.entity_id),
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
def test_typed_entities_round_trip_through_storage(self):
|
|
337
|
+
resource = self.create(Group, name='Engineering')
|
|
338
|
+
grant = self.create(Grant, parent=resource, subject_id='user-1', role_name='viewer')
|
|
339
|
+
|
|
340
|
+
loaded_grant = self.load(grant.entity_id)
|
|
341
|
+
self.assertIsInstance(loaded_grant, Grant)
|
|
342
|
+
self.assertEqual(loaded_grant.subject_id, 'user-1')
|
|
343
|
+
self.assertEqual(loaded_grant.role_name, 'viewer')
|
|
344
|
+
self.assertEqual(self.load(resource.entity_id).name, 'Engineering')
|
|
345
|
+
|
|
346
|
+
def test_bag_and_typed_fields_remain_independent(self):
|
|
347
|
+
group = self.create(Group, name='Typed', member_ids=['member'])
|
|
348
|
+
bag = {'name': 'Bag', 'owner': 'bag owner', 'parent_id': 'bag parent',
|
|
349
|
+
'entity_type_id': 'caller type', 'member_ids': ['bag member'],
|
|
350
|
+
'attributes': {'nested': [1, None]}}
|
|
351
|
+
for key, value in bag.items():
|
|
352
|
+
group.set_attribute(key, value)
|
|
353
|
+
self.save(group)
|
|
354
|
+
loaded = self.load(group.entity_id)
|
|
355
|
+
self.assertEqual(loaded.to_dict()['attributes'], bag)
|
|
356
|
+
self.assertEqual(loaded.name, 'Typed')
|
|
357
|
+
self.assertEqual(loaded.member_ids, ['member'])
|
|
358
|
+
self.assertIsNone(loaded.parent_id)
|
|
359
|
+
self.assertEqual(self.list_ids(Equal('name', 'Typed')), {group.entity_id})
|
|
360
|
+
self.assertEqual(self.list_ids(Equal('name', 'Bag')), set())
|
|
361
|
+
loaded.remove_attribute('name')
|
|
362
|
+
self.save(loaded)
|
|
363
|
+
loaded = self.load(group.entity_id)
|
|
364
|
+
self.assertFalse(loaded.has_attribute('name'))
|
|
365
|
+
self.assertEqual(loaded.name, 'Typed')
|
|
366
|
+
|
|
367
|
+
def test_equal_position_order_survives_updates_and_recreation(self):
|
|
368
|
+
parent = self.create()
|
|
369
|
+
first = self.create(parent=parent, sibling_position=1)
|
|
370
|
+
second = self.create(parent=parent, sibling_position=1)
|
|
371
|
+
self.save(first)
|
|
372
|
+
self.assertEqual(self.load(parent.entity_id).children_ids, (first.entity_id, second.entity_id))
|
|
373
|
+
async_to_sync(self.repository.delete_many)([first.entity_id])
|
|
374
|
+
recreated = Entity(first.entity_id)
|
|
375
|
+
recreated.set_parent(parent.entity_id)
|
|
376
|
+
recreated.sibling_position = 1
|
|
377
|
+
self.save(recreated)
|
|
378
|
+
self.assertEqual(self.load(parent.entity_id).children_ids, (second.entity_id, first.entity_id))
|
|
379
|
+
|
|
380
|
+
# --- reference_ids --------------------------------------------------
|
|
381
|
+
|
|
382
|
+
def reference_fixture(self):
|
|
383
|
+
"""One entity holding the same target three ways - scalar, nested,
|
|
384
|
+
and in prose - plus a second, distinct target."""
|
|
385
|
+
target, other = self.new(), self.new()
|
|
386
|
+
source = self.create()
|
|
387
|
+
for key, value in {
|
|
388
|
+
'items': [target.entity_id, {'nested': target.entity_id}],
|
|
389
|
+
'note': f'see {target.entity_id} for details',
|
|
390
|
+
'customer': other.entity_id,
|
|
391
|
+
}.items():
|
|
392
|
+
source.set_attribute(key, value)
|
|
393
|
+
self.save(source)
|
|
394
|
+
return source, target, other
|
|
395
|
+
|
|
396
|
+
def test_references_are_collected_from_anywhere_in_the_attributes(self):
|
|
397
|
+
source, target, other = self.reference_fixture()
|
|
398
|
+
|
|
399
|
+
self.assertEqual(
|
|
400
|
+
self.load(source.entity_id).reference_ids,
|
|
401
|
+
tuple(sorted({target.entity_id, other.entity_id})),
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
def test_the_saved_entity_carries_its_own_projection(self):
|
|
405
|
+
source, target, other = self.reference_fixture()
|
|
406
|
+
|
|
407
|
+
self.assertEqual(set(source.reference_ids), {target.entity_id, other.entity_id})
|
|
408
|
+
|
|
409
|
+
def test_containment_is_a_reference(self):
|
|
410
|
+
parent = self.create()
|
|
411
|
+
child = self.create(parent=parent)
|
|
412
|
+
|
|
413
|
+
self.assertEqual(self.load(child.entity_id).reference_ids, (parent.entity_id,))
|
|
414
|
+
|
|
415
|
+
def test_an_owner_identifier_is_a_reference(self):
|
|
416
|
+
owner = self.new()
|
|
417
|
+
entity = self.create(owner=owner.entity_id)
|
|
418
|
+
|
|
419
|
+
self.assertEqual(self.load(entity.entity_id).reference_ids, (owner.entity_id,))
|
|
420
|
+
|
|
421
|
+
def test_a_typed_column_reference_is_projected(self):
|
|
422
|
+
resource, subject = self.create(), self.new()
|
|
423
|
+
grant = self.create(
|
|
424
|
+
Grant, parent=resource, subject_id=subject.entity_id, role_name='viewer',
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
self.assertEqual(
|
|
428
|
+
set(self.load(grant.entity_id).reference_ids),
|
|
429
|
+
{resource.entity_id, subject.entity_id},
|
|
430
|
+
)
|
|
431
|
+
|
|
432
|
+
def test_an_entity_is_not_its_own_reference(self):
|
|
433
|
+
entity = self.create()
|
|
434
|
+
entity.set_attribute('self', entity.entity_id)
|
|
435
|
+
self.save(entity)
|
|
436
|
+
|
|
437
|
+
self.assertEqual(self.load(entity.entity_id).reference_ids, ())
|
|
438
|
+
|
|
439
|
+
def test_removing_a_reference_drops_it_from_the_projection(self):
|
|
440
|
+
source, target, other = self.reference_fixture()
|
|
441
|
+
for key in ('items', 'note'):
|
|
442
|
+
source.remove_attribute(key)
|
|
443
|
+
self.save(source)
|
|
444
|
+
|
|
445
|
+
self.assertEqual(self.load(source.entity_id).reference_ids, (other.entity_id,))
|
|
446
|
+
|
|
447
|
+
# --- referrers ------------------------------------------------------
|
|
448
|
+
|
|
449
|
+
def referrers(self, target_id, predicate=None, limit=25, cursor=None):
|
|
450
|
+
return async_to_sync(self.repository.referrers)(
|
|
451
|
+
target_id, predicate or AllOf(()), PageRequest(limit=limit, cursor=cursor),
|
|
452
|
+
)
|
|
453
|
+
|
|
454
|
+
def referrer_ids(self, target_id, predicate=None):
|
|
455
|
+
return [entity.entity_id for entity in self.referrers(target_id, predicate).entities]
|
|
456
|
+
|
|
457
|
+
def source_of(self, target, owner=None):
|
|
458
|
+
source = self.create(owner=owner)
|
|
459
|
+
source.set_attribute('spec', target.entity_id)
|
|
460
|
+
self.save(source)
|
|
461
|
+
return source
|
|
462
|
+
|
|
463
|
+
def test_referrers_finds_every_source(self):
|
|
464
|
+
target = self.create()
|
|
465
|
+
sources = {self.source_of(target).entity_id for _ in range(3)}
|
|
466
|
+
self.create()
|
|
467
|
+
|
|
468
|
+
self.assertEqual(self.referrer_ids(target.entity_id), sorted(sources))
|
|
469
|
+
|
|
470
|
+
def test_referrers_pages_in_entity_id_order(self):
|
|
471
|
+
target = self.create()
|
|
472
|
+
expected = sorted(self.source_of(target).entity_id for _ in range(7))
|
|
473
|
+
|
|
474
|
+
seen, cursor, sizes = [], None, []
|
|
475
|
+
while True:
|
|
476
|
+
page = self.referrers(target.entity_id, limit=3, cursor=cursor)
|
|
477
|
+
sizes.append(len(page.entities))
|
|
478
|
+
seen += [entity.entity_id for entity in page.entities]
|
|
479
|
+
if page.next_cursor is None:
|
|
480
|
+
break
|
|
481
|
+
self.assertEqual(page.next_cursor, seen[-1])
|
|
482
|
+
cursor = page.next_cursor
|
|
483
|
+
|
|
484
|
+
self.assertEqual(sizes, [3, 3, 1])
|
|
485
|
+
self.assertEqual(seen, expected)
|
|
486
|
+
|
|
487
|
+
def test_referrers_applies_the_predicate_before_paging(self):
|
|
488
|
+
target = self.create()
|
|
489
|
+
admitted = sorted(self.source_of(target, owner='u1').entity_id for _ in range(5))
|
|
490
|
+
for _ in range(5):
|
|
491
|
+
self.source_of(target, owner='u2')
|
|
492
|
+
|
|
493
|
+
seen, cursor, sizes = [], None, []
|
|
494
|
+
while True:
|
|
495
|
+
page = self.referrers(target.entity_id, Equal('owner', 'u1'), limit=2, cursor=cursor)
|
|
496
|
+
sizes.append(len(page.entities))
|
|
497
|
+
seen += [entity.entity_id for entity in page.entities]
|
|
498
|
+
if page.next_cursor is None:
|
|
499
|
+
break
|
|
500
|
+
cursor = page.next_cursor
|
|
501
|
+
|
|
502
|
+
self.assertEqual(sizes, [2, 2, 1])
|
|
503
|
+
self.assertEqual(seen, admitted)
|
|
504
|
+
|
|
505
|
+
def test_a_source_that_drops_the_reference_leaves(self):
|
|
506
|
+
target = self.create()
|
|
507
|
+
source = self.source_of(target)
|
|
508
|
+
source.remove_attribute('spec')
|
|
509
|
+
self.save(source)
|
|
510
|
+
|
|
511
|
+
self.assertEqual(self.referrer_ids(target.entity_id), [])
|
|
512
|
+
|
|
513
|
+
def test_a_deleted_source_leaves(self):
|
|
514
|
+
target = self.create()
|
|
515
|
+
source = self.source_of(target)
|
|
516
|
+
async_to_sync(self.repository.delete_many)([source.entity_id])
|
|
517
|
+
|
|
518
|
+
self.assertEqual(self.referrer_ids(target.entity_id), [])
|
|
519
|
+
|
|
520
|
+
def test_a_deleted_target_keeps_its_referrers(self):
|
|
521
|
+
target = self.create()
|
|
522
|
+
source = self.source_of(target)
|
|
523
|
+
async_to_sync(self.repository.delete_many)([target.entity_id])
|
|
524
|
+
|
|
525
|
+
self.assertEqual(self.referrer_ids(target.entity_id), [source.entity_id])
|
|
526
|
+
|
|
527
|
+
def test_a_target_held_in_two_fields_is_one_referrer(self):
|
|
528
|
+
target = self.create()
|
|
529
|
+
child = self.create(parent=target)
|
|
530
|
+
child.set_attribute('spec', target.entity_id)
|
|
531
|
+
self.save(child)
|
|
532
|
+
|
|
533
|
+
self.assertEqual(self.referrer_ids(target.entity_id), [child.entity_id])
|
|
534
|
+
|
|
535
|
+
def test_an_unreferenced_target_has_no_referrers(self):
|
|
536
|
+
page = self.referrers(self.create().entity_id)
|
|
537
|
+
|
|
538
|
+
self.assertEqual((page.entities, page.next_cursor), ([], None))
|
|
539
|
+
|
|
540
|
+
# --- predicates -----------------------------------------------------
|
|
541
|
+
|
|
542
|
+
def test_equal_and_in_values(self):
|
|
543
|
+
owned = self.create(owner='u1')
|
|
544
|
+
self.create(owner='u2')
|
|
545
|
+
|
|
546
|
+
self.assertEqual(self.list_ids(Equal('owner', 'u1')), {owned.entity_id})
|
|
547
|
+
self.assertEqual(self.list_ids(InValues('entity_id', [owned.entity_id])), {owned.entity_id})
|
|
548
|
+
self.assertEqual(self.list_ids(InValues('entity_id', [])), set())
|
|
549
|
+
|
|
550
|
+
def test_equal_none_is_a_null_check(self):
|
|
551
|
+
root = self.create()
|
|
552
|
+
child = self.create(parent=root)
|
|
553
|
+
|
|
554
|
+
roots = self.list_ids(Equal('parent_id', None))
|
|
555
|
+
self.assertIn(root.entity_id, roots)
|
|
556
|
+
self.assertNotIn(child.entity_id, roots)
|
|
557
|
+
|
|
558
|
+
def test_contains_value_on_a_roster(self):
|
|
559
|
+
group = self.create(Group, member_ids=['u1', 'u2'])
|
|
560
|
+
self.create(Group, member_ids=['u3'])
|
|
561
|
+
|
|
562
|
+
groups = Equal('entity_type_id', Group.get_entity_type_id())
|
|
563
|
+
self.assertEqual(self.list_ids(AllOf((groups, ContainsValue('member_ids', 'u2')))), {group.entity_id})
|
|
564
|
+
self.assertEqual(self.list_ids(AllOf((groups, ContainsValue('member_ids', 'nobody')))), set())
|
|
565
|
+
|
|
566
|
+
def test_contains_any_on_ancestors(self):
|
|
567
|
+
root, middle, leaf = self.chain(2)
|
|
568
|
+
unrelated = self.create()
|
|
569
|
+
|
|
570
|
+
matched = self.list_ids(ContainsAny('ancestor_ids', [root.entity_id]))
|
|
571
|
+
self.assertEqual(matched, {middle.entity_id, leaf.entity_id})
|
|
572
|
+
self.assertNotIn(unrelated.entity_id, matched)
|
|
573
|
+
self.assertEqual(self.list_ids(ContainsAny('ancestor_ids', [])), set())
|
|
574
|
+
|
|
575
|
+
def test_grant_columns_are_filterable(self):
|
|
576
|
+
resource = self.create(Group)
|
|
577
|
+
grant = self.create(Grant, parent=resource, subject_id='u1', role_name='viewer')
|
|
578
|
+
self.create(Grant, parent=resource, subject_id='u2', role_name='admin')
|
|
579
|
+
|
|
580
|
+
self.assertEqual(self.list_ids(Equal('subject_id', 'u1')), {grant.entity_id})
|
|
581
|
+
self.assertEqual(self.list_ids(InValues('subject_id', ['u1'])), {grant.entity_id})
|
|
582
|
+
# A non-grant row has no subject_id at all, so it never matches.
|
|
583
|
+
self.assertNotIn(resource.entity_id, self.list_ids(InValues('subject_id', ['u1', 'u2'])))
|
|
584
|
+
|
|
585
|
+
def test_null_on_a_child_column_does_not_leak_other_types(self):
|
|
586
|
+
"""The `LEFT OUTER JOIN` null-check hazard: a child-table column
|
|
587
|
+
being unset on a row of a *different* type - which has no child row
|
|
588
|
+
at all, so the join itself is null - must not satisfy
|
|
589
|
+
`Equal(child_column, None)`."""
|
|
590
|
+
with_username = self.create(User, username='alice')
|
|
591
|
+
without_username = self.create(User)
|
|
592
|
+
other = self.create()
|
|
593
|
+
|
|
594
|
+
matched = self.list_ids(Equal('username', None))
|
|
595
|
+
self.assertEqual(matched, {without_username.entity_id})
|
|
596
|
+
self.assertNotIn(with_username.entity_id, matched)
|
|
597
|
+
self.assertNotIn(other.entity_id, matched)
|
|
598
|
+
|
|
599
|
+
def test_nested_composition(self):
|
|
600
|
+
root = self.create()
|
|
601
|
+
mine = self.create(parent=root, owner='u1')
|
|
602
|
+
self.create(parent=root, owner='u2')
|
|
603
|
+
|
|
604
|
+
matched = self.list_ids(AllOf((
|
|
605
|
+
ContainsAny('ancestor_ids', [root.entity_id]),
|
|
606
|
+
Equal('owner', 'u1'),
|
|
607
|
+
)))
|
|
608
|
+
self.assertEqual(matched, {mine.entity_id})
|
|
609
|
+
|
|
610
|
+
# --- paging ---------------------------------------------------------
|
|
611
|
+
|
|
612
|
+
def test_paging_walks_the_whole_result_in_id_order(self):
|
|
613
|
+
root = self.create()
|
|
614
|
+
expected = sorted(self.create(parent=root).entity_id for _ in range(5))
|
|
615
|
+
|
|
616
|
+
predicate = ContainsAny('ancestor_ids', [root.entity_id])
|
|
617
|
+
seen, cursor = [], None
|
|
618
|
+
for _ in range(3):
|
|
619
|
+
page = async_to_sync(self.repository.list_entities)(
|
|
620
|
+
EntityQuery(predicate, PageRequest(limit=2, cursor=cursor))
|
|
621
|
+
)
|
|
622
|
+
seen.extend(entity.entity_id for entity in page.entities)
|
|
623
|
+
cursor = page.next_cursor
|
|
624
|
+
if cursor is None:
|
|
625
|
+
break
|
|
626
|
+
|
|
627
|
+
self.assertEqual(seen, expected)
|
|
628
|
+
self.assertIsNone(cursor)
|
|
629
|
+
|
|
630
|
+
def test_a_full_final_page_reports_no_cursor(self):
|
|
631
|
+
root = self.create()
|
|
632
|
+
for _ in range(2):
|
|
633
|
+
self.create(parent=root)
|
|
634
|
+
|
|
635
|
+
page = async_to_sync(self.repository.list_entities)(
|
|
636
|
+
EntityQuery(ContainsAny('ancestor_ids', [root.entity_id]), PageRequest(limit=2))
|
|
637
|
+
)
|
|
638
|
+
self.assertEqual(len(page.entities), 2)
|
|
639
|
+
self.assertIsNone(page.next_cursor)
|
|
640
|
+
|
|
641
|
+
# --- delete ---------------------------------------------------------
|
|
642
|
+
|
|
643
|
+
# --- dependent_ids --------------------------------------------------
|
|
644
|
+
|
|
645
|
+
def dependent_ids(self, entity_cls, field, target_ids):
|
|
646
|
+
return async_to_sync(self.repository.dependent_ids)(
|
|
647
|
+
frozenset({entity_cls.get_entity_type_id()}), field, set(target_ids),
|
|
648
|
+
)
|
|
649
|
+
|
|
650
|
+
def test_dependent_ids_matches_a_string_column(self):
|
|
651
|
+
target, other = self.create(), self.create()
|
|
652
|
+
resource = self.create(Group)
|
|
653
|
+
holder = self.create(Grant, parent=resource, subject_id=target.entity_id, role_name='viewer')
|
|
654
|
+
self.create(Grant, parent=resource, subject_id=other.entity_id, role_name='viewer')
|
|
655
|
+
|
|
656
|
+
self.assertEqual(self.dependent_ids(Grant, 'subject_id', [target.entity_id]), {holder.entity_id})
|
|
657
|
+
self.assertEqual(self.dependent_ids(Grant, 'subject_id', []), set())
|
|
658
|
+
|
|
659
|
+
def test_dependent_ids_matches_a_list_column(self):
|
|
660
|
+
target, other = self.create(), self.create()
|
|
661
|
+
holder = self.create(Group, member_ids=[other.entity_id, target.entity_id])
|
|
662
|
+
self.create(Group, member_ids=[other.entity_id])
|
|
663
|
+
|
|
664
|
+
self.assertEqual(self.dependent_ids(Group, 'member_ids', [target.entity_id]), {holder.entity_id})
|
|
665
|
+
|
|
666
|
+
def test_dependent_ids_is_limited_to_the_given_types(self):
|
|
667
|
+
target = self.create()
|
|
668
|
+
resource = self.create(Group)
|
|
669
|
+
self.create(Grant, parent=resource, subject_id=target.entity_id, role_name='viewer')
|
|
670
|
+
|
|
671
|
+
self.assertEqual(self.dependent_ids(Group, 'member_ids', [target.entity_id]), set())
|
|
672
|
+
|
|
673
|
+
def test_delete_reports_what_the_rows_were(self):
|
|
674
|
+
root = self.create()
|
|
675
|
+
child = self.create(parent=root, owner='u1')
|
|
676
|
+
|
|
677
|
+
receipt = async_to_sync(self.repository.delete_many)([child.entity_id, self.new().entity_id])
|
|
678
|
+
|
|
679
|
+
self.assertEqual(receipt.entity_ids, [child.entity_id])
|
|
680
|
+
deleted = receipt.entities[0]
|
|
681
|
+
self.assertEqual(deleted.parent_id, root.entity_id)
|
|
682
|
+
self.assertEqual(deleted.owner, 'u1')
|
|
683
|
+
self.assertEqual(deleted.entity_type_id, 'graphcore.Entity')
|
|
684
|
+
self.assertEqual(deleted.ancestor_ids, (root.entity_id,))
|
|
685
|
+
self.assertIsNone(self.load(child.entity_id))
|
|
686
|
+
|
|
687
|
+
def test_deleting_nothing_is_a_no_op(self):
|
|
688
|
+
self.assertEqual(async_to_sync(self.repository.delete_many)([]).entities, ())
|