Skip to content

Commit

Permalink
Merge pull request #195 from blueyed/doc-fixes
Browse files Browse the repository at this point in the history
Some doc fixes
  • Loading branch information
pelme committed Jan 24, 2015
2 parents b4b5d49 + d7f7ef0 commit cd6db15
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
4 changes: 2 additions & 2 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ used - use 2.6.1 or newer to avoid confusion.
-----

This release is *backward incompatible*. The biggest change is the need
to add the ``pytest.mark.django_db`` to tests which needs database
to add the ``pytest.mark.django_db`` to tests which require database
access.

Finding such tests is generally very easy: just run your test suite, the
tests which needs database access will fail. Add ``pytestmark =
tests which need database access will fail. Add ``pytestmark =
pytest.mark.django_db`` to the module/class or decorate them with
``@pytest.mark.django_db``.

Expand Down
6 changes: 3 additions & 3 deletions pytest_django/db_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def monkey_patch_creation_for_db_suffix(suffix=None):

if suffix is not None:
def _get_test_db_name(self):
"""
Internal implementation - returns the name of the test DB that will
be created. Only useful when called from create_test_db() and
"""Internal: return the name of the test DB that will be created.
This is only useful when called from create_test_db() and
_create_test_db() and when no external munging is done with the
'NAME' or 'TEST_NAME' settings.
"""
Expand Down
18 changes: 5 additions & 13 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ def client():

@pytest.fixture()
def django_user_model(db):
"""
The class of Django's user model.
"""
"""The class of Django's user model."""
try:
from django.contrib.auth import get_user_model
except ImportError:
Expand All @@ -205,9 +203,7 @@ def django_user_model(db):

@pytest.fixture()
def django_username_field(django_user_model):
"""
The fieldname for the username used with Django's user model.
"""
"""The fieldname for the username used with Django's user model."""
try:
return django_user_model.USERNAME_FIELD
except AttributeError:
Expand All @@ -217,8 +213,7 @@ def django_username_field(django_user_model):

@pytest.fixture()
def admin_user(db, django_user_model, django_username_field):
"""
A Django admin user.
"""A Django admin user.
This uses an existing user with username "admin", or creates a new one with
password "password".
Expand All @@ -239,10 +234,7 @@ def admin_user(db, django_user_model, django_username_field):

@pytest.fixture()
def admin_client(db, admin_user):
"""
A Django test client logged in as an admin user (via the ``admin_user``
fixture).
"""
"""A Django test client logged in as an admin user."""
from django.test.client import Client

client = Client()
Expand Down Expand Up @@ -326,7 +318,7 @@ def live_server(request):

@pytest.fixture(autouse=True, scope='function')
def _live_server_helper(request):
"""Helper to make live_server work, internal to pytest-django
"""Helper to make live_server work, internal to pytest-django.
This helper will dynamically request the transactional_db fixture
for a test which uses the live_server fixture. This allows the
Expand Down
23 changes: 12 additions & 11 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def pytest_runtest_setup(item):
@pytest.fixture(autouse=True, scope='session')
def _django_test_environment(request):
"""
Ensure that Django is loaded and has its testing environment setup
Ensure that Django is loaded and has its testing environment setup.
XXX It is a little dodgy that this is an autouse fixture. Perhaps
an email fixture should be requested in order to be able to
Expand All @@ -245,7 +245,7 @@ def _django_test_environment(request):

@pytest.fixture(autouse=True, scope='session')
def _django_cursor_wrapper(request):
"""The django cursor wrapper, internal to pytest-django
"""The django cursor wrapper, internal to pytest-django.
This will globally disable all database access. The object
returned has a .enable() and a .disable() method which can be used
Expand All @@ -270,7 +270,7 @@ def _django_cursor_wrapper(request):

@pytest.fixture(autouse=True)
def _django_db_marker(request):
"""Implement the django_db marker, internal to pytest-django
"""Implement the django_db marker, internal to pytest-django.
This will dynamically request the ``db`` or ``transactional_db``
fixtures as required by the django_db marker.
Expand All @@ -286,7 +286,7 @@ def _django_db_marker(request):

@pytest.fixture(autouse=True, scope='class')
def _django_setup_unittest(request, _django_cursor_wrapper):
"""Setup a django unittest, internal to pytest-django"""
"""Setup a django unittest, internal to pytest-django."""
if django_settings_is_configured() and is_django_unittest(request):
request.getfuncargvalue('_django_test_environment')
request.getfuncargvalue('_django_db_setup')
Expand All @@ -303,15 +303,15 @@ def teardown():

@pytest.fixture(autouse=True, scope='function')
def _django_clear_outbox(request):
"""Clear the django outbox, internal to pytest-django"""
"""Clear the django outbox, internal to pytest-django."""
if django_settings_is_configured():
from django.core import mail
mail.outbox = []


@pytest.fixture(autouse=True, scope='function')
def _django_set_urlconf(request):
"""Apply the @pytest.mark.urls marker, internal to pytest-django"""
"""Apply the @pytest.mark.urls marker, internal to pytest-django."""
marker = request.keywords.get('urls', None)
if marker:
skip_if_no_django()
Expand All @@ -333,7 +333,7 @@ def restore():


class CursorManager(object):
"""Manager for django.db.backends.util.CursorWrapper
"""Manager for django.db.backends.util.CursorWrapper.
This is the object returned by _django_cursor_wrapper.
Expand All @@ -353,14 +353,15 @@ def _blocking_wrapper(*args, **kwargs):
__tracebackhide__ = True
__tracebackhide__ # Silence pyflakes
pytest.fail('Database access not allowed, '
'use the "django_db" mark to enable')
'use the "django_db" mark to enable it.')

def enable(self):
"""Enable access to the django database"""
"""Enable access to the Django database."""
self._save_active_wrapper()
self._dbutil.CursorWrapper = self._real_wrapper

def disable(self):
"""Disable access to the Django database."""
self._save_active_wrapper()
self._dbutil.CursorWrapper = self._blocking_wrapper

Expand All @@ -375,7 +376,7 @@ def __exit__(self, exc_type, exc_value, traceback):


def validate_django_db(marker):
"""This function validates the django_db marker
"""Validate the django_db marker.
It checks the signature and creates the `transaction` attribute on
the marker which will have the correct value.
Expand All @@ -386,7 +387,7 @@ def apifun(transaction=False):


def validate_urls(marker):
"""This function validates the urls marker
"""Validate the urls marker.
It checks the signature and creates the `urls` attribute on the
marker which will have the correct value.
Expand Down
3 changes: 2 additions & 1 deletion tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ def test_trans_db(self, fixture_with_transdb, fixture_with_db):


class TestDatabaseMarker:
"Tests for the django_db marker."

@pytest.mark.django_db
def test_access(self):
Item.objects.create(name='spam')

@pytest.mark.django_db
def test_clean_db(self):
# Relies on the order: test_access created an object
# Relies on the order: test_access created an object.
assert Item.objects.count() == 0

@pytest.mark.django_db
Expand Down

0 comments on commit cd6db15

Please sign in to comment.