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,51 @@
|
|
|
1
|
+
"""Reference implementation of the query vocabulary, in pure Python.
|
|
2
|
+
|
|
3
|
+
Test-only. Production never filters in memory - a predicate is pushed into
|
|
4
|
+
storage so filtering happens before the limit - so this exists to say what
|
|
5
|
+
each node *means*, in the shortest form that can be read and tested.
|
|
6
|
+
``MemoryEntityRepository`` runs on it, and ``contract.py`` holds every
|
|
7
|
+
backend to it: a compiler that disagrees with `matches` fails there.
|
|
8
|
+
|
|
9
|
+
`record` must be a read-side record (what a repository reads back), never a
|
|
10
|
+
bare `Entity.to_dict()` - that omits the derived `ancestor_ids`, so
|
|
11
|
+
predicates over it would silently evaluate false.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from typing import Mapping
|
|
16
|
+
|
|
17
|
+
from graphcore.errors import UnsupportedQuery
|
|
18
|
+
from graphcore.types.query import (
|
|
19
|
+
AllOf, AnyOf, ContainsAny, ContainsValue, Equal, InValues, Predicate,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _collection(record: Mapping, field: str) -> tuple:
|
|
24
|
+
value = record.get(field)
|
|
25
|
+
if value is None or isinstance(value, (str, bytes)):
|
|
26
|
+
return ()
|
|
27
|
+
try:
|
|
28
|
+
return tuple(value)
|
|
29
|
+
except TypeError:
|
|
30
|
+
return ()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
_HANDLERS = {
|
|
34
|
+
# Presence is pinned explicitly, not folded into `.get(field)` returning
|
|
35
|
+
# `None` for a missing key - the Django compiler pins the same meaning
|
|
36
|
+
# for a child-table column (see the null-check hazard in `compiler.py`),
|
|
37
|
+
# so a type without the field never matches `Equal(field, None)`.
|
|
38
|
+
Equal: lambda node, record: node.field in record and record[node.field] == node.value,
|
|
39
|
+
InValues: lambda node, record: node.field in record and record[node.field] in node.values,
|
|
40
|
+
ContainsValue: lambda node, record: node.value in _collection(record, node.field),
|
|
41
|
+
ContainsAny: lambda node, record: bool(set(_collection(record, node.field)) & node.values),
|
|
42
|
+
AllOf: lambda node, record: all(matches(child, record) for child in node.predicates),
|
|
43
|
+
AnyOf: lambda node, record: any(matches(child, record) for child in node.predicates),
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def matches(predicate: Predicate, record: Mapping) -> bool:
|
|
48
|
+
handler = _HANDLERS.get(type(predicate))
|
|
49
|
+
if handler is None:
|
|
50
|
+
raise UnsupportedQuery(f"Unsupported predicate node: {type(predicate).__name__}")
|
|
51
|
+
return handler(predicate, record)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Persisted entities for tests that deliberately bypass `EntityService`.
|
|
2
|
+
|
|
3
|
+
Writes straight through the repository: no permission check, no cache, no
|
|
4
|
+
broadcast, no access hooks - but the `ancestor_ids` invariant is maintained,
|
|
5
|
+
because that belongs to the repository.
|
|
6
|
+
"""
|
|
7
|
+
from asgiref.sync import async_to_sync
|
|
8
|
+
|
|
9
|
+
from graphcore.persistence.django.repository import DjangoEntityRepository
|
|
10
|
+
from graphcore.types.entity_id import new_entity_id
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_repository(registry):
|
|
14
|
+
return DjangoEntityRepository(registry.entities)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
async def apersist(entity, registry):
|
|
18
|
+
await test_repository(registry).save(entity)
|
|
19
|
+
return entity
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def persist(entity, registry):
|
|
23
|
+
async_to_sync(test_repository(registry).save)(entity)
|
|
24
|
+
return entity
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def make(entity_cls, registry, **kwargs):
|
|
28
|
+
"""An unsaved entity carrying the test store's own identity - for tests
|
|
29
|
+
with no `EntityService` in scope to mint one."""
|
|
30
|
+
return entity_cls(
|
|
31
|
+
entity_id=new_entity_id(test_repository(registry).backend_id()), **kwargs
|
|
32
|
+
)
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"""An `EntityRepository` over a dict of records.
|
|
2
|
+
|
|
3
|
+
Backend-free: a record is `Entity.to_dict()` plus the fields the repository
|
|
4
|
+
itself owns (`ancestor_ids`, `version`), and `evaluate.matches` stands in
|
|
5
|
+
for a compiler - so `graph/` and `auth/authz` tests can exercise real
|
|
6
|
+
repository semantics without SQL, and the shared contract suite can pin
|
|
7
|
+
both implementations to one behaviour. There is no mapper and no storage
|
|
8
|
+
split: the nested record is the entity's complete neutral state.
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import itertools
|
|
13
|
+
from typing import Optional
|
|
14
|
+
|
|
15
|
+
from graphcore.domain.entity.Entity import Entity
|
|
16
|
+
from graphcore.domain.entity.service_account.ServiceAccount import ServiceAccount
|
|
17
|
+
from graphcore.domain.entity.user.User import User
|
|
18
|
+
from graphcore.domain.entity.view.View import View
|
|
19
|
+
from graphcore.errors import ConcurrentModificationError, DuplicateEntity, PreconditionFailed
|
|
20
|
+
from graphcore.persistence.tests.evaluate import matches
|
|
21
|
+
from graphcore.persistence.repository import (
|
|
22
|
+
NOT_LOADED, DeleteReceipt, DeletedEntity, EntityRepository, Precondition, SaveReceipt,
|
|
23
|
+
project_reference_ids,
|
|
24
|
+
)
|
|
25
|
+
from graphcore.tests.ids import FIXTURE_BACKEND
|
|
26
|
+
from graphcore.types.entities import EntityPage
|
|
27
|
+
from graphcore.types.query import ContainsValue, EntityQuery, PageRequest, Predicate
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# Mirrors the Django models' unique constraints; a null in any field exempts the row.
|
|
31
|
+
UNIQUE_FIELDS = {
|
|
32
|
+
User: (('issuer', 'subject'),),
|
|
33
|
+
ServiceAccount: (('client_id',),),
|
|
34
|
+
View: (('position_key',),),
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class MemoryEntityRepository(EntityRepository):
|
|
39
|
+
def __init__(self, entity_directory, backend_id: str = FIXTURE_BACKEND) -> None:
|
|
40
|
+
self.entity_directory = entity_directory
|
|
41
|
+
self._records: dict[str, dict] = {}
|
|
42
|
+
self._sequence = itertools.count()
|
|
43
|
+
self._seq: dict[str, int] = {}
|
|
44
|
+
self._backend_id = backend_id
|
|
45
|
+
|
|
46
|
+
def backend_id(self) -> str:
|
|
47
|
+
return self._backend_id
|
|
48
|
+
|
|
49
|
+
# --- hydration ------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
def _entity_class(self, record: dict):
|
|
52
|
+
return self.entity_directory.get_entity_class(record.get('entity_type_id'))
|
|
53
|
+
|
|
54
|
+
def _hydrate(self, record: dict) -> Optional[Entity]:
|
|
55
|
+
entity_cls = self._entity_class(record)
|
|
56
|
+
if entity_cls is None:
|
|
57
|
+
return None
|
|
58
|
+
entity = entity_cls.from_dict(record)
|
|
59
|
+
entity._ancestor_ids = tuple(str(x) for x in (record.get('ancestor_ids') or ()))
|
|
60
|
+
entity._reference_ids = tuple(str(x) for x in (record.get('reference_ids') or ()))
|
|
61
|
+
return entity
|
|
62
|
+
|
|
63
|
+
def _attach_children(self, entities: dict) -> None:
|
|
64
|
+
if not entities:
|
|
65
|
+
return
|
|
66
|
+
children = sorted(
|
|
67
|
+
(r for r in self._records.values() if str(r.get('parent_id') or '') in entities),
|
|
68
|
+
key=lambda r: (r.get('sibling_position') or 0.0, self._seq[r['entity_id']], r['entity_id']),
|
|
69
|
+
)
|
|
70
|
+
for record in children:
|
|
71
|
+
parent = entities[str(record['parent_id'])]
|
|
72
|
+
parent._children_ids = parent._children_ids + (record['entity_id'],)
|
|
73
|
+
|
|
74
|
+
# --- reads ----------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
async def load(self, entity_id: str) -> Optional[Entity]:
|
|
77
|
+
return (await self.load_many([entity_id])).get(str(entity_id))
|
|
78
|
+
|
|
79
|
+
async def load_many(self, entity_ids) -> dict:
|
|
80
|
+
entities = {}
|
|
81
|
+
for entity_id in {str(eid) for eid in entity_ids}:
|
|
82
|
+
record = self._records.get(entity_id)
|
|
83
|
+
if record is None:
|
|
84
|
+
continue
|
|
85
|
+
entity = self._hydrate(record)
|
|
86
|
+
if entity is not None:
|
|
87
|
+
entities[entity_id] = entity
|
|
88
|
+
self._attach_children(entities)
|
|
89
|
+
return entities
|
|
90
|
+
|
|
91
|
+
async def versions(self, entity_ids) -> dict:
|
|
92
|
+
return {
|
|
93
|
+
str(eid): self._records[str(eid)]['version']
|
|
94
|
+
for eid in entity_ids if str(eid) in self._records
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async def list_entities(self, query: EntityQuery) -> EntityPage:
|
|
98
|
+
return await self._page(self._matching_ids(query.predicate), query.page)
|
|
99
|
+
|
|
100
|
+
async def referrers(self, target_id: str, predicate: Predicate, page: PageRequest) -> EntityPage:
|
|
101
|
+
ids = {
|
|
102
|
+
eid for eid in self._matching_ids(predicate)
|
|
103
|
+
if str(target_id) in self._records[eid]['reference_ids']
|
|
104
|
+
}
|
|
105
|
+
return await self._page(ids, page)
|
|
106
|
+
|
|
107
|
+
async def _page(self, ids, page: PageRequest) -> EntityPage:
|
|
108
|
+
ids = sorted(ids)
|
|
109
|
+
if page.cursor:
|
|
110
|
+
ids = [eid for eid in ids if eid > page.cursor]
|
|
111
|
+
probed = ids[:page.limit + 1]
|
|
112
|
+
has_more = len(probed) > page.limit
|
|
113
|
+
page_ids = probed[:page.limit]
|
|
114
|
+
hydrated = await self.load_many(page_ids)
|
|
115
|
+
return EntityPage(
|
|
116
|
+
entities=[hydrated[eid] for eid in page_ids if eid in hydrated],
|
|
117
|
+
next_cursor=page_ids[-1] if has_more and page_ids else None,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
def _matching_ids(self, predicate: Predicate) -> set:
|
|
121
|
+
return {eid for eid, record in self._records.items() if matches(predicate, record)}
|
|
122
|
+
|
|
123
|
+
async def list_ids(self, predicate: Predicate) -> set:
|
|
124
|
+
return self._matching_ids(predicate)
|
|
125
|
+
|
|
126
|
+
async def descendant_ids(self, root_id: str) -> set:
|
|
127
|
+
return await self.list_ids(ContainsValue('ancestor_ids', str(root_id)))
|
|
128
|
+
|
|
129
|
+
async def dependent_ids(self, type_ids: frozenset, field: str, target_ids: set) -> set:
|
|
130
|
+
targets = {str(t) for t in target_ids}
|
|
131
|
+
hits = set()
|
|
132
|
+
for entity_id, record in self._records.items():
|
|
133
|
+
if record.get('entity_type_id') not in type_ids:
|
|
134
|
+
continue
|
|
135
|
+
value = record.get(field)
|
|
136
|
+
held = {value} if isinstance(value, str) else set(value or ())
|
|
137
|
+
if held & targets:
|
|
138
|
+
hits.add(entity_id)
|
|
139
|
+
return hits
|
|
140
|
+
|
|
141
|
+
# --- writes ---------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
def _ancestors_for(self, parent_id: Optional[str]) -> list:
|
|
144
|
+
if not parent_id:
|
|
145
|
+
return []
|
|
146
|
+
parent = self._records.get(str(parent_id))
|
|
147
|
+
return (list(parent['ancestor_ids']) if parent else []) + [str(parent_id)]
|
|
148
|
+
|
|
149
|
+
def _hold(self, precondition: Optional[Precondition]) -> None:
|
|
150
|
+
if precondition is None:
|
|
151
|
+
return
|
|
152
|
+
record = self._records.get(str(precondition.entity_id))
|
|
153
|
+
if record is None or not matches(precondition.where, record):
|
|
154
|
+
raise PreconditionFailed(
|
|
155
|
+
f"Precondition on {precondition.entity_id} does not hold",
|
|
156
|
+
entity_id=precondition.entity_id,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
async def save(
|
|
160
|
+
self, entity: Entity, precondition: Optional[Precondition] = None, previous=NOT_LOADED,
|
|
161
|
+
) -> SaveReceipt:
|
|
162
|
+
self._hold(precondition)
|
|
163
|
+
entity_id = str(entity.entity_id)
|
|
164
|
+
existing = self._records.get(entity_id)
|
|
165
|
+
if previous is NOT_LOADED or (previous is not None and previous.version != entity.version):
|
|
166
|
+
previous = self._hydrate(existing) if existing is not None else None
|
|
167
|
+
|
|
168
|
+
reparented = existing is not None and (existing.get('parent_id') or None) != entity.parent_id
|
|
169
|
+
if existing is None or reparented:
|
|
170
|
+
ancestor_ids = self._ancestors_for(entity.parent_id)
|
|
171
|
+
else:
|
|
172
|
+
ancestor_ids = list(existing['ancestor_ids'])
|
|
173
|
+
|
|
174
|
+
record = entity.to_dict()
|
|
175
|
+
record['reference_ids'] = project_reference_ids(record)
|
|
176
|
+
record['ancestor_ids'] = ancestor_ids
|
|
177
|
+
|
|
178
|
+
if existing is None:
|
|
179
|
+
self._check_unique(entity, record)
|
|
180
|
+
record['version'] = 1
|
|
181
|
+
self._seq[entity_id] = next(self._sequence)
|
|
182
|
+
else:
|
|
183
|
+
if existing['version'] != entity.version:
|
|
184
|
+
raise ConcurrentModificationError(
|
|
185
|
+
f"Entity {entity_id} was modified by another process. "
|
|
186
|
+
f"Version mismatch during save: expected {entity.version}, "
|
|
187
|
+
f"found {existing['version']}",
|
|
188
|
+
entity_id=entity_id,
|
|
189
|
+
expected_version=entity.version,
|
|
190
|
+
found_version=existing['version'],
|
|
191
|
+
)
|
|
192
|
+
record['version'] = entity.version + 1
|
|
193
|
+
|
|
194
|
+
moved_descendant_ids = frozenset()
|
|
195
|
+
if reparented:
|
|
196
|
+
moved_descendant_ids = self._rewrite_subtree(
|
|
197
|
+
entity_id, list(existing['ancestor_ids']), ancestor_ids,
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
self._records[entity_id] = record
|
|
201
|
+
entity.version = record['version']
|
|
202
|
+
entity._ancestor_ids = tuple(ancestor_ids)
|
|
203
|
+
entity._reference_ids = tuple(record['reference_ids'])
|
|
204
|
+
|
|
205
|
+
return SaveReceipt(
|
|
206
|
+
entity=entity,
|
|
207
|
+
previous=previous,
|
|
208
|
+
created=existing is None,
|
|
209
|
+
touched_parent_ids=frozenset(self._touched_parent_ids(existing, entity)),
|
|
210
|
+
moved_descendant_ids=moved_descendant_ids,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
def _check_unique(self, entity: Entity, record: dict) -> None:
|
|
214
|
+
for fields in UNIQUE_FIELDS.get(type(entity), ()):
|
|
215
|
+
values = tuple(record.get(f) for f in fields)
|
|
216
|
+
if None in values:
|
|
217
|
+
continue
|
|
218
|
+
for other in self._records.values():
|
|
219
|
+
if (other.get('entity_type_id') == record.get('entity_type_id')
|
|
220
|
+
and tuple(other.get(f) for f in fields) == values):
|
|
221
|
+
raise DuplicateEntity(
|
|
222
|
+
f"Entity {entity.entity_id} duplicates {fields} held by {other['entity_id']}",
|
|
223
|
+
entity_id=str(entity.entity_id),
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
def _rewrite_subtree(self, root_id: str, old_ancestors: list, new_ancestors: list) -> frozenset:
|
|
227
|
+
old_prefix = old_ancestors + [root_id]
|
|
228
|
+
new_prefix = new_ancestors + [root_id]
|
|
229
|
+
moved = set()
|
|
230
|
+
for entity_id, record in self._records.items():
|
|
231
|
+
if root_id in (record['ancestor_ids'] or []):
|
|
232
|
+
record['ancestor_ids'] = new_prefix + list(record['ancestor_ids'])[len(old_prefix):]
|
|
233
|
+
moved.add(entity_id)
|
|
234
|
+
return frozenset(moved)
|
|
235
|
+
|
|
236
|
+
@staticmethod
|
|
237
|
+
def _touched_parent_ids(existing: Optional[dict], entity: Entity) -> set:
|
|
238
|
+
new_parent_id = entity.parent_id
|
|
239
|
+
if existing is None:
|
|
240
|
+
return {new_parent_id} if new_parent_id else set()
|
|
241
|
+
old_parent_id = str(existing['parent_id']) if existing.get('parent_id') else None
|
|
242
|
+
if old_parent_id != new_parent_id:
|
|
243
|
+
return {pid for pid in (old_parent_id, new_parent_id) if pid}
|
|
244
|
+
if new_parent_id and existing.get('sibling_position') != entity.sibling_position:
|
|
245
|
+
return {new_parent_id}
|
|
246
|
+
return set()
|
|
247
|
+
|
|
248
|
+
async def delete_many(self, entity_ids, precondition: Optional[Precondition] = None) -> DeleteReceipt:
|
|
249
|
+
self._hold(precondition)
|
|
250
|
+
deleted = []
|
|
251
|
+
for entity_id in dict.fromkeys(str(eid) for eid in entity_ids):
|
|
252
|
+
record = self._records.pop(entity_id, None)
|
|
253
|
+
self._seq.pop(entity_id, None)
|
|
254
|
+
if record is None:
|
|
255
|
+
continue
|
|
256
|
+
deleted.append(DeletedEntity(
|
|
257
|
+
entity_id=entity_id,
|
|
258
|
+
parent_id=str(record['parent_id']) if record.get('parent_id') else None,
|
|
259
|
+
entity_type_id=record.get('entity_type_id'),
|
|
260
|
+
owner=record.get('owner'),
|
|
261
|
+
ancestor_ids=tuple(record.get('ancestor_ids') or ()),
|
|
262
|
+
))
|
|
263
|
+
return DeleteReceipt(entities=tuple(deleted))
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from django.test import SimpleTestCase
|
|
2
|
+
|
|
3
|
+
from graphcore.persistence.tests.contract import EntityRepositoryContract
|
|
4
|
+
from graphcore.persistence.tests.memory import MemoryEntityRepository
|
|
5
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MemoryEntityRepositoryTests(EntityRepositoryContract, SimpleTestCase):
|
|
9
|
+
def make_repository(self):
|
|
10
|
+
return MemoryEntityRepository(DEFAULT_REGISTRY.entities)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""Query vocabulary semantics: node shapes and what `matches` decides."""
|
|
2
|
+
from dataclasses import FrozenInstanceError
|
|
3
|
+
|
|
4
|
+
from django.test import SimpleTestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.errors import InvalidQuery, UnsupportedQuery
|
|
7
|
+
from graphcore.persistence.tests.evaluate import matches
|
|
8
|
+
from graphcore.types.entities import EntityFilter
|
|
9
|
+
from graphcore.types.query import (
|
|
10
|
+
AllOf, AnyOf, ContainsAny, ContainsValue, Equal, EntityQuery, InValues,
|
|
11
|
+
PageRequest, Predicate,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class QueryNodeTests(SimpleTestCase):
|
|
16
|
+
def test_nodes_are_frozen_and_hashable(self):
|
|
17
|
+
nodes = [
|
|
18
|
+
Equal('owner', 'u1'),
|
|
19
|
+
InValues('entity_type_id', ['a', 'b']),
|
|
20
|
+
ContainsValue('member_ids', 'u1'),
|
|
21
|
+
ContainsAny('ancestor_ids', ['s1']),
|
|
22
|
+
AllOf([Equal('owner', 'u1')]),
|
|
23
|
+
AnyOf([Equal('owner', 'u1')]),
|
|
24
|
+
]
|
|
25
|
+
for node in nodes:
|
|
26
|
+
with self.subTest(node=type(node).__name__):
|
|
27
|
+
self.assertIsInstance(node, Predicate)
|
|
28
|
+
self.assertEqual(hash(node), hash(node))
|
|
29
|
+
with self.assertRaises(FrozenInstanceError):
|
|
30
|
+
node.field = 'other'
|
|
31
|
+
|
|
32
|
+
def test_collections_are_coerced_to_hashable_forms(self):
|
|
33
|
+
self.assertEqual(InValues('f', ['a', 'a', 'b']).values, frozenset({'a', 'b'}))
|
|
34
|
+
self.assertEqual(ContainsAny('f', {'a'}).values, frozenset({'a'}))
|
|
35
|
+
self.assertEqual(AllOf([Equal('f', 1)]).predicates, (Equal('f', 1),))
|
|
36
|
+
self.assertEqual(AnyOf([Equal('f', 1)]).predicates, (Equal('f', 1),))
|
|
37
|
+
|
|
38
|
+
def test_page_request_requires_a_positive_limit(self):
|
|
39
|
+
for limit in (0, -1):
|
|
40
|
+
with self.subTest(limit=limit), self.assertRaises(InvalidQuery):
|
|
41
|
+
PageRequest(limit=limit)
|
|
42
|
+
self.assertEqual(PageRequest(limit=25).cursor, None)
|
|
43
|
+
|
|
44
|
+
def test_from_filter_shapes(self):
|
|
45
|
+
self.assertEqual(
|
|
46
|
+
Predicate.from_filter(EntityFilter(type_ids=frozenset({'t1'}))),
|
|
47
|
+
AllOf((InValues('entity_type_id', frozenset({'t1'})),)),
|
|
48
|
+
)
|
|
49
|
+
self.assertEqual(
|
|
50
|
+
Predicate.from_filter(EntityFilter(type_ids=frozenset({'t1'}), parent_id='p1')),
|
|
51
|
+
AllOf((InValues('entity_type_id', frozenset({'t1'})), Equal('parent_id', 'p1'))),
|
|
52
|
+
)
|
|
53
|
+
self.assertEqual(
|
|
54
|
+
Predicate.from_filter(EntityFilter(
|
|
55
|
+
type_ids=frozenset({'t1'}), parent_id='p1', where=Equal('slug', 'x'),
|
|
56
|
+
)),
|
|
57
|
+
AllOf((
|
|
58
|
+
InValues('entity_type_id', frozenset({'t1'})),
|
|
59
|
+
Equal('parent_id', 'p1'),
|
|
60
|
+
Equal('slug', 'x'),
|
|
61
|
+
)),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
def test_entity_query_carries_predicate_and_page(self):
|
|
65
|
+
query = EntityQuery(Equal('owner', 'u1'), PageRequest(limit=10, cursor='c'))
|
|
66
|
+
self.assertEqual(query.predicate, Equal('owner', 'u1'))
|
|
67
|
+
self.assertEqual((query.page.limit, query.page.cursor), (10, 'c'))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class MatchesTests(SimpleTestCase):
|
|
71
|
+
RECORD = {
|
|
72
|
+
'entity_id': 'e1',
|
|
73
|
+
'entity_type_id': 'graphcore.Entity',
|
|
74
|
+
'parent_id': None,
|
|
75
|
+
'owner': 'u1',
|
|
76
|
+
'member_ids': ['u1', 'u2'],
|
|
77
|
+
'ancestor_ids': ['s1', 's2'],
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
def test_equal_including_explicit_null(self):
|
|
81
|
+
self.assertTrue(matches(Equal('owner', 'u1'), self.RECORD))
|
|
82
|
+
self.assertFalse(matches(Equal('owner', 'u2'), self.RECORD))
|
|
83
|
+
self.assertTrue(matches(Equal('parent_id', None), self.RECORD))
|
|
84
|
+
self.assertFalse(matches(Equal('parent_id', 'p1'), self.RECORD))
|
|
85
|
+
|
|
86
|
+
def test_equal_on_a_missing_field_never_matches(self):
|
|
87
|
+
"""The null-check hazard, pinned the same way the Django compiler
|
|
88
|
+
pins it: a record that doesn't carry the field at all - a type
|
|
89
|
+
without that column - never matches `Equal(field, None)`, only a
|
|
90
|
+
record that carries the field with an explicit `None`."""
|
|
91
|
+
self.assertFalse(matches(Equal('subject_id', None), self.RECORD))
|
|
92
|
+
self.assertFalse(matches(Equal('subject_id', 'u1'), self.RECORD))
|
|
93
|
+
|
|
94
|
+
def test_in_values(self):
|
|
95
|
+
self.assertTrue(matches(InValues('entity_id', ['e1', 'e2']), self.RECORD))
|
|
96
|
+
self.assertFalse(matches(InValues('entity_id', ['e2']), self.RECORD))
|
|
97
|
+
self.assertFalse(matches(InValues('entity_id', []), self.RECORD))
|
|
98
|
+
|
|
99
|
+
def test_contains_value(self):
|
|
100
|
+
self.assertTrue(matches(ContainsValue('member_ids', 'u2'), self.RECORD))
|
|
101
|
+
self.assertFalse(matches(ContainsValue('member_ids', 'u3'), self.RECORD))
|
|
102
|
+
self.assertFalse(matches(ContainsValue('missing', 'u1'), self.RECORD))
|
|
103
|
+
self.assertFalse(matches(ContainsValue('owner', 'u'), self.RECORD))
|
|
104
|
+
|
|
105
|
+
def test_contains_any(self):
|
|
106
|
+
self.assertTrue(matches(ContainsAny('ancestor_ids', ['s2', 'x']), self.RECORD))
|
|
107
|
+
self.assertFalse(matches(ContainsAny('ancestor_ids', ['x']), self.RECORD))
|
|
108
|
+
self.assertFalse(matches(ContainsAny('ancestor_ids', []), self.RECORD))
|
|
109
|
+
self.assertFalse(matches(ContainsAny('missing', ['s1']), self.RECORD))
|
|
110
|
+
|
|
111
|
+
def test_empty_conjunction_is_true_and_empty_disjunction_is_false(self):
|
|
112
|
+
self.assertTrue(matches(AllOf(()), self.RECORD))
|
|
113
|
+
self.assertFalse(matches(AnyOf(()), self.RECORD))
|
|
114
|
+
|
|
115
|
+
def test_nested_composition(self):
|
|
116
|
+
predicate = AnyOf((
|
|
117
|
+
AllOf((Equal('owner', 'nobody'), ContainsValue('member_ids', 'u1'))),
|
|
118
|
+
AllOf((Equal('owner', 'u1'), ContainsAny('ancestor_ids', ['s1']))),
|
|
119
|
+
))
|
|
120
|
+
self.assertTrue(matches(predicate, self.RECORD))
|
|
121
|
+
|
|
122
|
+
def test_unknown_node_type_is_refused(self):
|
|
123
|
+
class Custom(Predicate):
|
|
124
|
+
pass
|
|
125
|
+
|
|
126
|
+
with self.assertRaises(UnsupportedQuery):
|
|
127
|
+
matches(Custom(), self.RECORD)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""App-author public API namespace.
|
|
2
|
+
|
|
3
|
+
A namespace only - this package never re-exports its children's symbols.
|
|
4
|
+
Import the capability you need directly: ``graphcore.public.apps``,
|
|
5
|
+
``graphcore.public.entities``, ``graphcore.public.strategies``,
|
|
6
|
+
``graphcore.public.persistence``, ``graphcore.public.scheduling``,
|
|
7
|
+
``graphcore.public.api``, ``graphcore.public.errors``, or
|
|
8
|
+
``graphcore.public.auth.{perms,roles,subjects,policy,grants}``.
|
|
9
|
+
"""
|
graphcore/public/api.py
ADDED
graphcore/public/apps.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""App config base for GraphCore apps.
|
|
2
|
+
|
|
3
|
+
from graphcore.public.apps import GraphCoreAppConfig
|
|
4
|
+
|
|
5
|
+
Safe to import before ``django.setup()``. ``graphcore.public`` re-exports
|
|
6
|
+
nothing, so this does not pull in entity types.
|
|
7
|
+
"""
|
|
8
|
+
from graphcore.apps import GraphCoreAppConfig
|
|
9
|
+
|
|
10
|
+
__all__ = ["GraphCoreAppConfig"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Authentication (authn) and authorization (authz) facades for app authors."""
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""What a `graphcore_principal_created` hook returns for a new principal.
|
|
2
|
+
|
|
3
|
+
Safe to import before ``django.setup()``. See
|
|
4
|
+
``graphcore docs distro/deployment-access`` for usage.
|
|
5
|
+
"""
|
|
6
|
+
from graphcore.types.onboarding import Onboarding
|
|
7
|
+
|
|
8
|
+
__all__ = ["Onboarding"]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Permission tokens for grants and policy declarations.
|
|
2
|
+
|
|
3
|
+
Safe to import before ``django.setup()``.
|
|
4
|
+
"""
|
|
5
|
+
from graphcore.types.access import Create, CreatePermission, Delete, ManageAccess, Perm, Read, Write
|
|
6
|
+
|
|
7
|
+
__all__ = ["Create", "CreatePermission", "Delete", "ManageAccess", "Perm", "Read", "Write"]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Boot-time authorization policy: one ``policy.define`` per entity type,
|
|
2
|
+
``policy.extend`` for another app's type, and ``policy.describe`` to read a
|
|
3
|
+
type's full effective policy.
|
|
4
|
+
|
|
5
|
+
Safe to import before ``django.setup()``. See
|
|
6
|
+
``graphcore docs distro/custom-authorization`` for usage.
|
|
7
|
+
"""
|
|
8
|
+
from graphcore.auth.authz.policy import AccessPolicy, EffectivePolicy
|
|
9
|
+
|
|
10
|
+
__all__ = ["AccessPolicy", "EffectivePolicy"]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""Named permission bundles: the vocabulary of grants.
|
|
2
|
+
|
|
3
|
+
Safe to import before ``django.setup()``.
|
|
4
|
+
"""
|
|
5
|
+
from graphcore.types.access import Role
|
|
6
|
+
from graphcore.auth.authz.defaults import ADMIN, DEFAULT_ROLES, EDITOR, MEMBER, VIEWER
|
|
7
|
+
|
|
8
|
+
__all__ = ["ADMIN", "DEFAULT_ROLES", "EDITOR", "MEMBER", "VIEWER", "Role"]
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Entity types for app authors: the base class, the decorator, and the
|
|
2
|
+
kernel's built-in entity types.
|
|
3
|
+
|
|
4
|
+
from graphcore.public.entities import Entity, register_entity
|
|
5
|
+
|
|
6
|
+
@register_entity()
|
|
7
|
+
class Board(Entity):
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
Storage is a separate surface: ``graphcore.public.persistence`` for the
|
|
11
|
+
repository contract, ``graphcore.public.persistence.django`` for
|
|
12
|
+
``EntityModel``. Requires ``django.setup()`` — touches the registry.
|
|
13
|
+
"""
|
|
14
|
+
from graphcore.domain.entity.Entity import Entity
|
|
15
|
+
from graphcore.domain.entity.deployment.Deployment import Deployment
|
|
16
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
17
|
+
from graphcore.domain.entity.group.Group import Group
|
|
18
|
+
from graphcore.domain.entity.service_account.ServiceAccount import ServiceAccount
|
|
19
|
+
from graphcore.domain.entity.user.User import User
|
|
20
|
+
from graphcore.domain.entity.view.View import View
|
|
21
|
+
from graphcore.registry.decorators import register_entity
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"Deployment",
|
|
25
|
+
"Entity",
|
|
26
|
+
"Grant",
|
|
27
|
+
"Group",
|
|
28
|
+
"ServiceAccount",
|
|
29
|
+
"User",
|
|
30
|
+
"View",
|
|
31
|
+
"register_entity",
|
|
32
|
+
]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""App-author kernel errors.
|
|
2
|
+
|
|
3
|
+
from graphcore.public.errors import Conflict, Forbidden
|
|
4
|
+
|
|
5
|
+
Raise one from a strategy to refuse a request: the run fails with the
|
|
6
|
+
error's `code` and keeps its keyword arguments as `details`, and callers
|
|
7
|
+
receive both as `error_code` and `error_details`.
|
|
8
|
+
|
|
9
|
+
Safe to import before ``django.setup()``.
|
|
10
|
+
"""
|
|
11
|
+
from graphcore.errors import (
|
|
12
|
+
ConcurrentModificationError, Conflict, DuplicateEntity, Forbidden, InvalidRequest, KernelError, NotFound,
|
|
13
|
+
PreconditionFailed, StrategyFailed,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"ConcurrentModificationError", "Conflict", "DuplicateEntity", "Forbidden", "InvalidRequest", "KernelError",
|
|
18
|
+
"NotFound", "PreconditionFailed", "StrategyFailed",
|
|
19
|
+
]
|