Skip to content

Commit

Permalink
added some unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: lindenmckenzie <[email protected]>
  • Loading branch information
KamenDimitrov97 authored and lindenmckenzie committed Jun 21, 2023
1 parent a9ad5e1 commit 63825f4
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .env.default
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BERLN_API_HOST=0.0.0.0
BERLN_API_PORT=28700
BERLN_API_PORT=28900

BERLIN_API_NAMESPACE=dp_nlp_berlin_api
BERLIN_API_DATA_LOCATION=data/
Expand Down
2 changes: 1 addition & 1 deletion .env.local
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BERLIN_API_PORT = 28700
BERLIN_API_PORT = 28900
BERLIN_API_HOST = "0.0.0.0"
BERLIN_API_DATA_LOCATION = "data/"
BERLIN_API_LOGGING_NAMESPACE = "dp_nlp_berlin_api"
Expand Down
30 changes: 16 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ export BERLIN_API_GIT_COMMIT=$(shell git rev-parse HEAD)
export BERLIN_API_VERSION ?= 0.1.0
export BERLIN_API_BUILD_TIME=$(shell date +%s)

.PHONY: build build-bin run lint test help audit deps all test-component
.PHONY: all audit build build-bin deps help lint run test-component unit

all: audit lint format

audit: deps ## Makes sure dep are installed and audits code for vulnerable dependencies
audit: ## audits code for vulnerable dependencies
poetry run safety check -i 51457

build: deps
build:
docker build --build-arg build_time="${BUILD_TIME}" --build-arg commit="${GIT_COMMIT}" --build-arg version="${VERSION}" -t berlin_api .

build-bin: deps
build-bin:
poetry build

deps: ## Installs dependencies
Expand All @@ -36,25 +36,27 @@ deps: ## Installs dependencies
fi; \
poetry install --quiet || poetry install; \
fi; \

format: ## Formats your code automatically.
poetry run isort .
poetry run black .

lint: deps ## Lints code
lint: ## Lints code
poetry run ruff .

run: deps ## Start the api locally on port 28900.
run: ## Start the api locally on port 28900.
FLASK_APP=${FLASK_APP} poetry run flask run --port ${BERLIN_API_PORT}

run-container: deps
run-container:
docker run --env BUILD_TIME='${BUILD_TIME}' -e GIT_COMMIT="${GIT_COMMIT}" -e VERSION="${VERSION}" -ti berlin_api

test: deps ## Runs all available tests and generates a coverage report located in htmlcov
poetry run ./scripts/run_tests_unit.sh

test-component: deps ## Makes sure dep are installed and runs component tests
poetry run pytest tests/api
test-component: ## runs component tests
poetry run pytest -v tests/api

format: deps ## Formats your code automatically.
poetry run isort .
poetry run black .

unit: ## runs component tests
poetry run pytest -v tests/unit

help: ## Show this help.
@echo ''
Expand Down
1 change: 0 additions & 1 deletion app/healthcheck.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import time
from datetime import datetime, timedelta
from app import __version__ as VERSION
from app.settings import BUILD_TIME, GIT_COMMIT
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/healthcheck_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import sys
from datetime import datetime, timedelta
from unittest.mock import patch

import pytest

from app import __version__ as VERSION
from app.settings import BUILD_TIME, GIT_COMMIT
from app.healthcheck import Healthcheck, OK

start_time = datetime.now()


@pytest.fixture
def healthcheck():
checks = {"check1": OK, "check2": OK}
return Healthcheck(OK, checks, start_time)


@patch("app.healthcheck.datetime")
def test_to_json(mock_datetime, healthcheck):
formatted_build_time = datetime.fromtimestamp(int(BUILD_TIME))
build_time = formatted_build_time.strftime('%Y-%m-%dT%H:%M:%S%z')

expected_json = {
"status": OK,
"version": {
"version": VERSION,
"build_time": build_time,
"git_commit": GIT_COMMIT,
"language": "python",
"language_version": sys.version,
},
"uptime": 1,
"start_time": start_time.strftime('%Y-%m-%dT%H:%M:%S%z'),
"checks": {"check1": OK, "check2": OK},
}

result = healthcheck.to_json()
print ("this is: ", result)
print ("this is: ", expected_json)

assert result == expected_json


@patch("app.healthcheck.datetime")
def test_get_uptime(mock_datetime, healthcheck):
mock_datetime.now.return_value = healthcheck.start_time + timedelta(milliseconds=500)

result = healthcheck.get_uptime()

assert result == 500
24 changes: 24 additions & 0 deletions tests/unit/settings_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import time
from dynaconf import Dynaconf
from unittest.mock import patch
import pytest


@pytest.fixture
def mock_settings():
with patch("app.settings.settings") as mock_settings:
mock_settings.HOST = "mock_host"
mock_settings.PORT = 8080
mock_settings.NAMESPACE = "mock_namespace"
mock_settings.DATA_LOCATION = "/path/to/data"
mock_settings.BUILD_TIME = 1234567890
mock_settings.GIT_COMMIT = "mock_commit"
yield mock_settings

def test_settings(mock_settings):
assert mock_settings.HOST == "mock_host"
assert mock_settings.PORT == 8080
assert mock_settings.NAMESPACE == "mock_namespace"
assert mock_settings.DATA_LOCATION == "/path/to/data"
assert mock_settings.BUILD_TIME == 1234567890
assert mock_settings.GIT_COMMIT == "mock_commit"

0 comments on commit 63825f4

Please sign in to comment.