Skip to content

Commit e247204

Browse files
fix tests & gloabl app_config
1 parent e4c6644 commit e247204

File tree

7 files changed

+87
-53
lines changed

7 files changed

+87
-53
lines changed

app.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
from src.opengeodeweb_back.routes import blueprint_routes
1010
from src.opengeodeweb_back.utils_functions import handle_exception
11+
from src.opengeodeweb_back import app_config
12+
13+
14+
print("Hello World", app_config)
1115

1216

1317
""" Global config """
@@ -17,12 +21,13 @@
1721
FLASK_DEBUG = True if os.environ.get("FLASK_DEBUG", default=None) == "True" else False
1822

1923
if FLASK_DEBUG == False:
20-
app.config.from_object("config.ProdConfig")
24+
app.config.from_object(app_config.ProdConfig)
2125
else:
22-
app.config.from_object("config.DevConfig")
26+
app.config.from_object(app_config.DevConfig)
2327

2428

25-
PORT = int(app.config.get("PORT"))
29+
print(f"{app.config=}", flush=True)
30+
PORT = int(app.config.get("DEFAULT_PORT"))
2631
ORIGINS = app.config.get("ORIGINS")
2732
SSL = app.config.get("SSL")
2833

config.py

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Standard library imports
2+
import os
3+
import time
4+
5+
# Third party imports
6+
# Local application imports
7+
8+
class Config(object):
9+
FLASK_DEBUG = os.environ.get("FLASK_DEBUG", default=False)
10+
DEFAULT_PORT = "5000"
11+
CORS_HEADERS = "Content-Type"
12+
UPLOAD_FOLDER = "./uploads"
13+
DESKTOP_APP = False
14+
REQUEST_COUNTER = 0
15+
LAST_REQUEST_TIME = time.time()
16+
LAST_PING_TIME = time.time()
17+
18+
19+
class ProdConfig(Config):
20+
SSL = None
21+
ORIGINS = ""
22+
MINUTES_BEFORE_TIMEOUT = "1"
23+
SECONDS_BETWEEN_SHUTDOWNS = "10"
24+
DATA_FOLDER_PATH = "/data/"
25+
26+
27+
class DevConfig(Config):
28+
SSL = None
29+
ORIGINS = "*"
30+
MINUTES_BEFORE_TIMEOUT = "1"
31+
SECONDS_BETWEEN_SHUTDOWNS = "10"
32+
DATA_FOLDER_PATH = "./data/"

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111

1212
routes = flask.Blueprint("routes", __name__)
1313

14-
# @routes.before_request
15-
# def before_request():
16-
# if "ping" not in flask.request.path:
17-
# utils_functions.increment_request_counter(flask.current_app)
18-
19-
# @routes.teardown_request
20-
# def teardown_request(exception):
21-
# if "ping" not in flask.request.path:
22-
# utils_functions.decrement_request_counter(flask.current_app)
23-
# utils_functions.update_last_request_time(flask.current_app)
14+
@routes.before_request
15+
def before_request():
16+
if "ping" not in flask.request.path:
17+
utils_functions.increment_request_counter(flask.current_app)
18+
19+
@routes.teardown_request
20+
def teardown_request(exception):
21+
if "ping" not in flask.request.path:
22+
utils_functions.decrement_request_counter(flask.current_app)
23+
utils_functions.update_last_request_time(flask.current_app)
2424

2525
schemas = os.path.join(os.path.dirname(__file__), "schemas")
2626

src/opengeodeweb_back/utils_functions.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@
1313
# Local application imports
1414

1515
def increment_request_counter(current_app):
16-
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER"))
17-
REQUEST_COUNTER += 1
18-
current_app.config.update(REQUEST_COUNTER=REQUEST_COUNTER)
16+
if "REQUEST_COUNTER" in current_app.config:
17+
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER"))
18+
REQUEST_COUNTER += 1
19+
current_app.config.update(REQUEST_COUNTER=REQUEST_COUNTER)
1920

2021
def decrement_request_counter(current_app):
21-
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER"))
22-
REQUEST_COUNTER -= 1
23-
current_app.config.update(REQUEST_COUNTER=REQUEST_COUNTER)
22+
if "REQUEST_COUNTER" in current_app.config:
23+
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER"))
24+
REQUEST_COUNTER -= 1
25+
current_app.config.update(REQUEST_COUNTER=REQUEST_COUNTER)
2426

2527
def update_last_request_time(current_app):
26-
LAST_REQUEST_TIME = time.time()
27-
current_app.config.update(LAST_REQUEST_TIME=LAST_REQUEST_TIME)
28+
if "LAST_REQUEST_TIME" in current_app.config:
29+
LAST_REQUEST_TIME = time.time()
30+
current_app.config.update(LAST_REQUEST_TIME=LAST_REQUEST_TIME)
2831

2932

3033
def kill_task(current_app):

tests/conftest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import pytest
22
from app import app
3+
import time
34

45

56
@pytest.fixture
67
def client():
78
app.config["TESTING"] = True
89
app.config["SERVER_NAME"] = "TEST"
9-
app.config["DATA_FOLDER"] = "./data/"
10+
app.config["DATA_FOLDER_PATH"] = "./data/"
11+
app.config["REQUEST_COUNTER"] = 0
12+
app.config["LAST_REQUEST_TIME"] = time.time()
1013
client = app.test_client()
1114
client.headers = {"Content-type": "application/json", "Accept": "application/json"}
1215
yield client
16+
17+
@pytest.fixture
18+
def app_context():
19+
with app.app_context():
20+
yield

tests/test_utils_functions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
import flask
12
from src.opengeodeweb_back import utils_functions
23

34

5+
def test_increment_request_counter(app_context):
6+
assert flask.current_app.config.get("REQUEST_COUNTER") == 0
7+
utils_functions.increment_request_counter(flask.current_app)
8+
assert flask.current_app.config.get("REQUEST_COUNTER") == 1
9+
10+
def test_decrement_request_counter(app_context):
11+
assert flask.current_app.config.get("REQUEST_COUNTER") == 1
12+
utils_functions.decrement_request_counter(flask.current_app)
13+
assert flask.current_app.config.get("REQUEST_COUNTER") == 0
14+
15+
def test_update_last_request_time(app_context):
16+
LAST_REQUEST_TIME = flask.current_app.config.get("LAST_REQUEST_TIME")
17+
utils_functions.update_last_request_time(flask.current_app)
18+
assert flask.current_app.config.get("LAST_REQUEST_TIME") >= LAST_REQUEST_TIME
19+
20+
421
def test_versions():
522
list_packages = [
623
"OpenGeode-core",

0 commit comments

Comments
 (0)