-
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.
- Loading branch information
0 parents
commit 32182ad
Showing
24 changed files
with
532 additions
and
0 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,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 }} |
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,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/ |
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,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.
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,4 @@ | ||
connections = { | ||
"MYSQL": "mysql+mysqldb://gervasgu:gervasgu@0.0.0.0/FoodOps", | ||
"REDIS": {"host": "localhost", "port": 6379}, | ||
} |
Empty file.
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,6 @@ | ||
from infrastructure.cache import tab_cache | ||
import json | ||
|
||
|
||
def get_cached(id): | ||
return json.loads(tab_cache.get_value(id)) |
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,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() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
<html><body><img src="coverage.svg" alt="coverage badge"></body></html> |
Empty file.
Empty file.
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,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.
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,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) |
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,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) |
Oops, something went wrong.