Skip to main content

bazis-test-utils

PyPI version Python Versions License

A utility package for testing in the Bazis ecosystem — a framework based on Django + FastAPI + Pydantic.

Quick Start

# Install the package
uv add bazis-test-utils

# Create test models
from bazis_test_utils.models_abstract import ParentEntityBase

class TestEntity(ParentEntityBase):
    pass

# Use in tests
from bazis_test_utils.factories_abstract import ParentEntityFactoryAbstract

class TestEntityFactory(ParentEntityFactoryAbstract):
    class Meta:
        model = TestEntity

Table of Contents

Description

bazis-test-utils provides base models and factories to simplify test writing in Bazis packages. The package includes standard models for testing hierarchical and dependent entities, as well as utilities for working with the FastAPI test client.

Features

  • Base Django test models:

    • ChildEntityBase — base model for child entities
    • DependentEntityBase — base model for dependent entities
    • ExtendedEntityBase — base model for extended entities
    • ParentEntityBase — base model for parent entities
  • Abstract factory classes for rapid test data generation with factory_boy integration

  • API testing utilities:

    • get_api_client() — function to create a FastAPI test client with optional authentication

Installation

uv add bazis-test-utils

Using pip

pip install bazis-test-utils

Development installation

git clone https://github.com/ecofuture-tech/bazis-test-utils.git
cd bazis-test-utils
uv sync --dev

Usage

Creating Test Models

In your project, create models by inheriting from the base classes:

from bazis_test_utils.models_abstract import ParentEntityBase, ChildEntityBase

class TestParentEntity(ParentEntityBase):
    """Model for testing parent entities"""
    pass

class TestChildEntity(ChildEntityBase):
    """Model for testing child entities"""
    pass

Using Factories

Create factories for your models using abstract factory classes:

from bazis_test_utils.factories_abstract import ParentEntityFactoryAbstract
from .models import TestParentEntity

class ParentEntityFactory(ParentEntityFactoryAbstract):
    class Meta:
        model = TestParentEntity

Then use them in your tests:

import pytest

@pytest.mark.django_db
def test_parent_entity_creation():
    parent = ParentEntityFactory.create()
    assert parent.id is not None
    assert parent.created_at is not None

Testing FastAPI Endpoints

Use get_api_client to create a test client:

# conftest.py
import pytest

@pytest.fixture(scope='function')
def sample_app():
    from sample.main import app
    return app

# test_sample_app.py
from bazis_test_utils.utils import get_api_client

def test_api_endpoint(sample_app):
    # Without authentication
    client = get_api_client(sample_app)
    response = client.get("/api/v1/entities/")
    assert response.status_code == 200

def test_authenticated_endpoint(sample_app):
    # With authentication token
    token = "your-test-token"
    client = get_api_client(sample_app, token=token)
    response = client.get("/api/v1/protected/")
    assert response.status_code == 200

Examples

Basic Factory Usage

from bazis_test_utils.factories_abstract import (
    ParentEntityFactoryAbstract,
    ChildEntityFactoryAbstract
)

# Create a single instance
parent = ParentEntityFactory.create(name="Test Parent")

# Create multiple instances
parents = ParentEntityFactory.create_batch(5)

# Create without saving to database
parent = ParentEntityFactory.build()

Testing Hierarchical Relationships

import pytest
from .factories import ParentEntityFactory, ChildEntityFactory

@pytest.mark.django_db
def test_parent_child_relationship():
    parent = ParentEntityFactory.create()
    child = ChildEntityFactory.create(parent=parent)
    
    assert child.parent == parent
    assert child in parent.children.all()

Using with pytest-factoryboy

# conftest.py
from pytest_factoryboy import register
from .factories import ParentEntityFactory, ChildEntityFactory

register(ParentEntityFactory)
register(ChildEntityFactory)

# test_entities.py
@pytest.mark.django_db
def test_with_fixtures(parent_entity, child_entity):
    # Fixtures are automatically created
    assert parent_entity.id is not None
    assert child_entity.id is not None

API Integration Testing

from bazis_test_utils.utils import get_api_client
from .factories import ParentEntityFactory

def test_crud_operations(sample_app):
    client = get_api_client(sample_app)
    
    # Create
    response = client.post("/api/v1/entities/", json={"name": "Test"})
    assert response.status_code == 201
    entity_id = response.json()["id"]
    
    # Read
    response = client.get(f"/api/v1/entities/{entity_id}/")
    assert response.status_code == 200
    
    # Update
    response = client.put(f"/api/v1/entities/{entity_id}/", json={"name": "Updated"})
    assert response.status_code == 200
    
    # Delete
    response = client.delete(f"/api/v1/entities/{entity_id}/")
    assert response.status_code == 204

Usage in Bazis

This package is used in the sample project in the Bazis core to create standard test models. Models in the sample project inherit from bazis-test-utils models, and factories are used to generate test data across all main framework packages.

The typical workflow in Bazis packages:

  1. Define concrete models in sample/models.py by inheriting from abstract base models
  2. Create factories in sample/factories.py using abstract factory classes
  3. Register factories in conftest.py for use across all tests
  4. Use generated fixtures in integration and unit tests

The pytest plugin

Installed, the package is a pytest plugin (entry point pytest11 bazis):

  • the triggers of django-pgtrigger are installed in the test database once it is set up, if pgtrigger is in INSTALLED_APPS (a conftest no longer overrides django_db_setup for them);
  • the roles of bazis-permit (roles.py) and the workflows of bazis-statusy (workflow.py) declared in the code are applied by migrate and flush, so the test database has them; bazis_test_utils.plugin.apply_declarations() applies them again and forgets the cached permissions and content types, and the fixture bazis_declared calls it, for a test that changes the declared rows itself (--reuse-db still runs migrate, which applies them):
def test_the_client_sees_his_tickets(bazis_declared, client_user):
    ...

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests (pytest)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

Please make sure to:

  • Write tests for new features
  • Update documentation as needed
  • Follow the existing code style
  • Add your changes to the changelog

Development

Setup development environment

# Clone the repository
git clone https://github.com/ecofuture-tech/bazis-test-utils.git
cd bazis-test-utils

# Install dependencies with development extras
uv sync --dev

# Run linting
ruff check .

# Format code
ruff format .

License

Apache License 2.0

See LICENSE file for details.

Support

If you have questions or issues, please:


Made with ❤️ by the Bazis team

Metadata

Release files for bazis-test-utils 2.5.1

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

Source distribution (sdist)

Source distribution for bazis-test-utils 2.5.1
File Size Uploaded
bazis_test_utils-2.5.1.tar.gz 28.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bazis-test-utils 2.5.1
File Interpreter ABI Platform
bazis_test_utils-2.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 51.4 kB

Release files / bazis_test_utils-2.5.1.tar.gz

Download URL bazis_test_utils-2.5.1.tar.gz
Size 28.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e9b2264d95a5cb0dcfd89bb029e6959c470e7994d4b11c62a21d06f8f59992db
BLAKE2b-256 checksum
How to use checksums
dcef1829a96003ad05b558160018ef973a067b2f398eb27d97588de5f5539602
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / bazis_test_utils-2.5.1-py3-none-any.whl

Download URL bazis_test_utils-2.5.1-py3-none-any.whl
Size 22.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c99bf77138b6c28b80ce80b582c39114c9a5a28fc9f2f63fb815b230f2057b1
BLAKE2b-256 checksum
How to use checksums
c4c8729d7ba17cbf1f2c9697dd5793bb62074b90c414fdaf3b6e22cec549242d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

2.5.1 This release

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

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