Debian Codegraph @

tests/conftest.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 import inspect
2 import json
3 import random
4
5 import pytest
•6 from click.testing import CliRunner
7 from flasgger import Swagger
8 from flasgger.utils import get_examples
9
10 from flask import Flask
11
12
13 def get_specs_data(mod):
14 """
15 return all specs dictionary for some app
16 """
17 # for each example app in /examples folder
18 from flask.testing import FlaskClient
19 client: FlaskClient = mod.app.test_client()
20 # init swag if not yet inititalized (no-routes example)
21 specs_route = None
22 specs_data = {}
23 if getattr(mod.app, 'swag', None) is None:
24 _swag = Swagger()
25 _swag.config['endpoint'] = str(random.randint(1, 5000))
26 _swag.init_app(mod.app)
27 # get all the specs defined for the example app
28 else:
29 try:
30 flasgger_config = mod.swag.config
31
32 if flasgger_config.get('swagger_ui') is False:
33 return specs_data
34
35 specs_route = flasgger_config.get('specs_route', '/apidocs/')
36 except AttributeError:
37 pass
38
39 if specs_route is None:
40 specs_route = '/apidocs/'
41
42 apidocs = client.get('?'.join((specs_route, 'json=true')))
43 if 200 <= apidocs.status_code < 300:
44 specs = json.loads(apidocs.data.decode("utf-8")).get('specs')
45
46 for spec in specs:
47 # for each spec get the spec url
48 url = spec['url']
49 response = client.get(url)
50 decoded = response.data.decode("utf-8")
51 if 200 <= response.status_code < 300:
52 specs_data[url] = json.loads(decoded)
53 else:
54 raise ValueError(f'specs failure: {mod.__name__} {response.status}')
55 else:
56 raise ValueError(f'specs meta failure: {mod.__name__} {apidocs.status}')
57
58 return specs_data
59
60
61 def get_test_metadata(mod):
62 """Create a dictionary of test metadata defined in an example
63
64 Every top-level constant prefixed with "_TEST_META_" is treated as
65 metadata which may control test behavior. The prefix is stripped and the
66 remaining text is lowercased to form the key in the metadata dictionary.
67
68 Example: '_TEST_META_SKIP_FULL_VALIDATION' -> 'skip_full_validation'
69 """
70
71 test_metadata_prefix = '_TEST_META_'
72 return {key[len(test_metadata_prefix):].lower(): getattr(mod, key)
73 for key in mod.__dict__
74 if key.startswith(test_metadata_prefix)}
75
76
77 def pytest_generate_tests(metafunc):
78 """
79 parametrize tests using examples() function
80 to generate one test for each examples/
81 """
82
83 if 'test_data' in metafunc.fixturenames:
84 test_data = [
85 (mod, mod.app.test_client(),
86 get_specs_data(mod), get_test_metadata(mod))
87 for mod in get_examples()
88 ]
89
90 metafunc.parametrize(
91 'test_data',
92 test_data,
93 ids=lambda x: x[0].__name__
94 )
95
96
97 @pytest.fixture(scope="function")
98 def app():
99 yield Flask(__name__)
100
101
102 @pytest.fixture(scope="function")
103 def cli_runner(app):
•104 kwargs = {}
105 if "mix_stderr" in inspect.signature(CliRunner).parameters:
106 # click < 8.2
107 kwargs["mix_stderr"] = False
108 yield app.test_cli_runner(**kwargs)