{"id":16543170,"url":"https://github.com/s3rius/factory_boy_extra","last_synced_at":"2025-03-21T10:31:31.166Z","repository":{"id":57428179,"uuid":"412023670","full_name":"s3rius/factory_boy_extra","owner":"s3rius","description":null,"archived":false,"fork":false,"pushed_at":"2022-10-03T10:30:38.000Z","size":24,"stargazers_count":12,"open_issues_count":2,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-12T18:59:27.899Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/s3rius.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-30T10:40:32.000Z","updated_at":"2024-05-30T12:28:49.000Z","dependencies_parsed_at":"2022-09-02T15:31:18.773Z","dependency_job_id":null,"html_url":"https://github.com/s3rius/factory_boy_extra","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s3rius%2Ffactory_boy_extra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s3rius%2Ffactory_boy_extra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s3rius%2Ffactory_boy_extra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s3rius%2Ffactory_boy_extra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s3rius","download_url":"https://codeload.github.com/s3rius/factory_boy_extra/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221814057,"owners_count":16884971,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T18:59:33.305Z","updated_at":"2024-10-28T09:49:55.621Z","avatar_url":"https://github.com/s3rius.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Extra factories for factory_boy\n\nThis library contains 2 base factories.\n* AsyncSQLAlchemyModelFactory\n* TortoiseModelFactory\n\n\n## TortoiseModelFactory\nIs made to use it with tortoise-orm.\n\n### Usage\n\nIt works aout of the box, if you have already initialized \ntortoise-orm for testing.\n\nYou can check how to do this in [tortoise docs](https://tortoise-orm.readthedocs.io/en/latest/contrib/unittest.html#py-test).\n\n```python\nimport factory\nfrom tortoise import fields, models\nfrom factory_boy_extra.tortoise_factory import TortoiseModelFactory\n\n\nclass TargetModel(models.Model):\n    name = fields.CharField(max_length=200)\n\n\nclass TargetModelFactory(TortoiseModelFactory):\n    name = factory.Faker(\"word\")\n\n    class Meta:\n        model = TargetModel\n```\n\nThat's it. Now you can use it in your tests, E.G.\n\n```python\n@pytest.mark.asyncio\nasync def test_factories():\n    targets = TargetModelFactory.create_batch(10)\n    actual_models = await TargetModel.all()\n    assert len(actual_models) == 10\n```\n\n## AsyncSQLAlchemyModelFactory\n\n### Usage\nAt your conftest.py initialize your factories\nwith AsyncSession.\n\n```python\n@pytest.fixture(autouse=True)\ndef init_factories(dbsession: AsyncSession) -\u003e None:\n    \"\"\"Init factories.\"\"\"\n    BaseFactory.session = dbsession\n```\n\nThe dbsession factory can be obtained in [pytest-async-sqlalchemy](https://pypi.org/project/pytest-async-sqlalchemy/) library,\nor you can add it by yourself:\n\n```python\nimport pytest\nfrom sqlalchemy.ext.asyncio import create_async_engine, AsyncSession\nfrom sqlalchemy.orm import sessionmaker\n\n\n@pytest.fixture()\nasync def dbsession():\n    \"\"\"\n    Fixture that returns a SQLAlchemy session with a SAVEPOINT, and the rollback to it\n    after the test completes.\n    \"\"\"\n    engine = create_async_engine(database_url) # You must provide your database URL.\n    connection = await engine.connect()\n    trans = await connection.begin()\n\n    Session = sessionmaker(connection, expire_on_commit=False, class_=AsyncSession)\n    session = Session()\n\n    try:\n        yield session\n    finally:\n        await session.close()\n        await trans.rollback()\n        await connection.close()\n        await engine.dispose()\n```\n\nNow you can create factories and use them in your tests.\n\n```python\nfrom factory_boy_extra.async_sqlalchemy_factory import AsyncSQLAlchemyModelFactory\n\nclass TargetModel(Base):\n\n    __tablename__ = \"targetmodel\"\n\n    name = Column(String(length=120), nullable=False)  # noqa: WPS432\n\n\nclass TargetModelFactory(AsyncSQLAlchemyModelFactory):\n    name = factory.Faker(\"word\")\n\n    class Meta:\n        model = TargetModel\n```\n\nIn tests it wil look like this:\n```python\nimport pytest\n\nfrom sqlalchemy.ext.asyncio import AsyncSession\nfrom sqlalchemy import select\n\n\n@pytest.mark.asyncio\nasync def test_successful_notification(dbsession: AsyncSession) -\u003e None:\n    TargetModelFactory.create_batch(10)\n    actual_models = (await dbsession.execute(select(TargetModel))).fetchall()\n    assert len(actual_models) == 10\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs3rius%2Ffactory_boy_extra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs3rius%2Ffactory_boy_extra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs3rius%2Ffactory_boy_extra/lists"}