Skip to main content

Extra factories for factory_boy

This library contains 2 base factories.

  • AsyncSQLAlchemyModelFactory
  • TortoiseModelFactory

TortoiseModelFactory

Is made to use it with tortoise-orm.

Usage

It works aout of the box, if you have already initialized tortoise-orm for testing.

You can check how to do this in tortoise docs.

import factory
from tortoise import fields, models
from factory_boy_extra.tortoise_factory import TortoiseModelFactory


class TargetModel(models.Model):
    name = fields.CharField(max_length=200)


class TargetModelFactory(TortoiseModelFactory):
    name = factory.Faker("word")

    class Meta:
        model = TargetModel

That's it. Now you can use it in your tests, E.G.

@pytest.mark.asyncio
async def test_factories():
    targets = TargetModelFactory.create_batch(10)
    actual_models = await TargetModel.all()
    assert len(actual_models) == 10

AsyncSQLAlchemyModelFactory

Usage

At your conftest.py initialize your factories with AsyncSession.

@pytest.fixture(autouse=True)
def init_factories(dbsession: AsyncSession) -> None:
    """Init factories."""
    BaseFactory.session = dbsession

The dbsession factory can be obtained in pytest-async-sqlalchemy library, or you can add it by yourself:

import pytest
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker


@pytest.fixture()
async def dbsession():
    """
    Fixture that returns a SQLAlchemy session with a SAVEPOINT, and the rollback to it
    after the test completes.
    """
    engine = create_async_engine(database_url) # You must provide your database URL.
    connection = await engine.connect()
    trans = await connection.begin()

    Session = sessionmaker(connection, expire_on_commit=False, class_=AsyncSession)
    session = Session()

    try:
        yield session
    finally:
        await session.close()
        await trans.rollback()
        await connection.close()
        await engine.dispose()

Now you can create factories and use them in your tests.

from factory_boy_extra.async_sqlalchemy_factory import AsyncSQLAlchemyModelFactory

class TargetModel(Base):

    __tablename__ = "targetmodel"

    name = Column(String(length=120), nullable=False)  # noqa: WPS432


class TargetModelFactory(AsyncSQLAlchemyModelFactory):
    name = factory.Faker("word")

    class Meta:
        model = TargetModel

In tests it wil look like this:

import pytest

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select


@pytest.mark.asyncio
async def test_successful_notification(dbsession: AsyncSession) -> None:
    TargetModelFactory.create_batch(10)
    actual_models = (await dbsession.execute(select(TargetModel))).fetchall()
    assert len(actual_models) == 10

Metadata

Release files for factory-boy-extra 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for factory-boy-extra 0.1.3
File Size Uploaded Provenance Actions
factory_boy_extra-0.1.3.tar.gz 4.3 kB Without provenance Details for factory_boy_extra-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for factory-boy-extra 0.1.3
File Interpreter ABI Platform Actions
factory_boy_extra-0.1.3-py3-none-any.whl Python 3 none any Details for factory_boy_extra-0.1.3-py3-none-any.whl

Total release size: 9.1 kB

Release files / factory_boy_extra-0.1.3.tar.gz

Download URL factory_boy_extra-0.1.3.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4cc2791232d591cb5910cddd560ae3e6323e4f1b107b469a882ba56cbc159c82
BLAKE2b-256 checksum
How to use checksums
3db03b7a168333534800e74bfea98e57665073f4c91c4841569fe1169063d029
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.10 CPython/3.9.7 Linux/5.8.0-1042-azure

Release files / factory_boy_extra-0.1.3-py3-none-any.whl

Download URL factory_boy_extra-0.1.3-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
770e82bdb0e9a98249741641e69fd8f09254e9ea8d6d29c1a7302ed3d1f8bc2b
BLAKE2b-256 checksum
How to use checksums
e30a8ba049a5c4389e358203d265f2cf4471e6029a0a366861b5974edeb625ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.10 CPython/3.9.7 Linux/5.8.0-1042-azure

Security for factory-boy-extra 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page