Skip to content

Commit

Permalink
feat: Add CI for merging into main (Merge pull request #11 from ric…
Browse files Browse the repository at this point in the history
…hardnguyen99/ci)

# Add GitHub Action for pre-merging pull requests

- Add CI for merging to the `main` branch.
- Add `pytest` and `coverage` for testing and coverage.
- Add `conftest.py` for Pytest configuration
  • Loading branch information
richardnguyen99 authored Dec 28, 2023
2 parents 6fb6a68 + b83e343 commit 8bd8757
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 30 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This GitHub Action will perform a checking of pull request to the main branch

name: CI

on:
pull_request:
branches: [main]
push:
branches: [main]

jobs:
build:
services:
redis:
image: redis
ports:
- 6379:6379
options: --entrypoint redis-server

runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .
pip install pylint
- name: Lint with pylint
id: pylint
continue-on-error: true
run: |
pylint --rcfile=.pylintrc --fail-under=9.0 --exit-zero *.py
- name: Lint with pylint
if: ${{ steps.pylint.outcome == 'failure' }}
run: |
echo "Pylint failed"
exit 1
- name: Run tests
env:
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
TEST_DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
CACHE_TYPE: redis
CACHE_REDIS_URL: redis://redis:6379/0
CACHE_DEFAULT_TIMEOUT: 500
TESTING: true
FLASK_ENV: testing
DEBUG: true
SWAGGER_API_SPEC_URL: https://cursus.onrender.com/api/v1/swagger.json
SECRET_KEY: ${{ secrets.SECRET_KEY }}
APP_SETTINGS: cursus.config.TestingConfig
run: |
python -m coverage run -m pytest
Empty file added .pylintrc
Empty file.
3 changes: 3 additions & 0 deletions cursus/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class DevConfig(Config):
CSRF_ENABLED = True
LOG_LEVEL = "DEBUG"
DATABASE_URL = os.environ.get("DATABASE_URL")
FLASK_ENV = "development"


class ProdConfig(Config):
Expand All @@ -125,6 +126,7 @@ class ProdConfig(Config):

DEBUG = False
LOG_LEVEL = "INFO"
FLASK_ENV = "production"
PREFERRED_URL_SCHEME = "https"
DATABASE_URL = os.environ.get("DATABASE_URL")

Expand All @@ -143,3 +145,4 @@ class TestingConfig(Config):
SWAGGER_API_SPEC_URL = ""
DATABASE_URL = os.environ.get("TEST_DATABASE_URL")
SQLALCHEMY_DATABASE_URI = os.environ.get("TEST_DATABASE_URL")
FLASK_ENV = "testing"
4 changes: 2 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def app():
env_file = find_dotenv(os.path.join("..", ".env"))
load_dotenv(env_file)

os.environ["FLASK_ENV"] = "development"
assert os.environ.get("TEST_DATABASE_URL")

assets._named_bundles = {} # pylint: disable=protected-access
app = create_app("cursus.config.TestingConfig")

assert os.environ.get("TEST_DATABASE_URL")
assert app.config["DATABASE_URL"] == os.environ.get("TEST_DATABASE_URL")

with app.app_context():
Expand Down
32 changes: 4 additions & 28 deletions test/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ def test_config():
load_dotenv(env_file, override=True)

assets._named_bundles = {} # pylint: disable=protected-access
os.environ["APP_SETTINGS"] = "cursus.config.DevConfig"
app = create_app()

assert not app.config["TESTING"]
assert app.config["FLASK_ENV"] == "development"
assert app.config["DATABASE_URL"] == os.environ.get("DATABASE_URL")

assets._named_bundles = {} # pylint: disable=protected-access
app = create_app("cursus.config.TestingConfig")

assert app.config["TESTING"]
assert app.config["FLASK_ENV"] == "testing"
assert app.config["DATABASE_URL"] == os.environ.get("TEST_DATABASE_URL")


Expand Down Expand Up @@ -161,34 +165,6 @@ def test_logout(client):
assert response.status_code == 302


def test_registered_bundle(client):
# Test if the Flask application has a registered bundle after calling
# the `create_app` function.

# print(assets._named_bundles)

assert type(assets._named_bundles) is dict

named_bundles = assets._named_bundles

assert len(named_bundles) == 2
assert "css_all" in named_bundles
assert "js_all" in named_bundles

css_bundle = named_bundles["css_all"]

assert type(css_bundle) is Bundle
assert len(css_bundle.contents) == 1
assert css_bundle.output == "css/min.bundle.css"

js_bundle = named_bundles["js_all"]

assert type(js_bundle) is Bundle
assert len(js_bundle.contents) == 2
assert js_bundle.contents[0].output == "js/min.bundle.js"
assert js_bundle.contents[1].output == "js/vendor.bundle.js"


def test_extensions(client):
assert client.application.extensions["sqlalchemy"]
assert client.application.extensions["migrate"]
Expand Down

0 comments on commit 8bd8757

Please sign in to comment.