macss-modular-api-sqlserver 0.5.0__tar.gz → 0.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 (16) hide show
  1. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/PKG-INFO +6 -3
  2. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/README.md +4 -2
  3. macss_modular_api_sqlserver-0.7.0/pyproject.toml +85 -0
  4. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/src/macss_modular_api_sqlserver.egg-info/PKG-INFO +6 -3
  5. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/src/macss_modular_api_sqlserver.egg-info/SOURCES.txt +1 -0
  6. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/src/macss_modular_api_sqlserver.egg-info/requires.txt +1 -0
  7. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/src/modular_api_sqlserver/__init__.py +6 -0
  8. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/src/modular_api_sqlserver/db_client.py +76 -34
  9. macss_modular_api_sqlserver-0.7.0/src/modular_api_sqlserver/py.typed +0 -0
  10. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/tests/test_db_client.py +113 -3
  11. macss_modular_api_sqlserver-0.7.0/tests/test_db_conformance.py +121 -0
  12. macss_modular_api_sqlserver-0.5.0/pyproject.toml +0 -46
  13. macss_modular_api_sqlserver-0.5.0/tests/test_db_conformance.py +0 -68
  14. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/setup.cfg +0 -0
  15. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/src/macss_modular_api_sqlserver.egg-info/dependency_links.txt +0 -0
  16. {macss_modular_api_sqlserver-0.5.0 → macss_modular_api_sqlserver-0.7.0}/src/macss_modular_api_sqlserver.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: macss-modular-api-sqlserver
3
- Version: 0.5.0
3
+ Version: 0.7.0
4
4
  Summary: Official MACSS SQL Server integration package for Python.
5
5
  Author: ccisne.dev
6
6
  License-Expression: MIT
@@ -20,6 +20,7 @@ Classifier: Typing :: Typed
20
20
  Requires-Python: >=3.11
21
21
  Description-Content-Type: text/markdown
22
22
  Provides-Extra: dev
23
+ Requires-Dist: pyright>=1.1.400; extra == "dev"
23
24
  Requires-Dist: pytest>=8.0; extra == "dev"
24
25
 
25
26
  # macss-modular-api-sqlserver
@@ -56,10 +57,12 @@ else:
56
57
 
57
58
  See [example/example.py](example/example.py) for a complete in-memory wiring sample.
58
59
 
59
- ## Current slice
60
+ ## What this package provides
60
61
 
61
62
  - normalized SQL Server connection defaults and redacted summaries
62
63
  - engine-agnostic `DbClient`, `DbRepository`, and transaction contracts
63
64
  - explicit lease ownership semantics for package-owned and application-owned sessions
64
65
  - health contributor and GraphQL support bundle for higher-level integrations
65
- - real driver bindings intentionally remain outside this first slice
66
+ - the application supplies the driver binding (adapter) for its chosen engine and driver
67
+
68
+ This package is **contracts-only by design** and will never ship a driver binding; you choose your engine and driver and provide the adapter. See [ADR-0004](../../../docs/adr/0004-contracts-only-database-packages.md).
@@ -32,10 +32,12 @@ else:
32
32
 
33
33
  See [example/example.py](example/example.py) for a complete in-memory wiring sample.
34
34
 
35
- ## Current slice
35
+ ## What this package provides
36
36
 
37
37
  - normalized SQL Server connection defaults and redacted summaries
38
38
  - engine-agnostic `DbClient`, `DbRepository`, and transaction contracts
39
39
  - explicit lease ownership semantics for package-owned and application-owned sessions
40
40
  - health contributor and GraphQL support bundle for higher-level integrations
41
- - real driver bindings intentionally remain outside this first slice
41
+ - the application supplies the driver binding (adapter) for its chosen engine and driver
42
+
43
+ This package is **contracts-only by design** and will never ship a driver binding; you choose your engine and driver and provide the adapter. See [ADR-0004](../../../docs/adr/0004-contracts-only-database-packages.md).
@@ -0,0 +1,85 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "macss-modular-api-sqlserver"
7
+ version = "0.7.0"
8
+ description = "Official MACSS SQL Server integration package for Python."
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ requires-python = ">=3.11"
12
+ authors = [{ name = "ccisne.dev" }]
13
+ keywords = ["macss", "sqlserver", "database"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Intended Audience :: Developers",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.11",
19
+ "Programming Language :: Python :: 3.12",
20
+ "Programming Language :: Python :: 3.13",
21
+ "Topic :: Database",
22
+ "Typing :: Typed",
23
+ ]
24
+ dependencies = []
25
+
26
+ [project.optional-dependencies]
27
+ dev = [
28
+ # Configured below at `typeCheckingMode = "strict"`. Declared here because a checker the
29
+ # repo configures and never installs is a checker that has never run.
30
+ "pyright>=1.1.400",
31
+ "pytest>=8.0",
32
+ ]
33
+
34
+ [project.urls]
35
+ Homepage = "https://github.com/macss-dev/modular_api"
36
+ Repository = "https://github.com/macss-dev/modular_api/tree/main/code/py/modular_api_sqlserver"
37
+ Issues = "https://github.com/macss-dev/modular_api/issues"
38
+ Documentation = "https://github.com/macss-dev/modular_api/tree/main/code/py/modular_api_sqlserver#readme"
39
+
40
+ [tool.setuptools.packages.find]
41
+ where = ["src"]
42
+
43
+ [tool.pytest.ini_options]
44
+ testpaths = ["tests"]
45
+ pythonpath = ["src"]
46
+
47
+ [tool.pyright]
48
+ pythonVersion = "3.11"
49
+ # `src/` is checked at full strict, because it is what a consumer's own type checker sees: these
50
+ # packages ship `py.typed`, so every annotation here becomes part of the public contract.
51
+ typeCheckingMode = "strict"
52
+
53
+ # `tests/` is strict too, minus the family that means "annotate this expression". Test code binds
54
+ # only this repo, and most of those diagnostics come from incomplete third-party stubs — annotating
55
+ # around them adds noise to a test without making anything more correct. Everything that can indicate
56
+ # a real defect stays on.
57
+ [[tool.pyright.executionEnvironments]]
58
+ root = "tests"
59
+ reportUnknownMemberType = false
60
+ reportUnknownArgumentType = false
61
+ reportUnknownVariableType = false
62
+ reportUnknownParameterType = false
63
+ reportUnknownLambdaType = false
64
+ reportMissingParameterType = false
65
+ reportMissingTypeArgument = false
66
+ # A pytest fixture is called by name through collection, never by a reference the checker can see, so
67
+ # `reportUnusedFunction` reports every `autouse` fixture in this tree. That is the tool not modelling
68
+ # pytest, not dead code. `reportUnusedImport` and `reportUnusedVariable` stay on — those do find dead
69
+ # code, and they did.
70
+ reportUnusedFunction = false
71
+ reportUnusedClass = false
72
+ # Tests legitimately reach into internals to exercise them directly. In `src/` this rule found real
73
+ # coupling and it was fixed rather than silenced.
74
+ reportPrivateUsage = false
75
+ # `example/` is documentation a consumer copies, not a published typed surface — same relaxed family
76
+ # as `tests/`, and the same expectation that nothing which can indicate a real defect is silenced.
77
+ [[tool.pyright.executionEnvironments]]
78
+ root = "example"
79
+ reportUnknownMemberType = false
80
+ reportUnknownArgumentType = false
81
+ reportUnknownVariableType = false
82
+ reportUnknownParameterType = false
83
+ reportUnknownLambdaType = false
84
+ reportMissingParameterType = false
85
+ reportMissingTypeArgument = false
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: macss-modular-api-sqlserver
3
- Version: 0.5.0
3
+ Version: 0.7.0
4
4
  Summary: Official MACSS SQL Server integration package for Python.
5
5
  Author: ccisne.dev
6
6
  License-Expression: MIT
@@ -20,6 +20,7 @@ Classifier: Typing :: Typed
20
20
  Requires-Python: >=3.11
21
21
  Description-Content-Type: text/markdown
22
22
  Provides-Extra: dev
23
+ Requires-Dist: pyright>=1.1.400; extra == "dev"
23
24
  Requires-Dist: pytest>=8.0; extra == "dev"
24
25
 
25
26
  # macss-modular-api-sqlserver
@@ -56,10 +57,12 @@ else:
56
57
 
57
58
  See [example/example.py](example/example.py) for a complete in-memory wiring sample.
58
59
 
59
- ## Current slice
60
+ ## What this package provides
60
61
 
61
62
  - normalized SQL Server connection defaults and redacted summaries
62
63
  - engine-agnostic `DbClient`, `DbRepository`, and transaction contracts
63
64
  - explicit lease ownership semantics for package-owned and application-owned sessions
64
65
  - health contributor and GraphQL support bundle for higher-level integrations
65
- - real driver bindings intentionally remain outside this first slice
66
+ - the application supplies the driver binding (adapter) for its chosen engine and driver
67
+
68
+ This package is **contracts-only by design** and will never ship a driver binding; you choose your engine and driver and provide the adapter. See [ADR-0004](../../../docs/adr/0004-contracts-only-database-packages.md).
@@ -7,5 +7,6 @@ src/macss_modular_api_sqlserver.egg-info/requires.txt
7
7
  src/macss_modular_api_sqlserver.egg-info/top_level.txt
8
8
  src/modular_api_sqlserver/__init__.py
9
9
  src/modular_api_sqlserver/db_client.py
10
+ src/modular_api_sqlserver/py.typed
10
11
  tests/test_db_client.py
11
12
  tests/test_db_conformance.py
@@ -14,6 +14,9 @@ from .db_client import (
14
14
  DbHealthContributor,
15
15
  DbHealthReport,
16
16
  DbHealthStatus,
17
+ DbParameter,
18
+ DbParameterDirection,
19
+ DbProcedureOutcome,
17
20
  DbProviderDescription,
18
21
  DbRepository,
19
22
  DbRepositoryContext,
@@ -40,6 +43,9 @@ __all__ = [
40
43
  "DbHealthContributor",
41
44
  "DbHealthReport",
42
45
  "DbHealthStatus",
46
+ "DbParameter",
47
+ "DbParameterDirection",
48
+ "DbProcedureOutcome",
43
49
  "DbProviderDescription",
44
50
  "DbRepository",
45
51
  "DbRepositoryContext",
@@ -4,9 +4,11 @@ import os
4
4
  import time
5
5
  from dataclasses import dataclass, field
6
6
  from enum import Enum
7
- from typing import Any, Callable, Generic, Mapping, Protocol, TypeVar, cast
7
+ from typing import Callable, Generic, Mapping, Protocol, TypeVar, cast
8
8
 
9
9
  S = TypeVar("S")
10
+ #: Contravariant because the protocols below only ever *accept* a session, never return one.
11
+ S_contra = TypeVar("S_contra", contravariant=True)
10
12
  T = TypeVar("T")
11
13
  R = TypeVar("R")
12
14
  _MISSING = object()
@@ -17,6 +19,13 @@ class DbCommandKind(str, Enum):
17
19
  EXECUTE = "execute"
18
20
  BATCH = "batch"
19
21
  SCALAR = "scalar"
22
+ PROCEDURE = "procedure"
23
+
24
+
25
+ class DbParameterDirection(str, Enum):
26
+ INPUT = "input"
27
+ OUTPUT = "output"
28
+ INPUT_OUTPUT = "inputOutput"
20
29
 
21
30
 
22
31
  class DbFailureKind(str, Enum):
@@ -45,7 +54,7 @@ class DbConnectionSettings:
45
54
  username: str
46
55
  password: str
47
56
  driver: str
48
- options: Mapping[str, object] = field(default_factory=dict)
57
+ options: Mapping[str, object] = field(default_factory=dict[str, object])
49
58
 
50
59
  @classmethod
51
60
  def from_environment(
@@ -84,6 +93,39 @@ class DbConnectionSettings:
84
93
  )
85
94
 
86
95
 
96
+ @dataclass(frozen=True, slots=True)
97
+ class DbParameter:
98
+ name: str
99
+ value: object | None = None
100
+ direction: DbParameterDirection = DbParameterDirection.INPUT
101
+ type_hint: str | None = None
102
+
103
+ @classmethod
104
+ def input(cls, name: str, value: object | None, type_hint: str | None = None) -> DbParameter:
105
+ return cls(name=name, value=value, direction=DbParameterDirection.INPUT, type_hint=type_hint)
106
+
107
+ @classmethod
108
+ def output(cls, name: str, type_hint: str | None = None) -> DbParameter:
109
+ return cls(name=name, direction=DbParameterDirection.OUTPUT, type_hint=type_hint)
110
+
111
+ @classmethod
112
+ def input_output(
113
+ cls, name: str, value: object | None, type_hint: str | None = None
114
+ ) -> DbParameter:
115
+ return cls(
116
+ name=name,
117
+ value=value,
118
+ direction=DbParameterDirection.INPUT_OUTPUT,
119
+ type_hint=type_hint,
120
+ )
121
+
122
+
123
+ @dataclass(frozen=True, slots=True)
124
+ class DbProcedureOutcome:
125
+ return_value: object | None = None
126
+ output_parameters: Mapping[str, object] | None = None
127
+
128
+
87
129
  @dataclass(frozen=True, slots=True)
88
130
  class DbCommand:
89
131
  kind: DbCommandKind
@@ -104,12 +146,14 @@ class DbExecutionMetadata:
104
146
  class DbRowSet:
105
147
  rows: list[Mapping[str, object]]
106
148
  metadata: DbExecutionMetadata
149
+ procedure: DbProcedureOutcome | None = None
107
150
 
108
151
 
109
152
  @dataclass(frozen=True, slots=True)
110
153
  class DbExecutionSummary:
111
154
  affected_count: int
112
155
  metadata: DbExecutionMetadata
156
+ procedure: DbProcedureOutcome | None = None
113
157
 
114
158
 
115
159
  @dataclass(frozen=True, slots=True)
@@ -164,18 +208,18 @@ class DbResult(Generic[T]):
164
208
 
165
209
  def map(self, transform: Callable[[T], R]) -> DbResult[R]:
166
210
  if self.is_failure:
167
- return DbResult.from_failure(self.failure)
168
- return DbResult.success(transform(self.value))
211
+ return DbResult[R].from_failure(self.failure)
212
+ return DbResult[R].success(transform(self.value))
169
213
 
170
214
  def flat_map(self, transform: Callable[[T], DbResult[R]]) -> DbResult[R]:
171
215
  if self.is_failure:
172
- return DbResult.from_failure(self.failure)
216
+ return DbResult[R].from_failure(self.failure)
173
217
  return transform(self.value)
174
218
 
175
219
  def map_failure(self, transform: Callable[[DbFailure], DbFailure]) -> DbResult[T]:
176
220
  if self.is_success:
177
- return DbResult.success(self.value)
178
- return DbResult.from_failure(transform(self.failure))
221
+ return DbResult[T].success(self.value)
222
+ return DbResult[T].from_failure(transform(self.failure))
179
223
 
180
224
  def get_or_throw(self, message: str | None = None) -> T:
181
225
  if self.is_failure:
@@ -205,7 +249,7 @@ class DbSessionLease(Generic[S]):
205
249
 
206
250
  def release(self) -> DbResult[None]:
207
251
  if not self.owned_by_package:
208
- return DbResult.success(None)
252
+ return DbResult[None].success(None)
209
253
  return self._releaser()
210
254
 
211
255
 
@@ -217,12 +261,16 @@ class DbSessionProvider(Protocol[S]):
217
261
  def describe(self) -> DbProviderDescription: ...
218
262
 
219
263
 
220
- class DbCommandExecutor(Protocol[S]):
221
- def query(self, session: S, command: DbCommand) -> DbResult[DbRowSet]: ...
264
+ class DbCommandExecutor(Protocol[S_contra]):
265
+ def query(self, session: S_contra, command: DbCommand) -> DbResult[DbRowSet]: ...
222
266
 
223
- def execute(self, session: S, command: DbCommand) -> DbResult[DbExecutionSummary]: ...
267
+ def execute(
268
+ self, session: S_contra, command: DbCommand
269
+ ) -> DbResult[DbExecutionSummary]: ...
224
270
 
225
- def scalar(self, session: S, command: DbCommand) -> DbResult[DbScalar[object]]: ...
271
+ def scalar(
272
+ self, session: S_contra, command: DbCommand
273
+ ) -> DbResult[DbScalar[object]]: ...
226
274
 
227
275
 
228
276
  @dataclass(frozen=True, slots=True)
@@ -237,8 +285,8 @@ class DbTransactionContext(Generic[S]):
237
285
  def execute(self, command: DbCommand) -> DbResult[DbExecutionSummary]:
238
286
  return self.command_executor.execute(self.session, command)
239
287
 
240
- def scalar(self, command: DbCommand) -> DbResult[DbScalar[T]]:
241
- return cast(DbResult[DbScalar[T]], self.command_executor.scalar(self.session, command))
288
+ def scalar(self, command: DbCommand) -> DbResult[DbScalar[object]]:
289
+ return self.command_executor.scalar(self.session, command)
242
290
 
243
291
 
244
292
  class DbTransactionRunner(Protocol[S]):
@@ -273,13 +321,10 @@ class DbRepository(Generic[S]):
273
321
  lambda lease: self.context.command_executor.execute(lease.session, command),
274
322
  )
275
323
 
276
- def scalar(self, command: DbCommand) -> DbResult[DbScalar[T]]:
277
- return cast(
278
- DbResult[DbScalar[T]],
279
- _with_lease(
280
- self.context.session_provider,
281
- lambda lease: self.context.command_executor.scalar(lease.session, command),
282
- ),
324
+ def scalar(self, command: DbCommand) -> DbResult[DbScalar[object]]:
325
+ return _with_lease(
326
+ self.context.session_provider,
327
+ lambda lease: self.context.command_executor.scalar(lease.session, command),
283
328
  )
284
329
 
285
330
  def transaction(self, body: Callable[[DbTransactionContext[S]], DbResult[T]]) -> DbResult[T]:
@@ -318,19 +363,16 @@ class DbClient(Generic[S]):
318
363
  lambda lease: self.command_executor.execute(lease.session, command),
319
364
  )
320
365
 
321
- def scalar(self, command: DbCommand) -> DbResult[DbScalar[T]]:
322
- return cast(
323
- DbResult[DbScalar[T]],
324
- _with_lease(
325
- self.session_provider,
326
- lambda lease: self.command_executor.scalar(lease.session, command),
327
- ),
366
+ def scalar(self, command: DbCommand) -> DbResult[DbScalar[object]]:
367
+ return _with_lease(
368
+ self.session_provider,
369
+ lambda lease: self.command_executor.scalar(lease.session, command),
328
370
  )
329
371
 
330
372
  def transaction(self, body: Callable[[DbTransactionContext[S]], DbResult[T]]) -> DbResult[T]:
331
373
  lease_result = self.session_provider.acquire()
332
374
  if lease_result.is_failure:
333
- return DbResult.from_failure(lease_result.failure)
375
+ return DbResult[T].from_failure(lease_result.failure)
334
376
 
335
377
  lease = lease_result.value
336
378
  context = DbTransactionContext(
@@ -342,9 +384,9 @@ class DbClient(Generic[S]):
342
384
  release_result = lease.release()
343
385
 
344
386
  if result.is_failure:
345
- return DbResult.from_failure(result.failure)
387
+ return DbResult[T].from_failure(result.failure)
346
388
  if release_result.is_failure:
347
- return DbResult.from_failure(release_result.failure)
389
+ return DbResult[T].from_failure(release_result.failure)
348
390
  return result
349
391
 
350
392
  def repository_context(self) -> DbRepositoryContext[S]:
@@ -415,14 +457,14 @@ def _with_lease(
415
457
  ) -> DbResult[T]:
416
458
  lease_result = session_provider.acquire()
417
459
  if lease_result.is_failure:
418
- return DbResult.from_failure(lease_result.failure)
460
+ return DbResult[T].from_failure(lease_result.failure)
419
461
 
420
462
  lease = lease_result.value
421
463
  operation_result = operation(lease)
422
464
  release_result = lease.release()
423
465
 
424
466
  if operation_result.is_failure:
425
- return DbResult.from_failure(operation_result.failure)
467
+ return DbResult[T].from_failure(operation_result.failure)
426
468
  if release_result.is_failure:
427
- return DbResult.from_failure(release_result.failure)
469
+ return DbResult[T].from_failure(release_result.failure)
428
470
  return operation_result
@@ -4,6 +4,11 @@ from dataclasses import dataclass
4
4
 
5
5
  import pytest
6
6
 
7
+ from collections.abc import Callable
8
+ from typing import TypeVar, cast
9
+
10
+ _T = TypeVar("_T")
11
+
7
12
  from modular_api_sqlserver import (
8
13
  DbClient,
9
14
  DbCommand,
@@ -16,6 +21,9 @@ from modular_api_sqlserver import (
16
21
  DbGraphqlSupport,
17
22
  DbHealthContributor,
18
23
  DbHealthStatus,
24
+ DbParameter,
25
+ DbParameterDirection,
26
+ DbProcedureOutcome,
19
27
  DbProviderDescription,
20
28
  DbRepository,
21
29
  DbRepositoryContext,
@@ -111,6 +119,7 @@ def test_client_delegates_query_calls_and_releases_package_owned_leases() -> Non
111
119
  assert provider.acquire_count == 1
112
120
  assert provider.release_count == 1
113
121
  assert executor.last_session == "db-session"
122
+ assert executor.last_command is not None
114
123
  assert executor.last_command.label == "users.list"
115
124
 
116
125
 
@@ -299,6 +308,7 @@ def test_repository_helpers_stay_thin_over_the_shared_context() -> None:
299
308
 
300
309
  assert result.is_success is True
301
310
  assert result.value == 9
311
+ assert executor.last_command is not None
302
312
  assert executor.last_command.label == "users.count"
303
313
 
304
314
 
@@ -454,8 +464,12 @@ class _FakeTransactionRunner:
454
464
  def run(
455
465
  self,
456
466
  context: DbTransactionContext[str],
457
- body: callable,
458
- ) -> DbResult[object]:
467
+ # Two fixes in one signature. `callable` is the builtin predicate, not a type — annotating with
468
+ # it left this fake failing to satisfy `DbTransactionRunner[str]`, which is the whole point of a
469
+ # fake. And `run` is generic in the body's result: pinning it to `object` still would not have
470
+ # satisfied the protocol, because a runner is transparent in what the body returns.
471
+ body: Callable[[DbTransactionContext[str]], DbResult[_T]],
472
+ ) -> DbResult[_T]:
459
473
  result = body(context)
460
474
  if result.is_success:
461
475
  self.commit_count += 1
@@ -473,4 +487,100 @@ class _UserStatsRepository(DbRepository[str]):
473
487
  label="users.count",
474
488
  )
475
489
  )
476
- return result.map(lambda value: int(value.value))
490
+ # `scalar()` yields `DbScalar[object]`: Python cannot parameterise a method call the way Dart
491
+ # and TypeScript can, so a consumer narrows the value it knows its own query returns.
492
+ return result.map(lambda value: int(cast("int | str", value.value)))
493
+
494
+
495
+ # --- 0.6.0: typed parameters and stored-procedure support ---
496
+
497
+
498
+ def test_db_parameter_input_captures_name_value_and_optional_type_hint() -> None:
499
+ plain = DbParameter.input("id", 42)
500
+ assert plain.name == "id"
501
+ assert plain.value == 42
502
+ assert plain.direction is DbParameterDirection.INPUT
503
+ assert plain.type_hint is None
504
+
505
+ hinted = DbParameter.input("picture", b"\x01\x02\x03", "varbinary(max)")
506
+ assert hinted.direction is DbParameterDirection.INPUT
507
+ assert hinted.type_hint == "varbinary(max)"
508
+
509
+
510
+ def test_db_parameter_output_carries_no_input_value_and_defaults_direction() -> None:
511
+ out = DbParameter.output("total", "int")
512
+ assert out.name == "total"
513
+ assert out.value is None
514
+ assert out.direction is DbParameterDirection.OUTPUT
515
+ assert out.type_hint == "int"
516
+
517
+
518
+ def test_db_parameter_input_output_marks_bidirectional_parameters() -> None:
519
+ io = DbParameter.input_output("counter", 1, "int")
520
+ assert io.direction is DbParameterDirection.INPUT_OUTPUT
521
+ assert io.value == 1
522
+
523
+
524
+ def test_db_parameter_defaults_direction_to_input_when_constructed_directly() -> None:
525
+ param = DbParameter(name="name", value="foto.jpg")
526
+ assert param.direction is DbParameterDirection.INPUT
527
+
528
+
529
+ def test_db_parameter_flows_through_db_command_parameters_unchanged() -> None:
530
+ command = DbCommand(
531
+ kind=DbCommandKind.PROCEDURE,
532
+ text="spEliminarFoto",
533
+ parameters=(DbParameter.input("nombre", "foto.jpg"), "positional-still-allowed"),
534
+ )
535
+ assert len(command.parameters) == 2
536
+ assert isinstance(command.parameters[0], DbParameter)
537
+ assert command.parameters[0].name == "nombre"
538
+ assert command.parameters[1] == "positional-still-allowed"
539
+
540
+
541
+ def test_db_command_kind_exposes_procedure() -> None:
542
+ assert DbCommandKind.PROCEDURE.value == "procedure"
543
+
544
+
545
+ def test_db_procedure_outcome_carries_return_value_and_output_parameters() -> None:
546
+ outcome = DbProcedureOutcome(return_value=0, output_parameters={"total": 5})
547
+ assert outcome.return_value == 0
548
+ assert outcome.output_parameters == {"total": 5}
549
+
550
+
551
+ def test_db_procedure_outcome_allows_both_fields_to_be_absent() -> None:
552
+ empty = DbProcedureOutcome()
553
+ assert empty.return_value is None
554
+ assert empty.output_parameters is None
555
+
556
+
557
+ def test_db_procedure_outcome_attaches_optionally_to_db_row_set() -> None:
558
+ without_outcome = DbRowSet(
559
+ rows=[{"id": 1}],
560
+ metadata=DbExecutionMetadata(duration=1),
561
+ )
562
+ assert without_outcome.procedure is None
563
+
564
+ with_outcome = DbRowSet(
565
+ rows=[{"id": 1}],
566
+ metadata=DbExecutionMetadata(duration=1),
567
+ procedure=DbProcedureOutcome(return_value=0),
568
+ )
569
+ assert with_outcome.procedure is not None
570
+ assert with_outcome.procedure.return_value == 0
571
+
572
+
573
+ def test_db_procedure_outcome_attaches_optionally_to_db_execution_summary() -> None:
574
+ without_outcome = DbExecutionSummary(
575
+ affected_count=1,
576
+ metadata=DbExecutionMetadata(duration=1),
577
+ )
578
+ assert without_outcome.procedure is None
579
+
580
+ with_outcome = DbExecutionSummary(
581
+ affected_count=1,
582
+ metadata=DbExecutionMetadata(duration=1),
583
+ procedure=DbProcedureOutcome(output_parameters={"id": 99}),
584
+ )
585
+ assert with_outcome.procedure is not None
586
+ assert with_outcome.procedure.output_parameters == {"id": 99}
@@ -0,0 +1,121 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from pathlib import Path
5
+ from typing import Any, cast
6
+
7
+ from modular_api_sqlserver import (
8
+ DbCommand,
9
+ DbCommandKind,
10
+ DbConnectionSettings,
11
+ DbFailure,
12
+ DbFailureKind,
13
+ DbParameter,
14
+ DbProcedureOutcome,
15
+ DbResult,
16
+ )
17
+
18
+
19
+ def _load_fixture() -> dict[str, Any]:
20
+ # `dict[str, Any]`, not `dict[str, object]`: every read below walks into the fixture, and `object`
21
+ # stops at the first subscript. The values really are arbitrary JSON.
22
+ return cast("dict[str, Any]", json.loads(
23
+ (Path(__file__).resolve().parents[3] / "tests" / "fixtures" / "db_client" / "sqlserver.json").read_text(
24
+ encoding="utf-8"
25
+ )
26
+ ))
27
+
28
+
29
+ def test_matches_the_shared_sqlserver_connection_fixture() -> None:
30
+ fixture = _load_fixture()
31
+ connection = fixture["connection"]
32
+ expected = connection["expected"]
33
+
34
+ settings = DbConnectionSettings.from_environment(connection["environment"])
35
+
36
+ assert settings.engine_id == expected["engineId"]
37
+ assert settings.host == expected["host"]
38
+ assert settings.port == expected["port"]
39
+ assert settings.database == expected["database"]
40
+ assert settings.username == expected["username"]
41
+ assert settings.password == expected["password"]
42
+ assert settings.driver == expected["driver"]
43
+
44
+ for fragment in connection["redactedContains"]:
45
+ assert fragment in settings.redacted_summary
46
+ for fragment in connection["redactedExcludes"]:
47
+ assert fragment not in settings.redacted_summary
48
+
49
+
50
+ def test_matches_the_shared_db_result_fixture() -> None:
51
+ fixture = _load_fixture()
52
+ result_fixture = fixture["result"]
53
+
54
+ success = DbResult.success(result_fixture["successValue"])
55
+ failure = DbResult.from_failure(
56
+ DbFailure(
57
+ kind=DbFailureKind.TIMEOUT,
58
+ code=result_fixture["timeoutCode"],
59
+ message="Timed out",
60
+ retryable=True,
61
+ transient=True,
62
+ )
63
+ )
64
+
65
+ assert success.map(lambda value: value * 2).value == result_fixture["mappedValue"]
66
+ assert success.flat_map(lambda value: DbResult.success(value + 1)).value == result_fixture[
67
+ "flatMappedValue"
68
+ ]
69
+
70
+ mapped_failure = failure.map_failure(
71
+ lambda current: DbFailure(
72
+ kind=current.kind,
73
+ code=result_fixture["wrappedFailureCode"],
74
+ message=current.message,
75
+ retryable=current.retryable,
76
+ transient=current.transient,
77
+ )
78
+ )
79
+
80
+ assert mapped_failure.failure.code == result_fixture["wrappedFailureCode"]
81
+
82
+
83
+ def test_matches_the_shared_typed_parameter_and_stored_procedure_fixture() -> None:
84
+ command_fixture = _load_fixture()["command"]
85
+ input_fixture = command_fixture["inputParameter"]
86
+
87
+ input_parameter = DbParameter.input(
88
+ input_fixture["name"], input_fixture["value"], input_fixture["typeHint"]
89
+ )
90
+ assert input_parameter.name == input_fixture["name"]
91
+ assert input_parameter.value == input_fixture["value"]
92
+ assert input_parameter.type_hint == input_fixture["typeHint"]
93
+ assert input_parameter.direction.value == input_fixture["expectedDirection"]
94
+
95
+ output_fixture = command_fixture["outputParameter"]
96
+ output_parameter = DbParameter.output(output_fixture["name"], output_fixture["typeHint"])
97
+ assert output_parameter.value is None
98
+ assert output_parameter.direction.value == output_fixture["expectedDirection"]
99
+
100
+ command = DbCommand(
101
+ kind=DbCommandKind.PROCEDURE,
102
+ text=command_fixture["procedureName"],
103
+ parameters=(input_parameter,),
104
+ )
105
+ assert command.kind.value == "procedure"
106
+ assert command.text == command_fixture["procedureName"]
107
+ assert isinstance(command.parameters[0], DbParameter)
108
+
109
+ outcome_fixture = command_fixture["outcome"]
110
+ outcome = DbProcedureOutcome(
111
+ return_value=outcome_fixture["returnValue"],
112
+ output_parameters={
113
+ outcome_fixture["outputParameterName"]: outcome_fixture["outputParameterValue"]
114
+ },
115
+ )
116
+ assert outcome.return_value == outcome_fixture["returnValue"]
117
+ assert outcome.output_parameters is not None
118
+ assert (
119
+ outcome.output_parameters[outcome_fixture["outputParameterName"]]
120
+ == outcome_fixture["outputParameterValue"]
121
+ )
@@ -1,46 +0,0 @@
1
- [build-system]
2
- requires = ["setuptools>=68", "wheel"]
3
- build-backend = "setuptools.build_meta"
4
-
5
- [project]
6
- name = "macss-modular-api-sqlserver"
7
- version = "0.5.0"
8
- description = "Official MACSS SQL Server integration package for Python."
9
- readme = "README.md"
10
- license = "MIT"
11
- requires-python = ">=3.11"
12
- authors = [{ name = "ccisne.dev" }]
13
- keywords = ["macss", "sqlserver", "database"]
14
- classifiers = [
15
- "Development Status :: 4 - Beta",
16
- "Intended Audience :: Developers",
17
- "Programming Language :: Python :: 3",
18
- "Programming Language :: Python :: 3.11",
19
- "Programming Language :: Python :: 3.12",
20
- "Programming Language :: Python :: 3.13",
21
- "Topic :: Database",
22
- "Typing :: Typed",
23
- ]
24
- dependencies = []
25
-
26
- [project.optional-dependencies]
27
- dev = [
28
- "pytest>=8.0",
29
- ]
30
-
31
- [project.urls]
32
- Homepage = "https://github.com/macss-dev/modular_api"
33
- Repository = "https://github.com/macss-dev/modular_api/tree/main/code/py/modular_api_sqlserver"
34
- Issues = "https://github.com/macss-dev/modular_api/issues"
35
- Documentation = "https://github.com/macss-dev/modular_api/tree/main/code/py/modular_api_sqlserver#readme"
36
-
37
- [tool.setuptools.packages.find]
38
- where = ["src"]
39
-
40
- [tool.pytest.ini_options]
41
- testpaths = ["tests"]
42
- pythonpath = ["src"]
43
-
44
- [tool.pyright]
45
- pythonVersion = "3.11"
46
- typeCheckingMode = "strict"
@@ -1,68 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import json
4
- from pathlib import Path
5
-
6
- from modular_api_sqlserver import DbConnectionSettings, DbFailure, DbFailureKind, DbResult
7
-
8
-
9
- def _load_fixture() -> dict[str, object]:
10
- return json.loads(
11
- (Path(__file__).resolve().parents[3] / "tests" / "fixtures" / "db_client" / "sqlserver.json").read_text(
12
- encoding="utf-8"
13
- )
14
- )
15
-
16
-
17
- def test_matches_the_shared_sqlserver_connection_fixture() -> None:
18
- fixture = _load_fixture()
19
- connection = fixture["connection"]
20
- expected = connection["expected"]
21
-
22
- settings = DbConnectionSettings.from_environment(connection["environment"])
23
-
24
- assert settings.engine_id == expected["engineId"]
25
- assert settings.host == expected["host"]
26
- assert settings.port == expected["port"]
27
- assert settings.database == expected["database"]
28
- assert settings.username == expected["username"]
29
- assert settings.password == expected["password"]
30
- assert settings.driver == expected["driver"]
31
-
32
- for fragment in connection["redactedContains"]:
33
- assert fragment in settings.redacted_summary
34
- for fragment in connection["redactedExcludes"]:
35
- assert fragment not in settings.redacted_summary
36
-
37
-
38
- def test_matches_the_shared_db_result_fixture() -> None:
39
- fixture = _load_fixture()
40
- result_fixture = fixture["result"]
41
-
42
- success = DbResult.success(result_fixture["successValue"])
43
- failure = DbResult.from_failure(
44
- DbFailure(
45
- kind=DbFailureKind.TIMEOUT,
46
- code=result_fixture["timeoutCode"],
47
- message="Timed out",
48
- retryable=True,
49
- transient=True,
50
- )
51
- )
52
-
53
- assert success.map(lambda value: value * 2).value == result_fixture["mappedValue"]
54
- assert success.flat_map(lambda value: DbResult.success(value + 1)).value == result_fixture[
55
- "flatMappedValue"
56
- ]
57
-
58
- mapped_failure = failure.map_failure(
59
- lambda current: DbFailure(
60
- kind=current.kind,
61
- code=result_fixture["wrappedFailureCode"],
62
- message=current.message,
63
- retryable=current.retryable,
64
- transient=current.transient,
65
- )
66
- )
67
-
68
- assert mapped_failure.failure.code == result_fixture["wrappedFailureCode"]