Skip to content

Commit 2dd254c

Browse files
committed
make session_factory a non-default required arg in uow
1 parent e19848f commit 2dd254c

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

‎src/allocation/service_layer/unit_of_work.py‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
# pylint: disable=attribute-defined-outside-init, ungrouped-imports
1+
# pylint: disable=attribute-defined-outside-init
22
from __future__ import annotations
33
import abc
4-
from typing import TYPE_CHECKING
5-
from sqlalchemy import create_engine
6-
from sqlalchemy.orm import sessionmaker
7-
8-
from allocation import config
4+
from typing import Callable
5+
from sqlalchemy.orm.session import Session
96
from allocation.adapters import repository
107
from . import messagebus
118

12-
if TYPE_CHECKING:
13-
from sqlalchemy.orm.session import Session
149

10+
SessionFactory = Callable[[], Session]
1511

1612

1713
class AbstractUnitOfWork(abc.ABC):
@@ -44,18 +40,13 @@ def rollback(self):
4440

4541

4642

47-
DEFAULT_SESSION_FACTORY = sessionmaker(bind=create_engine(
48-
config.get_postgres_uri(),
49-
isolation_level="SERIALIZABLE",
50-
))
51-
5243
class SqlAlchemyUnitOfWork(AbstractUnitOfWork):
5344

54-
def __init__(self, session_factory=DEFAULT_SESSION_FACTORY):
45+
def __init__(self, session_factory: SessionFactory):
5546
self.session_factory = session_factory
5647

5748
def __enter__(self):
58-
self.session = self.session_factory() # type: Session
49+
self.session = self.session_factory()
5950
self.products = repository.SqlAlchemyRepository(self.session)
6051
return super().__enter__()
6152

0 commit comments

Comments
 (0)