Skip to main content

SQLAlchemy Model Faker

rogervila/sqlalchemy_model_faker

Coverage Quality Gate Status Maintainability Rating

Generate SQLAlchemy models with fake data.

IMPORTANT: Documentation asumes previous knowledge on how to work with SQLAlchemy.

Install

pip install sqlalchemy_model_faker

Usage

The package expects a SQLAlchemy model that extends from declarative_base().

It reads the model columns and generates a fake value according with the column type.

Basic Usage

Let's create a Product model with a description and a price columns.

from sqlalchemy.ext.declarative import declarative_base

class Product(declarative_base()):
    __tablename__ = 'products'
    id = Column(Integer, primary_key=True, autoincrement=True)
    description = Column(Text)
    price = Column(Integer)

Use factory to create a fake Product model.

from sqlalchemy_model_faker import factory

product = factory(Product).make()

print(type(product.description)) # <class 'str'>
print(type(product.price)) # <class 'int'>

Use SQLAlchemy session to persist the product into the database.

Custom values

By passing a dict, you can force factory to use custom provided values.

Other column values will be set with fake data.

from sqlalchemy_model_faker import factory

product = factory(Product).make({'price': 288})

print(product.price) # 288

Specific fake types

Faker has methods to generate fake data in a specific format, like emails, addresses, IPs, etc.

The fake data types can be specified passing a dict with column names and fake data types.

from sqlalchemy_model_faker import factory

product = factory(Product).make(types={'description': 'email'})

# Emails have only 1 '@'
print(product.description.count('@')) # 1

# Emails have at least one '.'
print(product.description.count('.') # >= 1

Custom values and fake types can be passed together.

from sqlalchemy_model_faker import factory

product = factory(Product).make({'price': 288}, types={'description': 'email'})

print(product.price) # 288
print(product.description) # valid email string

Ignoring columns

Columns might be ignored. Their generated value will be None.

Other column values will be set with fake data.

from sqlalchemy_model_faker import factory

product = factory(Product).make(ignored_columns=['price'])

print(product.price) # None

Custom Faker instance

A custom faker instance can be passed to the factory constructor.

This is useful to extend Faker, or replace it with a Mock when running tests.

from faker import Faker
from sqlalchemy_model_faker import factory

faker = Faker() # Extend Faker as needed or replace it with a Mock
product = factory(Product, faker).make()

# etc

License

This project is open-sourced software licensed under the MIT license.

Metadata

Release files for sqlalchemy-model-faker 0.0.5

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

Source distribution (sdist)

Source distribution for sqlalchemy-model-faker 0.0.5
File Size Uploaded
sqlalchemy_model_faker-0.0.5.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-model-faker 0.0.5
File Interpreter ABI Platform
sqlalchemy_model_faker-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / sqlalchemy_model_faker-0.0.5.tar.gz

Download URL sqlalchemy_model_faker-0.0.5.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1bb4a6a41a7cd37373094fdeb080d791586625a493a7b9ee52f30bed11bb931a
BLAKE2b-256 checksum
How to use checksums
fac50bce7aeb0f17479dcca75e882f2276d9058af02e6de9b496a80b4f43360a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.14

Release files / sqlalchemy_model_faker-0.0.5-py3-none-any.whl

Download URL sqlalchemy_model_faker-0.0.5-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f72a4d71af54aefa3b4320cfc19efd73b4662645777750c018bb0a8720a8602c
BLAKE2b-256 checksum
How to use checksums
04dffe00f3b3d48c12faf337ddac26f2bfabe698230a9b7f729497fd8570f1a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.14

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

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