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,472 @@
|
|
|
1
|
+
from django.apps import apps
|
|
2
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
3
|
+
from django.db import DEFAULT_DB_ALIAS, connection
|
|
4
|
+
from django.test import TestCase, TransactionTestCase
|
|
5
|
+
from django.test.utils import CaptureQueriesContext
|
|
6
|
+
from unittest.mock import patch
|
|
7
|
+
|
|
8
|
+
from asgiref.sync import async_to_sync
|
|
9
|
+
|
|
10
|
+
from graphcore.persistence.django.identity import ensure_backend_identity
|
|
11
|
+
from graphcore.domain.entity.Entity import Entity
|
|
12
|
+
from graphcore.domain.entity.strategy_request.StrategyRequest import StrategyRequest
|
|
13
|
+
from graphcore.errors import ConcurrentModificationError
|
|
14
|
+
from graphcore.persistence.django.models import (
|
|
15
|
+
BackendIdentityModel, EntityModel, EntityReferenceModel, StrategyRequestModel,
|
|
16
|
+
)
|
|
17
|
+
from graphcore.persistence.django.repository import DjangoEntityRepository
|
|
18
|
+
from graphcore.persistence.tests.contract import EntityRepositoryContract
|
|
19
|
+
from graphcore.persistence.tests.fixtures import make
|
|
20
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
21
|
+
from graphcore.runtime import django_runtime
|
|
22
|
+
from graphcore.types.query import AllOf, ContainsAny, EntityQuery, PageRequest
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def make_repository():
|
|
26
|
+
return DjangoEntityRepository(DEFAULT_REGISTRY.entities)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class DjangoEntityRepositoryTests(EntityRepositoryContract, TestCase):
|
|
30
|
+
def make_repository(self):
|
|
31
|
+
return make_repository()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class RecordMappingTests(TestCase):
|
|
35
|
+
def test_unknown_typed_field_has_no_storage_fallback(self):
|
|
36
|
+
entity = make(Entity, DEFAULT_REGISTRY)
|
|
37
|
+
with patch.object(entity, 'to_dict', return_value={**entity.to_dict(), 'unbound': 'value'}):
|
|
38
|
+
with self.assertRaisesMessage(ImproperlyConfigured, "has no column for it"):
|
|
39
|
+
make_repository()._split(entity, EntityModel)
|
|
40
|
+
|
|
41
|
+
def test_bag_is_stored_directly_and_not_promoted_to_columns(self):
|
|
42
|
+
entity = make(Entity, DEFAULT_REGISTRY)
|
|
43
|
+
entity.set_attribute('owner', 'bag owner')
|
|
44
|
+
repository = make_repository()
|
|
45
|
+
async_to_sync(repository.save)(entity)
|
|
46
|
+
row = EntityModel.objects.get(pk=entity.entity_id)
|
|
47
|
+
self.assertEqual(row.attributes, {'owner': 'bag owner'})
|
|
48
|
+
self.assertIsNone(row.owner)
|
|
49
|
+
self.assertEqual(async_to_sync(repository.load)(entity.entity_id).get_attribute('owner'), 'bag owner')
|
|
50
|
+
|
|
51
|
+
def test_hydration_failure_is_not_reported_as_missing(self):
|
|
52
|
+
entity = make(Entity, DEFAULT_REGISTRY)
|
|
53
|
+
repository = make_repository()
|
|
54
|
+
async_to_sync(repository.save)(entity)
|
|
55
|
+
for attributes in (['invalid'], [], False, 0):
|
|
56
|
+
EntityModel.objects.filter(pk=entity.entity_id).update(attributes=attributes)
|
|
57
|
+
for read in (lambda: repository.load(entity.entity_id), lambda: repository.load_many([entity.entity_id])):
|
|
58
|
+
with self.subTest(attributes=attributes), self.assertRaisesMessage(ValueError, 'attributes must'):
|
|
59
|
+
async_to_sync(read)()
|
|
60
|
+
|
|
61
|
+
def test_unknown_type_is_explicitly_skipped(self):
|
|
62
|
+
entity = make(Entity, DEFAULT_REGISTRY)
|
|
63
|
+
repository = make_repository()
|
|
64
|
+
async_to_sync(repository.save)(entity)
|
|
65
|
+
EntityModel.objects.filter(pk=entity.entity_id).update(entity_type_id='missing.Entity')
|
|
66
|
+
with self.assertLogs('graphcore.persistence.django.repository', level='WARNING'):
|
|
67
|
+
self.assertEqual(async_to_sync(repository.load_many)([entity.entity_id]), {})
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class DependentStorageValidationTests(TestCase):
|
|
71
|
+
def _repository_with(self, field):
|
|
72
|
+
class _Holder(Entity):
|
|
73
|
+
dependent_on = (field,)
|
|
74
|
+
|
|
75
|
+
registry = DEFAULT_REGISTRY.fork_open()
|
|
76
|
+
registry.entities.register_entity(_Holder, app_label='graphcore')
|
|
77
|
+
return DjangoEntityRepository(registry.entities)
|
|
78
|
+
|
|
79
|
+
def test_an_attribute_bag_field_is_refused(self):
|
|
80
|
+
with self.assertRaises(ImproperlyConfigured):
|
|
81
|
+
self._repository_with('target')._validate_storage()
|
|
82
|
+
|
|
83
|
+
def test_a_non_string_column_is_refused(self):
|
|
84
|
+
with self.assertRaises(ImproperlyConfigured):
|
|
85
|
+
self._repository_with('sibling_position')._validate_storage()
|
|
86
|
+
|
|
87
|
+
def test_a_string_column_is_accepted(self):
|
|
88
|
+
self._repository_with('parent_id')._validate_storage()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class BackendIdentityTests(TestCase):
|
|
92
|
+
"""Ensured by `graphcore.apps` on post_migrate, one row for the store's life."""
|
|
93
|
+
|
|
94
|
+
def test_migrations_leave_exactly_one_identity_row(self):
|
|
95
|
+
self.assertEqual(BackendIdentityModel.objects.count(), 1)
|
|
96
|
+
|
|
97
|
+
def test_re_emitting_post_migrate_is_a_no_op(self):
|
|
98
|
+
ensure_backend_identity(apps.get_app_config('graphcore'), using=DEFAULT_DB_ALIAS)
|
|
99
|
+
|
|
100
|
+
self.assertEqual(BackendIdentityModel.objects.count(), 1)
|
|
101
|
+
|
|
102
|
+
def test_the_repository_reports_the_stored_identity(self):
|
|
103
|
+
self.assertEqual(
|
|
104
|
+
make_repository().backend_id(),
|
|
105
|
+
str(BackendIdentityModel.objects.values_list('backend_id', flat=True).get()),
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class BackendIdentitySurvivesFlushTests(TransactionTestCase):
|
|
110
|
+
"""A TransactionTestCase flushes every table between tests; the receiver
|
|
111
|
+
re-establishes the row, so a runtime built afterwards still resolves an
|
|
112
|
+
identity."""
|
|
113
|
+
|
|
114
|
+
def test_a_runtime_built_after_a_flush_resolves_a_backend_id(self):
|
|
115
|
+
runtime = django_runtime(DEFAULT_REGISTRY)
|
|
116
|
+
|
|
117
|
+
self.assertEqual(runtime.backend_id, make_repository().backend_id())
|
|
118
|
+
self.assertEqual(BackendIdentityModel.objects.count(), 1)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class QueryShapeTests(TestCase):
|
|
122
|
+
"""Access filtering is pushed into SQL: one SELECT with the predicate in
|
|
123
|
+
WHERE and a LIMIT, never a page narrowed in Python."""
|
|
124
|
+
|
|
125
|
+
def setUp(self):
|
|
126
|
+
self.repository = make_repository()
|
|
127
|
+
self.root = make(Entity, DEFAULT_REGISTRY)
|
|
128
|
+
async_to_sync(self.repository.save)(self.root)
|
|
129
|
+
for _ in range(5):
|
|
130
|
+
child = make(Entity, DEFAULT_REGISTRY)
|
|
131
|
+
child.set_parent(self.root.entity_id)
|
|
132
|
+
async_to_sync(self.repository.save)(child)
|
|
133
|
+
|
|
134
|
+
def test_id_selection_is_one_limited_select(self):
|
|
135
|
+
query = EntityQuery(ContainsAny('ancestor_ids', [self.root.entity_id]), PageRequest(limit=2))
|
|
136
|
+
|
|
137
|
+
with CaptureQueriesContext(connection) as captured:
|
|
138
|
+
page = async_to_sync(self.repository.list_entities)(query)
|
|
139
|
+
|
|
140
|
+
selects = [q['sql'] for q in captured.captured_queries if q['sql'].lstrip().upper().startswith('SELECT')]
|
|
141
|
+
id_selects = [sql for sql in selects if 'LIMIT 3' in sql]
|
|
142
|
+
self.assertEqual(len(id_selects), 1, selects)
|
|
143
|
+
self.assertIn(
|
|
144
|
+
'ancestor_ids' if connection.features.supports_json_field_contains else 'json_each',
|
|
145
|
+
id_selects[0],
|
|
146
|
+
)
|
|
147
|
+
self.assertIn(self.root.entity_id, id_selects[0])
|
|
148
|
+
self.assertEqual(len(page.entities), 2)
|
|
149
|
+
self.assertIsNotNone(page.next_cursor)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class TypeHintTests(TestCase):
|
|
153
|
+
"""`load` remembers each id's type to skip the base-row lookup, and
|
|
154
|
+
verifies the hint against the typed row it reads."""
|
|
155
|
+
|
|
156
|
+
def setUp(self):
|
|
157
|
+
self.repository = make_repository()
|
|
158
|
+
self.entity = make(Entity, DEFAULT_REGISTRY)
|
|
159
|
+
async_to_sync(self.repository.save)(self.entity)
|
|
160
|
+
|
|
161
|
+
def test_a_known_type_loads_in_one_select_with_its_children(self):
|
|
162
|
+
child = make(Entity, DEFAULT_REGISTRY)
|
|
163
|
+
child.set_parent(self.entity.entity_id)
|
|
164
|
+
async_to_sync(self.repository.save)(child)
|
|
165
|
+
|
|
166
|
+
with CaptureQueriesContext(connection) as captured:
|
|
167
|
+
loaded = async_to_sync(self.repository.load)(self.entity.entity_id)
|
|
168
|
+
|
|
169
|
+
self.assertEqual(loaded.children_ids, (child.entity_id,))
|
|
170
|
+
self.assertEqual(len(captured.captured_queries), 1 if connection.vendor == 'postgresql' else 2)
|
|
171
|
+
|
|
172
|
+
def test_an_id_recreated_as_another_type_loads_as_that_type(self):
|
|
173
|
+
EntityModel.objects.filter(entity_id=self.entity.entity_id).delete()
|
|
174
|
+
recreated = StrategyRequest(entity_id=self.entity.entity_id)
|
|
175
|
+
recreated.strategy_name = 'Recreated'
|
|
176
|
+
async_to_sync(make_repository().save)(recreated)
|
|
177
|
+
|
|
178
|
+
loaded = async_to_sync(self.repository.load)(self.entity.entity_id)
|
|
179
|
+
|
|
180
|
+
self.assertIsInstance(loaded, StrategyRequest)
|
|
181
|
+
self.assertEqual(loaded.strategy_name, 'Recreated')
|
|
182
|
+
|
|
183
|
+
def test_a_deleted_id_loads_as_missing(self):
|
|
184
|
+
EntityModel.objects.filter(entity_id=self.entity.entity_id).delete()
|
|
185
|
+
|
|
186
|
+
self.assertIsNone(async_to_sync(self.repository.load)(self.entity.entity_id))
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class StorageShapeTests(TestCase):
|
|
190
|
+
"""What the repository writes is exactly the row - typed columns and all."""
|
|
191
|
+
|
|
192
|
+
def setUp(self):
|
|
193
|
+
self.repository = make_repository()
|
|
194
|
+
|
|
195
|
+
def test_ancestors_land_in_their_own_column(self):
|
|
196
|
+
root = make(Entity, DEFAULT_REGISTRY)
|
|
197
|
+
async_to_sync(self.repository.save)(root)
|
|
198
|
+
child = make(Entity, DEFAULT_REGISTRY)
|
|
199
|
+
child.set_parent(root.entity_id)
|
|
200
|
+
async_to_sync(self.repository.save)(child)
|
|
201
|
+
|
|
202
|
+
row = EntityModel.objects.get(entity_id=child.entity_id)
|
|
203
|
+
self.assertEqual(row.ancestor_ids, [root.entity_id])
|
|
204
|
+
self.assertNotIn('ancestor_ids', row.attributes)
|
|
205
|
+
|
|
206
|
+
def test_non_identifiers_resolve_to_nothing_rather_than_raising(self):
|
|
207
|
+
self.assertEqual(async_to_sync(self.repository.load_many)(['*', 'not-an-id']), {})
|
|
208
|
+
self.assertEqual(async_to_sync(self.repository.versions)(['*']), {})
|
|
209
|
+
self.assertIsNone(async_to_sync(self.repository.load)('*'))
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
class ReferenceIndexTests(TestCase):
|
|
213
|
+
"""`EntityReferenceModel` mirrors `reference_ids` and is what `referrers`
|
|
214
|
+
reads; the jsonb column is never queried for reverse lookup."""
|
|
215
|
+
|
|
216
|
+
def setUp(self):
|
|
217
|
+
self.repository = make_repository()
|
|
218
|
+
|
|
219
|
+
def save(self, entity):
|
|
220
|
+
return async_to_sync(self.repository.save)(entity)
|
|
221
|
+
|
|
222
|
+
def entity(self, **attributes):
|
|
223
|
+
entity = make(Entity, DEFAULT_REGISTRY)
|
|
224
|
+
for key, value in attributes.items():
|
|
225
|
+
entity.set_attribute(key, value)
|
|
226
|
+
self.save(entity)
|
|
227
|
+
return entity
|
|
228
|
+
|
|
229
|
+
def edge_targets(self, source):
|
|
230
|
+
return set(
|
|
231
|
+
EntityReferenceModel.objects.filter(source_id=source.entity_id)
|
|
232
|
+
.values_list('target_id', flat=True)
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
def test_edges_mirror_the_projection_through_updates(self):
|
|
236
|
+
first, second = self.entity(), self.entity()
|
|
237
|
+
source = self.entity(spec=first.entity_id)
|
|
238
|
+
self.assertEqual(self.edge_targets(source), {first.entity_id})
|
|
239
|
+
|
|
240
|
+
source.set_attribute('customer', second.entity_id)
|
|
241
|
+
self.save(source)
|
|
242
|
+
self.assertEqual(self.edge_targets(source), {first.entity_id, second.entity_id})
|
|
243
|
+
|
|
244
|
+
source.remove_attribute('spec')
|
|
245
|
+
self.save(source)
|
|
246
|
+
self.assertEqual(self.edge_targets(source), {second.entity_id})
|
|
247
|
+
self.assertEqual(self.edge_targets(source), set(source.reference_ids))
|
|
248
|
+
|
|
249
|
+
def test_a_late_rebuild_follows_the_committed_row(self):
|
|
250
|
+
first, second = self.entity(), self.entity()
|
|
251
|
+
source = self.entity(spec=first.entity_id)
|
|
252
|
+
source.set_attribute('spec', second.entity_id)
|
|
253
|
+
self.save(source)
|
|
254
|
+
|
|
255
|
+
# The first save's rebuild running after the second's, as when two
|
|
256
|
+
# attempts save one entity concurrently.
|
|
257
|
+
for _ in range(2):
|
|
258
|
+
async_to_sync(self.repository._replace_edges)(source.entity_id)
|
|
259
|
+
|
|
260
|
+
self.assertEqual(self.edge_targets(source), {second.entity_id})
|
|
261
|
+
|
|
262
|
+
def test_an_unchanged_projection_writes_no_edges(self):
|
|
263
|
+
source = self.entity(spec=self.entity().entity_id)
|
|
264
|
+
source.set_attribute('name', 'renamed')
|
|
265
|
+
|
|
266
|
+
with CaptureQueriesContext(connection) as captured:
|
|
267
|
+
self.save(source)
|
|
268
|
+
|
|
269
|
+
edge_writes = [
|
|
270
|
+
q['sql'] for q in captured.captured_queries
|
|
271
|
+
if 'graphcore_entityreferencemodel' in q['sql']
|
|
272
|
+
]
|
|
273
|
+
self.assertEqual(edge_writes, [])
|
|
274
|
+
|
|
275
|
+
def test_deleting_a_source_removes_its_edges(self):
|
|
276
|
+
source = self.entity(spec=self.entity().entity_id)
|
|
277
|
+
async_to_sync(self.repository.delete_many)([source.entity_id])
|
|
278
|
+
|
|
279
|
+
self.assertFalse(EntityReferenceModel.objects.filter(source_id=source.entity_id).exists())
|
|
280
|
+
|
|
281
|
+
def test_referrers_is_one_limited_select_through_the_edge_table(self):
|
|
282
|
+
target = self.entity()
|
|
283
|
+
for _ in range(3):
|
|
284
|
+
self.entity(spec=target.entity_id)
|
|
285
|
+
|
|
286
|
+
with CaptureQueriesContext(connection) as captured:
|
|
287
|
+
page = async_to_sync(self.repository.referrers)(
|
|
288
|
+
target.entity_id, AllOf(()), PageRequest(limit=2),
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
id_selects = [q['sql'] for q in captured.captured_queries if 'LIMIT 3' in q['sql']]
|
|
292
|
+
self.assertEqual(len(id_selects), 1, id_selects)
|
|
293
|
+
self.assertIn('graphcore_entityreferencemodel', id_selects[0])
|
|
294
|
+
self.assertNotIn('reference_ids', id_selects[0])
|
|
295
|
+
self.assertEqual(len(page.entities), 2)
|
|
296
|
+
|
|
297
|
+
def test_a_cursor_bounds_the_edge_scan_itself(self):
|
|
298
|
+
target = self.entity()
|
|
299
|
+
for _ in range(3):
|
|
300
|
+
self.entity(spec=target.entity_id)
|
|
301
|
+
first = async_to_sync(self.repository.referrers)(
|
|
302
|
+
target.entity_id, AllOf(()), PageRequest(limit=1),
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
with CaptureQueriesContext(connection) as captured:
|
|
306
|
+
async_to_sync(self.repository.referrers)(
|
|
307
|
+
target.entity_id, AllOf(()), PageRequest(limit=1, cursor=first.next_cursor),
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
id_select = next(q['sql'] for q in captured.captured_queries if 'LIMIT 2' in q['sql'])
|
|
311
|
+
self.assertRegex(id_select, r'"graphcore_entityreferencemodel"\."source_id" > ')
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
class OptimisticLockingTests(TestCase):
|
|
315
|
+
"""The version check is a compare-and-swap in SQL, not a read-then-write."""
|
|
316
|
+
|
|
317
|
+
def setUp(self):
|
|
318
|
+
self.repository = make_repository()
|
|
319
|
+
|
|
320
|
+
def test_update_is_a_conditional_update_with_a_database_side_increment(self):
|
|
321
|
+
from django.db.models.expressions import BaseExpression
|
|
322
|
+
|
|
323
|
+
entity = make(Entity, DEFAULT_REGISTRY)
|
|
324
|
+
async_to_sync(self.repository.save)(entity)
|
|
325
|
+
entity.set_attribute('name', 'second')
|
|
326
|
+
|
|
327
|
+
with patch.object(
|
|
328
|
+
EntityModel.objects, 'filter', wraps=EntityModel.objects.filter,
|
|
329
|
+
) as spy:
|
|
330
|
+
async_to_sync(self.repository.save)(entity)
|
|
331
|
+
|
|
332
|
+
locking_calls = [call.kwargs for call in spy.call_args_list if 'version' in call.kwargs]
|
|
333
|
+
self.assertEqual(len(locking_calls), 1)
|
|
334
|
+
self.assertEqual(locking_calls[0]['entity_id'], entity.entity_id)
|
|
335
|
+
self.assertEqual(locking_calls[0]['version'], 1)
|
|
336
|
+
|
|
337
|
+
def test_version_increment_is_an_expression_not_a_python_value(self):
|
|
338
|
+
from django.db.models.expressions import BaseExpression
|
|
339
|
+
|
|
340
|
+
entity = make(Entity, DEFAULT_REGISTRY)
|
|
341
|
+
async_to_sync(self.repository.save)(entity)
|
|
342
|
+
|
|
343
|
+
captured = {}
|
|
344
|
+
original = EntityModel.objects.filter
|
|
345
|
+
|
|
346
|
+
def spy(*args, **kwargs):
|
|
347
|
+
queryset = original(*args, **kwargs)
|
|
348
|
+
if 'version' in kwargs:
|
|
349
|
+
real_update = queryset.update
|
|
350
|
+
|
|
351
|
+
def capture(**update_fields):
|
|
352
|
+
captured.update(update_fields)
|
|
353
|
+
return real_update(**update_fields)
|
|
354
|
+
|
|
355
|
+
queryset.update = capture
|
|
356
|
+
return queryset
|
|
357
|
+
|
|
358
|
+
with patch.object(EntityModel.objects, 'filter', spy):
|
|
359
|
+
async_to_sync(self.repository.save)(entity)
|
|
360
|
+
|
|
361
|
+
self.assertIsInstance(captured['version'], BaseExpression)
|
|
362
|
+
self.assertIn('version', str(captured['version']).lower())
|
|
363
|
+
|
|
364
|
+
def test_a_stale_version_never_writes(self):
|
|
365
|
+
entity = make(Entity, DEFAULT_REGISTRY)
|
|
366
|
+
async_to_sync(self.repository.save)(entity)
|
|
367
|
+
stale = async_to_sync(self.repository.load)(entity.entity_id)
|
|
368
|
+
async_to_sync(self.repository.save)(entity)
|
|
369
|
+
|
|
370
|
+
stale.set_attribute('name', 'lost update')
|
|
371
|
+
with self.assertRaises(ConcurrentModificationError):
|
|
372
|
+
async_to_sync(self.repository.save)(stale)
|
|
373
|
+
|
|
374
|
+
self.assertFalse(
|
|
375
|
+
async_to_sync(self.repository.load)(entity.entity_id).has_attribute('name')
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
class MultiTableInheritanceTests(TestCase):
|
|
380
|
+
"""A typed entity's own columns are written on create and on update, in
|
|
381
|
+
the child table - not smuggled into the base row's attributes bag."""
|
|
382
|
+
|
|
383
|
+
def setUp(self):
|
|
384
|
+
self.repository = make_repository()
|
|
385
|
+
|
|
386
|
+
def build(self):
|
|
387
|
+
entity = make(StrategyRequest, DEFAULT_REGISTRY)
|
|
388
|
+
entity.strategy_name = 'TestStrategy'
|
|
389
|
+
entity.param_config = {'key': 'value'}
|
|
390
|
+
entity.target_entity_id = str(make(Entity, DEFAULT_REGISTRY).entity_id)
|
|
391
|
+
entity.ret_val = {'result': 'success'}
|
|
392
|
+
entity.set_attribute('test_attr', 'test_value')
|
|
393
|
+
return entity
|
|
394
|
+
|
|
395
|
+
def test_create_writes_both_tables(self):
|
|
396
|
+
entity = self.build()
|
|
397
|
+
async_to_sync(self.repository.save)(entity)
|
|
398
|
+
|
|
399
|
+
row = StrategyRequestModel.objects.get(entity_id=entity.entity_id)
|
|
400
|
+
self.assertEqual(row.strategy_name, 'TestStrategy')
|
|
401
|
+
self.assertEqual(row.param_config, {'key': 'value'})
|
|
402
|
+
self.assertEqual(row.attributes['test_attr'], 'test_value')
|
|
403
|
+
self.assertEqual(
|
|
404
|
+
EntityModel.objects.get(entity_id=entity.entity_id).entity_type_id,
|
|
405
|
+
'graphcore.StrategyRequest',
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
def test_update_writes_typed_and_base_columns_together(self):
|
|
409
|
+
entity = self.build()
|
|
410
|
+
async_to_sync(self.repository.save)(entity)
|
|
411
|
+
|
|
412
|
+
entity.strategy_name = 'ChangedStrategy'
|
|
413
|
+
entity.ret_val = {'result': 'changed'}
|
|
414
|
+
entity.set_attribute('test_attr', 'changed')
|
|
415
|
+
async_to_sync(self.repository.save)(entity)
|
|
416
|
+
|
|
417
|
+
row = StrategyRequestModel.objects.get(entity_id=entity.entity_id)
|
|
418
|
+
self.assertEqual(row.strategy_name, 'ChangedStrategy')
|
|
419
|
+
self.assertEqual(row.ret_val, {'result': 'changed'})
|
|
420
|
+
self.assertEqual(row.attributes['test_attr'], 'changed')
|
|
421
|
+
self.assertEqual(row.version, 2)
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
class TypedConcurrentSaveTests(TransactionTestCase):
|
|
425
|
+
"""A stale save of a typed entity must not land behind a concurrent one."""
|
|
426
|
+
|
|
427
|
+
def test_a_stale_save_racing_an_uncommitted_one_is_refused(self):
|
|
428
|
+
import threading
|
|
429
|
+
from django.db import close_old_connections, transaction
|
|
430
|
+
|
|
431
|
+
if connection.vendor == 'sqlite':
|
|
432
|
+
self.skipTest('SQLite serializes writers')
|
|
433
|
+
repository = make_repository()
|
|
434
|
+
entity = make(StrategyRequest, DEFAULT_REGISTRY)
|
|
435
|
+
async_to_sync(repository.save)(entity)
|
|
436
|
+
first = async_to_sync(repository.load)(entity.entity_id)
|
|
437
|
+
stale = async_to_sync(repository.load)(entity.entity_id)
|
|
438
|
+
held, release, outcome = threading.Event(), threading.Event(), {}
|
|
439
|
+
|
|
440
|
+
def save_and_hold():
|
|
441
|
+
try:
|
|
442
|
+
with transaction.atomic():
|
|
443
|
+
first.ret_val = {'by': 'first'}
|
|
444
|
+
async_to_sync(repository.save)(first)
|
|
445
|
+
held.set()
|
|
446
|
+
release.wait(5)
|
|
447
|
+
finally:
|
|
448
|
+
close_old_connections()
|
|
449
|
+
|
|
450
|
+
def save_stale():
|
|
451
|
+
try:
|
|
452
|
+
stale.ret_val = {'by': 'stale'}
|
|
453
|
+
async_to_sync(repository.save)(stale)
|
|
454
|
+
outcome['saved'] = True
|
|
455
|
+
except ConcurrentModificationError:
|
|
456
|
+
outcome['saved'] = False
|
|
457
|
+
finally:
|
|
458
|
+
close_old_connections()
|
|
459
|
+
|
|
460
|
+
holder = threading.Thread(target=save_and_hold)
|
|
461
|
+
holder.start()
|
|
462
|
+
self.assertTrue(held.wait(5))
|
|
463
|
+
racer = threading.Thread(target=save_stale)
|
|
464
|
+
racer.start()
|
|
465
|
+
racer.join(0.5)
|
|
466
|
+
release.set()
|
|
467
|
+
holder.join(5)
|
|
468
|
+
racer.join(5)
|
|
469
|
+
|
|
470
|
+
self.assertEqual(outcome, {'saved': False})
|
|
471
|
+
row = StrategyRequestModel.objects.get(entity_id=entity.entity_id)
|
|
472
|
+
self.assertEqual((row.ret_val, row.version), ({'by': 'first'}, 2))
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""Two app models that both declare `slug`, against a real database: a type
|
|
2
|
+
pin picks one column, a pin over both matches either, and no pin is refused.
|
|
3
|
+
|
|
4
|
+
The models join the real `graphcore` app registry for this class only - an
|
|
5
|
+
MTI child's reverse join is resolved through its parent's registry, so an
|
|
6
|
+
isolated one would leave `EntityModel` unable to reach it.
|
|
7
|
+
"""
|
|
8
|
+
from asgiref.sync import async_to_sync
|
|
9
|
+
from django.apps import apps
|
|
10
|
+
from django.db import connection, models
|
|
11
|
+
from django.test import TestCase
|
|
12
|
+
|
|
13
|
+
from graphcore.domain.entity.Entity import Entity
|
|
14
|
+
from graphcore.errors import UnsupportedQuery
|
|
15
|
+
from graphcore.persistence.django.models import EntityModel
|
|
16
|
+
from graphcore.persistence.django.repository import DjangoEntityRepository
|
|
17
|
+
from graphcore.persistence.tests.fixtures import make
|
|
18
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
19
|
+
from graphcore.types.query import AllOf, Equal, InValues
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _slug_model(name):
|
|
23
|
+
return type(name, (EntityModel,), {
|
|
24
|
+
'__module__': __name__,
|
|
25
|
+
'slug': models.CharField(max_length=50, null=True),
|
|
26
|
+
'Meta': type('Meta', (), {'app_label': 'graphcore'}),
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class SlugBoard(Entity):
|
|
31
|
+
def __init__(self, entity_id):
|
|
32
|
+
super().__init__(entity_id)
|
|
33
|
+
self.slug = None
|
|
34
|
+
|
|
35
|
+
def to_dict(self):
|
|
36
|
+
return {**super().to_dict(), 'slug': self.slug}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class SlugDoc(SlugBoard):
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class SharedColumnTests(TestCase):
|
|
44
|
+
@classmethod
|
|
45
|
+
def setUpClass(cls):
|
|
46
|
+
# DDL before the class-wide transaction opens: SQLite's schema
|
|
47
|
+
# editor refuses to run inside one.
|
|
48
|
+
cls.board_model = _slug_model('SlugBoardModel')
|
|
49
|
+
cls.doc_model = _slug_model('SlugDocModel')
|
|
50
|
+
with connection.schema_editor() as editor:
|
|
51
|
+
editor.create_model(cls.board_model)
|
|
52
|
+
editor.create_model(cls.doc_model)
|
|
53
|
+
super().setUpClass()
|
|
54
|
+
|
|
55
|
+
@classmethod
|
|
56
|
+
def tearDownClass(cls):
|
|
57
|
+
super().tearDownClass()
|
|
58
|
+
with connection.schema_editor() as editor:
|
|
59
|
+
editor.delete_model(cls.board_model)
|
|
60
|
+
editor.delete_model(cls.doc_model)
|
|
61
|
+
for model in (cls.board_model, cls.doc_model):
|
|
62
|
+
del apps.all_models['graphcore'][model._meta.model_name]
|
|
63
|
+
delattr(EntityModel, model._meta.model_name)
|
|
64
|
+
apps.clear_cache()
|
|
65
|
+
|
|
66
|
+
def setUp(self):
|
|
67
|
+
self.registry = DEFAULT_REGISTRY.fork_open()
|
|
68
|
+
for entity_cls, model in ((SlugBoard, self.board_model), (SlugDoc, self.doc_model)):
|
|
69
|
+
self.registry.entities.register_entity(entity_cls, app_label='slugtest')
|
|
70
|
+
self.registry.entities.bind_storage(entity_cls, model)
|
|
71
|
+
self.repository = DjangoEntityRepository(self.registry.entities)
|
|
72
|
+
|
|
73
|
+
self.board_x = self._save(SlugBoard, 'x')
|
|
74
|
+
self.board_y = self._save(SlugBoard, 'y')
|
|
75
|
+
self.board_none = self._save(SlugBoard, None)
|
|
76
|
+
self.doc_x = self._save(SlugDoc, 'x')
|
|
77
|
+
self.plain = self._save(Entity, None)
|
|
78
|
+
|
|
79
|
+
def _save(self, entity_cls, slug):
|
|
80
|
+
entity = make(entity_cls, self.registry)
|
|
81
|
+
if entity_cls is not Entity:
|
|
82
|
+
entity.slug = slug
|
|
83
|
+
async_to_sync(self.repository.save)(entity)
|
|
84
|
+
return entity.entity_id
|
|
85
|
+
|
|
86
|
+
def _ids(self, predicate):
|
|
87
|
+
return async_to_sync(self.repository.list_ids)(predicate)
|
|
88
|
+
|
|
89
|
+
def _pin(self, *entity_classes):
|
|
90
|
+
return InValues('entity_type_id', [c.get_entity_type_id() for c in entity_classes])
|
|
91
|
+
|
|
92
|
+
def test_one_pinned_type_matches_its_own_column(self):
|
|
93
|
+
self.assertEqual(
|
|
94
|
+
self._ids(AllOf((self._pin(SlugBoard), Equal('slug', 'x')))),
|
|
95
|
+
{self.board_x},
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
def test_both_pinned_types_match_either_column(self):
|
|
99
|
+
self.assertEqual(
|
|
100
|
+
self._ids(AllOf((self._pin(SlugBoard, SlugDoc), Equal('slug', 'x')))),
|
|
101
|
+
{self.board_x, self.doc_x},
|
|
102
|
+
)
|
|
103
|
+
self.assertEqual(
|
|
104
|
+
self._ids(AllOf((self._pin(SlugBoard, SlugDoc), InValues('slug', ['x', 'y'])))),
|
|
105
|
+
{self.board_x, self.board_y, self.doc_x},
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
def test_a_null_check_over_both_types_never_matches_another_type(self):
|
|
109
|
+
self.assertEqual(
|
|
110
|
+
self._ids(AllOf((self._pin(SlugBoard, SlugDoc, Entity), Equal('slug', None)))),
|
|
111
|
+
{self.board_none},
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
def test_the_column_round_trips_through_hydration(self):
|
|
115
|
+
entity = async_to_sync(self.repository.load)(self.doc_x)
|
|
116
|
+
self.assertEqual(entity.slug, 'x')
|
|
117
|
+
|
|
118
|
+
def test_an_unpinned_shared_name_is_refused(self):
|
|
119
|
+
with self.assertRaises(UnsupportedQuery):
|
|
120
|
+
self._ids(Equal('slug', 'x'))
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import contextvars
|
|
3
|
+
import threading
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
from asgiref.sync import async_to_sync, sync_to_async
|
|
7
|
+
from django.test import SimpleTestCase
|
|
8
|
+
|
|
9
|
+
from graphcore.persistence.django.threads import use_shared_threads
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _blocking_thread_name():
|
|
13
|
+
time.sleep(0.05)
|
|
14
|
+
return threading.current_thread().name
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
THREADS = 3
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
async def _query(shared: bool) -> str:
|
|
21
|
+
if shared:
|
|
22
|
+
use_shared_threads(THREADS)
|
|
23
|
+
return await sync_to_async(_blocking_thread_name)()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class SharedThreadsTests(SimpleTestCase):
|
|
27
|
+
def _gather(self, count: int, shared: bool) -> list[str]:
|
|
28
|
+
async def run():
|
|
29
|
+
return await asyncio.gather(*(_query(shared) for _ in range(count)))
|
|
30
|
+
return asyncio.run(run())
|
|
31
|
+
|
|
32
|
+
def test_tasks_spread_over_the_shared_threads_up_to_the_cap(self):
|
|
33
|
+
names = self._gather(THREADS * 3, shared=True)
|
|
34
|
+
|
|
35
|
+
self.assertTrue(all(name.startswith('graphcore-db') for name in names), names)
|
|
36
|
+
self.assertEqual(len(set(names)), THREADS)
|
|
37
|
+
|
|
38
|
+
def test_a_task_that_does_not_ask_keeps_asgirefs_single_thread(self):
|
|
39
|
+
names = self._gather(4, shared=False)
|
|
40
|
+
|
|
41
|
+
self.assertEqual(len(set(names)), 1)
|
|
42
|
+
self.assertFalse(names[0].startswith('graphcore-db'))
|
|
43
|
+
|
|
44
|
+
def test_under_async_to_sync_the_waiting_thread_keeps_the_queries(self):
|
|
45
|
+
async def in_fresh_context():
|
|
46
|
+
# As asgiref's ApplicationCommunicator starts a consumer.
|
|
47
|
+
return await contextvars.Context().run(asyncio.create_task, _query(True))
|
|
48
|
+
|
|
49
|
+
self.assertEqual(async_to_sync(_query)(True), threading.current_thread().name)
|
|
50
|
+
self.assertEqual(async_to_sync(in_fresh_context)(), threading.current_thread().name)
|