Skip to content

Commit

Permalink
move code to src
Browse files Browse the repository at this point in the history
  • Loading branch information
arturmkr committed Feb 18, 2024
1 parent 37f7a94 commit acec68d
Show file tree
Hide file tree
Showing 30 changed files with 75 additions and 70 deletions.
25 changes: 15 additions & 10 deletions .github/workflows/app-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ jobs:
--health-retries 5
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v2
- name: Install pipenv
run: pip install pipenv

- uses: actions/setup-python@v3
with:
python-version: '3.9'

- name: Install dependencies
run: |
pip install pipenv
pipenv install --dev
cache: 'pipenv'
cache-dependency-path: |
Pipfile.lock
- run: pipenv install --ignore-pipfile

- name: Set up PostgreSQL client
run: sudo apt-get install -y postgresql-client
Expand All @@ -53,6 +54,10 @@ jobs:
run: |
PGPASSWORD=$POSTGRES_PASSWORD psql -h localhost -U $POSTGRES_USER -d $POSTGRES_DB -f init.sql
- name: Run Tests
- name: Run Unit Tests
run: |
pipenv run pytest tests/unit --cov
- name: Run Integration Tests
run: |
pipenv run pytest tests/ --cov
pipenv run pytest tests/integration/
File renamed without changes.
4 changes: 2 additions & 2 deletions app.py → src/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uvicorn

from config.default import APP_PORT
from rest_api import app
from src.rest_api import app
from src.config.default import APP_PORT

if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=APP_PORT)
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion db_connection.py → src/db_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from config.default import DB_USERNAME, DB_PASSWORD, DB_HOSTNAME, DB_PORT, DB_NAME
from src.config.default import DB_USERNAME, DB_PASSWORD, DB_HOSTNAME, DB_PORT, DB_NAME

db_url = f"postgresql+psycopg2://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOSTNAME}:{DB_PORT}/{DB_NAME}"

Expand Down
File renamed without changes.
Empty file added src/models/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion models/db_models.py → src/models/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy.dialects.postgresql import ARRAY, UUID
from sqlalchemy.orm import declarative_base, relationship

from models.enums import Status, ResourceOperationType
from src.models.enums import Status, ResourceOperationType

Base = declarative_base()

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion models/filters.py → src/models/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pydantic import BaseModel

from models.enums import Status, ResourceOperationType
from src.models.enums import Status, ResourceOperationType


class ResourceFilter(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pydantic import BaseModel

from models.enums import Status, ResourceOperationType
from src.models.enums import Status, ResourceOperationType


class Variable(BaseModel):
Expand Down
4 changes: 2 additions & 2 deletions models/transformers.py → src/models/transformers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from models.db_models import Resource, ResourceVariable
from models.pydantic_models import ResourceOut
from src.models.db_models import Resource, ResourceVariable
from src.models.pydantic_models import ResourceOut


def resource_db_model_to_pydantic(resource_db_obj: Resource) -> ResourceOut:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from sqlalchemy import func
from sqlalchemy.orm import Session

from models.db_models import ResourceHistory
from models.filters import ResourceHistoryFilter, PaginationParams
from src.models.db_models import ResourceHistory
from src.models.filters import ResourceHistoryFilter, PaginationParams


class ResourceHistoryRepository(ABC):
Expand Down
6 changes: 3 additions & 3 deletions resource_repository.py → src/resource_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from sqlalchemy import func
from sqlalchemy.orm import joinedload, Session

from exceptions import ResourceNotFoundException
from models.db_models import Resource
from models.filters import ResourceFilter, PaginationParams
from src.exceptions import ResourceNotFoundException
from src.models.db_models import Resource
from src.models.filters import ResourceFilter, PaginationParams


class ResourceRepository(ABC):
Expand Down
18 changes: 9 additions & 9 deletions resource_service.py → src/resource_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
from datetime import datetime
from typing import Optional

from db_connection import get_db_session
from exceptions import ResourceNotFoundException, ResourceBlockException, ResourceReleaseException
from models.db_models import ResourceHistory
from models.enums import Status, ResourceOperationType
from models.filters import ResourceFilter, ResourceHistoryFilter, PaginationParams
from models.pydantic_models import ResourceIn, ResourceOut, ResourcesOut, ResourceOperationOut, ResourcesOperationsOut, \
from src.db_connection import get_db_session
from src.exceptions import ResourceNotFoundException, ResourceBlockException, ResourceReleaseException
from src.models.db_models import ResourceHistory
from src.models.enums import Status, ResourceOperationType
from src.models.filters import ResourceFilter, ResourceHistoryFilter, PaginationParams
from src.models.pydantic_models import ResourceIn, ResourceOut, ResourcesOut, ResourceOperationOut, ResourcesOperationsOut, \
OperationRequest
from models.transformers import resource_db_model_to_pydantic, resource_pydantic_to_db_model
from resource_history_repository import create_resource_history_repository, ResourceHistoryRepository
from resource_repository import ResourceRepository, create_resource_repository
from src.models.transformers import resource_db_model_to_pydantic, resource_pydantic_to_db_model
from src.resource_history_repository import create_resource_history_repository, ResourceHistoryRepository
from src.resource_repository import ResourceRepository, create_resource_repository


class ResourceService(ABC):
Expand Down
10 changes: 5 additions & 5 deletions rest_api.py → src/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

from fastapi import FastAPI, HTTPException, Query, Depends

from exceptions import ResourceNotFoundException, ResourceBlockException, ResourceReleaseException
from models.enums import Status, ResourceOperationType
from models.filters import ResourceFilter, ResourceHistoryFilter, PaginationParams
from models.pydantic_models import ResourceOut, ResourcesOut, ResourceIn, ResourcesOperationsOut, OperationRequest
from resource_service import ResourceService, create_resource_service
from src.exceptions import ResourceNotFoundException, ResourceBlockException, ResourceReleaseException
from src.models.enums import Status, ResourceOperationType
from src.models.filters import ResourceFilter, ResourceHistoryFilter, PaginationParams
from src.models.pydantic_models import ResourceOut, ResourcesOut, ResourceIn, ResourcesOperationsOut, OperationRequest
from src.resource_service import ResourceService, create_resource_service

app = FastAPI(title="resource-usage-app")

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest

from db_connection import get_db_session
from models.db_models import Resource
from src.db_connection import get_db_session
from src.models.db_models import Resource


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_create_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from fastapi.testclient import TestClient

from app import app
from src.app import app

client = TestClient(app)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_get_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi.testclient import TestClient

from app import app
from src.app import app

client = TestClient(app)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_update_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from fastapi.testclient import TestClient

from app import app
from src.app import app

client = TestClient(app)

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from fastapi.testclient import TestClient

from app import app
from models.pydantic_models import ResourceOut
from resource_service import create_resource_service
from src.app import app
from src.models.pydantic_models import ResourceOut
from src.resource_service import create_resource_service

client = TestClient(app)

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_api_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from fastapi.testclient import TestClient

from app import app
from exceptions import ResourceNotFoundException, ResourceBlockException, ResourceReleaseException
from resource_service import create_resource_service
from src.app import app
from src.exceptions import ResourceNotFoundException, ResourceBlockException, ResourceReleaseException
from src.resource_service import create_resource_service

client = TestClient(app)

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_resource_history_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from unittest.mock import MagicMock, patch

from models.db_models import ResourceHistory
from resource_history_repository import ResourceHistoryRepositoryPostgreSQL
from src.models.db_models import ResourceHistory
from src.resource_history_repository import ResourceHistoryRepositoryPostgreSQL


@patch('db_connection.get_db_session')
@patch('src.db_connection.get_db_session')
def test_add_history(mock_get_db_session):
mock_session = MagicMock()
mock_get_db_session.return_value.__enter__.return_value = mock_session
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_resource_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from unittest.mock import MagicMock, patch

from models.filters import PaginationParams, ResourceFilter
from resource_repository import ResourceRepositoryPostgreSQL
from src.models.filters import ResourceFilter, PaginationParams
from src.resource_repository import ResourceRepositoryPostgreSQL


@patch('db_connection.get_db_session')
@patch('src.db_connection.get_db_session')
def test_resource_pagination(mock_get_db_session):
mock_session = MagicMock()
mock_get_db_session.return_value.__enter__.return_value = mock_session
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_resource_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import pytest

from exceptions import ResourceNotFoundException
from models.filters import ResourceFilter, PaginationParams
from resource_repository import ResourceRepositoryPostgreSQL
from src.exceptions import ResourceNotFoundException
from src.models.filters import ResourceFilter, PaginationParams
from src.resource_repository import ResourceRepositoryPostgreSQL


@patch('resource_repository.ResourceRepositoryPostgreSQL.get_resources')
@patch('src.resource_repository.ResourceRepositoryPostgreSQL.get_resources')
def test_get_resources(mock_get_resources):
mock_session = Mock()
repo = ResourceRepositoryPostgreSQL()
Expand All @@ -21,7 +21,7 @@ def test_get_resources(mock_get_resources):
mock_get_resources.assert_called_once_with(resource_filter, pagination)


@patch('db_connection.get_db_session')
@patch('src.db_connection.get_db_session')
def test_get_resource_filter(mock_get_db_session):
mock_session = MagicMock()

Expand All @@ -38,7 +38,7 @@ def test_get_resource_filter(mock_get_db_session):
assert filter_by_call_found, "filter_by method was not called with correct arguments"


@patch('db_connection.get_db_session')
@patch('src.db_connection.get_db_session')
def test_remove_resource(mock_get_db_session):
mock_session = MagicMock()
mock_session.query().filter_by().first.return_value = None # Simulate resource not found
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_resource_repository_filters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from unittest.mock import MagicMock

from models.db_models import Resource
from models.filters import PaginationParams, ResourceFilter
from resource_repository import ResourceRepositoryPostgreSQL
from src.models.db_models import Resource
from src.models.filters import ResourceFilter, PaginationParams
from src.resource_repository import ResourceRepositoryPostgreSQL


def test_apply_filters():
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_resource_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from datetime import datetime
from unittest.mock import Mock

from models.pydantic_models import ResourceIn
from resource_service import PgResourceService
from src.models.pydantic_models import ResourceIn
from src.resource_service import PgResourceService


def test_create_resource():
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_resource_service_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import pytest

from exceptions import ResourceNotFoundException
from resource_service import PgResourceService
from src.exceptions import ResourceNotFoundException
from src.resource_service import PgResourceService


def test_remove_resource_not_found_exception():
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_resource_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

import pytest

from models.db_models import Resource
from resource_repository import ResourceRepositoryPostgreSQL
from src.models.db_models import Resource
from src.resource_repository import ResourceRepositoryPostgreSQL


@pytest.fixture
def mock_db_session():
with patch('db_connection.get_db_session') as mock:
with patch('src.db_connection.get_db_session') as mock:
mock_session = MagicMock()
mock.return_value.__enter__.return_value = mock_session
yield mock_session
Expand Down

0 comments on commit acec68d

Please sign in to comment.