Skip to content

Commit 4e015b9

Browse files
authored
Merge pull request #152 from c-simpson/master
Fixes for testing when running locally
2 parents 16c8a23 + 8b72a4d commit 4e015b9

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

src/server/config.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,46 @@
33
from sqlalchemy_utils import database_exists, create_database
44

55
# Determine if app is ran from docker or local by testing the env var "IS_LOCAL"
6-
IS_LOCAL = os.getenv('IS_LOCAL')
7-
BASE_PATH = '../local_files/' if IS_LOCAL == 'True' else '/app/static/'
6+
IS_LOCAL = os.getenv("IS_LOCAL")
7+
BASE_PATH = "../local_files/" if IS_LOCAL == "True" else "/app/static/"
88

99
# Initiate postgres DB
1010
# best practices is to have only one engine per application process
1111
# https://docs.sqlalchemy.org/en/13/core/connections.html
12-
POSTGRES_PASSWORD = os.getenv('POSTGRES_PASSWORD', 'thispasswordisverysecure')
13-
POSTGRES_DATABASE = os.getenv('POSTGRES_DATABASE', 'paws')
14-
POSTGRES_USER = os.getenv('POSTGRES_USER', 'postgres')
15-
16-
if IS_LOCAL == 'True':
17-
DB = os.getenv('LOCAL_DB_IP', 'postgresql://postgres:' + POSTGRES_PASSWORD + '@localhost:5432/' + POSTGRES_DATABASE)
12+
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD", "thispasswordisverysecure")
13+
POSTGRES_DATABASE = os.getenv("POSTGRES_DATABASE", "paws")
14+
POSTGRES_USER = os.getenv("POSTGRES_USER", "postgres")
15+
16+
if IS_LOCAL == "True":
17+
DB = os.getenv(
18+
"LOCAL_DB_IP",
19+
"postgresql://postgres:"
20+
+ POSTGRES_PASSWORD
21+
+ "@localhost:5432/"
22+
+ POSTGRES_DATABASE,
23+
)
1824
else:
19-
DB = 'postgresql://' + POSTGRES_USER + ':' + POSTGRES_PASSWORD + '@paws-compose-db/' + POSTGRES_DATABASE
25+
DB = (
26+
"postgresql://"
27+
+ POSTGRES_USER
28+
+ ":"
29+
+ POSTGRES_PASSWORD
30+
+ "@paws-compose-db/"
31+
+ POSTGRES_DATABASE
32+
)
2033

2134
engine = db.create_engine(DB)
2235

2336
if not database_exists(engine.url):
2437
create_database(engine.url)
2538

2639
# Initiate local file system
27-
RAW_DATA_PATH = BASE_PATH + 'raw_data/'
28-
OUTPUT_PATH = BASE_PATH + 'output/'
29-
LOGS_PATH = BASE_PATH + 'logs/'
30-
CURRENT_SOURCE_FILES_PATH = RAW_DATA_PATH + 'current/'
31-
REPORT_PATH = OUTPUT_PATH + 'reports/'
32-
ZIPPED_FILES = BASE_PATH + 'zipped/'
40+
RAW_DATA_PATH = BASE_PATH + "raw_data/"
41+
OUTPUT_PATH = BASE_PATH + "output/"
42+
LOGS_PATH = BASE_PATH + "logs/"
43+
CURRENT_SOURCE_FILES_PATH = RAW_DATA_PATH + "current/"
44+
REPORT_PATH = OUTPUT_PATH + "reports/"
45+
ZIPPED_FILES = BASE_PATH + "zipped/"
3346

3447
os.makedirs(BASE_PATH, exist_ok=True)
3548
os.makedirs(RAW_DATA_PATH, exist_ok=True)
@@ -39,3 +52,7 @@
3952
os.makedirs(REPORT_PATH, exist_ok=True)
4053
os.makedirs(ZIPPED_FILES, exist_ok=True)
4154

55+
if not (os.path.exists(LOGS_PATH + "last_execution.json")):
56+
f = open(LOGS_PATH + "last_execution.json", "w") # Prevent 500 error from /api/statistics
57+
f.write("{}")
58+
f.close()

src/server/test_api.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pytest, socket, requests
1+
import pytest, socket, requests, os
22

33
#
44
# Run 'pytest' from the command line
@@ -14,7 +14,12 @@
1414
# These codes are represented by the pytest.ExitCode enum
1515

1616

17-
SERVER_URL = "http://server:5000"
17+
if os.getenv("IS_LOCAL") == "True":
18+
SERVER_URL = "http://localhost:3333"
19+
IS_LOCAL = True
20+
else:
21+
SERVER_URL = "http://server:5000"
22+
IS_LOCAL = False
1823

1924
### DNS lookup tests
2025

@@ -25,22 +30,28 @@ def test_bad_dns():
2530

2631

2732
# Do we get IPs for good names?
33+
34+
35+
@pytest.mark.skipif(IS_LOCAL, reason="Not run when IS_LOCAL")
2836
def test_db_dns():
2937
assert (
3038
len(socket.getaddrinfo("db", "5000")) > 0
3139
) # getaddrinfo works for IPv4 and v6
3240

3341

42+
@pytest.mark.skipif(IS_LOCAL, reason="Not run when IS_LOCAL")
3443
def test_server_dns():
3544
assert len(socket.getaddrinfo("server", "5000")) > 0
3645

3746

47+
@pytest.mark.skipif(IS_LOCAL, reason="Not run when IS_LOCAL")
3848
def test_client_dns():
3949
assert len(socket.getaddrinfo("client", "5000")) > 0
4050

4151

4252
# Simple API tests
4353

54+
4455
def test_currentFiles():
4556
response = requests.get(SERVER_URL + "/api/listCurrentFiles")
4657
assert response.status_code == 200

0 commit comments

Comments
 (0)