randog.sqlalchemy package

randog.sqlalchemy.custom(example, **kwargs)

custom_func for randog.factory.from_example to generate sqlalchemy-derived objects

This function is an add-on for randog.factory.from_example.

Specify this function as custom_func if you want to create a factory that generates sqlalchemy-derived objects in from_example.

Examples

>>> from sqlalchemy import Column, Integer, String
>>> from sqlalchemy.orm import declarative_base
>>> import randog.factory
>>> import randog.sqlalchemy
>>>
>>> Base = declarative_base()
>>>
>>> class User(Base):
...     __tablename__ = "user"
...
...     id = Column(Integer, primary_key=True, autoincrement=True)
...     name = Column(String)
>>>
>>> # specify `randog.sqlalchemy.custom` as `custom_func`
>>> factory = randog.factory.from_example(User,
...                                       custom_func=randog.sqlalchemy.custom)
>>> generated = factory.next()
randog.sqlalchemy.factory(model: Type[M], override_columns: Mapping[str, Factory | Any] = None, *, as_dict: bool = False, rnd: Random | None = None) Factory[M] | Factory[dict]

Return a factory generating Model instances randomly.

Parameters:
  • model (Type[Model]) – the model class of sqlalchemy

  • override_columns (Mapping[str, Union[Factory, Column, Any]], optional) – For each column that is specified in this dictionary, create a factory using it instead of the column of the model class. If factory is specified as value of this dict, it is used to generate the field; if other is specified, the factory obtained by using from_example with it as an example is used to generate the field.

  • as_dict (bool, default=False) – If it is True, values generated by the factory will be dict, not model instances.

  • rnd (Random, optional) – random number generator to be used

randog.sqlalchemy.factory_from_column(column: Column, *, rnd: Random | None = None) Factory

Return a factory generating values that matches the column.

Parameters:
  • column (Column) – the column of sqlalchemy

  • rnd (Random, optional) – random number generator to be used