Skip to content

Commit

Permalink
Merge pull request #6 from FilGoodInc/deployement_docker
Browse files Browse the repository at this point in the history
pytest for pre commit et ci
  • Loading branch information
olry authored Jul 3, 2024
2 parents 94bc7a5 + 74dc657 commit 9f9fcf2
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 5 deletions.
102 changes: 102 additions & 0 deletions .github/workflows/docker-image.yml
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 }}
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ repos:
rev: v3.2.5
hooks:
- id: pylint
- repo: local
hooks:
- id: pytest
name: pytest
entry: pytest test/test_unit.py
language: python
types: [python]
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pytest = "*"
pre-commit = "*"
pylint = "*"
black = "*"
requests-mock = "*"

[dev-packages]
pylint = "*"
Expand Down
15 changes: 12 additions & 3 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def start(self):
def setup_sensor_hub(self):
"""Configure hub connection and subscribe to sensor data events."""
url = f"{self.host}/SensorHub?token={self.token}"
print(f"Connecting to: {url}") # Debug print
print(f"Connecting to: {url}")
self._hub_connection = (
HubConnectionBuilder()
.with_url(url)
Expand Down Expand Up @@ -94,7 +94,7 @@ def take_action(self, temperature):
def send_action_to_hvac(self, action):
"""Send action query to the HVAC service."""
response = requests.get(
f"{self.host}/api/hvac/{self.token}/{action}/{self.ticks}"
f"{self.host}/api/hvac/{self.token}/{action}/{self.ticks}", timeout=10
)
details = json.loads(response.text)
print(details, flush=True)
Expand Down
61 changes: 61 additions & 0 deletions test/test_integration.py
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"

0 comments on commit 9f9fcf2

Please sign in to comment.