File: test_base.py

package info (click to toggle)
python-flasgger 0.9.7.2~dev2%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,684 kB
  • sloc: javascript: 6,384; python: 4,379; makefile: 8; sh: 1
file content (71 lines) | stat: -rw-r--r-- 2,102 bytes parent folder | download
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
69
70
71
import pytest
from flasgger.base import Swagger


def test_init_config(monkeypatch):

    def __init__(self, config=None, merge=False):
        self._init_config(config, merge)

    monkeypatch.setattr(Swagger, "__init__", __init__)

    # # Unspecified config will be initialized to dict()
    t = Swagger(config=None, merge=False)
    assert t.config == Swagger.DEFAULT_CONFIG

    # Empty dict passed to arguments will be overriden with default_config
    empty_dict = dict()
    t = Swagger(config=empty_dict, merge=False)
    assert t.config == Swagger.DEFAULT_CONFIG
    assert t.config is not empty_dict

    # Config will be merged
    d = {"a": 0}
    t = Swagger(config=d, merge=False)
    assert t.config is d

    # Config will be overridden
    t = Swagger(config={"a": 0}, merge=False)
    assert t.config == {"a": 0}

    # Config will be merged
    t = Swagger(config={"a": 0}, merge=True)
    assert t.config.items() > {"a": 0}.items()
    assert all( t.config[k] == v for k, v in Swagger.DEFAULT_CONFIG.items() )

    # Config will be merged
    empty_dict = dict()
    t = Swagger(config=empty_dict, merge=True)
    assert t.config == Swagger.DEFAULT_CONFIG

    # keys in DEFAULT_CONFIG will be overridden
    d = {
        "specs": [
            {
                "endpoint": "swagger",
                "route": "/characteristics/swagger.json",
                "rule_filter": lambda rule: True,  # all in
                "model_filter": lambda tag: True,  # all in
            }
        ],
    }
    t = Swagger(config=d, merge=True)
    assert all(t.config[k] == v for k, v in d.items())
    assert t.config["specs"] == d["specs"]


def test_get_apispecs_with_invalid_endpoint(app):
    swagger = Swagger(app)

    with app.app_context():
        with pytest.raises(RuntimeError) as e:
            bad_endpoint = "Bad endpoint"
            swagger.get_apispecs(bad_endpoint)
            assert bad_endpoint in e


def test_get_apispecs_with_valid_endpoint(app):
    swagger = Swagger(app)

    with app.app_context():
        assert swagger.get_apispecs(Swagger.DEFAULT_ENDPOINT)