Skip to content

Commit

Permalink
Add injector dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Pristupa committed Jan 29, 2020
1 parent 59261c9 commit adac9ca
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="sqlar",
version="0.0.8",
version="0.1.0",
author="Pavel V. Pristupa",
author_email="pristupa@gmail.com",
description="SQLAlchemy implementation for Python Persistance API (persipy)",
Expand All @@ -20,6 +20,7 @@
],
python_requires='>=3.6',
install_requires=[
'injector==0.18.2',
'persipy==0.0.4',
'sqlalchemy==1.3.13',
],
Expand Down
9 changes: 6 additions & 3 deletions sqlar/repository.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from typing import Iterable
from typing import Optional
from typing import TypeVar

from injector import inject
from sqlalchemy import and_
from sqlalchemy import exists
from sqlalchemy import func
Expand All @@ -7,9 +12,6 @@
from sqlalchemy.orm import class_mapper
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.exc import UnmappedClassError
from typing import Iterable
from typing import Optional
from typing import TypeVar

from persipy import CRUDRepository

Expand Down Expand Up @@ -119,6 +121,7 @@ def save(self, entity: T) -> T:
def save_many(self, entities: Iterable[T]) -> Iterable[T]:
return [self.save(entity) for entity in entities]

@inject
def __init__(self, engine: Engine):
self._engine = engine
self._session_factory = sessionmaker(bind=self._engine)
Expand Down
62 changes: 62 additions & 0 deletions tests/test_injector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from injector import ClassProvider
from injector import Injector
from injector import Module
from injector import singleton
from sqlalchemy import String
from typing import Optional

from sqlalchemy.engine import Engine
from sqlar.repository import sqla_crud

from persipy import CRUDRepository
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import create_engine
from sqlalchemy.orm import mapper


class Fixture:
def __init__(self):
class MyEntity:
def __init__(self, id_: int, name: Optional[str]):
self.id = id_
self.name = name

engine = self._engine = create_engine('sqlite://')
metadata = MetaData()
my_entity_table = Table(
'my_entities',
metadata,
Column('id', Integer, primary_key=True),
Column('name', String),
)
mapper(MyEntity, my_entity_table)

class MyRepository(CRUDRepository[MyEntity, int]):
pass

metadata.create_all(bind=self._engine)

class Configuration(Module):
def configure(self, binder):
binder.bind(Engine, engine)
binder.bind(MyRepository, to=ClassProvider(sqla_crud(MyRepository)), scope=singleton)

configuration = Configuration()
injector = Injector([configuration])
self.repository = injector.get(MyRepository)

def execute(self, sql):
return self._engine.execute(sql)


def test_count():
fixture = Fixture()
fixture.execute('INSERT INTO my_entities (id) VALUES (1), (2), (3);')

# Act
count = fixture.repository.count()

assert count == 3

0 comments on commit adac9ca

Please sign in to comment.