Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add github action #12

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Check code and Pytest

on: [push]

jobs:
build:

runs-on: ubuntu-latest

services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: filmin
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5


steps:
- uses: actions/checkout@v3

- name: psycopg prerequisites
run: sudo apt-get install libpq-dev

- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry ruff
poetry install

- name: Lint with ruff
run: |
ruff --format=github src/

- name: Test with pytest
working-directory: ./src
run: |
poetry run pytest -c pytest.github.ini
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repos:


- repo: https://github.com/myint/autoflake
rev: v1.4
rev: v2.2.0
hooks:
- id: autoflake
args:
Expand All @@ -31,14 +31,14 @@ repos:


- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.272
rev: v0.0.280
hooks:
- id: ruff
args:
- --fix # Enables autofix

- repo: https://github.com/psf/black # Refer to this repository for futher documentation about black hook
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black

Expand Down
29 changes: 21 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.7'
version: "3.7"
services:
api:
container_name: api.filmin
Expand All @@ -13,15 +13,20 @@ services:
- env-api-dev
environment:
DEBUGPY: ${DEBUGPY:-true}
DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${DB_HOST:-postgres}:5432/${POSTGRES_DB:-filmin}
WAIT_HOSTS: postgres:5432
DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${DB_HOST:-postgres}-woop:5432/${POSTGRES_DB:-filmin}
TEST_DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${DB_HOST:-postgres}-test:5432/${POSTGRES_DB:-filmin}
WAIT_HOSTS: postgres:5432,postgres-test:5432,redis:6379
WAIT_LOGGER_LEVEL: error
WAIT_TIMEOUT: 60
WAIT_SLEEP_INTERVAL: 5
PYDEVD_DISABLE_FILE_VALIDATION: 1
REDIS_URL: ${REDIS_URL-redis://redis:6379/0}
REDIS_USER: ${REDIS_USER-default}
REDIS_PASSWORD: ${REDIS_PASSWORD-redis}
depends_on:
- postgres
- postgres-test
- redis

volumes:
- ./src/:/app:cached
Expand Down Expand Up @@ -55,7 +60,16 @@ services:
PGPASSWORD: ${PGPASSWORD-postgres}
PGUSER: ${PGUSER-postgres}
PHOST: localhost
command: ["postgres", "-c", "max_connections=1000", "-c", "log_statement=all", "-c", "log_destination=stderr"]
command:
[
"postgres",
"-c",
"max_connections=1000",
"-c",
"log_statement=all",
"-c",
"log_destination=stderr",
]
ports:
- 5432:5432
volumes:
Expand All @@ -81,13 +95,12 @@ services:
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD-redis}
command:
- /bin/sh
- -c
- redis-server --appendonly yes --requirepass $$REDIS_PASSWORD
- /bin/sh
- -c
- redis-server --appendonly yes --requirepass $$REDIS_PASSWORD
volumes:
- redis_data:/data:delegated


volumes:
postgres_data:
postgres_test_data:
Expand Down
39 changes: 39 additions & 0 deletions src/infra/cache/memory_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from threading import RLock
import time

from typing import Any, Dict, Union

from infra.cache.ports import AbstractCacheRepository, EncodableT


class MemoryCache(AbstractCacheRepository):
def __init__(self) -> None:
self._data: Dict[str, tuple[EncodableT, float]] = {}
self.lock = RLock()

async def init(self) -> None:
pass

async def close(self) -> None:
with self.lock:
self._data = {}

async def get(self, key: str) -> Any:
with self.lock:
ret = self._data.get(key)
if ret is not None:
if ret[1] < time.time():
return ret[0]
else:
self._data.pop(key, None)

async def set(self, key: str, value: Union[EncodableT, None], expire: int) -> None:
with self.lock:
if value is None:
self._data.pop(key, None)
else:
self._data[key] = (value, time.time() + expire)

async def delete(self, key: str) -> None:
with self.lock:
self._data.pop(key, None)
25 changes: 25 additions & 0 deletions src/pytest.github.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[pytest]
norecursedirs = versions
testpaths = tests
python_files = tests.py test_*.py

env =
APP_ENV=test
DATABASE_URL=postgresql+asyncpg://postgres:[email protected]:5432/filmin


addopts =
-p no:warnings
--cov=.
--no-cov-on-fail
--cov-report term-missing
--cov-report term:skip-covered
--cov-report xml
--cov-branch


; http://doc.pytest.org/en/latest/example/markers.html
markers =
unit_test: Pure unit tests.
integration_test: Tests that access a database, API, etc.
functional_test: End to end tests that needs a browser.
2 changes: 1 addition & 1 deletion src/shared/api/schemas/page.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import IntEnum
from typing import Generic, List, Type, TypeVar
from typing import Generic, List, TypeVar

from pydantic import BaseModel, Field

Expand Down
4 changes: 2 additions & 2 deletions src/shared/repository/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Any, AsyncContextManager, AsyncIterator, Callable, List, Optional, Self, Tuple, Type, TypeVar, cast
from typing import Any, AsyncContextManager, Callable, List, Optional, Self, Tuple, Type, TypeVar, cast

from pydantic import BaseModel
from sqlalchemy.exc import NoResultFound
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import noload
from sqlalchemy.sql import Select, func, select, update
from sqlalchemy.sql import Select, func, select

from shared.exceptions import NotFound
from shared.repository.ports.generic import AbstractRepository, FilterBy
Expand Down
16 changes: 15 additions & 1 deletion src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_scoped_session, create_async_engine
from sqlalchemy.orm import sessionmaker

from app.app_container import AppContainer
from app.app_container import AppContainer, AppContainerMixin
from app.asgi import app
from auth.utils import create_access_token
from config import settings
from core.data.repositories.ports.user import AbstractUserRepository
from core.domain.schemas.user import User
from core.schemas.user.create_user import CreateUserInDTO
from infra.cache.memory_cache import MemoryCache
from infra.cache.ports import AbstractCacheRepository
import infra.database.sqlalchemy.models # noqa

from infra.database.sqlalchemy.sqlalchemy import metadata
from utils.di import di_singleton


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -129,3 +132,14 @@ async def async_normal_client(normal_user_access_token: str) -> AsyncClient:
async with AsyncClient(app=app, base_url='http://test/api/v1') as ac:
ac.headers.update({'Authorization': f'Bearer {normal_user_access_token}'})
yield ac


@pytest.fixture(autouse=True)
def memory_cache_database(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
@di_singleton
def fake_cache_gateway() -> AbstractCacheRepository:
return MemoryCache()

monkeypatch.setattr(AppContainerMixin, '_get_cache_repository', lambda _: fake_cache_gateway())

yield