Skip to content

Commit 429c888

Browse files
authored
Merge pull request #2 from obytes/tests
Chore: Add Test Command ✨
2 parents 9130313 + 671367f commit 429c888

File tree

12 files changed

+142
-56
lines changed

12 files changed

+142
-56
lines changed

.env.example

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
SECRET_KEY=
2-
QUEUE=
1+
SECRET_KEY
2+
QUEUE
3+
DATABASE_URL
4+
ACCESS_TOKEN_EXPIRE_SECONDS

.github/dependabot.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: 2
2+
updates:
3+
# Enable version updates for pip
4+
- package-ecosystem: "pip"
5+
directory: "/"
6+
# Check for updates everyday
7+
schedule:
8+
interval: "weekly"
9+
10+
# Enable version updates for Docker
11+
- package-ecosystem: "docker"
12+
# Look for a `Dockerfile` in the `root` directory
13+
directory: "/"
14+
# Check for updates once a week
15+
schedule:
16+
interval: "weekly"

.github/workflows/build.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: Build and Test
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches: ["master"]
6+
pull_request:
7+
branches: ["master"]
48

59
jobs:
610
build:
@@ -9,8 +13,8 @@ jobs:
913
steps:
1014
- uses: actions/checkout@v1
1115
- name: pull latest
12-
run: make pull
16+
run: docker-compose pull
1317
- name: Build image
14-
run: make build
18+
run: docker-compose build
1519
- name: Test code format -- pre-commit
1620
run: make lint

Dockerfile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
FROM python:3.9.2
2-
3-
WORKDIR /app
1+
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
42

53
ENV PYTHONDONTWRITEBYTECODE 1
64
ENV PYTHONUNBUFFERED 1
75

6+
RUN apt update && apt upgrade -y
7+
8+
RUN apt install -y -q build-essential python3-pip python3-dev
9+
RUN pip3 install -U pip setuptools wheel
10+
RUN pip3 install gunicorn uvloop httptools
11+
812
COPY . /app/
913

1014
RUN pip install -r requirements.txt
1115

16+
ENV ACCESS_LOG=${ACCESS_LOG:-/proc/1/fd/1}
17+
ENV ERROR_LOG=${ERROR_LOG:-/proc/1/fd/2}
1218

13-
ENV PYTHONPATH=/app
1419
EXPOSE 8000 8000

Makefile

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,18 @@ help:
99
@echo " make clean"
1010
@echo " make clean-test"
1111

12-
test:
13-
docker-compose run --rm backend pytest
14-
15-
start:
12+
up:
1613
docker-compose up -d
1714

1815
down:
1916
docker-compose down
2017

21-
pull:
22-
docker-compose pull db
23-
docker-compose pull queue
24-
docker-compose pull backend
25-
2618
build:
2719
docker-compose build
2820

21+
bash:
22+
docker-compose run --rm backend bash
23+
2924
lint:
3025
docker-compose run --rm backend pre-commit run --all-files
3126

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ $ make build
7373
$ make start
7474
```
7575

76-
__Note__: Running the test on the Container CLI `pytest` or when you use the command `make start` the container will be started and the tests will be run before the Uvicorn server is started.
76+
### Testing
77+
78+
While i use `HTTPX` an HTTP client for Python 3, to test the API, most of the tests are using a live log thats why need before to run a server using `uvicorn` and migrate the database, then you will have the ability to run the tests. To have a clean environment, recommended to use Docker for that, when you start the containers try to open the application container and then run `pytest` to test the API.
7779

7880
### Environment variables
7981

8082
```sh
81-
SECRET_KEY= #secret key for JWT token
82-
QUEUE= #rabbitMQ queue name
83+
SECRET_KEY #Secret Key
84+
QUEUE # RabbitMQ Link
85+
DATABASE_URL # Database URL
86+
ACCESS_TOKEN_EXPIRE_SECONDS # Access Token Expire Seconds
8387
```
8488

8589
> change all the environment variables in the `.env.example` and don't forget to rename it to `.env`.

core/rabbit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
async def produceblog(blog_id: int):
1010
connection = await aio_pika.connect(
11-
f"amqp://guest:guest@{config.queue}/", loop=asyncio.get_event_loop()
11+
f"{config.queue}", loop=asyncio.get_event_loop()
1212
)
1313

1414
async with connection:

database/config.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from decouple import config
22
from pydantic import BaseSettings
33

4-
Key = config("SECRET_KEY")
54
queue = config("QUEUE")
65

76

8-
class Settings((BaseSettings)):
9-
SECRET_KEY = Key
10-
ACCESS_TOKEN_EXPIRE_SECONDS: int = 60 * 60 * 24
11-
SQLALCHEMY_DATABASE_URI = "postgresql://postgres:password@db/fastql"
7+
class Settings(((BaseSettings))):
8+
SECRET_KEY = config("SECRET_KEY")
9+
ACCESS_TOKEN_EXPIRE_SECONDS: int = config("ACCESS_TOKEN_EXPIRE_SECONDS", cast=int)
10+
SQLALCHEMY_DATABASE_URI = config("DATABASE_URL")
1211
SQLALCHEMY_DATABASE_SSL = False
1312
SQLALCHEMY_DATABASE_MIN_POOL = 1
1413
SQLALCHEMY_DATABASE_MAX_POOL = 20

docker-compose.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ services:
55
ports:
66
- "5432:5432"
77
environment:
8-
- POSTGRES_DB=fastql
8+
- POSTGRES_DB=postgres
99
- POSTGRES_USER=postgres
10-
- POSTGRES_PASSWORD=password
10+
- POSTGRES_PASSWORD=postgres
1111
queue:
1212
image: rabbitmq:3-management
1313
environment:
@@ -33,7 +33,9 @@ services:
3333
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
3434
environment:
3535
- SECRET_KEY=GITHUBACTIONKEY
36-
- QUEUE=queue
36+
- QUEUE=amqp://guest:guest@rabbit:5672
37+
- DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres
38+
- ACCESS_TOKEN_EXPIRE_SECONDS=3600
3739
ports:
3840
- "8000:8000"
3941
image: fastql

pytest.ini

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
[pytest]
2-
norecursedirs = .gihtub *.egg-info .git appdir .tox
2+
norecursedirs = .gihtub *.egg-info .git appdir .tox
3+
log_cli = 1
4+
log_cli_level = INFO
5+
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
6+
log_cli_date_format=%Y-%m-%d %H:%M:%S
7+
testpaths =
8+
tests/

0 commit comments

Comments
 (0)