Skip to content

Commit 56d9b02

Browse files
Tomas-PytelTomas-Pytel
Tomas-Pytel
authored and
Tomas-Pytel
committed
Clear features attribute when load from config
Fix and extend tests
1 parent c39b2b2 commit 56d9b02

File tree

6 files changed

+80
-14
lines changed

6 files changed

+80
-14
lines changed

fastapi_featureflags/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ class FeatureFlags(object):
99

1010
@staticmethod
1111
def load_conf_from_url(conf_from_url):
12+
FeatureFlags.features.clear()
1213
FeatureFlags.conf_from_url = conf_from_url
1314
params = requests.get(conf_from_url).json()
1415
for k, v in params.items():
1516
FeatureFlags.features[k] = v
1617

1718
@staticmethod
1819
def load_conf_from_json(conf_from_json):
20+
FeatureFlags.features.clear()
1921
FeatureFlags.conf_from_json = conf_from_json
2022
with open(conf_from_json, "r") as f:
2123
params = json.loads(f.read())

tests/conftest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import pytest
21
import sys
32
from pathlib import Path
43

5-
sys.path.append(str(Path(".").absolute().parent))
4+
import pytest
65

76
from fastapi_featureflags import FeatureFlags
87

8+
sys.path.append(str(Path(".").absolute().parent))
9+
910

1011
@pytest.fixture(scope="module")
1112
def featureflags():
12-
return FeatureFlags()
13+
return FeatureFlags

tests/features.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"json_only": false,
3-
"ps": true,
4-
"mam": false,
5-
"ror": true,
6-
"dd": true
3+
"file_1": true,
4+
"file_2": false,
5+
"file_3": true,
6+
"file_4": true
77
}

tests/test_1.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/test_conf_from_json.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def test_ff_from_json(featureflags):
2+
featureflags.load_conf_from_json("tests/features.json")
3+
assert type(featureflags.get_features()) is dict
4+
assert featureflags.get_features() == {
5+
"json_only": False,
6+
"file_1": True,
7+
"file_2": False,
8+
"file_3": True,
9+
"file_4": True
10+
}
11+
12+
13+
def test_reload_features(featureflags):
14+
assert featureflags.get_features()["json_only"] is False
15+
assert featureflags.get_features()["file_1"] is True
16+
17+
featureflags.enable_feature("json_only")
18+
featureflags.disable_feature("file_1")
19+
assert featureflags.get_features()["json_only"] is True
20+
assert featureflags.get_features()["file_1"] is False
21+
22+
action = featureflags.reload_feature_flags()
23+
assert action is True
24+
assert featureflags.get_features()["json_only"] is False
25+
assert featureflags.get_features()["file_1"] is True

tests/test_conf_from_url.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from fastapi_featureflags import feature_enabled
2+
3+
4+
def test_ff_from_url(featureflags):
5+
featureflags.load_conf_from_url("https://pastebin.com/raw/4Ai3j2DC")
6+
assert type(featureflags.get_features()) is dict
7+
assert featureflags.get_features() == {
8+
"web_only": False,
9+
"web_1": True,
10+
"web_2": False,
11+
"web_3": True,
12+
"web_4": False
13+
}
14+
15+
16+
def test_enable_feature(featureflags):
17+
assert featureflags.get_features()["web_only"] is False
18+
action = featureflags.enable_feature("web_only")
19+
assert action is True
20+
assert featureflags.get_features()["web_only"] is True
21+
22+
23+
def test_disable_feature(featureflags):
24+
assert featureflags.get_features()["web_1"] is True
25+
action = featureflags.disable_feature("web_1")
26+
assert action is False
27+
assert featureflags.get_features()["web_1"] is False
28+
29+
30+
def test_feature_enabled(featureflags):
31+
assert featureflags.get_features()["web_4"] is False
32+
assert feature_enabled("web_4") is False
33+
assert featureflags.is_enabled("web_4") is False
34+
featureflags.enable_feature("web_4")
35+
assert feature_enabled("web_4") is True
36+
assert featureflags.is_enabled("web_4") is True
37+
38+
39+
def test_reload_features(featureflags):
40+
assert featureflags.get_features()["web_only"] is True
41+
assert featureflags.get_features()["web_1"] is False
42+
action = featureflags.reload_feature_flags()
43+
assert action is True
44+
assert featureflags.get_features()["web_only"] is False
45+
assert featureflags.get_features()["web_1"] is True

0 commit comments

Comments
 (0)