pytest-dev / pytest-dev/pytest-django

Create custom types

Open
#1,168 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

Hi All!

I have several databases in my project and one of them is external. I know, it's bad practice but that's how it is. And I need tests for it and pytest-django creates tables from models perfectly, but problems arise with custom types in postgres. I need to insert the creation of types in the interval between the creation of the database and the creation of tables. I found this solution and I don't like it. Is there another way?

@pytest.fixture(scope="session", autouse=True)
def _create_custom_types():
    from django.db.backends.base.schema import BaseDatabaseSchemaEditor

    def create_model(self, model):
        if model == User:
            self.execute("CREATE EXTENSION hstore;")
        orig_create_model(self, model)

    orig_create_model = BaseDatabaseSchemaEditor.create_model
    with patch.object(BaseDatabaseSchemaEditor, "create_model", new=create_model):
        yield

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pytest-django's test-database setup and Django's BaseDatabaseSchemaEditor.create_model entry point, then compare the fixture workaround with available database-creation hooks. Done means a supported way to create PostgreSQL custom types between database and table creation, covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, postgresql, python
Domain
databases, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.