Skip to main content

Generate SQLAlchemy models with fake data

Project description

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.

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

sqlalchemy_model_faker-0.0.5.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_model_faker-0.0.5-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_model_faker-0.0.5.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_model_faker-0.0.5.tar.gz
Algorithm Hash digest
SHA256 1bb4a6a41a7cd37373094fdeb080d791586625a493a7b9ee52f30bed11bb931a
MD5 4100d4715fd8d59a0cb4152d4ae341d6
BLAKE2b-256 fac50bce7aeb0f17479dcca75e882f2276d9058af02e6de9b496a80b4f43360a

See more details on using hashes here.

File details

Details for the file sqlalchemy_model_faker-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_model_faker-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f72a4d71af54aefa3b4320cfc19efd73b4662645777750c018bb0a8720a8602c
MD5 dbefac1faa2e4f3a41803751f7beced8
BLAKE2b-256 04dffe00f3b3d48c12faf337ddac26f2bfabe698230a9b7f729497fd8570f1a5

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