|
| 1 | +### IMPORTS |
| 2 | +### ============================================================================ |
| 3 | +## Future |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +## Standard Library |
| 7 | + |
| 8 | +## Installed |
| 9 | +import pytest |
| 10 | + |
| 11 | +## Application |
| 12 | +import pythonjsonlogger |
| 13 | +from pythonjsonlogger.utils import package_is_available |
| 14 | +from pythonjsonlogger.exception import MissingPackageError |
| 15 | + |
| 16 | +### CONSTANTS |
| 17 | +### ============================================================================ |
| 18 | +MISSING_PACKAGE_NAME = "package_name_is_definintely_not_available" |
| 19 | +MISSING_PACKAGE_EXTRA = "package_extra_that_is_unique" |
| 20 | + |
| 21 | + |
| 22 | +### TESTS |
| 23 | +### ============================================================================ |
| 24 | +def test_package_is_available(): |
| 25 | + assert package_is_available("json") |
| 26 | + return |
| 27 | + |
| 28 | + |
| 29 | +def test_package_not_available(): |
| 30 | + assert not package_is_available(MISSING_PACKAGE_NAME) |
| 31 | + return |
| 32 | + |
| 33 | + |
| 34 | +def test_package_not_available_throw(): |
| 35 | + with pytest.raises(MissingPackageError) as e: |
| 36 | + package_is_available(MISSING_PACKAGE_NAME, throw_error=True) |
| 37 | + assert MISSING_PACKAGE_NAME in e.value.msg |
| 38 | + assert MISSING_PACKAGE_EXTRA not in e.value.msg |
| 39 | + return |
| 40 | + |
| 41 | + |
| 42 | +def test_package_not_available_throw_extras(): |
| 43 | + with pytest.raises(MissingPackageError) as e: |
| 44 | + package_is_available( |
| 45 | + MISSING_PACKAGE_NAME, throw_error=True, extras_name=MISSING_PACKAGE_EXTRA |
| 46 | + ) |
| 47 | + assert MISSING_PACKAGE_NAME in e.value.msg |
| 48 | + assert MISSING_PACKAGE_EXTRA in e.value.msg |
| 49 | + return |
| 50 | + |
| 51 | + |
| 52 | +## Python JSON Logger Specific |
| 53 | +## ----------------------------------------------------------------------------- |
| 54 | +if not pythonjsonlogger.ORJSON_AVAILABLE: |
| 55 | + |
| 56 | + def test_orjson_import_error(): |
| 57 | + with pytest.raises(MissingPackageError, match="orjson"): |
| 58 | + import pythonjsonlogger.orjson |
| 59 | + return |
| 60 | + |
| 61 | + |
| 62 | +if not pythonjsonlogger.MSGSPEC_AVAILABLE: |
| 63 | + |
| 64 | + def test_msgspec_import_error(): |
| 65 | + with pytest.raises(MissingPackageError, match="msgspec"): |
| 66 | + import pythonjsonlogger.msgspec |
| 67 | + return |
0 commit comments