[e988c2]: / tests / unit / docs / test_common.py

Download this file

69 lines (52 with data), 1.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pytest
from ehrql.docs.common import get_docstring, get_function_body
class ExampleClass:
# Comments above
@staticmethod
def example_method_with_docstring(
arg1: int,
arg2: str,
) -> str: # pragma: no cover
"""
Docstring goes here
"""
# Make it bigger
arg1 = arg1 + 100
# Make it smaller
arg1 = arg1 // 2
return arg2 + str(arg1)
def example_method_no_docstring(self): # pragma: no cover
# Return the thing
return "foo"
EXPECTED_WITH_DOCSTRING = """\
# Make it bigger
arg1 = arg1 + 100
# Make it smaller
arg1 = arg1 // 2
return arg2 + str(arg1)
"""
EXPECTED_NO_DOCSTRING = """\
# Return the thing
return "foo"
"""
@pytest.mark.parametrize(
"method,expected",
[
(ExampleClass.example_method_with_docstring, EXPECTED_WITH_DOCSTRING),
(ExampleClass.example_method_no_docstring, EXPECTED_NO_DOCSTRING),
],
)
def test_get_function_body(method, expected):
assert get_function_body(method) == expected
def test_get_docstring():
assert (
get_docstring(ExampleClass.example_method_with_docstring)
== "Docstring goes here"
)
def test_get_docstring_with_default():
assert (
get_docstring(ExampleClass.example_method_no_docstring, default="foo") == "foo"
)
def test_get_docstring_with_error():
with pytest.raises(ValueError, match="No docstring defined for public object"):
get_docstring(ExampleClass.example_method_no_docstring)