storepy 5.0.0__tar.gz → 5.2.0__tar.gz

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.
Files changed (117) hide show
  1. {storepy-5.0.0/src/storepy.egg-info → storepy-5.2.0}/PKG-INFO +2 -2
  2. {storepy-5.0.0 → storepy-5.2.0}/pyproject.toml +2 -2
  3. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/schema.py +3 -0
  4. {storepy-5.0.0 → storepy-5.2.0/src/storepy.egg-info}/PKG-INFO +2 -2
  5. {storepy-5.0.0 → storepy-5.2.0}/src/storepy.egg-info/SOURCES.txt +2 -0
  6. {storepy-5.0.0 → storepy-5.2.0}/src/storepy.egg-info/requires.txt +1 -1
  7. storepy-5.2.0/tests/test_access_keys.py +233 -0
  8. storepy-5.2.0/tests/test_compute_override.py +90 -0
  9. {storepy-5.0.0 → storepy-5.2.0}/LICENSE +0 -0
  10. {storepy-5.0.0 → storepy-5.2.0}/README.md +0 -0
  11. {storepy-5.0.0 → storepy-5.2.0}/setup.cfg +0 -0
  12. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/__init__.py +0 -0
  13. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/adapters/__init__.py +0 -0
  14. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/adapters/http.py +0 -0
  15. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/ask.py +0 -0
  16. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/ask_knowledge.md +0 -0
  17. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/bootstrap.py +0 -0
  18. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/cache/__init__.py +0 -0
  19. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/cache/context.py +0 -0
  20. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/cache/provider.py +0 -0
  21. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/cache/redis_io.py +0 -0
  22. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/capabilities.py +0 -0
  23. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/core.py +0 -0
  24. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/crud/__init__.py +0 -0
  25. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/crud/claim.py +0 -0
  26. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/crud/exec.py +0 -0
  27. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/crud/id.py +0 -0
  28. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/crud/mutation.py +0 -0
  29. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/crud/query.py +0 -0
  30. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/crud/triggers.py +0 -0
  31. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/crud/write.py +0 -0
  32. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/datasource.py +0 -0
  33. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/ddl.py +0 -0
  34. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/degraded.py +0 -0
  35. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/executors/__init__.py +0 -0
  36. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/executors/_values.py +0 -0
  37. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/executors/mongo.py +0 -0
  38. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/executors/mysql.py +0 -0
  39. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/executors/postgres.py +0 -0
  40. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/executors/sqlite.py +0 -0
  41. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/feedback.py +0 -0
  42. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/introspect/__init__.py +0 -0
  43. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/introspect/mysql.py +0 -0
  44. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/introspect/postgres.py +0 -0
  45. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/introspect/sqlite.py +0 -0
  46. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/llm.py +0 -0
  47. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/load.py +0 -0
  48. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/local/__init__.py +0 -0
  49. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/local/handle.py +0 -0
  50. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/local/store.py +0 -0
  51. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/memory/__init__.py +0 -0
  52. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/metadef.py +0 -0
  53. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/migrate.py +0 -0
  54. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/naming.py +0 -0
  55. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/permission.py +0 -0
  56. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/reindex.py +0 -0
  57. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/resource/__init__.py +0 -0
  58. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/resource/providers/__init__.py +0 -0
  59. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/resource/providers/local.py +0 -0
  60. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/resource/providers/s3.py +0 -0
  61. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/scheduler/__init__.py +0 -0
  62. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/scope.py +0 -0
  63. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/secure.py +0 -0
  64. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/sync.py +0 -0
  65. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/vector_index.py +0 -0
  66. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/vector_scan.py +0 -0
  67. {storepy-5.0.0 → storepy-5.2.0}/src/py_store/workflow.py +0 -0
  68. {storepy-5.0.0 → storepy-5.2.0}/src/storepy.egg-info/dependency_links.txt +0 -0
  69. {storepy-5.0.0 → storepy-5.2.0}/src/storepy.egg-info/top_level.txt +0 -0
  70. {storepy-5.0.0 → storepy-5.2.0}/tests/test_adapters.py +0 -0
  71. {storepy-5.0.0 → storepy-5.2.0}/tests/test_ask.py +0 -0
  72. {storepy-5.0.0 → storepy-5.2.0}/tests/test_cache.py +0 -0
  73. {storepy-5.0.0 → storepy-5.2.0}/tests/test_cache_link.py +0 -0
  74. {storepy-5.0.0 → storepy-5.2.0}/tests/test_claim_concurrency.py +0 -0
  75. {storepy-5.0.0 → storepy-5.2.0}/tests/test_coverage_margin.py +0 -0
  76. {storepy-5.0.0 → storepy-5.2.0}/tests/test_federation_e2e.py +0 -0
  77. {storepy-5.0.0 → storepy-5.2.0}/tests/test_feedback_flush.py +0 -0
  78. {storepy-5.0.0 → storepy-5.2.0}/tests/test_field_name_type.py +0 -0
  79. {storepy-5.0.0 → storepy-5.2.0}/tests/test_fns_binding.py +0 -0
  80. {storepy-5.0.0 → storepy-5.2.0}/tests/test_host_contract.py +0 -0
  81. {storepy-5.0.0 → storepy-5.2.0}/tests/test_host_paths.py +0 -0
  82. {storepy-5.0.0 → storepy-5.2.0}/tests/test_inline_agg.py +0 -0
  83. {storepy-5.0.0 → storepy-5.2.0}/tests/test_load.py +0 -0
  84. {storepy-5.0.0 → storepy-5.2.0}/tests/test_local_store.py +0 -0
  85. {storepy-5.0.0 → storepy-5.2.0}/tests/test_memory.py +0 -0
  86. {storepy-5.0.0 → storepy-5.2.0}/tests/test_metadef.py +0 -0
  87. {storepy-5.0.0 → storepy-5.2.0}/tests/test_migrate.py +0 -0
  88. {storepy-5.0.0 → storepy-5.2.0}/tests/test_migration.py +0 -0
  89. {storepy-5.0.0 → storepy-5.2.0}/tests/test_mongo_executor.py +0 -0
  90. {storepy-5.0.0 → storepy-5.2.0}/tests/test_mongo_session.py +0 -0
  91. {storepy-5.0.0 → storepy-5.2.0}/tests/test_multi_datasource.py +0 -0
  92. {storepy-5.0.0 → storepy-5.2.0}/tests/test_parity_deny.py +0 -0
  93. {storepy-5.0.0 → storepy-5.2.0}/tests/test_parity_local.py +0 -0
  94. {storepy-5.0.0 → storepy-5.2.0}/tests/test_parity_local_memory.py +0 -0
  95. {storepy-5.0.0 → storepy-5.2.0}/tests/test_parity_scope.py +0 -0
  96. {storepy-5.0.0 → storepy-5.2.0}/tests/test_policy_isolation.py +0 -0
  97. {storepy-5.0.0 → storepy-5.2.0}/tests/test_profile.py +0 -0
  98. {storepy-5.0.0 → storepy-5.2.0}/tests/test_py_store.py +0 -0
  99. {storepy-5.0.0 → storepy-5.2.0}/tests/test_raw_sql_and_ddl.py +0 -0
  100. {storepy-5.0.0 → storepy-5.2.0}/tests/test_rbac_host.py +0 -0
  101. {storepy-5.0.0 → storepy-5.2.0}/tests/test_real_backends_e2e.py +0 -0
  102. {storepy-5.0.0 → storepy-5.2.0}/tests/test_require_context.py +0 -0
  103. {storepy-5.0.0 → storepy-5.2.0}/tests/test_resource.py +0 -0
  104. {storepy-5.0.0 → storepy-5.2.0}/tests/test_resource_e2e.py +0 -0
  105. {storepy-5.0.0 → storepy-5.2.0}/tests/test_resource_pools.py +0 -0
  106. {storepy-5.0.0 → storepy-5.2.0}/tests/test_resource_units.py +0 -0
  107. {storepy-5.0.0 → storepy-5.2.0}/tests/test_scenario_course_platform.py +0 -0
  108. {storepy-5.0.0 → storepy-5.2.0}/tests/test_scenario_manager_transaction.py +0 -0
  109. {storepy-5.0.0 → storepy-5.2.0}/tests/test_scheduler.py +0 -0
  110. {storepy-5.0.0 → storepy-5.2.0}/tests/test_session.py +0 -0
  111. {storepy-5.0.0 → storepy-5.2.0}/tests/test_sql_commit_and_registry.py +0 -0
  112. {storepy-5.0.0 → storepy-5.2.0}/tests/test_triggers.py +0 -0
  113. {storepy-5.0.0 → storepy-5.2.0}/tests/test_vector_conformance.py +0 -0
  114. {storepy-5.0.0 → storepy-5.2.0}/tests/test_vector_index.py +0 -0
  115. {storepy-5.0.0 → storepy-5.2.0}/tests/test_vector_scan.py +0 -0
  116. {storepy-5.0.0 → storepy-5.2.0}/tests/test_vector_units.py +0 -0
  117. {storepy-5.0.0 → storepy-5.2.0}/tests/test_workflow.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: storepy
3
- Version: 5.0.0
3
+ Version: 5.2.0
4
4
  Summary: Multi-backend data layer for Python asyncio (MongoDB, MySQL, SQLite, PostgreSQL, local disk): pure JSON schemas, GQL tree queries compiled to a single native query, GROUP BY/HAVING aggregation, computed columns, soft-delete and role-based access control
5
5
  Author-email: 丁滔 <coen_ddt@qq.com>
6
6
  License-Expression: MIT
@@ -24,7 +24,7 @@ Requires-Python: >=3.10
24
24
  Description-Content-Type: text/markdown
25
25
  License-File: LICENSE
26
26
  Requires-Dist: pymongo>=4.9
27
- Requires-Dist: rust-store-py<6.0.0,>=5.0.0
27
+ Requires-Dist: rust-store-py<6.0.0,>=5.2.0
28
28
  Provides-Extra: mysql
29
29
  Requires-Dist: asyncmy>=0.2.9; extra == "mysql"
30
30
  Provides-Extra: postgres
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "storepy"
7
- version = "5.0.0"
7
+ version = "5.2.0"
8
8
  description = "Multi-backend data layer for Python asyncio (MongoDB, MySQL, SQLite, PostgreSQL, local disk): pure JSON schemas, GQL tree queries compiled to a single native query, GROUP BY/HAVING aggregation, computed columns, soft-delete and role-based access control"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -26,7 +26,7 @@ authors = [
26
26
  ]
27
27
  dependencies = [
28
28
  "pymongo>=4.9",
29
- "rust-store-py>=5.0.0,<6.0.0",
29
+ "rust-store-py>=5.2.0,<6.0.0",
30
30
  ]
31
31
  classifiers = [
32
32
  "Development Status :: 4 - Beta",
@@ -194,6 +194,9 @@ def _mirror(defn):
194
194
  # 归属键元数据镜像(漉金Core 接入扩展 01):creator 伪角色判定/注入的键,
195
195
  # 判决唯一在 core(缺省 createdBy 由 core 兜底);值合法性由 core.register 校验
196
196
  'ownerKey': defn.get('ownerKey'),
197
+ # 行级权限声明镜像(accessKeys):判决唯一在 core,Host 仅镜像元数据供摘要/调试;
198
+ # 归档镜像不继承(与 ownerKey 现状一致)
199
+ 'accessKeys': defn.get('accessKeys'),
197
200
  # 数据源绑定(缺省视为 default,见 datasource.py)— Phase 3/4 多后端路由
198
201
  'datasource': defn.get('datasource'),
199
202
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: storepy
3
- Version: 5.0.0
3
+ Version: 5.2.0
4
4
  Summary: Multi-backend data layer for Python asyncio (MongoDB, MySQL, SQLite, PostgreSQL, local disk): pure JSON schemas, GQL tree queries compiled to a single native query, GROUP BY/HAVING aggregation, computed columns, soft-delete and role-based access control
5
5
  Author-email: 丁滔 <coen_ddt@qq.com>
6
6
  License-Expression: MIT
@@ -24,7 +24,7 @@ Requires-Python: >=3.10
24
24
  Description-Content-Type: text/markdown
25
25
  License-File: LICENSE
26
26
  Requires-Dist: pymongo>=4.9
27
- Requires-Dist: rust-store-py<6.0.0,>=5.0.0
27
+ Requires-Dist: rust-store-py<6.0.0,>=5.2.0
28
28
  Provides-Extra: mysql
29
29
  Requires-Dist: asyncmy>=0.2.9; extra == "mysql"
30
30
  Provides-Extra: postgres
@@ -63,11 +63,13 @@ src/storepy.egg-info/SOURCES.txt
63
63
  src/storepy.egg-info/dependency_links.txt
64
64
  src/storepy.egg-info/requires.txt
65
65
  src/storepy.egg-info/top_level.txt
66
+ tests/test_access_keys.py
66
67
  tests/test_adapters.py
67
68
  tests/test_ask.py
68
69
  tests/test_cache.py
69
70
  tests/test_cache_link.py
70
71
  tests/test_claim_concurrency.py
72
+ tests/test_compute_override.py
71
73
  tests/test_coverage_margin.py
72
74
  tests/test_federation_e2e.py
73
75
  tests/test_feedback_flush.py
@@ -1,5 +1,5 @@
1
1
  pymongo>=4.9
2
- rust-store-py<6.0.0,>=5.0.0
2
+ rust-store-py<6.0.0,>=5.2.0
3
3
 
4
4
  [dev]
5
5
  pytest>=8
@@ -0,0 +1,233 @@
1
+ """accessKeys 声明式行级权限(py 宿主端到端)
2
+
3
+ 判决唯一在 Rust core;本文件验证 py_HOST 侧的三件事:
4
+ 1. schema 声明原样透传(注册期校验由 core 执行,非法声明直接抛错);
5
+ 2. ctx 清单经 Host 透传后,读路径自动注入行条件、写路径自动做子集校验;
6
+ 3. 错误语义稳定:``ERR_PERMISSION:ACCESS:`` 前缀由 Host 映射为 ``PermissionError``。
7
+
8
+ 运行:PYTHONPATH=src LOCAL_CORE=1 python -m pytest tests/test_access_keys.py -q
9
+ """
10
+
11
+ import asyncio
12
+
13
+ import pytest
14
+
15
+ from py_store import datasource as _ds
16
+ from py_store import init, store
17
+ from py_store import permission as perm
18
+ from py_store import schema as _sc
19
+ from py_store.memory import connect
20
+
21
+ _PREFIX = "ERR_PERMISSION:ACCESS:"
22
+
23
+
24
+ def _run(coro):
25
+ return asyncio.run(coro)
26
+
27
+
28
+ def _register_test_schemas():
29
+ """conftest 模块夹具重放(清场后恢复本模块 schema)"""
30
+ _sc.register({
31
+ 'name': 'AkItem',
32
+ 'collection': 'ak_items',
33
+ 'idPrefix': 'ak_',
34
+ 'timestamps': False,
35
+ 'fields': {
36
+ 'satCode': {'type': 'string'},
37
+ 'title': {'type': 'string'},
38
+ 'tags': {'type': 'array'},
39
+ 'createdBy': {'type': 'string'},
40
+ },
41
+ 'relations': {},
42
+ 'accessKeys': {
43
+ 'satCode': {'read': 'satsRead', 'write': 'satsWrite', 'bypassRoles': ['admin']},
44
+ 'tags': {'read': 'tagsRead', 'write': 'tagsWrite'},
45
+ },
46
+ })
47
+ # owner 条件(read=['creator'])+ accessKeys:验证 bypass 只跳过该键,owner 仍生效
48
+ _sc.register({
49
+ 'name': 'AkOwned',
50
+ 'collection': 'ak_owned',
51
+ 'idPrefix': 'ako_',
52
+ 'timestamps': False,
53
+ 'read': ['creator'],
54
+ 'fields': {
55
+ 'satCode': {'type': 'string'},
56
+ 'title': {'type': 'string'},
57
+ 'createdBy': {'type': 'string'},
58
+ },
59
+ 'relations': {},
60
+ 'accessKeys': {
61
+ 'satCode': {'read': 'satsRead', 'write': 'satsWrite', 'bypassRoles': ['admin']},
62
+ },
63
+ })
64
+ # 字段白名单(fields.*.write)叠加:不可写字段被过滤,access 校验只看可落库字段
65
+ _sc.register({
66
+ 'name': 'AkGuarded',
67
+ 'collection': 'ak_guarded',
68
+ 'idPrefix': 'akg_',
69
+ 'timestamps': False,
70
+ 'fields': {
71
+ 'satCode': {'type': 'string', 'write': ['viewer']},
72
+ 'note': {'type': 'string', 'write': ['nobody']},
73
+ },
74
+ 'relations': {},
75
+ 'accessKeys': {
76
+ 'satCode': {'read': 'satsRead', 'write': 'satsWrite'},
77
+ },
78
+ })
79
+
80
+
81
+ def _setup():
82
+ """内存后端 + 预置数据(ctx=None 系统写入,不受 accessKeys 管控)"""
83
+ _run(init(connect({'database': 'akdb', 'store': {}})))
84
+
85
+ async def _seed():
86
+ await store.insert('AkItem', {
87
+ '_id': 'ak_r1', 'satCode': 'GEO', 'title': 'a', 'tags': ['x'], 'createdBy': 'u1',
88
+ })
89
+ await store.insert('AkItem', {
90
+ '_id': 'ak_r2', 'satCode': 'LEO', 'title': 'b', 'tags': ['x', 'y'], 'createdBy': 'u2',
91
+ })
92
+ await store.insert('AkItem', {
93
+ '_id': 'ak_r3', 'satCode': 'MARS', 'title': 'c', 'tags': ['y'], 'createdBy': 'u1',
94
+ })
95
+ await store.insert('AkOwned', {
96
+ '_id': 'ako_1', 'satCode': 'GEO', 'title': 'o1', 'createdBy': 'u1',
97
+ })
98
+ await store.insert('AkOwned', {
99
+ '_id': 'ako_2', 'satCode': 'GEO', 'title': 'o2', 'createdBy': 'u2',
100
+ })
101
+ await store.insert('AkOwned', {
102
+ '_id': 'ako_3', 'satCode': 'MARS', 'title': 'o3', 'createdBy': 'u1',
103
+ })
104
+
105
+ _run(_seed())
106
+
107
+
108
+ def _ctx(roles=('viewer',), user_id='u1', **lists):
109
+ perm.set_context({'userId': user_id, 'roles': list(roles), **lists})
110
+
111
+
112
+ def _teardown():
113
+ perm.set_context(None)
114
+ _ds.set_connections({})
115
+
116
+
117
+ def _ids(rows):
118
+ return sorted(r['_id'] for r in rows)
119
+
120
+
121
+ # ─── 1. 声明校验(注册期 fail-fast) ──────────────────────────
122
+
123
+ def test_invalid_declaration_rejected():
124
+ with pytest.raises(Exception) as ei:
125
+ _sc.register({
126
+ 'name': 'AkBad',
127
+ 'collection': 'ak_bad',
128
+ 'timestamps': False,
129
+ 'fields': {'title': {'type': 'string'}},
130
+ 'relations': {},
131
+ 'accessKeys': {'ghost': {'read': 'x'}},
132
+ })
133
+ assert 'accessKeys' in str(ei.value), f"错误应说明 accessKeys: {ei.value}"
134
+
135
+
136
+ # ─── 2. 读路径:单值成员 + 数组交集(多键 AND) ───────────────
137
+
138
+ def test_read_injects_member_and_intersection():
139
+ try:
140
+ _setup()
141
+ _ctx(satsRead=['GEO', 'LEO'], tagsRead=['x'])
142
+ rows = _run(store.query('AkItem{ _id title }'))
143
+ # r3 satCode 不在清单;r1/r2 命中(r2 tags 交集 x)
144
+ assert _ids(rows) == ['ak_r1', 'ak_r2'], rows
145
+ finally:
146
+ _teardown()
147
+
148
+
149
+ # ─── 3. ctx 缺键 → fail closed ────────────────────────────────
150
+
151
+ def test_missing_ctx_key_fails_closed():
152
+ try:
153
+ _setup()
154
+ _ctx(tagsRead=['x']) # 缺 satsRead
155
+ with pytest.raises(store.PermissionError) as ei:
156
+ _run(store.query('AkItem{ _id }'))
157
+ assert 'ACCESS:' in str(ei.value), f"错误应含 ACCESS 语义: {ei.value}"
158
+ finally:
159
+ _teardown()
160
+
161
+
162
+ # ─── 4. 空清单 → 读空结果(与缺键严格区分) ──────────────────
163
+
164
+ def test_empty_read_list_returns_nothing():
165
+ try:
166
+ _setup()
167
+ _ctx(satsRead=[], tagsRead=['x'])
168
+ assert _run(store.query('AkItem{ _id }')) == []
169
+ finally:
170
+ _teardown()
171
+
172
+
173
+ # ─── 5. 写路径:单值越界 / 数组非子集拒绝,子集放行 ──────────
174
+
175
+ def test_write_scalar_out_of_list_denied():
176
+ try:
177
+ _setup()
178
+ _ctx(satsWrite=['GEO', 'LEO'], tagsWrite=['x'])
179
+ with pytest.raises(store.PermissionError, match='ACCESS:'):
180
+ _run(store.insert('AkItem', {'satCode': 'MARS', 'title': 'n', 'tags': ['x']}))
181
+ finally:
182
+ _teardown()
183
+
184
+
185
+ def test_write_array_not_subset_denied():
186
+ try:
187
+ _setup()
188
+ _ctx(satsWrite=['GEO', 'LEO'], tagsWrite=['x'])
189
+ with pytest.raises(store.PermissionError, match='ACCESS:'):
190
+ _run(store.insert('AkItem', {'satCode': 'GEO', 'title': 'n', 'tags': ['x', 'z']}))
191
+ finally:
192
+ _teardown()
193
+
194
+
195
+ def test_write_subset_ok():
196
+ try:
197
+ _setup()
198
+ _ctx(satsWrite=['GEO', 'LEO'], tagsWrite=['x'])
199
+ row = _run(store.insert('AkItem', {'satCode': 'GEO', 'title': 'n', 'tags': ['x']}))
200
+ assert str(row['_id']).startswith('ak_')
201
+ finally:
202
+ _teardown()
203
+
204
+
205
+ # ─── 6. bypassRoles 只跳过该键,owner 条件仍生效 ─────────────
206
+
207
+ def test_bypass_role_skips_key_only():
208
+ try:
209
+ _setup()
210
+ _ctx(roles=('admin',), user_id='u1') # 未提供 satsRead,但 admin 命中 bypass
211
+ rows = _run(store.query('AkOwned{ _id title }'))
212
+ # access 键被跳过,但 read=['creator'] 的 owner 条件仍注入 → 只看到 u1 自己的行
213
+ assert _ids(rows) == ['ako_1', 'ako_3'], rows
214
+ finally:
215
+ _teardown()
216
+
217
+
218
+ # ─── 7. 字段白名单叠加:不可写字段被过滤、access 校验照常 ────
219
+
220
+ def test_field_whitelist_filters_before_access_check():
221
+ try:
222
+ _setup()
223
+ _ctx(satsRead=['GEO', 'LEO'], satsWrite=['GEO', 'LEO'])
224
+ # note 不可写 → 过滤;satCode=GEO 在清单 → 放行,落库不含 note
225
+ row = _run(store.insert('AkGuarded', {'satCode': 'GEO', 'note': 'secret'}))
226
+ stored = _run(store.query('AkGuarded{ _id satCode note }'))
227
+ assert [r['_id'] for r in stored] == [row['_id']]
228
+ assert 'note' not in stored[0], stored
229
+ # 同一 schema 下越界值仍被 access 校验拒绝
230
+ with pytest.raises(store.PermissionError, match='ACCESS:'):
231
+ _run(store.insert('AkGuarded', {'satCode': 'MARS', 'note': 'secret'}))
232
+ finally:
233
+ _teardown()
@@ -0,0 +1,90 @@
1
+ """fields ∩ computes 同名(计算列覆盖字段)—— local 载体端到端
2
+
3
+ 语义(对齐 nodejs-store):
4
+ - **查询期**:computes 拥有同名键的输出所有权 —— 输出的是计算列求值结果;
5
+ - **写入期**:字段仍在 fields 中,`$inc` 等原生增量更新照常按同名键落库。
6
+
7
+ 本用例固化三件事:
8
+ 1. `fields.favoriteCount` + `computes.favoriteCount`(asyncFn)注册成功、不抛
9
+ `ERR_NAME_CONFLICT`;
10
+ 2. 查询输出计算列值(DB 原值 7、实时计数 3 → 输出 3);
11
+ 3. `$inc favoriteCount` 后磁盘快照中的 DB 原值递增(7 → 8)。
12
+
13
+ 运行:PYTHONPATH=src LOCAL_CORE=1 python -m pytest tests/test_compute_override.py -q
14
+ """
15
+
16
+ import asyncio
17
+
18
+ from py_store import datasource as _ds
19
+ from py_store import init, store
20
+ from py_store import schema as _sc
21
+ from py_store.local import connect as connect_local
22
+ from py_store.local.store import read_snapshot
23
+
24
+
25
+ def _run(coro):
26
+ return asyncio.run(coro)
27
+
28
+
29
+ async def _live_favorite_count(items, ctx=None):
30
+ """实时计数 3:与 DB 存储值 7 不同,用于区分「计算列输出」与「字段存储值」。"""
31
+ for it in items:
32
+ it['favoriteCount'] = 3
33
+
34
+
35
+ def _register():
36
+ _sc.register({
37
+ 'name': 'FavoriteListing',
38
+ 'collection': 'favorite_listings',
39
+ 'idPrefix': 'fl_',
40
+ 'timestamps': False,
41
+ 'fields': {
42
+ '_id': {'type': 'string'},
43
+ 'title': {'type': 'string'},
44
+ # 与 computes 同名:保留类型声明,供写入校验 / DDL / $inc 使用
45
+ 'favoriteCount': {'type': 'int'},
46
+ },
47
+ 'relations': {},
48
+ 'computes': {
49
+ 'favoriteCount': {
50
+ 'type': 'int',
51
+ 'asyncFn': _live_favorite_count,
52
+ 'depends': [],
53
+ },
54
+ },
55
+ })
56
+
57
+
58
+ def test_fields_compute_same_name_local(tmp_path):
59
+ _register()
60
+ rows = []
61
+ snapshot = {}
62
+
63
+ try:
64
+ _run(init(connect_local({'dir': str(tmp_path)})))
65
+
66
+ async def _main():
67
+ await store.insert('FavoriteListing', {
68
+ '_id': 'fl_1', 'title': 'a', 'favoriteCount': 7,
69
+ })
70
+ got = await store.query(
71
+ 'FavoriteListing($sort:@s0){ _id favoriteCount }',
72
+ {'s0': {'_id': 1}},
73
+ )
74
+ await store.update(
75
+ 'FavoriteListing', {'_id': 'fl_1'}, {'$inc': {'favoriteCount': 1}},
76
+ )
77
+ return got
78
+
79
+ rows = _run(_main())
80
+ snapshot = read_snapshot(str(tmp_path))
81
+ finally:
82
+ _ds.set_connections({})
83
+
84
+ # 查询输出计算列值(DB 原值 7 被覆盖)
85
+ assert rows == [{'_id': 'fl_1', 'favoriteCount': 3}], '查询期计算列覆盖同名字段'
86
+
87
+ # 写入仍按 fields 同名键落库:$inc 在原值上递增(物理集合名经命名归一为 camelCase)
88
+ stored = snapshot.get('favoriteListings') or []
89
+ assert len(stored) == 1, f'本地集合应有 1 条文档:{snapshot}'
90
+ assert stored[0]['favoriteCount'] == 8, '$inc 按 fields 同名键落库(7 → 8)'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes