|
| 1 | +from datasette.utils.callable import check_callable |
| 2 | +import pytest |
| 3 | + |
| 4 | + |
| 5 | +class AsyncClass: |
| 6 | +async def __call__(self): |
| 7 | +pass |
| 8 | + |
| 9 | + |
| 10 | +class NotAsyncClass: |
| 11 | +def __call__(self): |
| 12 | +pass |
| 13 | + |
| 14 | + |
| 15 | +class ClassNoCall: |
| 16 | +pass |
| 17 | + |
| 18 | + |
| 19 | +async def async_func(): |
| 20 | +pass |
| 21 | + |
| 22 | + |
| 23 | +def non_async_func(): |
| 24 | +pass |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "obj,expected_is_callable,expected_is_async_callable", |
| 29 | + ( |
| 30 | + (async_func, True, True), |
| 31 | + (non_async_func, True, False), |
| 32 | + (AsyncClass(), True, True), |
| 33 | + (NotAsyncClass(), True, False), |
| 34 | + (ClassNoCall(), False, False), |
| 35 | + (AsyncClass, True, False), |
| 36 | + (NotAsyncClass, True, False), |
| 37 | + (ClassNoCall, True, False), |
| 38 | + ("", False, False), |
| 39 | + (1, False, False), |
| 40 | + (str, True, False), |
| 41 | + ), |
| 42 | +) |
| 43 | +def test_check_callable(obj, expected_is_callable, expected_is_async_callable): |
| 44 | +status = check_callable(obj) |
| 45 | +assert status.is_callable == expected_is_callable |
| 46 | +assert status.is_async_callable == expected_is_async_callable |