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,79 @@
|
|
|
1
|
+
"""The entity, strategy, and authorization type registries for one process."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import Callable
|
|
6
|
+
|
|
7
|
+
from graphcore.auth.authz.policy import AccessPolicy, DEFAULT_POLICY
|
|
8
|
+
from graphcore.routes import RouteTable
|
|
9
|
+
from graphcore.registry.EntityDirectory import EntityDirectory, EntityDirectoryClass
|
|
10
|
+
from graphcore.registry.StrategyDirectory import StrategyDirectory, StrategyDirectoryClass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class Registry:
|
|
15
|
+
"""The entity, strategy, and authorization registries for one runtime.
|
|
16
|
+
Registry binding, not dependency injection: a type is the lookup key,
|
|
17
|
+
never a consumer of the bound value. Live infrastructure (repository,
|
|
18
|
+
cache, access adapter, broadcaster) is composed separately - see
|
|
19
|
+
`graphcore.runtime`."""
|
|
20
|
+
|
|
21
|
+
entities: EntityDirectoryClass
|
|
22
|
+
strategies: StrategyDirectoryClass
|
|
23
|
+
policy: AccessPolicy
|
|
24
|
+
principal_hooks: list[Callable] = field(default_factory=list)
|
|
25
|
+
"""Principal-created hooks in INSTALLED_APPS order (see
|
|
26
|
+
`GraphCoreAppConfig.graphcore_principal_created`)."""
|
|
27
|
+
routes: RouteTable = field(default_factory=RouteTable)
|
|
28
|
+
|
|
29
|
+
def __post_init__(self) -> None:
|
|
30
|
+
self.policy.entity_directory = self.entities
|
|
31
|
+
|
|
32
|
+
def close(self) -> None:
|
|
33
|
+
self.entities.close()
|
|
34
|
+
self.strategies.close()
|
|
35
|
+
self.policy.close()
|
|
36
|
+
|
|
37
|
+
def register_pending(self, entity_registrations, strategy_registrations, app_id=None) -> None:
|
|
38
|
+
"""Register one discovery batch in this runtime; `app_id` owns its strategies."""
|
|
39
|
+
self.entities.register_pending(entity_registrations)
|
|
40
|
+
self.strategies.register_pending(strategy_registrations, app_id)
|
|
41
|
+
|
|
42
|
+
def finalize(self) -> None:
|
|
43
|
+
"""Freeze every registry for serving after policy configuration."""
|
|
44
|
+
self.routes.finalize()
|
|
45
|
+
self.close()
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def from_defaults(cls) -> 'Registry':
|
|
49
|
+
return cls(EntityDirectory, StrategyDirectory, DEFAULT_POLICY)
|
|
50
|
+
|
|
51
|
+
def fork_open(self) -> 'Registry':
|
|
52
|
+
"""Copy this registry into an isolated, mutable test registry."""
|
|
53
|
+
entities = EntityDirectoryClass()
|
|
54
|
+
entities._directory = self.entities._directory.copy()
|
|
55
|
+
entities._registered_classes = self.entities._registered_classes.copy()
|
|
56
|
+
entities._app_labels = self.entities._app_labels.copy()
|
|
57
|
+
entities._principal_types = self.entities._principal_types.copy()
|
|
58
|
+
entities._storage = self.entities._storage.copy()
|
|
59
|
+
|
|
60
|
+
strategies = StrategyDirectoryClass()
|
|
61
|
+
strategies._directory = {
|
|
62
|
+
key: value.copy() for key, value in self.strategies._directory.items()
|
|
63
|
+
}
|
|
64
|
+
strategies._class_cache = self.strategies._class_cache.copy()
|
|
65
|
+
strategies._read_only = self.strategies._read_only.copy()
|
|
66
|
+
strategies._owners = self.strategies._owners.copy()
|
|
67
|
+
strategies._paths_by_name = self.strategies._paths_by_name.copy()
|
|
68
|
+
|
|
69
|
+
policy = AccessPolicy(strategy_directory=strategies)
|
|
70
|
+
policy._defined = {key: dict(value) for key, value in self.policy._defined.items()}
|
|
71
|
+
policy._extended = {key: list(value) for key, value in self.policy._extended.items()}
|
|
72
|
+
policy._declared_groups = self.policy._declared_groups.copy()
|
|
73
|
+
|
|
74
|
+
routes = RouteTable()
|
|
75
|
+
routes._routes = list(self.routes._routes)
|
|
76
|
+
return Registry(entities, strategies, policy, list(self.principal_hooks), routes)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
DEFAULT_REGISTRY = Registry.from_defaults()
|
|
File without changes
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import tempfile
|
|
3
|
+
import unittest
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from unittest.mock import MagicMock, patch
|
|
6
|
+
|
|
7
|
+
from graphcore.registry.app_discovery import discover_agent_frontends
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _no_entry_points():
|
|
11
|
+
eps = MagicMock()
|
|
12
|
+
eps.select.return_value = []
|
|
13
|
+
return eps
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DiscoverAgentFrontendsTests(unittest.TestCase):
|
|
17
|
+
def setUp(self):
|
|
18
|
+
self.addCleanup(lambda saved=list(sys.path): sys.path.__setitem__(slice(None), saved))
|
|
19
|
+
|
|
20
|
+
def _package(self, *parts):
|
|
21
|
+
package = Path(*parts)
|
|
22
|
+
package.mkdir(parents=True)
|
|
23
|
+
(package / '__init__.py').write_text('')
|
|
24
|
+
|
|
25
|
+
def test_in_project_agent_frontend_packages_are_found_and_importable(self):
|
|
26
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
27
|
+
apps = Path(tmp)
|
|
28
|
+
self._package(apps, 'notes', 'backend', 'notes')
|
|
29
|
+
self._package(apps, 'notes', 'backend', 'notes_agent_frontend')
|
|
30
|
+
self._package(apps, 'misnamed', 'backend', 'other_agent_frontend')
|
|
31
|
+
self._package(apps, 'plain', 'backend', 'plain')
|
|
32
|
+
with patch('graphcore.registry.app_discovery.entry_points', _no_entry_points):
|
|
33
|
+
self.assertEqual(discover_agent_frontends(apps), ['notes_agent_frontend'])
|
|
34
|
+
self.assertEqual(sys.path.count(str((apps / 'notes' / 'backend').resolve())), 1)
|
|
35
|
+
|
|
36
|
+
def test_an_agent_frontend_beside_the_backend_package_is_not_found(self):
|
|
37
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
38
|
+
apps = Path(tmp)
|
|
39
|
+
self._package(apps, 'notes', 'agent_frontend', 'notes_agent_frontend')
|
|
40
|
+
with patch('graphcore.registry.app_discovery.entry_points', _no_entry_points):
|
|
41
|
+
self.assertEqual(discover_agent_frontends(apps), [])
|
|
42
|
+
|
|
43
|
+
def test_entry_points_come_first_and_an_app_found_both_ways_appears_once(self):
|
|
44
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
45
|
+
apps = Path(tmp)
|
|
46
|
+
self._package(apps, 'notes', 'backend', 'notes_agent_frontend')
|
|
47
|
+
eps = MagicMock()
|
|
48
|
+
eps.select.return_value = [MagicMock(value='notes_agent_frontend'), MagicMock(value='agents_agent_frontend')]
|
|
49
|
+
with patch('graphcore.registry.app_discovery.entry_points', return_value=eps):
|
|
50
|
+
self.assertEqual(
|
|
51
|
+
discover_agent_frontends(apps), ['agents_agent_frontend', 'notes_agent_frontend'],
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
def test_missing_apps_dir(self):
|
|
55
|
+
with patch('graphcore.registry.app_discovery.entry_points', _no_entry_points):
|
|
56
|
+
self.assertEqual(discover_agent_frontends('/nonexistent/apps'), [])
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from types import ModuleType
|
|
2
|
+
|
|
3
|
+
from django.test import SimpleTestCase
|
|
4
|
+
|
|
5
|
+
from graphcore.apps import GraphCoreAppConfig
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _config(cls):
|
|
9
|
+
module = ModuleType('notes')
|
|
10
|
+
module.__path__ = ['/nonexistent/notes']
|
|
11
|
+
return cls('notes', module)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ServiceClientsTests(SimpleTestCase):
|
|
15
|
+
def test_an_app_declares_no_service_clients_by_default(self):
|
|
16
|
+
class NotesConfig(GraphCoreAppConfig):
|
|
17
|
+
name = 'notes'
|
|
18
|
+
graphcore_app_id = 'notes'
|
|
19
|
+
|
|
20
|
+
self.assertEqual((), _config(NotesConfig).graphcore_service_clients)
|
|
21
|
+
|
|
22
|
+
def test_an_app_declares_its_service_clients(self):
|
|
23
|
+
class NotesConfig(GraphCoreAppConfig):
|
|
24
|
+
name = 'notes'
|
|
25
|
+
graphcore_app_id = 'notes'
|
|
26
|
+
graphcore_service_clients = ('gateway',)
|
|
27
|
+
|
|
28
|
+
self.assertEqual(('gateway',), _config(NotesConfig).graphcore_service_clients)
|
|
29
|
+
|
|
30
|
+
def test_ready_still_requires_an_app_id(self):
|
|
31
|
+
class NotesConfig(GraphCoreAppConfig):
|
|
32
|
+
name = 'notes'
|
|
33
|
+
graphcore_service_clients = ('gateway',)
|
|
34
|
+
|
|
35
|
+
with self.assertRaisesRegex(ValueError, 'graphcore_app_id'):
|
|
36
|
+
_config(NotesConfig).ready()
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
from types import SimpleNamespace
|
|
2
|
+
from unittest.mock import patch
|
|
3
|
+
|
|
4
|
+
from django.test import TestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.registry.EntityDirectory import (
|
|
7
|
+
EntityDirectory,
|
|
8
|
+
EntityAlreadyRegisteredError,
|
|
9
|
+
EntityDirectoryClass,
|
|
10
|
+
EntityDirectoryClosedError,
|
|
11
|
+
)
|
|
12
|
+
from graphcore.registry.decorators import (
|
|
13
|
+
get_pending_entity_registrations,
|
|
14
|
+
register_entity,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _patch_app_config(module_to_config):
|
|
19
|
+
"""Patch `apps.get_containing_app_config` as used by EntityDirectory."""
|
|
20
|
+
return patch(
|
|
21
|
+
'graphcore.registry.EntityDirectory.apps.get_containing_app_config',
|
|
22
|
+
side_effect=lambda module_name: module_to_config.get(module_name),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class DiscoveryTests(TestCase):
|
|
27
|
+
"""Discovery runs at startup, driven by the framework app config.
|
|
28
|
+
|
|
29
|
+
Most tests here call `EntityDirectory.register_entity()` directly rather
|
|
30
|
+
than draining `get_pending_entity_registrations()`, since that global
|
|
31
|
+
pending list can carry leftover registrations from unrelated test
|
|
32
|
+
modules imported earlier in the same process.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def setUp(self):
|
|
36
|
+
self.directory = EntityDirectoryClass()
|
|
37
|
+
|
|
38
|
+
def test_kernel_types_are_registered_under_the_framework_app_id(self):
|
|
39
|
+
registered = EntityDirectory.get_all_entities()
|
|
40
|
+
|
|
41
|
+
self.assertIn('graphcore.Entity', registered)
|
|
42
|
+
self.assertIn('graphcore.User', registered)
|
|
43
|
+
self.assertIn('graphcore.StrategyRequest', registered)
|
|
44
|
+
self.assertTrue(EntityDirectory.closed)
|
|
45
|
+
|
|
46
|
+
def test_decorator_pending_list_carries_its_app_label_override(self):
|
|
47
|
+
"""The `@register_entity()` -> pending list -> register_pending wiring
|
|
48
|
+
carries the (class, app_label) pair through end to end."""
|
|
49
|
+
@register_entity(app_label='explicit')
|
|
50
|
+
class Gadget:
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
pending = [pair for pair in get_pending_entity_registrations() if pair[0] is Gadget]
|
|
54
|
+
self.directory.register_pending(pending)
|
|
55
|
+
|
|
56
|
+
self.assertEqual('explicit.Gadget', self.directory.get_entity_type_id(Gadget))
|
|
57
|
+
|
|
58
|
+
def test_owning_app_id_is_resolved_from_the_class_module(self):
|
|
59
|
+
class Widget:
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
fake_config = SimpleNamespace(graphcore_app_id='tracks')
|
|
63
|
+
with _patch_app_config({Widget.__module__: fake_config}):
|
|
64
|
+
self.directory.register_entity(Widget)
|
|
65
|
+
|
|
66
|
+
self.assertEqual('tracks.Widget', self.directory.get_entity_type_id(Widget))
|
|
67
|
+
self.assertIs(Widget, self.directory.get_entity_class('tracks.Widget'))
|
|
68
|
+
self.assertEqual(
|
|
69
|
+
'tracks.Widget',
|
|
70
|
+
Widget.__dict__['_graphcore_entity_type_id'],
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
def test_class_cannot_have_different_type_ids_across_registries(self):
|
|
74
|
+
class Stable:
|
|
75
|
+
pass
|
|
76
|
+
|
|
77
|
+
self.directory.register_entity(Stable, app_label='first')
|
|
78
|
+
|
|
79
|
+
with self.assertRaises(EntityAlreadyRegisteredError):
|
|
80
|
+
EntityDirectoryClass().register_entity(Stable, app_label='second')
|
|
81
|
+
|
|
82
|
+
def test_unowned_entity_class_is_rejected(self):
|
|
83
|
+
class Orphan:
|
|
84
|
+
pass
|
|
85
|
+
|
|
86
|
+
with self.assertRaises(ValueError):
|
|
87
|
+
EntityDirectory.get_entity_type_id(Orphan)
|
|
88
|
+
|
|
89
|
+
def test_undecorated_subclass_does_not_inherit_its_parents_label(self):
|
|
90
|
+
class BaseWidget:
|
|
91
|
+
pass
|
|
92
|
+
|
|
93
|
+
fake_config = SimpleNamespace(graphcore_app_id='tracks')
|
|
94
|
+
with _patch_app_config({BaseWidget.__module__: fake_config}):
|
|
95
|
+
self.directory.register_entity(BaseWidget)
|
|
96
|
+
|
|
97
|
+
class UndecoratedChild(BaseWidget):
|
|
98
|
+
pass
|
|
99
|
+
|
|
100
|
+
with self.assertRaises(ValueError):
|
|
101
|
+
EntityDirectory.get_entity_type_id(UndecoratedChild)
|
|
102
|
+
|
|
103
|
+
def test_decorated_class_outside_any_installed_app_raises_naming_the_class(self):
|
|
104
|
+
class Outsider:
|
|
105
|
+
pass
|
|
106
|
+
|
|
107
|
+
with _patch_app_config({}): # no app claims this module
|
|
108
|
+
with self.assertRaises(ValueError) as ctx:
|
|
109
|
+
self.directory.register_entity(Outsider)
|
|
110
|
+
|
|
111
|
+
self.assertIn('Outsider', str(ctx.exception))
|
|
112
|
+
|
|
113
|
+
def test_entity_type_id_does_not_depend_on_registration_order(self):
|
|
114
|
+
"""A cross-app import can pull one app's entity class into another
|
|
115
|
+
app's module as a side effect during discovery. Ownership must come
|
|
116
|
+
from each class's own module, not from which app is drained first."""
|
|
117
|
+
class Alpha:
|
|
118
|
+
pass
|
|
119
|
+
|
|
120
|
+
class Beta:
|
|
121
|
+
pass
|
|
122
|
+
|
|
123
|
+
module_to_config = {
|
|
124
|
+
Alpha.__module__ + '.alpha': SimpleNamespace(graphcore_app_id='alpha'),
|
|
125
|
+
Alpha.__module__ + '.beta': SimpleNamespace(graphcore_app_id='beta'),
|
|
126
|
+
}
|
|
127
|
+
# Fake each class living in its own app's module, independent of the
|
|
128
|
+
# test module they were actually defined in.
|
|
129
|
+
Alpha.__module__ += '.alpha'
|
|
130
|
+
Beta.__module__ += '.beta'
|
|
131
|
+
|
|
132
|
+
with _patch_app_config(module_to_config):
|
|
133
|
+
# Beta drained first - the reverse of definition order - must not
|
|
134
|
+
# change the resulting ids.
|
|
135
|
+
self.directory.register_entity(Beta)
|
|
136
|
+
self.directory.register_entity(Alpha)
|
|
137
|
+
|
|
138
|
+
self.assertEqual('beta.Beta', self.directory.get_entity_type_id(Beta))
|
|
139
|
+
self.assertEqual('alpha.Alpha', self.directory.get_entity_type_id(Alpha))
|
|
140
|
+
|
|
141
|
+
def test_closed_directory_rejects_late_registration(self):
|
|
142
|
+
class Late:
|
|
143
|
+
pass
|
|
144
|
+
|
|
145
|
+
self.directory.close()
|
|
146
|
+
with self.assertRaises(EntityDirectoryClosedError):
|
|
147
|
+
self.directory.register_entity(Late, app_label='late')
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Storage bindings on `EntityDirectory`: resolution order, last-wins, and
|
|
2
|
+
isolation across a fork."""
|
|
3
|
+
from django.test import SimpleTestCase, TestCase
|
|
4
|
+
|
|
5
|
+
from graphcore.domain.entity.Entity import Entity
|
|
6
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
7
|
+
from graphcore.registry.EntityDirectory import EntityDirectoryClass, EntityDirectoryClosedError, StorageNotBound
|
|
8
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
9
|
+
from graphcore.runtime import django_runtime
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class StorageResolutionTests(SimpleTestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
self.directory = EntityDirectoryClass()
|
|
15
|
+
self.directory.bind_storage(Entity, 'entity-storage')
|
|
16
|
+
|
|
17
|
+
def test_exact_match_wins(self):
|
|
18
|
+
self.directory.bind_storage(Grant, 'grant-storage')
|
|
19
|
+
|
|
20
|
+
self.assertEqual(self.directory.storage(Grant), 'grant-storage')
|
|
21
|
+
|
|
22
|
+
def test_an_unbound_subclass_resolves_to_its_nearest_bound_ancestor(self):
|
|
23
|
+
class Board(Entity):
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
self.assertEqual(self.directory.storage(Board), 'entity-storage')
|
|
27
|
+
|
|
28
|
+
def test_last_registration_for_a_class_wins(self):
|
|
29
|
+
self.directory.bind_storage(Entity, 'other-storage')
|
|
30
|
+
|
|
31
|
+
self.assertEqual(self.directory.storage(Entity), 'other-storage')
|
|
32
|
+
|
|
33
|
+
def test_nothing_bound_is_an_error_not_a_guess(self):
|
|
34
|
+
with self.assertRaises(StorageNotBound):
|
|
35
|
+
EntityDirectoryClass().storage(Entity)
|
|
36
|
+
|
|
37
|
+
def test_binding_after_close_raises(self):
|
|
38
|
+
self.directory.close()
|
|
39
|
+
with self.assertRaises(EntityDirectoryClosedError):
|
|
40
|
+
self.directory.bind_storage(Grant, 'grant-storage')
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class KernelStorageBindingTests(TestCase):
|
|
44
|
+
"""Discovery binds the kernel defaults and freezes them."""
|
|
45
|
+
|
|
46
|
+
def test_kernel_types_are_bound_to_their_storage(self):
|
|
47
|
+
from graphcore.persistence.django.models import EntityModel, GrantModel
|
|
48
|
+
|
|
49
|
+
self.assertIs(DEFAULT_REGISTRY.entities.storage(Entity), EntityModel)
|
|
50
|
+
self.assertIs(DEFAULT_REGISTRY.entities.storage(Grant), GrantModel)
|
|
51
|
+
|
|
52
|
+
def test_a_forked_runtime_copies_and_isolates_bindings(self):
|
|
53
|
+
forked = DEFAULT_REGISTRY.fork_open()
|
|
54
|
+
|
|
55
|
+
class OverrideModel:
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
forked.entities.bind_storage(Grant, OverrideModel)
|
|
59
|
+
|
|
60
|
+
self.assertIs(forked.entities.storage(Grant), OverrideModel)
|
|
61
|
+
self.assertIsNot(DEFAULT_REGISTRY.entities.storage(Grant), OverrideModel)
|
|
62
|
+
|
|
63
|
+
def test_forked_runtime_repository_sees_its_own_override(self):
|
|
64
|
+
"""The repository built for a forked registry resolves storage
|
|
65
|
+
through that registry's own `EntityDirectory`, so a fork's override
|
|
66
|
+
is what its repository uses - this is the whole override path,
|
|
67
|
+
independent of any real table."""
|
|
68
|
+
forked = DEFAULT_REGISTRY.fork_open()
|
|
69
|
+
override = object()
|
|
70
|
+
forked.entities.bind_storage(Grant, override)
|
|
71
|
+
|
|
72
|
+
repository = django_runtime(forked).repository
|
|
73
|
+
self.assertIs(repository.entity_directory.storage(Grant), override)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import shutil
|
|
3
|
+
import sys
|
|
4
|
+
import tempfile
|
|
5
|
+
import unittest
|
|
6
|
+
|
|
7
|
+
from graphcore.registry.discovery import _import_package, _is_test_module
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ImportPackageTests(unittest.TestCase):
|
|
11
|
+
"""`_import_package` against real on-disk packages, so `pkgutil.walk_packages`
|
|
12
|
+
behaves exactly as it does for a real Django app."""
|
|
13
|
+
|
|
14
|
+
def setUp(self):
|
|
15
|
+
self._tmpdir = tempfile.mkdtemp()
|
|
16
|
+
sys.path.insert(0, self._tmpdir)
|
|
17
|
+
self._modules_before = set(sys.modules)
|
|
18
|
+
|
|
19
|
+
def tearDown(self):
|
|
20
|
+
sys.path.remove(self._tmpdir)
|
|
21
|
+
shutil.rmtree(self._tmpdir, ignore_errors=True)
|
|
22
|
+
for name in set(sys.modules) - self._modules_before:
|
|
23
|
+
del sys.modules[name]
|
|
24
|
+
|
|
25
|
+
def _make_package(self, name, files):
|
|
26
|
+
pkg_dir = os.path.join(self._tmpdir, name)
|
|
27
|
+
os.makedirs(pkg_dir, exist_ok=True)
|
|
28
|
+
for relpath, content in files.items():
|
|
29
|
+
full = os.path.join(pkg_dir, relpath)
|
|
30
|
+
os.makedirs(os.path.dirname(full), exist_ok=True)
|
|
31
|
+
with open(full, 'w') as f:
|
|
32
|
+
f.write(content)
|
|
33
|
+
|
|
34
|
+
def test_broken_module_raises_with_module_path_in_message(self):
|
|
35
|
+
self._make_package('brokenapp', {
|
|
36
|
+
'__init__.py': '',
|
|
37
|
+
'manifest.py': "raise RuntimeError('boom')",
|
|
38
|
+
})
|
|
39
|
+
with self.assertRaises(ImportError) as ctx:
|
|
40
|
+
_import_package('brokenapp')
|
|
41
|
+
self.assertIn('brokenapp.manifest', str(ctx.exception))
|
|
42
|
+
self.assertIsInstance(ctx.exception.__cause__, RuntimeError)
|
|
43
|
+
|
|
44
|
+
def test_manifest_module_is_imported_and_registers(self):
|
|
45
|
+
self._make_package('manifestapp', {
|
|
46
|
+
'__init__.py': '',
|
|
47
|
+
'manifest.py': 'IMPORTED = True',
|
|
48
|
+
})
|
|
49
|
+
_import_package('manifestapp')
|
|
50
|
+
self.assertTrue(sys.modules['manifestapp.manifest'].IMPORTED)
|
|
51
|
+
|
|
52
|
+
def test_test_dot_py_and_test_prefixed_modules_are_skipped(self):
|
|
53
|
+
self._make_package('skipapp', {
|
|
54
|
+
'__init__.py': '',
|
|
55
|
+
'test.py': "raise RuntimeError('should never import')",
|
|
56
|
+
'test_foo.py': "raise RuntimeError('should never import')",
|
|
57
|
+
'latest_run.py': 'IMPORTED = True',
|
|
58
|
+
'contest.py': 'IMPORTED = True',
|
|
59
|
+
})
|
|
60
|
+
_import_package('skipapp')
|
|
61
|
+
self.assertNotIn('skipapp.test', sys.modules)
|
|
62
|
+
self.assertNotIn('skipapp.test_foo', sys.modules)
|
|
63
|
+
self.assertTrue(sys.modules['skipapp.latest_run'].IMPORTED)
|
|
64
|
+
self.assertTrue(sys.modules['skipapp.contest'].IMPORTED)
|
|
65
|
+
|
|
66
|
+
def test_absent_package_is_skipped_without_error(self):
|
|
67
|
+
_import_package('this_package_does_not_exist_at_all')
|
|
68
|
+
|
|
69
|
+
def test_missing_third_party_dependency_raises(self):
|
|
70
|
+
self._make_package('depapp', {
|
|
71
|
+
'__init__.py': 'import this_dependency_does_not_exist_either',
|
|
72
|
+
})
|
|
73
|
+
with self.assertRaises(ModuleNotFoundError):
|
|
74
|
+
_import_package('depapp')
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class IsTestModuleTests(unittest.TestCase):
|
|
78
|
+
def test_skips_tests_package_and_test_module_segments(self):
|
|
79
|
+
self.assertTrue(_is_test_module('app.entities.tests'))
|
|
80
|
+
self.assertTrue(_is_test_module('app.entities.tests.test_widget'))
|
|
81
|
+
self.assertTrue(_is_test_module('app.entities.test'))
|
|
82
|
+
self.assertTrue(_is_test_module('app.entities.test_widget'))
|
|
83
|
+
|
|
84
|
+
def test_does_not_skip_names_that_merely_contain_test(self):
|
|
85
|
+
self.assertFalse(_is_test_module('app.entities.manifest'))
|
|
86
|
+
self.assertFalse(_is_test_module('app.entities.latest_run'))
|
|
87
|
+
self.assertFalse(_is_test_module('app.entities.contest'))
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from django.test import TestCase
|
|
2
|
+
|
|
3
|
+
from graphcore.registry.EntityDirectory import EntityDirectoryClass
|
|
4
|
+
from graphcore.registry.decorators import (
|
|
5
|
+
get_pending_entity_registrations,
|
|
6
|
+
register_entity,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PrincipalFlagTests(TestCase):
|
|
11
|
+
"""`principal` is an opt-in registration flag (default False), threaded
|
|
12
|
+
through discovery into `EntityDirectory`. A group is `Group` or a
|
|
13
|
+
subclass, and shareability is policy - neither is a registration flag."""
|
|
14
|
+
|
|
15
|
+
def setUp(self):
|
|
16
|
+
self.directory = EntityDirectoryClass()
|
|
17
|
+
|
|
18
|
+
def test_flag_defaults_false(self):
|
|
19
|
+
class Plain:
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
self.directory.register_entity(Plain, app_label='flagtest')
|
|
23
|
+
|
|
24
|
+
self.assertFalse(self.directory.is_principal(self.directory.get_entity_type_id(Plain)))
|
|
25
|
+
|
|
26
|
+
def test_directory_serializes_the_flag(self):
|
|
27
|
+
class Metadata:
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
self.directory.register_entity(Metadata, app_label='flagtest', principal=True)
|
|
31
|
+
|
|
32
|
+
metadata = self.directory.get_entity_directory()['flagtest.Metadata']
|
|
33
|
+
self.assertEqual(metadata['entity_type_id'], 'flagtest.Metadata')
|
|
34
|
+
self.assertTrue(metadata['principal'])
|
|
35
|
+
self.assertNotIn('shareable', metadata)
|
|
36
|
+
self.assertNotIn('group', metadata)
|
|
37
|
+
|
|
38
|
+
def test_unknown_type_id_answers_false_not_error(self):
|
|
39
|
+
self.assertFalse(self.directory.is_principal('nope.Nothing'))
|
|
40
|
+
|
|
41
|
+
def test_decorator_carries_the_flag_through_pending_list(self):
|
|
42
|
+
@register_entity(app_label='flagtest', principal=True)
|
|
43
|
+
class Decorated:
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
pending = [entry for entry in get_pending_entity_registrations() if entry[0] is Decorated]
|
|
47
|
+
self.directory.register_pending(pending)
|
|
48
|
+
|
|
49
|
+
self.assertTrue(self.directory.is_principal(self.directory.get_entity_type_id(Decorated)))
|
|
50
|
+
|
|
51
|
+
def test_group_is_not_a_registration_flag(self):
|
|
52
|
+
with self.assertRaises(TypeError):
|
|
53
|
+
register_entity(app_label='flagtest', group=True)
|
|
54
|
+
|
|
55
|
+
class Roster:
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
with self.assertRaises(TypeError):
|
|
59
|
+
self.directory.register_entity(Roster, app_label='flagtest', group=True)
|
|
60
|
+
|
|
61
|
+
def test_re_registration_conflict_checks_the_flag(self):
|
|
62
|
+
class Reregistered:
|
|
63
|
+
pass
|
|
64
|
+
|
|
65
|
+
self.directory.register_entity(Reregistered, app_label='flagtest')
|
|
66
|
+
with self.assertRaises(Exception):
|
|
67
|
+
self.directory.register_entity(Reregistered, app_label='flagtest', principal=True)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from django.test import SimpleTestCase
|
|
2
|
+
|
|
3
|
+
from graphcore.domain.strategy.BaseStrategy import SetAttributes
|
|
4
|
+
from graphcore.errors import InvalidRequest
|
|
5
|
+
from graphcore.registry.StrategyDirectory import DuplicateStrategyNameError, StrategyDirectoryClass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class StrategyDirectoryByNameTests(SimpleTestCase):
|
|
9
|
+
def setUp(self):
|
|
10
|
+
self.directory = StrategyDirectoryClass()
|
|
11
|
+
self.directory.register_strategy('Entity', SetAttributes)
|
|
12
|
+
|
|
13
|
+
def test_a_registered_name_resolves_to_its_class(self):
|
|
14
|
+
self.assertIs(self.directory.by_name('SetAttributes'), SetAttributes)
|
|
15
|
+
self.assertIs(self.directory.require('SetAttributes'), SetAttributes)
|
|
16
|
+
|
|
17
|
+
def test_an_unregistered_name_is_none_or_refused(self):
|
|
18
|
+
self.assertIsNone(self.directory.by_name('Missing'))
|
|
19
|
+
with self.assertRaisesMessage(InvalidRequest, 'Strategy Missing is not registered.'):
|
|
20
|
+
self.directory.require('Missing')
|
|
21
|
+
|
|
22
|
+
def test_one_class_may_serve_several_entity_types(self):
|
|
23
|
+
self.directory.register_strategy('Group', SetAttributes)
|
|
24
|
+
self.assertIs(self.directory.require('SetAttributes'), SetAttributes)
|
|
25
|
+
|
|
26
|
+
def test_two_classes_cannot_share_a_name(self):
|
|
27
|
+
impostor = type('SetAttributes', (), {'__module__': 'some_app.strategies'})
|
|
28
|
+
with self.assertRaises(DuplicateStrategyNameError):
|
|
29
|
+
self.directory.register_strategy('Entity', impostor)
|