Skip to content

Commit 8894278

Browse files
Tomas-PytelTomas-Pytel
authored andcommitted
Add tests for routers
1 parent f0b51c0 commit 8894278

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

tests/test_routes.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from fastapi import FastAPI
2+
from fastapi.testclient import TestClient
3+
from fastapi_featureflags import router as ff_router
4+
5+
app = FastAPI()
6+
app.include_router(ff_router, prefix="/ff", tags=["FeatureFlags"])
7+
client = TestClient(app)
8+
9+
10+
def test_openapi_json(featureflags):
11+
response = client.get("/openapi.json")
12+
assert response.status_code == 200
13+
assert "/all" in response.text
14+
15+
16+
def test_create_ff(featureflags):
17+
featureflags.load_conf_from_url("https://pastebin.com/raw/4Ai3j2DC")
18+
assert featureflags.get_features() == {
19+
"web_only": False,
20+
"web_1": True,
21+
"web_2": False,
22+
"web_3": True,
23+
"web_4": False,
24+
}
25+
26+
27+
def test_routes_all():
28+
response = client.get("/ff/all")
29+
assert response.status_code == 200
30+
assert response.json() == {
31+
"web_only": False,
32+
"web_1": True,
33+
"web_2": False,
34+
"web_3": True,
35+
"web_4": False,
36+
}
37+
38+
39+
def test_routes_enable_ff():
40+
response = client.get("/ff/enable/web_only")
41+
assert response.status_code == 200
42+
assert response.json() == {"feature_flag": "web_only", "enabled": True}
43+
44+
response = client.get("/ff/all")
45+
assert response.status_code == 200
46+
assert response.json()["web_only"] is True
47+
48+
49+
def test_routes_disable_ff():
50+
response = client.get("/ff/disable/web_1")
51+
assert response.status_code == 200
52+
assert response.json() == {"feature_flag": "web_1", "enabled": False}
53+
54+
response = client.get("/ff/all")
55+
assert response.status_code == 200
56+
assert response.json()["web_1"] is False
57+
58+
59+
def test_routes_reload_ff():
60+
response = client.get("/ff/reload")
61+
assert response.status_code == 200
62+
assert response.json() == {
63+
"feature_flags": {
64+
"web_only": False,
65+
"web_1": True,
66+
"web_2": False,
67+
"web_3": True,
68+
"web_4": False,
69+
},
70+
"reloaded": True,
71+
}
72+
73+
response = client.get("/ff/all")
74+
assert response.status_code == 200
75+
assert response.json()["web_1"] is True
76+
77+
78+
app = FastAPI()
79+
app.include_router(
80+
ff_router, prefix="/ff", tags=["FeatureFlags"], include_in_schema=False
81+
)
82+
client_wo_schema = TestClient(app)
83+
84+
85+
def test_schema_without_routes():
86+
response = client_wo_schema.get("/openapi.json")
87+
assert response.status_code == 200
88+
assert "/all" not in response.text
89+
assert response.json()["paths"] == {}

0 commit comments

Comments
 (0)