forked from ahenrij/etsmtl-log680-oxygencs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from FilGoodInc/deployement_docker
pytest for pre commit et ci
- Loading branch information
Showing
6 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
name: CI Pipeline | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
test_integration: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.8' | ||
|
||
- name: Install dependencies | ||
run: | | ||
pip install pipenv | ||
pipenv install --dev | ||
- name: Run integration test | ||
run: pipenv run pytest test/test_integration.py | ||
|
||
lint_and_format: | ||
runs-on: ubuntu-latest | ||
needs: test_integration | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.8' | ||
|
||
- name: Install dependencies | ||
run: | | ||
pip install pipenv | ||
pipenv install --dev | ||
pip install black pylint | ||
- name: Run linting | ||
run: pipenv run pylint src/*.py | ||
|
||
- name: Run formatting | ||
run: pipenv run black --check . | ||
|
||
build_docker: | ||
runs-on: ubuntu-latest | ||
needs: lint_and_format | ||
if: github.ref == 'refs/heads/main' | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
|
||
- name: Login to DockerHub | ||
uses: docker/login-action@v2 | ||
with: | ||
username: ${{ secrets.DOCKER_USERNAME }} | ||
password: ${{ secrets.DOCKER_PASSWORD }} | ||
|
||
- name: Build and tag Docker image | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: . | ||
push: true | ||
tags: log680equipe6ete24/myapp:latest,log680equipe6ete24/myapp:${{ github.sha }} | ||
|
||
deploy_docker: | ||
runs-on: ubuntu-latest | ||
needs: build_docker | ||
if: github.ref == 'refs/heads/main' | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Login to DockerHub | ||
uses: docker/login-action@v2 | ||
with: | ||
username: ${{ secrets.DOCKER_USERNAME }} | ||
password: ${{ secrets.DOCKER_PASSWORD }} | ||
|
||
- name: Push Docker image | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: . | ||
push: true | ||
tags: log680equipe6ete24/myapp:latest,log680equipe6ete24/myapp:${{ github.sha }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Integration tests for the App class in the src.main module. | ||
""" | ||
|
||
import os | ||
import pytest | ||
from dotenv import load_dotenv | ||
import requests_mock | ||
import psycopg2 | ||
from src.main import App | ||
|
||
load_dotenv(dotenv_path=".env.test") | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def db_connection(): | ||
"""Fixture for creating a database connection.""" | ||
conn = psycopg2.connect(os.getenv("DATABASE_URL")) | ||
yield conn | ||
conn.close() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def app_instance(db_connection): | ||
"""Fixture for creating an instance of the App class.""" | ||
app = App() | ||
app._db_connection = db_connection | ||
return app | ||
|
||
|
||
@pytest.fixture | ||
def mock_requests(): | ||
"""Fixture for mocking HTTP requests.""" | ||
with requests_mock.Mocker() as m: | ||
yield m | ||
|
||
|
||
def test_sensor_data_received(db_connection, app_instance, mock_requests): | ||
"""Test receiving sensor data and saving it to the database.""" | ||
mock_requests.get( | ||
f"{app_instance.host}/api/hvac/{app_instance.token}/TurnOnAc/{app_instance.ticks}", | ||
text='{"status": "success"}', | ||
) | ||
mock_requests.get( | ||
f"{app_instance.host}/api/hvac/{app_instance.token}/TurnOnHeater/{app_instance.ticks}", | ||
text='{"status": "success"}', | ||
) | ||
|
||
sensor_data = [{"date": "2024-07-01T12:00:00Z", "data": "35"}] | ||
app_instance.on_sensor_data_received(sensor_data) | ||
|
||
with db_connection.cursor() as cur: | ||
cur.execute( | ||
"SELECT timestamp, temperature, event FROM oxygen_temperatures WHERE timestamp = %s", | ||
("2024-07-01T12:00:00Z",), | ||
) | ||
result = cur.fetchone() | ||
assert result is not None | ||
assert result[0].isoformat() == "2024-07-01T12:00:00" | ||
assert result[1] == 35.0 | ||
assert result[2] == "TurnOnAc" |