Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mundobien2025_fastapi-basekit /tests /test_params_coercion_unit.py
| """Cobertura exhaustiva de la coerción de `_params` (refactor sin frames). | |
| `BaseController._params` ahora coacciona `request.query_params` (strings) al | |
| tipo declarado en la firma del endpoint. Estos tests martillan las piezas puras | |
| de esa coerción — `_unwrap_optional`, `_coerce_param`, `_as_int` — sobre tablas | |
| grandes, porque un fallo aquí filtra datos MAL en pulbot (filtros booleanos que | |
| se invierten, ints que quedan string, etc.). | |
| """ | |
| from typing import List, Optional, Union | |
| import pytest | |
| from fastapi_basekit.aio.controller.base import ( | |
| BaseController, | |
| _unwrap_optional, | |
| ) | |
| coerce = BaseController._coerce_param | |
| as_int = BaseController._as_int | |
| # --------------------------------------------------------------------------- | |
| # _unwrap_optional | |
| # --------------------------------------------------------------------------- | |
| def test_unwrap_optional(annotation, expected): | |
| assert _unwrap_optional(annotation) == expected | |
| def test_unwrap_optional_pep604(): | |
| # `int | None` (PEP 604, Python 3.10+) → int. Sintaxis nativa, sin eval. | |
| assert _unwrap_optional(int | None) is int | |
| assert _unwrap_optional(bool | None) is bool | |
| # `int | str` (2 no-None) → intacto | |
| assert _unwrap_optional(int | str) == (int | str) | |
| # --------------------------------------------------------------------------- | |
| # _coerce_param — BOOL (el riesgo #1: 'false' es truthy si queda string) | |
| # --------------------------------------------------------------------------- | |
| TRUE_STRINGS = ["1", "true", "True", "TRUE", "t", "T", "yes", "YES", "on", "ON", " true "] | |
| FALSE_STRINGS = ["0", "false", "False", "FALSE", "f", "no", "off", "", " ", "2", "random", "null", "None"] | |
| def test_coerce_bool_truthy(annotation, raw): | |
| assert coerce(raw, annotation) is True | |
| def test_coerce_bool_falsy(annotation, raw): | |
| assert coerce(raw, annotation) is False | |
| # --------------------------------------------------------------------------- | |
| # _coerce_param — INT | |
| # --------------------------------------------------------------------------- | |
| def test_coerce_int_valid(annotation, raw, expected): | |
| assert coerce(raw, annotation) == expected | |
| def test_coerce_int_invalid_stays_string(annotation, raw): | |
| # No debe reventar: valor inválido para int se devuelve tal cual (string). | |
| assert coerce(raw, annotation) == raw | |
| # --------------------------------------------------------------------------- | |
| # _coerce_param — FLOAT | |
| # --------------------------------------------------------------------------- | |
| def test_coerce_float_valid(annotation, raw, expected): | |
| assert coerce(raw, annotation) == expected | |
| def test_coerce_float_invalid_stays_string(annotation, raw): | |
| assert coerce(raw, annotation) == raw | |
| # --------------------------------------------------------------------------- | |
| # _coerce_param — STR / sin anotación / no-string | |
| # --------------------------------------------------------------------------- | |
| def test_coerce_str_or_none_annotation_passthrough(annotation, raw): | |
| # String annotation (o sin anotación): el valor NO se toca. | |
| assert coerce(raw, annotation) == raw | |
| def test_coerce_non_string_passthrough(annotation, raw): | |
| # Si el valor ya NO es string (raro, pero defensivo), se devuelve igual. | |
| assert coerce(raw, annotation) is raw | |
| # --------------------------------------------------------------------------- | |
| # _as_int | |
| # --------------------------------------------------------------------------- | |
| def test_as_int(value, default, expected): | |
| assert as_int(value, default) == expected | |
Xet Storage Details
- Size:
- 5.74 kB
- Xet hash:
- b7a0caff4d35f02f4e2c88e9aac1ff90c64772c358e8aaf54f5500343baf4dd8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.