Skip to content

Commit

Permalink
fix: Remove inheritance of DeclarativeBase in InspectionMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbukachi committed Jul 25, 2023
1 parent 5d48c45 commit 431ac80
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
7 changes: 1 addition & 6 deletions sqlalchemy_mixins/inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
from .utils import classproperty


class Base(DeclarativeBase):
__abstract__ = True


class InspectionMixin(Base):
__abstract__ = True
class InspectionMixin:

@classproperty
def columns(cls):
Expand Down
6 changes: 2 additions & 4 deletions sqlalchemy_mixins/inspection.pyi
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from typing import List, Protocol, Dict

from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.orm import Mapper, DeclarativeBase
from sqlalchemy.orm import Mapper
from sqlalchemy.orm.interfaces import MapperProperty

from sqlalchemy_mixins.utils import classproperty

class Base(DeclarativeBase):
__abstract__ = True

class MappingProtocol(Protocol):
__mapper__: Mapper

class InspectionMixin(Base):
class InspectionMixin:

@classproperty
def columns(cls) -> List[str]: ...
Expand Down
31 changes: 30 additions & 1 deletion sqlalchemy_mixins/tests/test_activerecord.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import Query, Session, DeclarativeBase
from sqlalchemy.orm import Query, Session, DeclarativeBase, declarative_base

from sqlalchemy_mixins import ActiveRecordMixin
from sqlalchemy_mixins.activerecord import ModelNotFoundError

class Base(DeclarativeBase):
__abstract__ = True

AlternativeBase = declarative_base()

engine = create_engine('sqlite:///:memory:', echo=False)
sess = Session(engine)
# sess = scoped_session(sessionmaker(bind=engine))
Expand All @@ -20,6 +23,9 @@ class BaseModel(Base, ActiveRecordMixin):
pass


class BaseModelAlternative(AlternativeBase, ActiveRecordMixin):
__abstract__ = True

class User(BaseModel):
__tablename__ = 'user'
__repr_attrs__ = ['name']
Expand All @@ -29,6 +35,13 @@ class User(BaseModel):
posts_viewonly = sa.orm.relationship('Post', viewonly=True)


class UserAlternative(BaseModelAlternative):
__tablename__ = 'user_alt'
__repr_attrs__ = ['name']
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)


class Post(BaseModel):
__tablename__ = 'post'
id = sa.Column(sa.Integer, primary_key=True)
Expand Down Expand Up @@ -204,5 +217,21 @@ def test_find_or_fail(self):
_ = User.find_or_fail(123456789)


class TestActiveRecordAlternative(unittest.TestCase):
def setUp(self):
sess.rollback()

BaseModelAlternative.set_session(None)
AlternativeBase.metadata.drop_all(engine)
AlternativeBase.metadata.create_all(engine)

BaseModelAlternative.set_session(sess)

def test_create(self):
u1 = UserAlternative.create(name='Bill u1')
self.assertEqual(u1, sess.query(UserAlternative).first())



if __name__ == '__main__': # pragma: no cover
unittest.main()

0 comments on commit 431ac80

Please sign in to comment.