Skip to content

Commit

Permalink
fix: ignore non-columns types (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
adhtruong authored Mar 10, 2024
1 parent 1d1c7f9 commit 719495e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion polyfactory/factories/sqlalchemy_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ def is_supported_type(cls, value: Any) -> TypeGuard[type[T]]:
return isinstance(inspected, (Mapper, InstanceState))

@classmethod
def should_column_be_set(cls, column: Column) -> bool:
def should_column_be_set(cls, column: Any) -> bool:
if not isinstance(column, Column):
return False

if not cls.__set_primary_key__ and column.primary_key:
return False

Expand Down
32 changes: 32 additions & 0 deletions tests/sqlalchemy_factory/test_sqlalchemy_factory_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sqlalchemy import Column, ForeignKey, Integer, String, create_engine, inspect, orm, types
from sqlalchemy.engine import Engine
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, create_async_engine
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import Session
from sqlalchemy.orm.decl_api import DeclarativeMeta, registry

Expand Down Expand Up @@ -119,6 +120,37 @@ class ModelFactory(SQLAlchemyFactory[Model]):
assert isinstance(instance.str_array_type[0], str)


def test_properties() -> None:
_registry = registry()

class Base(metaclass=DeclarativeMeta):
__abstract__ = True

registry = _registry
metadata = _registry.metadata

class Model(Base):
__tablename__ = "model"

id: Any = Column(Integer(), primary_key=True)
age: Any = Column(Integer(), nullable=False)

double_age = orm.column_property(age * 2)

@hybrid_property
def triple_age(self) -> int:
return self.age * 3 # type: ignore[no-any-return]

class ModelFactory(SQLAlchemyFactory[Model]):
...

instance = ModelFactory.build()
assert isinstance(instance, Model)
# Expect empty as requires session to be set
assert instance.double_age is None
assert instance.age * 3 == instance.triple_age


@pytest.mark.parametrize(
"type_",
tuple(SQLAlchemyFactory.get_sqlalchemy_types().keys()),
Expand Down

0 comments on commit 719495e

Please sign in to comment.