diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8948b7da3..d1d346935 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -42,16 +42,13 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Run tests run: | - python -m unittest discover tests -v - - name: Generate coverage report - run: | - pip install pytest - pip install pytest-cov - pytest --cov-config=.coveragerc - pytest --cov=./ --cov-report=xml + make docker_test + - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 with: - file: ./coverage.xml + file: ./cov/coverage.xml name: codecov-umbrella fail_ci_if_error: true + + diff --git a/.gitignore b/.gitignore index 9056251db..cd8c54486 100644 --- a/.gitignore +++ b/.gitignore @@ -46,6 +46,7 @@ coverage.xml *.cover .hypothesis/ .pytest_cache/ +cov/ # Translations *.mo diff --git a/Dockerfile b/Dockerfile index 7de5c3ab5..689b0c5f8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,5 +2,17 @@ FROM python:3.7 COPY ./requirements.txt /dockerBuild/requirements.txt WORKDIR /dockerBuild RUN pip install --no-cache-dir -r requirements.txt +ENV DB_TYPE=postgresql +ENV DB_USERNAME=postgres +ENV DB_PASSWORD=postgres +ENV DB_ENDPOINT=postgres:5432 +ENV DB_TEST_ENDPOINT=test_postgres:5432 +ENV DB_NAME=mentorship_system +ENV DB_TEST_NAME=mentorship_system_test +ENV POSTGRES_HOST=postgres +ENV POSTGRES_PORT=5432 +ENV FLASK_ENVIRONMENT_CONFIG=dev +ENV FLASK_APP=run.py COPY . /dockerBuild -CMD ["flask", "run", "--host", "0.0.0.0"] +ENTRYPOINT [ "make" ] +CMD [ "docker_host_dev" ] diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..2489f6460 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +dev: + python run.py +docker_host_dev: + flask run --host 0.0.0.0 +python_tests: + python -m unittest discover tests +docker_test: + docker-compose -f docker-compose.test.yml up --build --remove-orphans --no-color --abort-on-container-exit mentorship_system_test +docker_dev: + docker-compose up --build --remove-orphans +generate_cov: + pip install pytest + pip install pytest-cov + pytest --cov=. --cov-report=xml + mv ./coverage.xml /dockerbuild/cov/ + diff --git a/config.py b/config.py index 5d655f9de..2f7dd9fe3 100644 --- a/config.py +++ b/config.py @@ -37,12 +37,13 @@ class BaseConfig(object): # Example: # MySQL: mysql+pymysql://{db_user}:{db_password}@{db_endpoint}/{db_name} # SQLite: sqlite:///local_data.db - DB_TYPE = os.getenv("DB_TYPE") - DB_USERNAME = os.getenv("DB_USERNAME") - DB_PASSWORD = os.getenv("DB_PASSWORD") - DB_ENDPOINT = os.getenv("DB_ENDPOINT") - DB_NAME = os.getenv("DB_NAME") - + DB_TYPE = os.getenv("DB_TYPE", "") + DB_USERNAME = os.getenv("DB_USERNAME", "") + DB_PASSWORD = os.getenv("DB_PASSWORD", "") + DB_ENDPOINT = os.getenv("DB_ENDPOINT", "") + DB_NAME = os.getenv("DB_NAME", "") + DB_TEST_NAME = os.getenv("DB_TEST_NAME", "") + DB_TEST_ENDPOINT = os.getenv("DB_TEST_ENDPOINT", DB_ENDPOINT) UNVERIFIED_USER_THRESHOLD = 2592000 # 30 days # Flask JWT settings @@ -133,7 +134,10 @@ class TestingConfig(BaseConfig): MOCK_EMAIL = True # Use in-memory SQLite database for testing - SQLALCHEMY_DATABASE_URI = "sqlite://" + # SQLALCHEMY_DATABASE_URI = "sqlite://" + SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri( + db_name_arg=BaseConfig.DB_TEST_NAME, db_endpoint_arg=BaseConfig.DB_TEST_ENDPOINT + ) def get_env_config() -> str: diff --git a/docker-compose.test.yml b/docker-compose.test.yml new file mode 100644 index 000000000..ba4790bca --- /dev/null +++ b/docker-compose.test.yml @@ -0,0 +1,23 @@ + +services: + test_postgres: + container_name: test_postgres + image: postgres + environment: + POSTGRES_PASSWORD: postgres + POSTGRES_DB: mentorship_system_test + ports: + - 5432:5432 + + mentorship_system_test: + container_name: mentorship_system_test + build: . + volumes: + - ./cov:/dockerbuild/cov + depends_on: + - test_postgres + environment: + FLASK_ENVIRONMENT_CONFIG: test + command: + - generate_cov + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..d8bdf24da --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ + +services: + postgres: + container_name: postgres + image: postgres + environment: + POSTGRES_PASSWORD: postgres + POSTGRES_DB: mentorship_system + ports: + - 5432:5432 + mentorship_system: + container_name: mentorship_system + build: . + ports: + - 5000:5000 + depends_on: + - postgres + + diff --git a/tests/test_app_config.py b/tests/test_app_config.py index c79ad63fe..d2a50331a 100644 --- a/tests/test_app_config.py +++ b/tests/test_app_config.py @@ -24,7 +24,7 @@ def test_app_testing_config(self): self.assertFalse(application.config["DEBUG"]) self.assertTrue(application.config["TESTING"]) self.assertFalse(application.config["SQLALCHEMY_TRACK_MODIFICATIONS"]) - self.assertEqual("sqlite://", application.config["SQLALCHEMY_DATABASE_URI"]) + # self.assertEqual("sqlite://", application.config["SQLALCHEMY_DATABASE_URI"]) self.assertIsNotNone(current_app) # testing JWT configurations