Skip to content

Commit

Permalink
fix test db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
dmkjfs committed Oct 6, 2024
1 parent 427a431 commit 8a6eeb0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 84 deletions.
45 changes: 4 additions & 41 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@ __pycache__/
*.py[cod]
*$py.class

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

# Database
# Database backup
db/

# Unit test / coverage reports
htmlcov/
coverage/
.coverage
.coverage.*
.cache
coverage.xml
*.cover
*.py,cover
.pytest_cache/

# Translations
Expand All @@ -33,49 +25,20 @@ poetry.lock

# Environments
.env
.env.production
.env*.local
.env.test
.venv/

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

# dependencies
node_modules/
.pnp/
.pnp.js
.yarn/install-state.gz

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

# IDE/editor settings
.idea/
.vscode/

# Qodana
qodana.yaml

Makefile
# Running
Makefile
4 changes: 2 additions & 2 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ services:
container_name: pgadmin_app
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: '[email protected]'
PGADMIN_DEFAULT_PASSWORD: '12345678'
PGADMIN_DEFAULT_EMAIL: '[email protected]'
PGADMIN_DEFAULT_PASSWORD: 'qwerty'
ports:
- "5050:80"
networks:
Expand Down
20 changes: 11 additions & 9 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
version: '3.8'

services:
db:
container_name: db
db_test:
container_name: db_test
image: postgres:16.2
env_file:
- .env.test
environment:
- POSTGRES_DB=${DB_NAME_TEST}
- POSTGRES_USER=${DB_USER_TEST}
- POSTGRES_PASSWORD=${DB_PASS_TEST}
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASS}
ports:
- ${DB_PORT_TEST}:5432
restart: always
- ${DB_PORT}:5432
restart: on-failure
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U ${DB_USER}']
interval: 10s
Expand All @@ -24,12 +26,12 @@ services:
dockerfile: Dockerfile.test
container_name: tests
depends_on:
db:
db_test:
condition: service_healthy
ports:
- '8000:8000'
env_file:
- .env
- .env.test
volumes:
- ./:/backend/
networks:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pytest-dependency = "^0.6.0"
python-dateutil = "^2.9.0.post0"
python-jose="^3.3.0"
pytest-mock = "^3.14.0"
pytest-dotenv = "^0.5.2"

[build-system]
requires = ["poetry-core"]
Expand Down Expand Up @@ -83,3 +84,4 @@ ignore_missing_imports = true
[tool.pytest.ini_options]
pythonpath = [ ".", "src",]
asyncio_mode="auto"
env_files = [".env.test"]
10 changes: 10 additions & 0 deletions src/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from src.core.config import settings
from src.core.loggers import core as logger

logger.debug(f'''Database settings:
host={settings.db_host}
port={settings.db_port}
name={settings.db_name}
username={settings.db_user}
password={settings.db_pass}
url={settings.db_url}''')
28 changes: 2 additions & 26 deletions src/core/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from pydantic import Field, EmailStr
from pydantic_settings import BaseSettings, SettingsConfigDict

from src.core.loggers import core as logger
from pydantic_settings import BaseSettings


class Settings(BaseSettings):

model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
# model_config = SettingsConfigDict(env_file_encoding='utf-8')

root_url: str = Field(default='http://localhost:8000', alias='ROOT_URL')

Expand All @@ -25,20 +23,10 @@ class Settings(BaseSettings):
db_user: str = Field(default='postgres', alias='DB_USER')
db_pass: str = Field(default='postgres', alias='DB_PASS')

db_host_test: str = Field(default='localhost', alias='DB_HOST_TEST')
db_port_test: int = Field(default=6000, alias='DB_PORT_TEST')
db_name_test: str = Field(default='postgres', alias='DB_NAME_TEST')
db_user_test: str = Field(default='postgres', alias='DB_USER_TEST')
db_pass_test: str = Field(default='postgres', alias='DB_PASS_TEST')

redis_host: str = Field(default='localhost', alias='REDIS_HOST')
redis_port: int = Field(default=6379, alias='REDIS_PORT')
redis_pass: str = Field(default='qwerty', alias='REDIS_PASS')

redis_host_test: str = Field(default='localhost', alias='REDIS_HOST_TEST')
redis_port_test: int = Field(default=6379, alias='REDIS_PORT_TEST')
redis_pass_test: str = Field(default='qwerty', alias='REDIS_PASS_TEST')

echo: bool = True

jwt_secret_key: str = Field(default='', alias='JWT_SECRET_KEY')
Expand All @@ -55,17 +43,5 @@ class Settings(BaseSettings):
def db_url(self) -> str:
return f"postgresql+asyncpg://{self.db_user}:{self.db_pass}@{self.db_host}:{self.db_port}/{self.db_name}"

@property
def db_url_test(self) -> str:
return f'postgresql+asyncpg://{self.db_user_test}:{self.db_pass_test}@{self.db_host_test}:{self.db_port_test}/{self.db_name_test}'


settings = Settings()

logger.debug(f'''Database settings:
host={settings.db_host}
port={settings.db_port}
name={settings.db_name}
username={settings.db_user}
password={settings.db_pass}
url={settings.db_url}''')
8 changes: 2 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import pytest
from fastapi.testclient import TestClient
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker

from src.api.app import app
from src.core.config import settings
from src.repositories.database.base import SQLAlchemyRepository # noqa: F401

engine_test = create_async_engine(settings.db_url_test, echo=True)
sessionmaker_test = async_sessionmaker(engine_test)


@pytest.fixture(scope='function')
def client() -> TestClient:
return TestClient(app=app)


@pytest.fixture(scope='session')
def replace_sessionmaker(mocker): # type: ignore[no-untyped-def]
mocker.patch('SQLAlchemyRepository.sessionmaker', new=sessionmaker_test)
def replace_db_url(mocker): # type: ignore[no-untyped-def]
mocker.patch('settings.db_url', new=settings.db_url_test)

0 comments on commit 8a6eeb0

Please sign in to comment.