Skip to main content

Populate Tortoise Model with faker output in a factory for tests

Project description

Usage

from tortoise import Model, fields
from tortoise_factory import model_factory


class User(Model):
    id = fields.IntField(primary_key=True)
    first_name = fields.CharField(max_length=100)
    last_name = fields.CharField(max_length=100)
    email = fields.CharField(max_length=200)


class Product(Model):
    id = fields.IntField(primary_key=True)
    name = fields.CharField(max_length=200)
    description = fields.TextField(null=True, default=None)
    price = fields.FloatField()
    created_by = fields.ForeignKeyField(
        "models.User",
        on_delete=fields.CASCADE,
        related_name="products",
    )

    def __str__(self) -> str:
        return self.name

Generalities

All returned models are saved in db, meaning they will all have a .id value, the same is true for any ForeignKeyField

Random values

To get a simple random product:

product = await model_factory(Product)

the created_by will be created automaticaly since it cannot be None, when possible, the factory will put None into optional fields.

Specify some fields

product = await model_factory(Product, name="something", created_by__name="Bob")

we use __ to go throught relations

Fill optional values

product = await model_factory(Product, _optionals=["description"])

now you will have a product.description wich won't be None

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tortoise_factory-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

tortoise_factory-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file tortoise_factory-0.1.0.tar.gz.

File metadata

  • Download URL: tortoise_factory-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.10.8-arch1-1

File hashes

Hashes for tortoise_factory-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b351f8b8d9d3e4727e91e26af697b1b963cf64b660d51d9838fa6bda9580c428
MD5 a1aa387978c0db3eb1c325e6eb5e9179
BLAKE2b-256 8beedba834b5fc25c2fb7f94c9674a9594673b029bc8dbdf1b85ffc95f3a71d8

See more details on using hashes here.

File details

Details for the file tortoise_factory-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tortoise_factory-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.10.8-arch1-1

File hashes

Hashes for tortoise_factory-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21c9727b2ba83e905a9453f40dd9b20d79041cefadcfca7b0d234583c5f6eb94
MD5 0668328e4cf25027cef02476bdaef84e
BLAKE2b-256 6edfbdb5145086944dffc6e400926832474eed0b559c2fa8e2ba3efcd6f567bb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page