In factory boy it is possible to create a sqlalchemy object with a relationship to another table. There is a common recipe for Choosing from a populated table which is made for django, and I want to do this now for flask. So I’d think that I have use this line. user = factory.Iterator(User.query.all()) But ..
What am I trying to do? I am trying to create a base factory boy class with the class meta set as follows: class DBFactoryBase(factory.alchemy.SQLAlchemyModelFactory): class Meta: sqlalchemy_session = common_session sqlalchemy_session_persistence = ‘flush’ I then want to inherit from this and use it in my factories instead of factory.alchemy.SQLAlchemyModelFactory: class SomeFactory(DBFactoryBase): class Meta: model = ..
I’m currently struggling to get the foreign key of a tables index from another factory working. My SQLAlchemy classes look as follows: class Rfq(Base): __tablename__ = "rfq_rfq" id = Column( Integer, primary_key=True, ) title = Column(Text) description = Column(Text) creator_id = Column( ForeignKey(User.id, deferrable=True, initially="DEFERRED"), index=True ) class User(Base): __tablename__ = "users_user" id = Column( ..
I have 2 models, django User model and Employee class Employee(TimeStampedModel): creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name="employees") first_name = models.CharField(max_length=255, blank=True) last_name = models.CharField(max_length=255, blank=True) phone = models.CharField(validators=[phone_regex], max_length=17, blank=True) email = models.EmailField(validators=[email_regex], max_length=255, blank=True) user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, related_name="employee") 2 factories: class UserFactory(factory.django.DjangoModelFactory): class Meta: model = User employee = factory.RelatedFactory( "control_room.tests.factories.EmployeeFactory", factory_related_name=’user’ ) ..
Assume I have the following classes: @dataclass class Image: array: np.ndarray @dataclass class Label: array: np.ndarray @dataclass class Sample: image: Image label: Label with the following factory_boy factories: class ImageFactory(factory.Factory): class Meta: model = Image class Params: shape = (64, 64) array = factory.LazyAttribute(lambda o: np.random.random(o.shape)) class LabelFactory(factory.Factory): class Meta: model = Label class Params: ..
I try to add a role to a manager through @factory.post_generation. It seems to work when I access manager.roles, but it doesn’t create a real record in relationship table. # models.py assoc_managers_roles = sa.Table( "assoc_managers_roles", Base.metadata, sa.Column( "manager_id", sa.Integer, sa.ForeignKey("managers.id", ondelete="CASCADE"), primary_key=True, ), sa.Column( "role_id", sa.Integer, sa.ForeignKey("roles.id", ondelete="CASCADE"), primary_key=True, ), ) class Manager(Base, TimestampMixin): id ..
I’m trying to create some objects in setUp method of Django test case. I use FactoryBoy that helps me with creating the objects. But it seems that FactoryBoy can’t find any objects in the database. factories.py class ProductFactory(DjangoModelFactory): … market_category = factory.fuzzy.FuzzyChoice(list(MarketplaceCategory.objects.all())) class Meta: model = Product tests.py from django.test import TestCase from marketplaces.models import ..
I’m trying to create a fixture data to dump into a table that has a relation (entity) table (that does not need to be populated) Factory boy gives a sqlalchmy error. How can I exclude the entity table? Project hierarchy: > app — src — model — fixtures — abc — abc_dump.py — abc.json inside ..
I have a factoryboy factory for sqlalchemy model which uses faker from faker import Faker from db_init import session fake = Faker() class ClientAppFactory(factory.alchemy.SQLAlchemyModelFactory): class Meta: model = ClientApp sqlalchemy_session = session sqlalchemy_session_persistence = ‘commit’ name = fake.word() display_name = fake.word() This factory creates rows in database. Field name of my model must be unique. ..
I’m using FactoryBoy and Faker to generate some models for unit tests. Generating data for fields is easy enough, but how to I generate a string that incorporates a value produced from a Faker provider? import factory import MyModel class MyFactory(factory.django.DjangoModelFactory): class Meta: model = MyModel # my_ip will be a temporary variable that is ..