When running pytest on a plugin: RuntimeError: Model class baserow.contrib.builder.pages.models.Page doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

This used to work for me, so not sure whether this is a new issue after upgrading my plugin from 1.13.2 to 1.15.2.
To run the tests, I get into the docker container and run pytest like this:

docker compose -f docker-compose.dev.yml exec baserow-vocabai-plugin /baserow.sh backend-cmd bash -c bash
cd /baserow/data/plugins/baserow_vocabai_plugin/backend/tests
pytest

a google search here suggests this is an import related issue, I plan on debugging this but wanted to post this in case anyone has faced the same problem.

        if getattr(meta, 'app_label', None) is None:
            if app_config is None:
                if not abstract:
>                   raise RuntimeError(
                        "Model class %s.%s doesn't declare an explicit "
                        "app_label and isn't in an application in "
                        "INSTALLED_APPS." % (module, name)
                    )
E                   RuntimeError: Model class baserow.contrib.builder.pages.models.Page doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

/baserow/venv/lib/python3.9/site-packages/django/db/models/base.py:113: RuntimeError

============================================================================================================= ERRORS ==============================================================================================================________________________________________________________________________________ ERROR at setup of test_can_query_starting_endpoint_as_authed_user ________________________________________________________________________________

    @pytest.fixture
    def data_fixture():
>       from .fixtures import Fixtures

/baserow/backend/src/baserow/test_utils/pytest_conftest.py:34:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/baserow/backend/src/baserow/test_utils/fixtures/__init__.py:4: in <module>
    from .application import ApplicationFixtures
/baserow/backend/src/baserow/test_utils/fixtures/application.py:1: in <module>
    from baserow.contrib.builder.models import Builder
/baserow/backend/src/baserow/contrib/builder/models.py:1: in <module>
    from baserow.contrib.builder.pages.models import Page
/baserow/backend/src/baserow/contrib/builder/pages/models.py:22: in <module>
    class Page(
/usr/lib/python3.9/abc.py:85: in __new__
    cls = super().__new__(mcls, name, bases, namespace, **kwargs)

Looks like I can make the issue go away by adding this in plugins/baserow_vocabai_plugin/backend/src/baserow_vocabai_plugin/config/settings/settings.py:

settings.INSTALLED_APPS += ["baserow.contrib.builder"]

After that, my tests run. I have no idea what i’m doing though. Maybe I should stop resisting and just learn Django once and for all !

I ended up doing this in my plugin’s settings.py, seems like a safe thing to do:

import sys

def setup(settings):
    """
    This function is called after Baserow as setup its own Django settings file but
    before Django starts. Read and modify provided settings object as appropriate
    just like you would in a normal Django settings file. E.g.:

    settings.INSTALLED_APPS += ["some_custom_plugin_dep"]
    for db, value in settings.DATABASES:
        value['engine'] = 'some custom engine'
    """

    # see here: https://community.baserow.io/t/when-running-pytest-on-a-plugin-runtimeerror-model-class-baserow-contrib-builder-pages-models-page-doesnt-declare-an-explicit-app-label-and-isnt-in-an-application-in-installed-apps/2548/2
    if "pytest" in sys.modules:
        settings.INSTALLED_APPS += ["baserow.contrib.builder"]

Hey @lucw ,

Sorry about the issue you are facing. It’s related to a new unpublished WIP module we have added. I like the solution you’ve suggested and and I’ll try to fix the problem directly in the core to avoid impacting plugins writer. Thanks for reporting it. I’ll let you know when (if?) I have a better solution.

Great, thank you for your help !

For your information the patch has been merged but it hasn’t been released yet. See Fix plugin tests (!1366) · Merge requests · Bram Wiepjes / baserow · GitLab if you want to see the fix which is largely inspired by your solution :wink:

many thanks @jrmi, your work on this is appreciated !

@jrmi FYI looks like I had to undo the fix above in my plugin after upgrading my plugin to baserow 1.17.2.

Sorry about that, may be adding the same app twice is not a good idea :smiley:

However, i’m happy you don’t need your fix anymore.