Skip to main content

A library for generating fake Pydantic models for testing and development purposes

Project description

Fauxdantic

A library for generating fake Pydantic models for testing. Fauxdantic makes it easy to create realistic test data for your Pydantic models. Pairs well with testing of fastapi endpoints.

Installation

poetry add fauxdantic

Features

  • Generate fake data for any Pydantic model
  • Support for nested models
  • Support for common Python types:
    • Basic types (str, int, float, bool)
    • Optional types
    • Lists
    • Dicts
    • UUIDs
    • Datetimes
    • Enums
  • Customizable values through keyword arguments
  • Generate dictionaries of fake data without creating model instances

Usage

Basic Usage

from pydantic import BaseModel
from fauxdantic import faux, faux_dict

class User(BaseModel):
    name: str
    age: int
    email: str
    is_active: bool

# Generate a fake user
fake_user = faux(User)
print(fake_user)
# Output: name='Smith' age=2045 email='smith@example.com' is_active=True

# Generate a dictionary of fake values
fake_dict = faux_dict(User)
print(fake_dict)
# Output: {'name': 'Smith', 'age': 2045, 'email': 'smith@example.com', 'is_active': True}

Nested Models

from pydantic import BaseModel
from fauxdantic import faux, faux_dict

class Address(BaseModel):
    street: str
    city: str
    zip_code: str

class User(BaseModel):
    name: str
    age: int
    address: Address

# Generate a fake user with nested address
fake_user = faux(User)
print(fake_user)
# Output: name='Smith' age=2045 address=Address(street='123 Main St', city='Anytown', zip_code='12345')

# Generate a dictionary with nested address
fake_dict = faux_dict(User)
print(fake_dict)
# Output: {'name': 'Smith', 'age': 2045, 'address': {'street': '123 Main St', 'city': 'Anytown', 'zip_code': '12345'}}

Optional Fields

from typing import Optional
from pydantic import BaseModel
from fauxdantic import faux, faux_dict

class User(BaseModel):
    name: str
    age: Optional[int]
    email: Optional[str]

# Generate a fake user with optional fields
fake_user = faux(User)
print(fake_user)
# Output: name='Smith' age=None email='smith@example.com'

# Generate a dictionary with optional fields
fake_dict = faux_dict(User)
print(fake_dict)
# Output: {'name': 'Smith', 'age': None, 'email': 'smith@example.com'}

Lists and Dicts

from typing import List, Dict
from pydantic import BaseModel
from fauxdantic import faux, faux_dict

class User(BaseModel):
    name: str
    tags: List[str]
    preferences: Dict[str, str]

# Generate a fake user with lists and dicts
fake_user = faux(User)
print(fake_user)
# Output: name='Smith' tags=['tag1', 'tag2'] preferences={'key1': 'value1', 'key2': 'value2'}

# Generate a dictionary with lists and dicts
fake_dict = faux_dict(User)
print(fake_dict)
# Output: {'name': 'Smith', 'tags': ['tag1', 'tag2'], 'preferences': {'key1': 'value1', 'key2': 'value2'}}

Custom Values

from pydantic import BaseModel
from fauxdantic import faux, faux_dict

class User(BaseModel):
    name: str
    age: int
    email: str

# Generate a fake user with custom values
fake_user = faux(User, name="John Doe", age=30)
print(fake_user)
# Output: name='John Doe' age=30 email='smith@example.com'

# Generate a dictionary with custom values
fake_dict = faux_dict(User, name="John Doe", age=30)
print(fake_dict)
# Output: {'name': 'John Doe', 'age': 30, 'email': 'smith@example.com'}

Unique String Generation

Fauxdantic supports generating truly unique string values using the _unique pattern. This is useful for creating unique identifiers, route numbers, or any field that requires uniqueness.

from typing import Optional
from pydantic import BaseModel, Field
from fauxdantic import faux

class Bus(BaseModel):
    route_number: Optional[str] = Field(None, max_length=50)

# Generate buses with unique route numbers
bus1 = faux(Bus, route_number="SW_unique")
bus2 = faux(Bus, route_number="SW_unique")
bus3 = faux(Bus, route_number="EXPRESS_unique")

print(bus1.route_number)  # SW_1753986564318970_793119f2
print(bus2.route_number)  # SW_1753986564319017_f33460cc
print(bus3.route_number)  # EXPRESS_1753986564319059_9f1de0da

Examples with Different Constraints

class ShortBus(BaseModel):
    route_number: Optional[str] = Field(None, max_length=10)

class MediumBus(BaseModel):
    route_number: Optional[str] = Field(None, max_length=15)

class LongBus(BaseModel):
    route_number: Optional[str] = Field(None, max_length=50)

# Different constraint lengths
short_bus = faux(ShortBus, route_number="SW_unique")    # SW_f2830b (9 chars)
medium_bus = faux(MediumBus, route_number="SW_unique")  # SW_208936f1 (11 chars)
long_bus = faux(LongBus, route_number="SW_unique")      # SW_1753986564318970_793119f2 (28 chars)

Enums

from enum import Enum
from pydantic import BaseModel
from fauxdantic import faux, faux_dict

class UserRole(str, Enum):
    ADMIN = "admin"
    USER = "user"
    GUEST = "guest"

class User(BaseModel):
    name: str
    role: UserRole

# Generate a fake user with enum
fake_user = faux(User)
print(fake_user)
# Output: name='Smith' role=<UserRole.ADMIN: 'admin'>

# Generate a dictionary with enum
fake_dict = faux_dict(User)
print(fake_dict)
# Output: {'name': 'Smith', 'role': 'admin'}

Development

# Install dependencies
poetry install

# Run tests
poetry run pytest

# Format code
poetry run black .
poetry run isort .

# Type checking
poetry run mypy .

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

fauxdantic-0.1.7.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fauxdantic-0.1.7-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file fauxdantic-0.1.7.tar.gz.

File metadata

  • Download URL: fauxdantic-0.1.7.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Darwin/24.5.0

File hashes

Hashes for fauxdantic-0.1.7.tar.gz
Algorithm Hash digest
SHA256 803d1a7a47ee972234148161bbf2c3162bf4c38011ff1e47c57c4c52bafdb303
MD5 cc6c2db9e23eca367c835d91b1ac8fa4
BLAKE2b-256 7d17e67775564c301c86f3666009d2651bcacf457d859c52d6fb00ac7eb80a45

See more details on using hashes here.

File details

Details for the file fauxdantic-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: fauxdantic-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Darwin/24.5.0

File hashes

Hashes for fauxdantic-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b1c15e0702a8a25e38f0323e1c31dd486d648b0ce6ca95cb14fd84ce4d917ea3
MD5 3ea5aee674b81be472787e5515f34f9c
BLAKE2b-256 85e5b25cc1ded28523ac0ccd3ec5dbb127e25b84707430bbcadfe0e504f8920d

See more details on using hashes here.

Supported by

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