babelqueue 1.6.0__tar.gz → 1.7.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 (66) hide show
  1. {babelqueue-1.6.0 → babelqueue-1.7.0}/PKG-INFO +1 -1
  2. {babelqueue-1.6.0 → babelqueue-1.7.0}/pyproject.toml +1 -1
  3. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/__init__.py +5 -1
  4. babelqueue-1.7.0/src/babelqueue/exceptions.py +25 -0
  5. babelqueue-1.7.0/src/babelqueue/idempotency.py +95 -0
  6. babelqueue-1.7.0/src/babelqueue/schema.py +261 -0
  7. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/conformance/manifest.json +23 -0
  8. babelqueue-1.7.0/tests/test_idempotency.py +89 -0
  9. babelqueue-1.7.0/tests/test_schema.py +145 -0
  10. babelqueue-1.7.0/tests/test_schema_conformance.py +33 -0
  11. babelqueue-1.6.0/src/babelqueue/exceptions.py +0 -11
  12. {babelqueue-1.6.0 → babelqueue-1.7.0}/.github/FUNDING.yml +0 -0
  13. {babelqueue-1.6.0 → babelqueue-1.7.0}/.github/workflows/ci.yml +0 -0
  14. {babelqueue-1.6.0 → babelqueue-1.7.0}/.github/workflows/release.yml +0 -0
  15. {babelqueue-1.6.0 → babelqueue-1.7.0}/.gitignore +0 -0
  16. {babelqueue-1.6.0 → babelqueue-1.7.0}/CHANGELOG.md +0 -0
  17. {babelqueue-1.6.0 → babelqueue-1.7.0}/LICENSE +0 -0
  18. {babelqueue-1.6.0 → babelqueue-1.7.0}/README.md +0 -0
  19. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/app.py +0 -0
  20. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/artemis_transport.py +0 -0
  21. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/asb_transport.py +0 -0
  22. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/celery.py +0 -0
  23. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/codec.py +0 -0
  24. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/contracts.py +0 -0
  25. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/dead_letter.py +0 -0
  26. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/django/__init__.py +0 -0
  27. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/django/apps.py +0 -0
  28. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/django/management/__init__.py +0 -0
  29. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/django/management/commands/__init__.py +0 -0
  30. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/django/management/commands/babelqueue_worker.py +0 -0
  31. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/kafka_transport.py +0 -0
  32. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/pika_transport.py +0 -0
  33. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/pulsar_transport.py +0 -0
  34. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/py.typed +0 -0
  35. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/redis_transport.py +0 -0
  36. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/routing.py +0 -0
  37. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/sqs_transport.py +0 -0
  38. {babelqueue-1.6.0 → babelqueue-1.7.0}/src/babelqueue/transport.py +0 -0
  39. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/conformance/fixtures/dead-lettered.json +0 -0
  40. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/conformance/fixtures/invalid-missing-urn.json +0 -0
  41. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/conformance/fixtures/invalid-unknown-schema-version.json +0 -0
  42. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/conformance/fixtures/order-created.json +0 -0
  43. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/conformance/fixtures/unicode-and-numbers.json +0 -0
  44. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/conformance/fixtures/urn-alias.json +0 -0
  45. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/conformance/schema/message-envelope.schema.json +0 -0
  46. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/fixtures/dead-lettered.json +0 -0
  47. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/fixtures/order-created.json +0 -0
  48. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_app.py +0 -0
  49. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_artemis_conformance.py +0 -0
  50. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_artemis_transport.py +0 -0
  51. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_asb_conformance.py +0 -0
  52. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_asb_transport.py +0 -0
  53. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_celery.py +0 -0
  54. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_codec.py +0 -0
  55. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_conformance.py +0 -0
  56. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_dead_letter.py +0 -0
  57. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_django.py +0 -0
  58. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_kafka_conformance.py +0 -0
  59. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_kafka_transport.py +0 -0
  60. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_overhead.py +0 -0
  61. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_pika_transport.py +0 -0
  62. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_pulsar_conformance.py +0 -0
  63. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_pulsar_transport.py +0 -0
  64. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_redis_transport.py +0 -0
  65. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_sqs_conformance.py +0 -0
  66. {babelqueue-1.6.0 → babelqueue-1.7.0}/tests/test_sqs_transport.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: babelqueue
3
- Version: 1.6.0
3
+ Version: 1.7.0
4
4
  Summary: Polyglot Queues, Simplified — the Python core: the canonical BabelQueue wire-envelope codec, contracts and dead-letter helpers.
5
5
  Project-URL: Homepage, https://babelqueue.com
6
6
  Project-URL: Source, https://github.com/BabelQueue/babelqueue-python
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "babelqueue"
7
- version = "1.6.0"
7
+ version = "1.7.0"
8
8
  description = "Polyglot Queues, Simplified — the Python core: the canonical BabelQueue wire-envelope codec, contracts and dead-letter helpers."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -11,10 +11,11 @@ and dead-letter helpers. Framework adapters (Celery, Django, ...) build on this.
11
11
 
12
12
  from __future__ import annotations
13
13
 
14
- from . import dead_letter
14
+ from . import dead_letter, idempotency
15
15
  from .app import BabelQueue
16
16
  from .codec import SCHEMA_VERSION, SOURCE_LANG, EnvelopeCodec
17
17
  from .contracts import HasTraceId, PolyglotMessage
18
+ from .idempotency import IdempotencyStore, InMemoryStore
18
19
  from .exceptions import BabelQueueError, UnknownUrnError
19
20
  from .routing import UnknownUrnStrategy
20
21
  from .transport import InMemoryTransport, ReceivedMessage, Transport
@@ -35,5 +36,8 @@ __all__ = [
35
36
  "BabelQueueError",
36
37
  "UnknownUrnError",
37
38
  "dead_letter",
39
+ "idempotency",
40
+ "IdempotencyStore",
41
+ "InMemoryStore",
38
42
  "__version__",
39
43
  ]
@@ -0,0 +1,25 @@
1
+ """Exception hierarchy for BabelQueue."""
2
+
3
+ from __future__ import annotations
4
+
5
+
6
+ class BabelQueueError(Exception):
7
+ """Base for every recoverable BabelQueue error (bad config, empty URN, ...)."""
8
+
9
+
10
+ class UnknownUrnError(BabelQueueError):
11
+ """A consumed message carries a URN with no mapped handler (strategy "fail")."""
12
+
13
+
14
+ class InvalidPayloadError(BabelQueueError):
15
+ """A message's ``data`` does not match the JSON Schema registered for its URN (ADR-0024).
16
+
17
+ Raised by the producer-side :func:`babelqueue.schema.validate` and the consumer-side
18
+ :func:`babelqueue.schema.wrap`; the latter lets the runtime redeliver (and eventually
19
+ dead-letter) a poison message.
20
+ """
21
+
22
+ def __init__(self, urn: str, violation: str) -> None:
23
+ super().__init__(f"data for {urn!r} does not match its URN schema: {violation}")
24
+ self.urn = urn
25
+ self.violation = violation
@@ -0,0 +1,95 @@
1
+ """Optional idempotency helper (ADR-0022): dedupe a consume handler on ``meta.id``.
2
+
3
+ The Python mirror of the PHP ``BabelQueue\\Idempotency`` and Go ``idempotency``
4
+ helpers. It wraps a handler so a message whose ``meta.id`` was already processed
5
+ successfully is skipped instead of run again, composing with the runtime's
6
+ ack-on-return / redeliver-on-raise contract::
7
+
8
+ from babelqueue import BabelQueue
9
+ from babelqueue.idempotency import InMemoryStore, wrap
10
+
11
+ app = BabelQueue("redis://localhost:6379/0", queue="orders")
12
+ store = InMemoryStore()
13
+ app.register("urn:babel:orders:created", wrap(store, on_order_created))
14
+
15
+ A previously-seen id returns early (the runtime acks it, so the broker stops
16
+ redelivering); a raising handler leaves the id unmarked so a redelivery runs it again
17
+ (retry / dead-letter still apply); a message with no usable ``meta.id`` runs unchanged.
18
+ This is "seen-set" post-success dedupe — not exactly-once and not in-flight concurrency
19
+ locking; a transactional / outbox mode is a documented future direction.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ import functools
25
+ import threading
26
+ from typing import Any, Callable, Mapping, Protocol, runtime_checkable
27
+
28
+ Handler = Callable[..., None]
29
+
30
+
31
+ @runtime_checkable
32
+ class IdempotencyStore(Protocol):
33
+ """A record of message ids already processed, keyed on ``meta.id``."""
34
+
35
+ def seen(self, message_id: str) -> bool:
36
+ """Whether this id has already been processed (remembered)."""
37
+
38
+ def remember(self, message_id: str) -> None:
39
+ """Record this id as processed."""
40
+
41
+ def forget(self, message_id: str) -> None:
42
+ """Drop an id from the store (manual eviction; a backend may also expire ids)."""
43
+
44
+
45
+ class InMemoryStore:
46
+ """Process-local, thread-safe :class:`IdempotencyStore`.
47
+
48
+ For tests and single-process consumers; it is not shared across workers and not
49
+ persistent — use a Redis- or database-backed store for production fleets.
50
+ """
51
+
52
+ def __init__(self) -> None:
53
+ self._seen: set[str] = set()
54
+ self._lock = threading.Lock()
55
+
56
+ def seen(self, message_id: str) -> bool:
57
+ with self._lock:
58
+ return message_id in self._seen
59
+
60
+ def remember(self, message_id: str) -> None:
61
+ with self._lock:
62
+ self._seen.add(message_id)
63
+
64
+ def forget(self, message_id: str) -> None:
65
+ with self._lock:
66
+ self._seen.discard(message_id)
67
+
68
+
69
+ def wrap(store: IdempotencyStore, handler: Handler) -> Handler:
70
+ """Wrap ``handler`` so a message whose ``meta.id`` was already processed is skipped.
71
+
72
+ The returned callable keeps ``handler``'s signature (via :func:`functools.wraps`),
73
+ so the runtime's introspection still passes it the right number of positional args
74
+ (``data, meta`` or ``data, meta, envelope``).
75
+ """
76
+
77
+ @functools.wraps(handler)
78
+ def wrapped(*args: Any) -> None:
79
+ meta = args[1] if len(args) > 1 and isinstance(args[1], Mapping) else {}
80
+ message_id = meta.get("id")
81
+
82
+ # No usable id → cannot dedupe; run the handler unchanged.
83
+ if not isinstance(message_id, str) or message_id == "":
84
+ handler(*args)
85
+ return
86
+
87
+ # Already processed on an earlier delivery: return so the runtime acks it.
88
+ if store.seen(message_id):
89
+ return
90
+
91
+ # First success wins; a raise here leaves the id unmarked → retry/DLQ apply.
92
+ handler(*args)
93
+ store.remember(message_id)
94
+
95
+ return wrapped
@@ -0,0 +1,261 @@
1
+ """Optional per-URN payload schema validation (ADR-0024).
2
+
3
+ The Python mirror of the Go ``schema`` package and PHP ``BabelQueue\\Schema``. A
4
+ :class:`SchemaProvider` supplies a JSON Schema for a message URN — typically read from a
5
+ babelqueue-registry ``registry.json`` — and the message's ``data`` is validated against it.
6
+ It is opt-in: a URN with no registered schema is never validated.
7
+
8
+ - **Producer-side (recommended):** call :func:`validate` before publishing so invalid data
9
+ never enters the queue, or :func:`check` to branch without raising::
10
+
11
+ from babelqueue.schema import MapProvider, validate
12
+
13
+ provider = MapProvider.from_json({"urn:babel:orders:created": ORDERS_SCHEMA_JSON})
14
+ validate(provider, "urn:babel:orders:created", {"order_id": 7}) # raises on mismatch
15
+
16
+ - **Consumer-side (safety net):** wrap a handler with :func:`wrap`. Because a Python handler
17
+ receives ``data`` (and ``meta``, ``envelope``) positionally rather than a message object,
18
+ the URN is passed explicitly — usually the same URN you register under::
19
+
20
+ app.register(URN, wrap(provider, URN, on_order_created))
21
+
22
+ Invalid data raises :class:`~babelqueue.exceptions.InvalidPayloadError`, so the runtime
23
+ redelivers (and eventually dead-letters) the poison message; a URN with no schema runs the
24
+ handler unchanged.
25
+
26
+ The validator is a small subset of JSON Schema (draft-07) whose verdicts match the Go and
27
+ PHP validators and babelqueue-registry's ``compat`` linter: ``type``, ``required``,
28
+ ``properties``, ``additionalProperties``, ``items``, ``enum``, ``const``, ``minLength``,
29
+ ``minimum``. Unknown keywords are ignored. Zero dependencies (stdlib only).
30
+ """
31
+
32
+ from __future__ import annotations
33
+
34
+ import functools
35
+ import json
36
+ import os
37
+ import threading
38
+ from typing import Any, Callable, Mapping, Optional, Protocol, runtime_checkable
39
+
40
+ from .exceptions import InvalidPayloadError
41
+
42
+ Handler = Callable[..., None]
43
+
44
+
45
+ @runtime_checkable
46
+ class SchemaProvider(Protocol):
47
+ """A source of per-URN ``data`` schemas, keyed on the message URN."""
48
+
49
+ def schema_for(self, urn: str) -> Optional[Mapping[str, Any]]:
50
+ """The JSON Schema registered for ``urn``, or None when none is registered."""
51
+
52
+
53
+ class MapProvider:
54
+ """In-memory :class:`SchemaProvider`, for tests and for embedding schemas in code."""
55
+
56
+ def __init__(self, schemas: Mapping[str, Mapping[str, Any]]) -> None:
57
+ self._schemas: dict[str, Mapping[str, Any]] = dict(schemas)
58
+
59
+ @classmethod
60
+ def from_json(cls, raw: Mapping[str, str]) -> "MapProvider":
61
+ """Build a provider from URN -> raw JSON Schema strings, decoding each."""
62
+ schemas: dict[str, Mapping[str, Any]] = {}
63
+ for urn, body in raw.items():
64
+ decoded = json.loads(body)
65
+ if not isinstance(decoded, dict):
66
+ raise ValueError(f"schema: invalid JSON schema for {urn!r}")
67
+ schemas[urn] = decoded
68
+ return cls(schemas)
69
+
70
+ def schema_for(self, urn: str) -> Optional[Mapping[str, Any]]:
71
+ return self._schemas.get(urn)
72
+
73
+
74
+ class DirProvider:
75
+ """Reads schemas from a babelqueue-registry manifest (``registry.json``): a list of
76
+ ``{urn, schema}`` entries mapping each URN to a schema file for its ``data`` block. The
77
+ bridge that makes the registry's governed schemas enforceable at runtime. Schema files
78
+ are read and decoded lazily and cached (thread-safe). A URN absent from the manifest
79
+ returns None (skip); a URN whose schema file is missing raises (config/IO error)."""
80
+
81
+ def __init__(self, manifest_path: str) -> None:
82
+ with open(manifest_path, "r", encoding="utf-8") as fh:
83
+ manifest = json.load(fh)
84
+ if not isinstance(manifest, dict):
85
+ raise ValueError(f"schema: invalid registry manifest {manifest_path!r}")
86
+
87
+ self._dir = os.path.dirname(manifest_path)
88
+ self._files: dict[str, str] = {}
89
+ self._cache: dict[str, Mapping[str, Any]] = {}
90
+ self._lock = threading.Lock()
91
+
92
+ entries = manifest.get("schemas") or []
93
+ if isinstance(entries, list):
94
+ for entry in entries:
95
+ if not isinstance(entry, Mapping):
96
+ continue
97
+ urn = entry.get("urn")
98
+ file = entry.get("schema")
99
+ if isinstance(urn, str) and urn and isinstance(file, str) and file:
100
+ self._files[urn] = file
101
+
102
+ def schema_for(self, urn: str) -> Optional[Mapping[str, Any]]:
103
+ with self._lock:
104
+ cached = self._cache.get(urn)
105
+ if cached is not None:
106
+ return cached
107
+ file = self._files.get(urn)
108
+ if file is None:
109
+ return None
110
+ path = file if os.path.isabs(file) else os.path.join(self._dir, file)
111
+ with open(path, "r", encoding="utf-8") as fh:
112
+ decoded = json.load(fh)
113
+ if not isinstance(decoded, dict):
114
+ raise ValueError(f"schema: invalid schema for {urn!r} ({file})")
115
+ self._cache[urn] = decoded
116
+ return decoded
117
+
118
+
119
+ def check(provider: SchemaProvider, urn: str, data: Mapping[str, Any]) -> Optional[str]:
120
+ """The first ``data`` violation for ``(urn, data)``, or None when it is valid or when no
121
+ schema is registered for the URN (opt-in). Non-raising; for producer-side branching."""
122
+ schema = provider.schema_for(urn)
123
+ if schema is None:
124
+ return None
125
+ return validate_schema(schema, dict(data))
126
+
127
+
128
+ def validate(provider: SchemaProvider, urn: str, data: Mapping[str, Any]) -> None:
129
+ """Validate ``(urn, data)`` against its registered schema, raising otherwise. The
130
+ producer-side guard; call it before publishing.
131
+
132
+ :raises InvalidPayloadError: when the data does not match the URN's schema.
133
+ """
134
+ violation = check(provider, urn, data)
135
+ if violation is not None:
136
+ raise InvalidPayloadError(urn, violation)
137
+
138
+
139
+ def wrap(provider: SchemaProvider, urn: str, handler: Handler) -> Handler:
140
+ """Wrap a consume handler so each message's ``data`` is validated against ``urn``'s
141
+ schema before the handler runs. The returned callable keeps ``handler``'s signature (via
142
+ :func:`functools.wraps`), so the runtime still passes it the right number of positional
143
+ args (``data, meta`` or ``data, meta, envelope``)."""
144
+
145
+ @functools.wraps(handler)
146
+ def wrapped(*args: Any) -> None:
147
+ data = args[0] if args and isinstance(args[0], Mapping) else {}
148
+ validate(provider, urn, data)
149
+ handler(*args)
150
+
151
+ return wrapped
152
+
153
+
154
+ def validate_schema(schema: Mapping[str, Any], value: Any, path: str = "") -> Optional[str]:
155
+ """The first violation of ``value`` against a (subset) JSON Schema node, or None."""
156
+ if "const" in schema and not _equal(value, schema["const"]):
157
+ return _violation(path, "wrong_const")
158
+
159
+ enum = schema.get("enum")
160
+ if isinstance(enum, list) and not any(_equal(value, item) for item in enum):
161
+ return _violation(path, "not_in_enum")
162
+
163
+ typ = schema.get("type")
164
+ if typ == "object":
165
+ return _check_object(schema, value, path)
166
+ if typ == "array":
167
+ return _check_array(schema, value, path)
168
+ if typ == "string":
169
+ if not isinstance(value, str):
170
+ return _violation(path, "not_a_string")
171
+ min_len = schema.get("minLength")
172
+ if isinstance(min_len, (int, float)) and len(value) < int(min_len):
173
+ return _violation(path, "below_min_length")
174
+ return None
175
+ if typ == "integer":
176
+ if not _is_integer(value):
177
+ return _violation(path, "not_an_integer")
178
+ return _check_minimum(schema, value, path)
179
+ if typ == "number":
180
+ if not _is_number(value):
181
+ return _violation(path, "not_a_number")
182
+ return _check_minimum(schema, value, path)
183
+ if typ == "boolean":
184
+ return None if isinstance(value, bool) else _violation(path, "not_a_boolean")
185
+ if typ == "null":
186
+ return None if value is None else _violation(path, "not_null")
187
+ return None
188
+
189
+
190
+ def _check_object(schema: Mapping[str, Any], value: Any, path: str) -> Optional[str]:
191
+ if not isinstance(value, Mapping):
192
+ return _violation(path, "not_an_object")
193
+
194
+ required = schema.get("required")
195
+ if isinstance(required, list):
196
+ for key in required:
197
+ if isinstance(key, str) and key not in value:
198
+ return _violation(_join(path, key), "missing_required")
199
+
200
+ properties = schema.get("properties")
201
+ properties = properties if isinstance(properties, Mapping) else {}
202
+ additional_allowed = schema.get("additionalProperties") is not False
203
+
204
+ for key, item in value.items():
205
+ name = str(key)
206
+ prop = properties.get(name)
207
+ if isinstance(prop, Mapping):
208
+ violation = validate_schema(prop, item, _join(path, name))
209
+ if violation is not None:
210
+ return violation
211
+ continue
212
+ if not additional_allowed:
213
+ return _violation(_join(path, name), "additional_not_allowed")
214
+
215
+ return None
216
+
217
+
218
+ def _check_array(schema: Mapping[str, Any], value: Any, path: str) -> Optional[str]:
219
+ if not isinstance(value, list):
220
+ return _violation(path, "not_an_array")
221
+ items = schema.get("items")
222
+ if not isinstance(items, Mapping):
223
+ return None
224
+ for i, item in enumerate(value):
225
+ violation = validate_schema(items, item, f"{path}[{i}]")
226
+ if violation is not None:
227
+ return violation
228
+ return None
229
+
230
+
231
+ def _check_minimum(schema: Mapping[str, Any], value: Any, path: str) -> Optional[str]:
232
+ minimum = schema.get("minimum")
233
+ if isinstance(minimum, (int, float)) and not isinstance(minimum, bool) and float(value) < float(minimum):
234
+ return _violation(path, "below_minimum")
235
+ return None
236
+
237
+
238
+ def _is_integer(value: Any) -> bool:
239
+ if isinstance(value, bool):
240
+ return False
241
+ if isinstance(value, int):
242
+ return True
243
+ return isinstance(value, float) and value.is_integer()
244
+
245
+
246
+ def _is_number(value: Any) -> bool:
247
+ return isinstance(value, (int, float)) and not isinstance(value, bool)
248
+
249
+
250
+ def _equal(a: Any, b: Any) -> bool:
251
+ # Type-aware equality so True != 1 and an integer const never matches a float value,
252
+ # matching the strict comparisons in the Go and PHP validators.
253
+ return type(a) is type(b) and a == b
254
+
255
+
256
+ def _violation(path: str, reason: str) -> str:
257
+ return f"{path or '<root>'}: {reason}"
258
+
259
+
260
+ def _join(path: str, key: str) -> str:
261
+ return key if path == "" else f"{path}.{key}"
@@ -226,5 +226,28 @@
226
226
  "x-attempts": 0
227
227
  }
228
228
  }
229
+ },
230
+ "payload_schema": {
231
+ "description": "Per-URN data schema validation (ADR-0024). Each case validates `data` against `schema`; every SDK's optional payload validator (Go schema, PHP BabelQueue\\Schema, Python babelqueue.schema) MUST agree on `valid`. The wire envelope stays frozen — this governs the data block only, and is opt-in (consumers/producers without a registered schema skip it).",
232
+ "schema": {
233
+ "$schema": "http://json-schema.org/draft-07/schema#",
234
+ "type": "object",
235
+ "required": ["order_id"],
236
+ "properties": {
237
+ "order_id": { "type": "integer", "minimum": 1 },
238
+ "amount": { "type": "number", "minimum": 0 },
239
+ "currency": { "enum": ["USD", "EUR", "TRY"] }
240
+ },
241
+ "additionalProperties": false
242
+ },
243
+ "cases": [
244
+ { "name": "valid-minimal", "valid": true, "data": { "order_id": 1042 } },
245
+ { "name": "valid-full", "valid": true, "data": { "order_id": 1042, "amount": 99.9, "currency": "USD" } },
246
+ { "name": "invalid-missing-required", "valid": false, "data": { "amount": 10 } },
247
+ { "name": "invalid-wrong-type", "valid": false, "data": { "order_id": "x" } },
248
+ { "name": "invalid-additional-property", "valid": false, "data": { "order_id": 1, "extra": true } },
249
+ { "name": "invalid-enum", "valid": false, "data": { "order_id": 1, "currency": "GBP" } },
250
+ { "name": "invalid-below-minimum", "valid": false, "data": { "order_id": 0 } }
251
+ ]
229
252
  }
230
253
  }
@@ -0,0 +1,89 @@
1
+ from __future__ import annotations
2
+
3
+ import unittest
4
+ from typing import Any, Dict, List
5
+
6
+ from babelqueue.app import _handler_wants_envelope
7
+ from babelqueue.idempotency import InMemoryStore, wrap
8
+
9
+
10
+ class IdempotencyTest(unittest.TestCase):
11
+ def test_runs_and_remembers_on_first_delivery(self) -> None:
12
+ store = InMemoryStore()
13
+ calls: List[str] = []
14
+
15
+ def handler(data: Dict[str, Any], meta: Dict[str, Any]) -> None:
16
+ calls.append(meta["id"])
17
+
18
+ wrapped = wrap(store, handler)
19
+ wrapped({}, {"id": "m1"})
20
+
21
+ self.assertEqual(calls, ["m1"])
22
+ self.assertTrue(store.seen("m1"))
23
+
24
+ def test_skips_redelivery_of_same_id(self) -> None:
25
+ store = InMemoryStore()
26
+ calls: List[int] = []
27
+
28
+ wrapped = wrap(store, lambda data, meta: calls.append(1))
29
+ wrapped({}, {"id": "m1"})
30
+ wrapped({}, {"id": "m1"}) # redelivery → skipped
31
+
32
+ self.assertEqual(len(calls), 1)
33
+
34
+ def test_runs_again_for_a_different_id(self) -> None:
35
+ store = InMemoryStore()
36
+ calls: List[int] = []
37
+
38
+ wrapped = wrap(store, lambda data, meta: calls.append(1))
39
+ wrapped({}, {"id": "m1"})
40
+ wrapped({}, {"id": "m2"})
41
+
42
+ self.assertEqual(len(calls), 2)
43
+
44
+ def test_does_not_remember_when_handler_raises(self) -> None:
45
+ store = InMemoryStore()
46
+ calls: List[int] = []
47
+
48
+ def boom(data: Dict[str, Any], meta: Dict[str, Any]) -> None:
49
+ calls.append(1)
50
+ raise RuntimeError("boom")
51
+
52
+ wrapped = wrap(store, boom)
53
+ with self.assertRaises(RuntimeError):
54
+ wrapped({}, {"id": "m1"})
55
+ self.assertFalse(store.seen("m1"))
56
+
57
+ # A redelivery runs the handler again — retry works.
58
+ with self.assertRaises(RuntimeError):
59
+ wrapped({}, {"id": "m1"})
60
+ self.assertEqual(len(calls), 2)
61
+
62
+ def test_runs_when_no_usable_id(self) -> None:
63
+ store = InMemoryStore()
64
+ calls: List[int] = []
65
+
66
+ wrapped = wrap(store, lambda data, meta: calls.append(1))
67
+ wrapped({}, {"id": ""}) # empty id → cannot dedupe → runs
68
+ wrapped({}, {}) # no id at all → runs
69
+
70
+ self.assertEqual(len(calls), 2)
71
+
72
+ def test_preserves_handler_arity_for_the_runtime(self) -> None:
73
+ # functools.wraps keeps inspect.signature transparent, so the runtime still
74
+ # passes the wrapped handler the right number of positional args.
75
+ store = InMemoryStore()
76
+ self.assertTrue(_handler_wants_envelope(wrap(store, lambda d, m, e: None)))
77
+ self.assertFalse(_handler_wants_envelope(wrap(store, lambda d, m: None)))
78
+
79
+ def test_forget_removes_a_remembered_id(self) -> None:
80
+ store = InMemoryStore()
81
+ store.remember("m1")
82
+ self.assertTrue(store.seen("m1"))
83
+
84
+ store.forget("m1")
85
+ self.assertFalse(store.seen("m1"))
86
+
87
+
88
+ if __name__ == "__main__":
89
+ unittest.main()
@@ -0,0 +1,145 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import tempfile
6
+ import unittest
7
+ from typing import List
8
+
9
+ from babelqueue.exceptions import InvalidPayloadError
10
+ from babelqueue.schema import (
11
+ DirProvider,
12
+ MapProvider,
13
+ check,
14
+ validate,
15
+ validate_schema,
16
+ wrap,
17
+ )
18
+
19
+ ORDERS = (
20
+ '{"type":"object","required":["order_id"],'
21
+ '"properties":{"order_id":{"type":"integer"}},"additionalProperties":false}'
22
+ )
23
+
24
+
25
+ class ValidatorTest(unittest.TestCase):
26
+ def test_object_required_types_and_additional(self) -> None:
27
+ schema = json.loads(
28
+ '{"type":"object","required":["order_id"],'
29
+ '"properties":{"order_id":{"type":"integer"},"note":{"type":"string","minLength":1}},'
30
+ '"additionalProperties":false}'
31
+ )
32
+ self.assertIsNone(validate_schema(schema, {"order_id": 7}))
33
+ self.assertIsNotNone(validate_schema(schema, {}))
34
+ self.assertIsNotNone(validate_schema(schema, {"order_id": "x"}))
35
+ self.assertIsNotNone(validate_schema(schema, {"order_id": 7, "extra": 1}))
36
+ self.assertIsNotNone(validate_schema(schema, {"order_id": 7, "note": ""}))
37
+
38
+ def test_enum_minimum_and_array_items(self) -> None:
39
+ schema = json.loads(
40
+ '{"type":"object","properties":{"status":{"enum":["new","paid"]},'
41
+ '"qty":{"type":"integer","minimum":1},'
42
+ '"tags":{"type":"array","items":{"type":"string"}}}}'
43
+ )
44
+ self.assertIsNone(validate_schema(schema, {"status": "paid", "qty": 2, "tags": ["a", "b"]}))
45
+ self.assertIsNotNone(validate_schema(schema, {"status": "cancelled"}))
46
+ self.assertIsNotNone(validate_schema(schema, {"qty": 0}))
47
+ self.assertIsNotNone(validate_schema(schema, {"tags": ["a", 1]}))
48
+
49
+ def test_scalar_types(self) -> None:
50
+ cases = [
51
+ ('{"type":"boolean"}', True, True),
52
+ ('{"type":"boolean"}', "x", False),
53
+ ('{"type":"null"}', None, True),
54
+ ('{"type":"null"}', 1, False),
55
+ ('{"type":"number","minimum":0.5}', 0.6, True),
56
+ ('{"type":"number","minimum":0.5}', 0.4, False),
57
+ ('{"type":"number"}', "x", False),
58
+ ('{"type":"string"}', 5, False),
59
+ ('{"type":"integer"}', 1.0, True),
60
+ ('{"type":"integer"}', 1.5, False),
61
+ ('{"type":"integer"}', True, False), # bool is not an integer
62
+ ('{"const":"v1"}', "v1", True),
63
+ ('{"const":"v1"}', "v2", False),
64
+ ]
65
+ for src, value, valid in cases:
66
+ schema = json.loads(src)
67
+ violation = validate_schema(schema, value)
68
+ self.assertEqual(valid, violation is None, f"{src} / {value!r}: {violation}")
69
+
70
+
71
+ class ProviderTest(unittest.TestCase):
72
+ def test_map_provider_from_json(self) -> None:
73
+ provider = MapProvider.from_json({"urn:babel:orders:created": ORDERS})
74
+ self.assertIsNotNone(provider.schema_for("urn:babel:orders:created"))
75
+ self.assertIsNone(provider.schema_for("urn:babel:unknown"))
76
+
77
+ def test_map_provider_invalid_json(self) -> None:
78
+ with self.assertRaises(ValueError):
79
+ MapProvider.from_json({"u": "not json"})
80
+
81
+ def test_dir_provider_reads_registry_lazily(self) -> None:
82
+ with tempfile.TemporaryDirectory() as d:
83
+ os.makedirs(os.path.join(d, "schemas"))
84
+ with open(os.path.join(d, "schemas", "orders.json"), "w", encoding="utf-8") as fh:
85
+ fh.write('{"type":"object","required":["order_id"],"properties":{"order_id":{"type":"integer"}}}')
86
+ with open(os.path.join(d, "registry.json"), "w", encoding="utf-8") as fh:
87
+ fh.write('{"schemas":[{"urn":"urn:babel:orders:created","schema":"schemas/orders.json"},{"urn":"","schema":"x"}]}')
88
+
89
+ provider = DirProvider(os.path.join(d, "registry.json"))
90
+ for _ in range(2): # the second call hits the cache
91
+ self.assertIsNotNone(provider.schema_for("urn:babel:orders:created"))
92
+ self.assertIsNone(provider.schema_for("urn:babel:unknown"))
93
+
94
+ def test_dir_provider_missing_manifest(self) -> None:
95
+ with self.assertRaises(OSError):
96
+ DirProvider(os.path.join(tempfile.gettempdir(), "nope_registry_xyz.json"))
97
+
98
+ def test_dir_provider_missing_schema_file(self) -> None:
99
+ with tempfile.TemporaryDirectory() as d:
100
+ with open(os.path.join(d, "registry.json"), "w", encoding="utf-8") as fh:
101
+ fh.write('{"schemas":[{"urn":"u","schema":"missing.json"}]}')
102
+ provider = DirProvider(os.path.join(d, "registry.json"))
103
+ with self.assertRaises(OSError):
104
+ provider.schema_for("u")
105
+
106
+
107
+ class FacadeTest(unittest.TestCase):
108
+ def _provider(self) -> MapProvider:
109
+ return MapProvider.from_json({"urn:babel:orders:created": ORDERS})
110
+
111
+ def test_check_valid_invalid_unregistered(self) -> None:
112
+ provider = self._provider()
113
+ self.assertIsNone(check(provider, "urn:babel:orders:created", {"order_id": 1}))
114
+ self.assertIsNone(check(provider, "urn:babel:unknown", {"x": 1})) # opt-in
115
+ self.assertIsNotNone(check(provider, "urn:babel:orders:created", {}))
116
+
117
+ def test_validate_raises_on_invalid(self) -> None:
118
+ with self.assertRaises(InvalidPayloadError):
119
+ validate(self._provider(), "urn:babel:orders:created", {"order_id": "x"})
120
+
121
+ def test_validate_passes_unregistered(self) -> None:
122
+ validate(self._provider(), "urn:babel:unknown", {"anything": True}) # no raise
123
+
124
+ def test_wrap_runs_handler_on_valid(self) -> None:
125
+ calls: List[int] = []
126
+ wrapped = wrap(self._provider(), "urn:babel:orders:created", lambda data, meta: calls.append(1))
127
+ wrapped({"order_id": 1}, {"id": "m1"})
128
+ self.assertEqual(calls, [1])
129
+
130
+ def test_wrap_raises_and_skips_on_invalid(self) -> None:
131
+ calls: List[int] = []
132
+ wrapped = wrap(self._provider(), "urn:babel:orders:created", lambda data, meta: calls.append(1))
133
+ with self.assertRaises(InvalidPayloadError):
134
+ wrapped({}, {"id": "m1"})
135
+ self.assertEqual(calls, [])
136
+
137
+ def test_wrap_runs_handler_for_unregistered_urn(self) -> None:
138
+ calls: List[int] = []
139
+ wrapped = wrap(self._provider(), "urn:babel:unknown", lambda data, meta: calls.append(1))
140
+ wrapped({"anything": True}, {"id": "m1"})
141
+ self.assertEqual(calls, [1])
142
+
143
+
144
+ if __name__ == "__main__":
145
+ unittest.main()
@@ -0,0 +1,33 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import unittest
5
+ from pathlib import Path
6
+
7
+ from babelqueue.schema import validate_schema
8
+
9
+ MANIFEST = Path(__file__).parent / "conformance" / "manifest.json"
10
+
11
+
12
+ class PayloadConformanceTest(unittest.TestCase):
13
+ """Runs the shared cross-SDK payload-schema cases (ADR-0024) from the vendored conformance
14
+ suite: this validator must agree with the Go and PHP ones on each case's ``valid`` flag."""
15
+
16
+ def test_payload_cases_match_across_sdks(self) -> None:
17
+ if not MANIFEST.is_file():
18
+ self.skipTest("vendored conformance suite not present")
19
+ manifest = json.loads(MANIFEST.read_text(encoding="utf-8"))
20
+ section = manifest.get("payload_schema")
21
+ if not isinstance(section, dict):
22
+ self.skipTest("manifest has no payload_schema section")
23
+
24
+ schema = section["schema"]
25
+ cases = section["cases"]
26
+ self.assertTrue(cases)
27
+ for case in cases:
28
+ valid = validate_schema(schema, case["data"]) is None
29
+ self.assertEqual(case["valid"], valid, f"case {case['name']}")
30
+
31
+
32
+ if __name__ == "__main__":
33
+ unittest.main()
@@ -1,11 +0,0 @@
1
- """Exception hierarchy for BabelQueue."""
2
-
3
- from __future__ import annotations
4
-
5
-
6
- class BabelQueueError(Exception):
7
- """Base for every recoverable BabelQueue error (bad config, empty URN, ...)."""
8
-
9
-
10
- class UnknownUrnError(BabelQueueError):
11
- """A consumed message carries a URN with no mapped handler (strategy "fail")."""
File without changes
File without changes
File without changes
File without changes
File without changes