Skip to content

Commit

Permalink
Update coverage badge
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jun 24, 2024
0 parents commit bfc3f69
Show file tree
Hide file tree
Showing 24 changed files with 519 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
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.10.12'

- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run tests with coverage
run: |
coverage run -m pytest
coverage xml
coverage-badge -o coverage.svg
- name: Setup Git for Pages
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
- name: Checkout gh-pages branch

run: |
git fetch origin
git checkout gh-pages || git checkout --orphan gh-pages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Push to gh-pages branch
run: |
echo '<html><body><img src="coverage.svg" alt="coverage badge"></body></html>' > index.html
git add coverage.svg index.html
git commit -m "Update coverage badge"
git push --force origin gh-pages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
128 changes: 128 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/
doc/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# Celery
celerybeat-schedule.*
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# VS Code settings
.vscode/

# Local virtual environment directory
.venv/
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Test Containers with FastAPI

![Coverage](https://gil-air-may.github.io/test-containers/coverage.svg)

## Overview

This project demonstrates how to set up and use Testcontainers with FastAPI to ensure reliable and isolated test environments. By automating your tests, you can maintain high code quality and gain valuable insights into your system's performance and behavior.

## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Running Tests](#running-tests)
- [GitHub Actions Setup](#github-actions-setup)
- [Contributing](#contributing)
- [License](#license)

## Installation

Clone the repository and install the dependencies:

```bash
git clone https://github.com/gil-air-may/test-containers.git
cd test-containers
pip install -r requirements.txt
```

## Usage

Start the FastAPI application:

```bash
uvicorn main:app --reload
```

## Running Tests

Run tests with coverage:

```bash
pytest --cov=app tests/
```

## GitHub Actions Setup

This project uses GitHub Actions for continuous integration (CI) to automate testing. The CI workflow is defined in the `.github/workflows/ci.yml` file and includes the following steps:

1. **Checkout code**: Checks out the repository code.
2. **Set up Python**: Sets up the specified version of Python.
3. **Install dependencies**: Installs the required dependencies listed in `requirements.txt`.
4. **Run tests with coverage**: Executes tests and generates a coverage report.
5. **Set up Git for Pages**: Configures Git with GitHub Actions bot credentials.
6. **Checkout `gh-pages` branch**: Checks out the `gh-pages` branch for updating the coverage badge.
7. **Copy coverage badge**: Copies the generated coverage badge to the correct location.
8. **Add and commit coverage badge**: Adds and commits the coverage badge.
9. **Push to `gh-pages` branch**: Pushes the updated badge to the `gh-pages` branch.

## Contributing

Contributions are welcome! Please open an issue or submit a pull request.

## License

This project is licensed under the MIT License.

---
Empty file added __init__.py
Empty file.
4 changes: 4 additions & 0 deletions config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
connections = {
"MYSQL": "mysql+mysqldb://gervasgu:gervasgu@0.0.0.0/FoodOps",
"REDIS": {"host": "localhost", "port": 6379},
}
Empty file added core/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions core/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from infrastructure.cache import tab_cache
import json


def get_cached(id):
return json.loads(tab_cache.get_value(id))
9 changes: 9 additions & 0 deletions core/repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from infrastructure.db import tab_db


def get_all_tabs():
return tab_db.get_all_tabs()


def get_todays_tabs():
return tab_db.get_todays_tabs()
21 changes: 21 additions & 0 deletions coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body><img src="coverage.svg" alt="coverage badge"></body></html>
Empty file added infrastructure/__init__.py
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions infrastructure/cache/tab_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import redis
import ipdb
from config import connections


redis_host = connections["REDIS"]["host"]
redis_port = connections["REDIS"]["port"]

r = redis.Redis(host=redis_host, port=redis_port, decode_responses=True)


def get_value(key):
return r.get(key)
Empty file added infrastructure/db/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions infrastructure/db/tab_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sqlalchemy
from config import connections
from sqlalchemy import text

engine = sqlalchemy.create_engine(connections["MYSQL"])


def execute_raw_query(raw_query, params=None):
with engine.connect() as connection:
result = connection.execute(text(raw_query))
return result.mappings().all()


def get_all_tabs():
sql = "select * from Tab"
return execute_raw_query(sql)
22 changes: 22 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from fastapi import FastAPI
from core import repo, cache
from dotenv import load_dotenv

load_dotenv()

app = FastAPI()


@app.get("/")
def read_root():
return {"Hello": "world"}


@app.get("/tabs")
def get_tabs():
return repo.get_all_tabs()


@app.get("/cache")
def get_cache(tab: int):
return cache.get_cached(tab)
Loading

0 comments on commit bfc3f69

Please sign in to comment.