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,269 @@
|
|
|
1
|
+
from django.core.exceptions import EmptyResultSet
|
|
2
|
+
from django.db import connection
|
|
3
|
+
from django.test import SimpleTestCase
|
|
4
|
+
|
|
5
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
6
|
+
from graphcore.domain.entity.group.Group import Group
|
|
7
|
+
from graphcore.errors import InvalidQuery, UnsupportedQuery
|
|
8
|
+
from graphcore.persistence.django.compiler import QueryCompiler
|
|
9
|
+
from graphcore.persistence.django.models import EntityModel
|
|
10
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
11
|
+
from graphcore.types.query import (
|
|
12
|
+
AllOf, AnyOf, ContainsAny, ContainsValue, Equal, InValues, Predicate,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def compiler() -> QueryCompiler:
|
|
17
|
+
return QueryCompiler(DEFAULT_REGISTRY.entities)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def sql_for(predicate) -> str:
|
|
21
|
+
return str(EntityModel.objects.filter(compiler().compile(predicate)).query)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _FakeField:
|
|
25
|
+
"""Just enough of a Django `Field` for `QueryCompiler._build_paths`."""
|
|
26
|
+
|
|
27
|
+
def __init__(self, attname, primary_key=False, internal_type='CharField'):
|
|
28
|
+
self.attname = attname
|
|
29
|
+
self.primary_key = primary_key
|
|
30
|
+
self._internal_type = internal_type
|
|
31
|
+
|
|
32
|
+
def get_internal_type(self):
|
|
33
|
+
return self._internal_type
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class _FakeModel:
|
|
37
|
+
"""Just enough of a Django `Model` for `QueryCompiler._build_paths`."""
|
|
38
|
+
|
|
39
|
+
def __init__(self, model_name, fields):
|
|
40
|
+
self._meta = type('Meta', (), {
|
|
41
|
+
'model_name': model_name,
|
|
42
|
+
'local_concrete_fields': fields,
|
|
43
|
+
'get_parent_list': staticmethod(lambda: [EntityModel]),
|
|
44
|
+
})()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class _FakeDirectory:
|
|
48
|
+
"""`models` maps a type id to its bound model."""
|
|
49
|
+
|
|
50
|
+
def __init__(self, models):
|
|
51
|
+
self._models = models
|
|
52
|
+
|
|
53
|
+
def storage_classes(self):
|
|
54
|
+
return {EntityModel, *self._models.values()}
|
|
55
|
+
|
|
56
|
+
def get_entity_class(self, entity_type_id):
|
|
57
|
+
return entity_type_id if entity_type_id in self._models else None
|
|
58
|
+
|
|
59
|
+
def storage(self, entity_cls):
|
|
60
|
+
return self._models[entity_cls]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def colliding_compiler(doc_slug_type='CharField') -> QueryCompiler:
|
|
64
|
+
"""Two apps whose bound models both declare `slug`."""
|
|
65
|
+
return QueryCompiler(_FakeDirectory({
|
|
66
|
+
'a.Board': _FakeModel('model_a', [_FakeField('slug')]),
|
|
67
|
+
'b.Doc': _FakeModel('model_b', [_FakeField('slug', internal_type=doc_slug_type)]),
|
|
68
|
+
}))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
BOTH_TYPES = InValues('entity_type_id', ['a.Board', 'b.Doc'])
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class CompiledSqlTests(SimpleTestCase):
|
|
75
|
+
def test_equal(self):
|
|
76
|
+
self.assertIn('"owner" = u1', sql_for(Equal('owner', 'u1')))
|
|
77
|
+
|
|
78
|
+
def test_equal_none_is_a_null_check(self):
|
|
79
|
+
self.assertIn('"parent_id" IS NULL', sql_for(Equal('parent_id', None)))
|
|
80
|
+
|
|
81
|
+
def test_in_values(self):
|
|
82
|
+
self.assertIn('"entity_type_id" IN (t1)', sql_for(InValues('entity_type_id', ['t1'])))
|
|
83
|
+
|
|
84
|
+
def test_empty_in_values_matches_nothing(self):
|
|
85
|
+
with self.assertRaises(EmptyResultSet):
|
|
86
|
+
EntityModel.objects.filter(
|
|
87
|
+
compiler().compile(InValues('entity_id', []))
|
|
88
|
+
).query.sql_with_params()
|
|
89
|
+
|
|
90
|
+
def test_collection_containment_uses_the_backend_operator(self):
|
|
91
|
+
sql = sql_for(ContainsValue('ancestor_ids', 's1'))
|
|
92
|
+
if connection.features.supports_json_field_contains:
|
|
93
|
+
self.assertIn('@>', sql)
|
|
94
|
+
else:
|
|
95
|
+
self.assertIn('json_each', sql)
|
|
96
|
+
self.assertIn('"ancestor_ids"', sql)
|
|
97
|
+
|
|
98
|
+
def test_a_pinned_child_table_collection_reaches_the_child_column(self):
|
|
99
|
+
sql = sql_for(AllOf((
|
|
100
|
+
Equal('entity_type_id', Group.get_entity_type_id()),
|
|
101
|
+
ContainsValue('member_ids', 'u1'),
|
|
102
|
+
)))
|
|
103
|
+
self.assertIn('groupmodel', sql.lower())
|
|
104
|
+
self.assertIn('"member_ids"', sql)
|
|
105
|
+
if connection.features.supports_json_field_contains:
|
|
106
|
+
self.assertIn('@>', sql)
|
|
107
|
+
else:
|
|
108
|
+
self.assertIn('json_each', sql)
|
|
109
|
+
|
|
110
|
+
def test_an_unpinned_child_table_collection_is_refused(self):
|
|
111
|
+
with self.assertRaisesMessage(UnsupportedQuery, 'entity_type_id'):
|
|
112
|
+
compiler().compile(ContainsValue('member_ids', 'u1'))
|
|
113
|
+
|
|
114
|
+
def test_contains_any_covers_every_value(self):
|
|
115
|
+
sql = sql_for(ContainsAny('ancestor_ids', ['s1', 's2']))
|
|
116
|
+
self.assertIn('s1', sql)
|
|
117
|
+
self.assertIn('s2', sql)
|
|
118
|
+
|
|
119
|
+
def test_grant_columns_join_through_multi_table_inheritance(self):
|
|
120
|
+
sql = sql_for(Equal('subject_id', 'u1'))
|
|
121
|
+
self.assertIn('grantmodel', sql.lower())
|
|
122
|
+
|
|
123
|
+
def test_child_column_null_check_pins_the_join(self):
|
|
124
|
+
"""A `LEFT OUTER JOIN` row that doesn't exist for this entity is also
|
|
125
|
+
`NULL` on every child column - the compiler must pin the join with
|
|
126
|
+
an explicit `IS NOT NULL` alongside the column's `IS NULL`, or every
|
|
127
|
+
non-Grant entity would match too."""
|
|
128
|
+
sql = sql_for(Equal('subject_id', None))
|
|
129
|
+
self.assertIn('grantmodel', sql.lower())
|
|
130
|
+
self.assertIn('IS NOT NULL', sql)
|
|
131
|
+
self.assertIn('IS NULL', sql)
|
|
132
|
+
|
|
133
|
+
def test_conjunction_and_disjunction(self):
|
|
134
|
+
sql = sql_for(AllOf((Equal('owner', 'u1'), Equal('parent_id', None))))
|
|
135
|
+
self.assertIn('AND', sql)
|
|
136
|
+
self.assertIn('OR', sql_for(AnyOf((Equal('owner', 'u1'), Equal('owner', 'u2')))))
|
|
137
|
+
|
|
138
|
+
def test_empty_conjunction_selects_everything(self):
|
|
139
|
+
self.assertNotIn('WHERE', sql_for(AllOf(())))
|
|
140
|
+
|
|
141
|
+
def test_a_column_absent_from_to_dict_is_still_filterable(self):
|
|
142
|
+
"""`created_at`/`version` are real columns on the bound model, so
|
|
143
|
+
they are filterable even though they never round-trip through
|
|
144
|
+
`Entity.to_dict()` (see the plan's accepted trade-offs)."""
|
|
145
|
+
self.assertIn('"version" = 1', sql_for(Equal('version', 1)))
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class RefusalTests(SimpleTestCase):
|
|
149
|
+
def test_unknown_field_is_refused_before_any_query(self):
|
|
150
|
+
with self.assertRaises(UnsupportedQuery):
|
|
151
|
+
compiler().compile(Equal('no_such_column', 'x'))
|
|
152
|
+
|
|
153
|
+
def test_non_collection_field_cannot_be_asked_for_containment(self):
|
|
154
|
+
with self.assertRaises(UnsupportedQuery):
|
|
155
|
+
compiler().compile(ContainsValue('owner', 'u1'))
|
|
156
|
+
|
|
157
|
+
def test_attributes_is_not_a_collection_field(self):
|
|
158
|
+
with self.assertRaises(UnsupportedQuery):
|
|
159
|
+
compiler().compile(ContainsValue('attributes', 'x'))
|
|
160
|
+
|
|
161
|
+
def test_a_child_table_json_column_cannot_be_asked_for_containment(self):
|
|
162
|
+
with self.assertRaises(UnsupportedQuery):
|
|
163
|
+
compiler().compile(ContainsValue('callbacks', 'x'))
|
|
164
|
+
|
|
165
|
+
def test_a_field_name_bound_on_two_models_is_ambiguous(self):
|
|
166
|
+
"""Two distinct bound models that both declare the same column name
|
|
167
|
+
refuse rather than silently OR-ing across types - a name collision
|
|
168
|
+
is an app author's problem to rename, not the compiler's to guess
|
|
169
|
+
at (see the plan's accepted trade-offs)."""
|
|
170
|
+
with self.assertRaises(UnsupportedQuery):
|
|
171
|
+
colliding_compiler().compile(Equal('slug', 'x'))
|
|
172
|
+
|
|
173
|
+
def test_unregistered_node_type_is_refused(self):
|
|
174
|
+
class Custom(Predicate):
|
|
175
|
+
pass
|
|
176
|
+
|
|
177
|
+
with self.assertRaises(UnsupportedQuery):
|
|
178
|
+
compiler().compile(Custom())
|
|
179
|
+
|
|
180
|
+
def test_a_predicate_subclass_does_not_inherit_its_base_handler(self):
|
|
181
|
+
class SneakyEqual(Equal):
|
|
182
|
+
pass
|
|
183
|
+
|
|
184
|
+
with self.assertRaises(UnsupportedQuery):
|
|
185
|
+
compiler().compile(SneakyEqual('owner', 'u1'))
|
|
186
|
+
|
|
187
|
+
def test_unsupported_literal_type_is_refused(self):
|
|
188
|
+
with self.assertRaises(InvalidQuery):
|
|
189
|
+
compiler().compile(Equal('owner', {'not': 'a literal'}))
|
|
190
|
+
with self.assertRaises(InvalidQuery):
|
|
191
|
+
compiler().compile(InValues('owner', [('tuple',)]))
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class TypePinnedResolutionTests(SimpleTestCase):
|
|
195
|
+
"""An `AllOf` that pins `entity_type_id` resolves its field names
|
|
196
|
+
against those types' models only: one pinned type picks its column, and
|
|
197
|
+
several pinned types that share a name match any of their columns."""
|
|
198
|
+
|
|
199
|
+
def test_an_equal_type_pin_picks_that_types_column(self):
|
|
200
|
+
q = colliding_compiler().compile(AllOf((
|
|
201
|
+
Equal('entity_type_id', 'a.Board'), Equal('slug', 'x'),
|
|
202
|
+
)))
|
|
203
|
+
self.assertIn('model_a__slug', str(q))
|
|
204
|
+
self.assertNotIn('model_b__slug', str(q))
|
|
205
|
+
|
|
206
|
+
def test_an_in_values_type_pin_picks_that_types_column(self):
|
|
207
|
+
q = colliding_compiler().compile(AllOf((
|
|
208
|
+
InValues('entity_type_id', ['b.Doc']), Equal('slug', 'x'),
|
|
209
|
+
)))
|
|
210
|
+
self.assertIn('model_b__slug', str(q))
|
|
211
|
+
|
|
212
|
+
def test_the_pin_reaches_nested_predicates(self):
|
|
213
|
+
q = colliding_compiler().compile(AllOf((
|
|
214
|
+
InValues('entity_type_id', ['a.Board']),
|
|
215
|
+
AnyOf((Equal('slug', 'x'), AllOf((InValues('slug', ['y']),)))),
|
|
216
|
+
)))
|
|
217
|
+
self.assertNotIn('model_b__slug', str(q))
|
|
218
|
+
|
|
219
|
+
def test_a_pin_spanning_both_types_matches_either_column(self):
|
|
220
|
+
q = str(colliding_compiler().compile(AllOf((BOTH_TYPES, Equal('slug', 'x')))))
|
|
221
|
+
self.assertIn("('model_a__slug', 'x')", q)
|
|
222
|
+
self.assertIn("('model_b__slug', 'x')", q)
|
|
223
|
+
self.assertIn('OR', q)
|
|
224
|
+
|
|
225
|
+
def test_in_values_over_both_types_matches_either_column(self):
|
|
226
|
+
q = str(colliding_compiler().compile(AllOf((BOTH_TYPES, InValues('slug', ['x'])))))
|
|
227
|
+
self.assertIn('model_a__slug__in', q)
|
|
228
|
+
self.assertIn('model_b__slug__in', q)
|
|
229
|
+
self.assertIn('OR', q)
|
|
230
|
+
|
|
231
|
+
def test_null_check_over_both_types_guards_each_join(self):
|
|
232
|
+
q = str(colliding_compiler().compile(AllOf((BOTH_TYPES, Equal('slug', None)))))
|
|
233
|
+
for model in ('model_a', 'model_b'):
|
|
234
|
+
self.assertIn(f"('{model}__isnull', False)", q)
|
|
235
|
+
self.assertIn(f"('{model}__slug__isnull', True)", q)
|
|
236
|
+
|
|
237
|
+
def test_differing_column_types_are_refused(self):
|
|
238
|
+
with self.assertRaises(UnsupportedQuery):
|
|
239
|
+
colliding_compiler(doc_slug_type='IntegerField').compile(
|
|
240
|
+
AllOf((BOTH_TYPES, Equal('slug', 'x')))
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
def test_a_pin_inside_a_disjunction_does_not_scope_its_siblings(self):
|
|
244
|
+
"""One branch of an `AnyOf` naming a type says nothing about the
|
|
245
|
+
rows its other branches match."""
|
|
246
|
+
with self.assertRaises(UnsupportedQuery):
|
|
247
|
+
colliding_compiler().compile(AnyOf((
|
|
248
|
+
Equal('entity_type_id', 'a.Board'), Equal('slug', 'x'),
|
|
249
|
+
)))
|
|
250
|
+
|
|
251
|
+
def test_base_columns_resolve_under_a_pin(self):
|
|
252
|
+
q = colliding_compiler().compile(AllOf((
|
|
253
|
+
Equal('entity_type_id', 'a.Board'), Equal('owner', 'u1'),
|
|
254
|
+
)))
|
|
255
|
+
self.assertIn("('owner', 'u1')", str(q))
|
|
256
|
+
|
|
257
|
+
def test_a_column_the_pinned_type_lacks_is_refused(self):
|
|
258
|
+
with self.assertRaises(UnsupportedQuery):
|
|
259
|
+
compiler().compile(AllOf((
|
|
260
|
+
Equal('entity_type_id', DEFAULT_REGISTRY.entities.get_entity_type_id(Grant)),
|
|
261
|
+
Equal('username', 'x'),
|
|
262
|
+
)))
|
|
263
|
+
|
|
264
|
+
def test_a_grant_pinned_query_joins_the_grant_table(self):
|
|
265
|
+
sql = sql_for(AllOf((
|
|
266
|
+
Equal('entity_type_id', DEFAULT_REGISTRY.entities.get_entity_type_id(Grant)),
|
|
267
|
+
InValues('subject_id', ['u1']),
|
|
268
|
+
)))
|
|
269
|
+
self.assertIn('grantmodel', sql.lower())
|