Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get configuration values from environment variables and run unittest in a docker container #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docker/Dockerfile_unittest
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Docker file to create the image to run PSS (Panosc Search Scoring)
#
# use python3.9 container
FROM python:3.9

# create app folder
RUN mkdir /app
# copy our application
COPY app /app/app
# copy unittest
COPY test /app/test

# copy script to download stopwords
COPY download_nltk_stopwords.py /app/.
# copy python requirements (for api and test)
COPY requirements.txt /app/requirements.txt

WORKDIR /app

# install python requirements
RUN pip install --no-cache-dir -r /app/requirements.txt

# now downloads the NLTK stopwords
RUN python download_nltk_stopwords.py

CMD ["pytest", "test"]
32 changes: 32 additions & 0 deletions docker/docker-compose_unittest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3.9"
services:

pss-api-unittest:

build:
context: ../.
dockerfile: docker/Dockerfile_unittest
image: "panosc-search-scoring-unittest"

container_name: "panosc-search-scoring-unittest"
environment:
PSS_DEBUG: 0
PSS_MONGODB_URL: mongodb://pss-db:27017
PSS_DATABASE: pss_test
PSS_PORT: 8000
PSS_VERSION: "vTest"
PSS_DEPLOYMENT: "Local scoring-api docker unittest"
depends_on:
- pss-db
networks:
- pss-internal

pss-db:
image: bitnami/mongodb:4.4.14
networks:
- pss-internal
#ports:
# - 27017:27017

networks:
pss-internal:
16 changes: 5 additions & 11 deletions run_tests_in_terminal.bash
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
#!/bin/bash
#
#

# env variable do not work
#PSS_MONGODB_URL="mongodb://127.0.0.1:27017" PSS_DATABASE="pss" PSS_PORT="8000"
clear
export PSS_MONGODB_URL="mongodb://127.0.0.1:27017"
export PSS_DATABASE="pss_test"
export PSS_PORT="8000"
export PSS_VERSION="vTest"

# copy config to correct place
cp test/test_config_file.json config/pss_config.json

python -m pytest

rm config/pss_config.json

python -m pytest test



8 changes: 8 additions & 0 deletions run_tests_in_terminal_with_docker.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

clear
docker-compose -f docker/docker-compose_unittest.yml up -d
RET_CODE=$(docker wait panosc-search-scoring-unittest)
docker logs panosc-search-scoring-unittest
docker-compose -f docker/docker-compose_unittest.yml down
exit $RET_CODE
4 changes: 3 additions & 1 deletion test/test_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import datetime
from app.common.config import Config

conf = Config()
# database connection
test_database_uri = "mongodb://127.0.0.1:27017"
test_database_uri = conf.mongodb_url # "mongodb://127.0.0.1:27017"
test_database = "pss_test"

# entries to use in testing the items endpoints
Expand Down
29 changes: 17 additions & 12 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
#

from fastapi.testclient import TestClient
from app import app
from app import app, common
from app.common.config import Config

conf = common.config.Config()

client = TestClient(app.app)


def test_main():
response = client.get("/")

assert response.status_code == 200
response = client.get("/")

assert response.status_code == 200

payload = response.json()
payload_keys = list(payload.keys())
for key in ["application", "description", "version", "started-time", "current-time", "uptime"]:
assert key in payload_keys

payload = response.json()
payload_keys = list(payload.keys())
for key in ["application", "description", "version","started-time","current-time","uptime"]:
assert key in payload_keys

def test_app_config():
config = client.app.state.config
config = client.app.state.config

assert 'Config' in str(type(config))
assert config.database == 'pss_test'
assert config.mongodb_url == "mongodb://127.0.0.1:27017"
assert 'Config' in str(type(config))
assert config.database == 'pss_test'
assert config.mongodb_url == conf.mongodb_url