Skip to main content

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

Project description

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

Using uv (recommended)

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

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.

Links

Support

If you have questions or issues, please:


Made with ❤️ by the Bazis team

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

bazis_test_utils-2.2.2.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

bazis_test_utils-2.2.2-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file bazis_test_utils-2.2.2.tar.gz.

File metadata

  • Download URL: bazis_test_utils-2.2.2.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

File hashes

Hashes for bazis_test_utils-2.2.2.tar.gz
Algorithm Hash digest
SHA256 6355ed343e3c77c6186e9d021a412d8ad90cb01519bb2bf99ebf9952d04f1527
MD5 c1abf1361025210a07989dee20de1672
BLAKE2b-256 ec4a5318e404f56552537cff4ad2092db7c823b5ff43a8cce03e5c83ff684577

See more details on using hashes here.

File details

Details for the file bazis_test_utils-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: bazis_test_utils-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

File hashes

Hashes for bazis_test_utils-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 86c57d8377b3a9fa10152d9bcacbf7c47b12dec425e140b7d5a7187152e08b9d
MD5 c5d7b9a76e8338db646173273ab61720
BLAKE2b-256 cb38cab0f638ae16c3ab5c527fd8e83174b442ff7292e1dec0ae66173e642467

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