MCPcopy Index your code
hub / github.com/hugapi/hug / test_basic_documentation

Function test_basic_documentation

tests/test_documentation.py:34–155  ·  view source on GitHub ↗

Ensure creating and then documenting APIs with Hug works as intuitively as expected

()

Source from the content-addressed store, hash-verified

32
33
34def test_basic_documentation():
35 """Ensure creating and then documenting APIs with Hug works as intuitively as expected"""
36
37 @hug.get()
38 def hello_world():
39 """Returns hello world"""
40 return "Hello World!"
41
42 @hug.post()
43 def echo(text):
44 """Returns back whatever data it is given in the text parameter"""
45 return text
46
47 @hug.post("/happy_birthday", examples="name=HUG&age=1")
48 def birthday(name, age: hug.types.number = 1):
49 """Says happy birthday to a user"""
50 return "Happy {age} Birthday {name}!".format(**locals())
51
52 @hug.post()
53 def noop(request, response):
54 """Performs no action"""
55 pass
56
57 @hug.get()
58 def string_docs(data: "Takes data", ignore_directive: hug.directives.Timer) -> "Returns data":
59 """Annotations defined with strings should be documentation only"""
60 pass
61
62 @hug.get(private=True)
63 def private():
64 """Hidden from documentation"""
65 pass
66
67 documentation = api.http.documentation()
68 assert "test_documentation" in documentation["overview"]
69
70 assert "/hello_world" in documentation["handlers"]
71 assert "/echo" in documentation["handlers"]
72 assert "/happy_birthday" in documentation["handlers"]
73 assert "/birthday" not in documentation["handlers"]
74 assert "/noop" in documentation["handlers"]
75 assert "/string_docs" in documentation["handlers"]
76 assert "/private" not in documentation["handlers"]
77
78 assert documentation["handlers"]["/hello_world"]["GET"]["usage"] == "Returns hello world"
79 assert documentation["handlers"]["/hello_world"]["GET"]["examples"] == ["/hello_world"]
80 assert documentation["handlers"]["/hello_world"]["GET"]["outputs"]["content_type"] in [
81 "application/json",
82 "application/json; charset=utf-8",
83 ]
84 assert "inputs" not in documentation["handlers"]["/hello_world"]["GET"]
85
86 assert "text" in documentation["handlers"]["/echo"]["POST"]["inputs"]["text"]["type"]
87 assert "default" not in documentation["handlers"]["/echo"]["POST"]["inputs"]["text"]
88
89 assert "number" in documentation["handlers"]["/happy_birthday"]["POST"]["inputs"]["age"]["type"]
90 assert documentation["handlers"]["/happy_birthday"]["POST"]["inputs"]["age"]["default"] == 1
91

Callers

nothing calls this directly

Calls 3

documentation_404Method · 0.80
documentationMethod · 0.45
loadsMethod · 0.45

Tested by

no test coverage detected