diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6eb3a27..c0b4ba1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,12 @@ CHANGELOG ========= +0.5.4 +----- + +* Only enable pytest plugin when parasolr is in Django installed apps + and a Solr connection is configured + 0.5.3 --- diff --git a/parasolr/pytest_plugin.py b/parasolr/pytest_plugin.py index 148d6a4..7c3ae36 100644 --- a/parasolr/pytest_plugin.py +++ b/parasolr/pytest_plugin.py @@ -4,6 +4,7 @@ try: import django + from django.apps import apps from django.conf import settings from django.test import override_settings except ImportError: @@ -25,7 +26,16 @@ def get_test_solr_config(): options specified are used; if no test collection name is specified, generates one based on the configured collection.''' - # copy default config for basic connection optiosn (e.g. url) + # skip if parasolr is not actually in django installed apps + if not apps.is_installed("parasolr"): + return + + # if no solr connection is configured, bail out + if not getattr(settings, 'SOLR_CONNECTIONS', None): + logger.warn('No Solr configuration found') + return + + # copy default config for basic connection options (e.g. url) test_config = settings.SOLR_CONNECTIONS['default'].copy() # use test settings as primary: anything in test settings @@ -67,6 +77,8 @@ def configure_django_test_solr(): """ solr_config_opts = get_test_solr_config() + if not solr_config_opts: + return logger.info('Configuring Solr for tests %(URL)s%(COLLECTION)s', solr_config_opts) diff --git a/parasolr/tests/test_pytest_plugin.py b/parasolr/tests/test_pytest_plugin.py index a060b15..3ccd789 100644 --- a/parasolr/tests/test_pytest_plugin.py +++ b/parasolr/tests/test_pytest_plugin.py @@ -1,7 +1,7 @@ """ Test pytest plugin fixture for django """ -import uuid +from unittest.mock import patch import pytest try: @@ -93,6 +93,34 @@ def test_solr_client(): # run all tests with pytest with all pytest-django plugins turned off # result = testdir.runpytest('-p', 'no:django') - result = testdir.runpytest_subprocess('--capture', 'no') #, '-p', 'no:django') + result = testdir.runpytest_subprocess('--capture', 'no') # check that test case passed result.assert_outcomes(passed=1) + + +@skipif_no_django +def test_not_configured(testdir): + """skip without error if not configured.""" + + with override_settings(SOLR_CONNECTIONS=None): + assert not get_test_solr_config() + + # create a temporary pytest test file with no solr use + testdir.makepyfile(""" + def test_unrelated(): + assert 1 + 1 == 2 + """) + # run all tests with pytest with all pytest-django plugins turned off + result = testdir.runpytest_subprocess('--capture', 'no') + # check that test case passed + result.assert_outcomes(passed=1) + + +@skipif_no_django +def test_app_not_installed(testdir): + """skip without error if not configured.""" + + with patch('parasolr.pytest_plugin.apps') as mockapps: + mockapps.is_installed.return_value = False + assert not get_test_solr_config() + mockapps.is_installed.assert_called_with('parasolr')