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

General improvements #6

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ jobs:
alembic -x data=true -x test=true upgrade head

- name: Unit Tests
env:
DEBUG: true
run: |
pytest tests/unit --color=yes --showlocals -v

- name: Integration Tests
env:
DEBUG: true
run: |
pytest tests/integration --color=yes --showlocals -v
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ default_language_version:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
exclude: requirements.txt

- repo: https://github.com/ambv/black
rev: 21.9b0
rev: 22.8.0
hooks:
- id: black
args: [--line-length=79]
Expand All @@ -26,7 +26,7 @@ repos:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.9.3
rev: v5.10.1
hooks:
- id: isort
args: [--profile, black]
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## UNRELEASED
## [1.0.0] - 2021-12-27
### Added
- Database schemas support.
### Removed
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

# Use this makefile to run development procedures.

style: black flake8 isort pydocstyle mypy requirements
Expand Down Expand Up @@ -49,3 +51,6 @@ clean:
rm -rf dist
rm -rf *.egg-info
@echo "♲ clean done."

dev:
pre-commit install
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
# 🏰 Kingdom Core
# 🏰 Kingdom SDK

Library containing the core modules for the kingdom-python-server.

## Test

To test the database package, you need do it manually, running a migration. Make sure the database is configured before.

```bash
cd tests/poc/
alembic revision --autogenerate
alembic upgrade head
```
## Features

The rest, run `pytest`.

Don't commit the generated revision.
See the [changelog](./CHANGELOG.md) to see all the features supported.

## Installation

Use the package manager [pip](https://pip.pypa.io/en/stable/) to install `kingdom-core`.
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install `kingdom-sdk`.

```bash
pip install kingdom-core
pip install kingdom-sdk
```

You can use [poetry]() as well.
You can use [poetry](https://python-poetry.org/) as well.

```bash
poetry add kingdom-core
poetry add kingdom-sdk
```

## Usage
Expand All @@ -38,12 +28,24 @@ from kingdom_sdk.utils import files
orm_files = files.find("orm.py", "/")
```

## Test

To test the database package, you need do it manually, running a migration. Make sure the database is configured before.

```bash
cd tests/poc/
alembic revision --autogenerate
alembic upgrade head
```

The rest, run `pytest`.

Don't commit the generated revision.

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License
[MIT](https://choosealicense.com/licenses/mit/)

> This file is based on [Make a README](https://www.makeareadme.com/).
4 changes: 4 additions & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ script_location = tests/poc/migration_example/

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# YYYY-mm-dd-HH-MM-SS_<rev>_<message_slug>
# better migration sort? does branching impact? what about using `rev-id` param?
# file_template = %%(year)d-%%(month).2d-%%(day)-2d-%%(hour).2d-%%(minute).2d-%%(second).2d_%%(rev)s_%%(slug)s


# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
Expand Down
2 changes: 1 addition & 1 deletion kingdom_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Define the package's version.
# - Is used for indexing and distributing the package
# - Follow the conventions defined by PEP440
__version__ = "1.0.0dev1"
__version__ = "1.0.0"


def _get_base_dir() -> str:
Expand Down
24 changes: 12 additions & 12 deletions kingdom_sdk/adapters/message_bus.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import logging
from typing import Any, AsyncGenerator, Callable, Dict, List, Type
from typing import Any, AsyncGenerator, Callable, Type

from kingdom_sdk.domain.exception import KingdomError
from kingdom_sdk.domain.message import Command, Event, Message
Expand All @@ -14,16 +14,16 @@

class MessageBus(AbstractMessageBus):
_uow: AbstractUnitOfWork
_event_handlers: Dict[Type[Event], List[Callable]]
_command_handlers: Dict[Type[Command], Callable]
_queue: List[Message]
_event_handlers: dict[Type[Event], list[Callable]]
_command_handlers: dict[Type[Command], Callable]
_queue: list[Message]

def __init__(
self,
uow: AbstractUnitOfWork,
event_handlers: Dict[Type[Event], List[Callable]],
command_handlers: Dict[Type[Command], Callable],
queue: List[Message],
event_handlers: dict[Type[Event], list[Callable]],
command_handlers: dict[Type[Command], Callable],
queue: list[Message],
) -> None:
self._uow = uow
self._event_handlers = event_handlers
Expand All @@ -34,9 +34,9 @@ def __init__(
def create(
cls,
uow: AbstractUnitOfWork,
event_handlers: Dict[Type[Event], List[Callable]],
command_handlers: Dict[Type[Command], Callable],
dependencies: Dict[str, Any],
event_handlers: dict[Type[Event], list[Callable]],
command_handlers: dict[Type[Command], Callable],
dependencies: dict[str, Any],
) -> MessageBus:
"""Create a message bus with its handlers dependencies set
programmatically.
Expand Down Expand Up @@ -95,7 +95,7 @@ def _handle_map(self, message: Message) -> Callable:
else:
raise UnknownMessage()

async def handle(self, message: Message) -> List[Warning]:
async def handle(self, message: Message) -> list[Warning]:
self._queue = [message]
warnings = []
while self._queue:
Expand All @@ -108,7 +108,7 @@ async def handle(self, message: Message) -> List[Warning]:
return warnings

@staticmethod
async def _run(handler: AsyncGenerator) -> List[Any]:
async def _run(handler: AsyncGenerator) -> list[Any]:
return [r async for r in handler]


Expand Down
4 changes: 2 additions & 2 deletions kingdom_sdk/adapters/query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Tuple
from typing import Any

from jinjasql import JinjaSql
from sqlalchemy.engine import CursorResult
Expand All @@ -15,7 +15,7 @@ class JinjaTemplateSqlMixin(AbstractTemplateSQLMixin):

_sql_file_path: str

def _build_statement(self, **params: Any) -> Tuple[str, Dict]:
def _build_statement(self, **params: Any) -> tuple[str, dict]:
with open(self._sql_file_path) as sql_file:
file_content = sql_file.read()
query, bind_params = self._jinja.prepare_query(file_content, params)
Expand Down
14 changes: 7 additions & 7 deletions kingdom_sdk/adapters/repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC
from typing import List, Optional, Set, Type
from typing import Optional, Type

from sqlalchemy.orm import Query, Session

Expand All @@ -20,11 +20,11 @@ class SQLAlchemyRepository(AbstractRepository, ABC):
__model__: Type[Aggregate]

_session: Session
_seen: Set[Aggregate]
_seen: set[Aggregate]

def __init__(self, session: Session) -> None:
self._session = session
self._seen: Set[Aggregate] = set()
self._seen: set[Aggregate] = set()

@property
def query(self) -> Query:
Expand All @@ -34,14 +34,14 @@ def add(self, aggregate: Aggregate) -> None:
self._seen.add(aggregate)
self._session.add(aggregate)

def list(self) -> List[Aggregate]:
# XXX: reserved word
def list(self) -> list[Aggregate]:
return self.query.all() # type: ignore

def get(self, id: PrimaryKey_T) -> Optional[Aggregate]: # noqa
aggregate = self._get(id)
if aggregate:
if aggregate := self._get(id):
self._seen.add(aggregate)
return aggregate
return None

def _get(self, id: PrimaryKey_T) -> Optional[Aggregate]: # noqa
return self.query.filter( # type: ignore
Expand Down
9 changes: 5 additions & 4 deletions kingdom_sdk/adapters/unit_of_work.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC
from typing import Any, Generator, Iterator, List, Set, Tuple
from typing import Any, Generator, Iterator

from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
Expand All @@ -15,6 +15,7 @@
bind=create_engine(
config.get_database_url(),
isolation_level="REPEATABLE_READ",
echo=config.is_debug_active(),
),
autoflush=False,
)
Expand All @@ -29,7 +30,7 @@ class SQLAlchemyUnitOfWork(AbstractUnitOfWork, ABC):
... repository: ...
"""

_errors: List[Any]
_errors: list[Any]
_session_factory: sessionmaker
_session: Session

Expand Down Expand Up @@ -58,7 +59,7 @@ def execute_native_statement(self, statement: str, **params: Any) -> Any:
return self._session.execute(statement, params)

def collect_new_events(self) -> Generator:
dirty: Set[Aggregate] = set()
dirty: set[Aggregate] = set()

for field_name, _ in self._repositories:
try:
Expand All @@ -78,7 +79,7 @@ def _initialize_repositories(self, session: Session) -> None:
self.__dict__[field_name] = repository(session)

@property
def _repositories(self) -> Iterator[Tuple[str, Any]]:
def _repositories(self) -> Iterator[tuple[str, Any]]:
return (
(field, module)
for field, module in self.__annotations__.items()
Expand Down
4 changes: 3 additions & 1 deletion kingdom_sdk/database/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def run_migrations_online(
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = create_engine(config.get_database_url())
connectable = create_engine(
config.get_database_url(), echo=config.is_debug_active()
)

with connectable.connect() as connection:
if schema:
Expand Down
7 changes: 3 additions & 4 deletions kingdom_sdk/domain/aggregate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from abc import ABC
from datetime import datetime
from typing import List
from uuid import UUID

from kingdom_sdk.domain.entity import Entity
Expand All @@ -10,11 +9,11 @@
class Aggregate(Entity, ABC):
"""Base class for aggregates."""

_events: List[Event]
_events: list[Event]

def __init__(
self,
id: UUID, # noqa
id: UUID,
version: int,
is_discarded: bool,
registered_at: datetime,
Expand All @@ -36,5 +35,5 @@ def next_event(self) -> Event:
return self._events.pop(0)

@property
def events(self) -> List[Event]:
def events(self) -> list[Event]:
return self._events
2 changes: 1 addition & 1 deletion kingdom_sdk/domain/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Entity(ABC):

def __init__(
self,
id: UUID, # noqa
id: UUID,
version: int,
is_discarded: bool,
registered_at: datetime,
Expand Down
4 changes: 2 additions & 2 deletions kingdom_sdk/domain/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC
from dataclasses import asdict, dataclass
from datetime import datetime
from typing import Any, Dict
from typing import Any
from uuid import UUID

from kingdom_sdk.domain.value_object import ValueObject
Expand Down Expand Up @@ -62,7 +62,7 @@ class Event(Message, ABC):
class PersistentMessage(ValueObject):
module: str
classname: str
data: Dict[str, Any]
data: dict[str, Any]

@classmethod
def create(cls, message: Message) -> PersistentMessage: # type: ignore
Expand Down