|
1 | | -from elasticapm.utils import get_url_dict |
| 1 | +from functools import partial |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from elasticapm.utils import get_name_from_func, get_url_dict |
2 | 6 | from elasticapm.utils.deprecation import deprecated |
3 | 7 |
|
| 8 | +try: |
| 9 | + from functools import partialmethod |
| 10 | +except ImportError: |
| 11 | + # Python 2 |
| 12 | + partialmethod = None |
| 13 | + |
4 | 14 |
|
5 | 15 | @deprecated("alternative") |
6 | 16 | def deprecated_function(): |
@@ -51,3 +61,53 @@ def test_get_url_dict(): |
51 | 61 | } |
52 | 62 | for url, expected in data.items(): |
53 | 63 | assert get_url_dict(url) == expected |
| 64 | + |
| 65 | + |
| 66 | +def test_get_name_from_func(): |
| 67 | + def x(): |
| 68 | + pass |
| 69 | + |
| 70 | + assert "tests.utils.tests.x" == get_name_from_func(x) |
| 71 | + |
| 72 | + |
| 73 | +def test_get_name_from_func_class(): |
| 74 | + class X(object): |
| 75 | + def x(self): |
| 76 | + pass |
| 77 | + |
| 78 | + assert "tests.utils.tests.x" == get_name_from_func(X.x) |
| 79 | + assert "tests.utils.tests.x" == get_name_from_func(X().x) |
| 80 | + |
| 81 | + |
| 82 | +def test_get_name_from_func_partial(): |
| 83 | + def x(x): |
| 84 | + pass |
| 85 | + |
| 86 | + p = partial(x, "x") |
| 87 | + assert "partial(tests.utils.tests.x)" == get_name_from_func(p) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.skipif(partialmethod is None, reason="partialmethod not available on Python 2") |
| 91 | +def test_get_name_from_func_partialmethod_unbound(): |
| 92 | + class X(object): |
| 93 | + def x(self, x): |
| 94 | + pass |
| 95 | + |
| 96 | + p = partialmethod(x, "x") |
| 97 | + |
| 98 | + assert "partial(tests.utils.tests.x)" == get_name_from_func(X.p) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.skipif(partialmethod is None, reason="partialmethod not available on Python 2") |
| 102 | +def test_get_name_from_func_partialmethod_bound(): |
| 103 | + class X(object): |
| 104 | + def x(self, x): |
| 105 | + pass |
| 106 | + |
| 107 | + p = partialmethod(x, "x") |
| 108 | + |
| 109 | + assert "partial(tests.utils.tests.x)" == get_name_from_func(X().p) |
| 110 | + |
| 111 | + |
| 112 | +def test_get_name_from_func_lambda(): |
| 113 | + assert "tests.utils.tests.<lambda>" == get_name_from_func(lambda x: "x") |
0 commit comments