prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Ensure tests can be run with `--reuse-db`
  When iterating over a test that require DB access (or a few of them), currently a site-wide setup is made which in such scenario ends up being relatively time-consuming and tedious.
  
  Ideally one could use [pytest-django's `--reuse-db` flag](http://pytest-django.readthedocs.org/en/latest/database.html#reuse-db-reuse-the-testing-database-between-test-runs) to considerably reduce setup time on test iterations, however at the current state of things such feature cannot be used due to the way the Pootle test DB environment is setup.
  
  Let's try to fix that so we can benefit from `--reuse-db`.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `pytest_pootle/plugin.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 #
  3 # Copyright (C) Pootle contributors.
  4 #
  5 # This file is a part of the Pootle project. It is distributed under the GPL3
  6 # or later license. See the LICENSE file for a copy of the license and the
  7 # AUTHORS file for copyright and authorship information.
  8 
  9 import os
  10 import shutil
  11 from pkgutil import iter_modules
  12 
  13 import pytest
  14 
  15 from . import fixtures
  16 from .env import PootleTestEnv
  17 from .fixtures import models as fixtures_models
  18 from .fixtures.core import management as fixtures_core_management
  19 from .fixtures.core import utils as fixtures_core_utils
  20 from .fixtures import formats as fixtures_formats
  21 from .fixtures import pootle_fs as fixtures_fs
  22 
  23 
  24 def _load_fixtures(*modules):
  25     for mod in modules:
  26         path = mod.__path__
  27         prefix = '%s.' % mod.__name__
  28 
  29         for loader_, name, is_pkg in iter_modules(path, prefix):
  30             if not is_pkg:
  31                 yield name
  32 
  33 
  34 @pytest.fixture
  35 def po_test_dir(request, tmpdir):
  36     po_dir = str(tmpdir.mkdir("po"))
  37 
  38     def rm_po_dir():
  39         if os.path.exists(po_dir):
  40             shutil.rmtree(po_dir)
  41 
  42     request.addfinalizer(rm_po_dir)
  43     return po_dir
  44 
  45 
  46 @pytest.fixture
  47 def po_directory(request, po_test_dir, settings):
  48     """Sets up a tmp directory for PO files."""
  49     from pootle_store.models import fs
  50 
  51     translation_directory = settings.POOTLE_TRANSLATION_DIRECTORY
  52 
  53     # Adjust locations
  54     settings.POOTLE_TRANSLATION_DIRECTORY = po_test_dir
  55     fs.location = po_test_dir
  56 
  57     def _cleanup():
  58         settings.POOTLE_TRANSLATION_DIRECTORY = translation_directory
  59 
  60     request.addfinalizer(_cleanup)
  61 
  62 
  63 @pytest.fixture(scope='session')
  64 def tests_use_db(request):
  65     return bool(
  66         [item for item in request.node.items
  67          if item.get_marker('django_db')])
  68 
  69 
  70 @pytest.fixture(scope='session')
  71 def tests_use_vfolders(request):
  72     return bool(
  73         [item for item in request.node.items
  74          if item.get_marker('pootle_vfolders')])
  75 
  76 
  77 @pytest.fixture(scope='session')
  78 def tests_use_migration(request, tests_use_db):
  79     return bool(
  80         tests_use_db
  81         and [item for item in request.node.items
  82              if item.get_marker('django_migration')])
  83 
  84 
  85 @pytest.fixture(autouse=True, scope='session')
  86 def setup_db_if_needed(request, tests_use_db):
  87     """Sets up the site DB only if tests requested to use the DB (autouse)."""
  88     if tests_use_db:
  89         return request.getfuncargvalue('post_db_setup')
  90 
  91 
  92 @pytest.fixture(scope='session')
  93 def post_db_setup(translations_directory, django_db_setup, django_db_blocker,
  94                   tests_use_db, tests_use_vfolders, request):
  95     """Sets up the site DB for the test session."""
  96     if tests_use_db:
  97         with django_db_blocker.unblock():
  98             PootleTestEnv().setup(
  99                 vfolders=tests_use_vfolders)
  100 
  101 
  102 @pytest.fixture(scope='session')
  103 def django_db_use_migrations(tests_use_migration):
  104     return tests_use_migration
  105 
  106 
  107 pytest_plugins = tuple(
  108     _load_fixtures(
  109         fixtures,
  110         fixtures_core_management,
  111         fixtures_core_utils,
  112         fixtures_formats,
  113         fixtures_models,
  114         fixtures_fs))
  115
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/pytest_pootle/plugin.py b/pytest_pootle/plugin.py
  --- a/pytest_pootle/plugin.py
  +++ b/pytest_pootle/plugin.py
  @@ -85,7 +85,7 @@
   @pytest.fixture(autouse=True, scope='session')
   def setup_db_if_needed(request, tests_use_db):
       """Sets up the site DB only if tests requested to use the DB (autouse)."""
  -    if tests_use_db:
  +    if tests_use_db and not request.config.getvalue('reuse_db'):
           return request.getfuncargvalue('post_db_setup')
  
verification_info: {"golden_diff": "diff --git a/pytest_pootle/plugin.py b/pytest_pootle/plugin.py\n--- a/pytest_pootle/plugin.py\n+++ b/pytest_pootle/plugin.py\n@@ -85,7 +85,7 @@\n @pytest.fixture(autouse=True, scope='session')\n def setup_db_if_needed(request, tests_use_db):\n     \"\"\"Sets up the site DB only if tests requested to use the DB (autouse).\"\"\"\n-    if tests_use_db:\n+    if tests_use_db and not request.config.getvalue('reuse_db'):\n         return request.getfuncargvalue('post_db_setup')\n", "issue": "Ensure tests can be run with `--reuse-db`\nWhen iterating over a test that require DB access (or a few of them), currently a site-wide setup is made which in such scenario ends up being relatively time-consuming and tedious.\n\nIdeally one could use [pytest-django's `--reuse-db` flag](http://pytest-django.readthedocs.org/en/latest/database.html#reuse-db-reuse-the-testing-database-between-test-runs) to considerably reduce setup time on test iterations, however at the current state of things such feature cannot be used due to the way the Pootle test DB environment is setup.\n\nLet's try to fix that so we can benefit from `--reuse-db`.\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nimport os\nimport shutil\nfrom pkgutil import iter_modules\n\nimport pytest\n\nfrom . import fixtures\nfrom .env import PootleTestEnv\nfrom .fixtures import models as fixtures_models\nfrom .fixtures.core import management as fixtures_core_management\nfrom .fixtures.core import utils as fixtures_core_utils\nfrom .fixtures import formats as fixtures_formats\nfrom .fixtures import pootle_fs as fixtures_fs\n\n\ndef _load_fixtures(*modules):\n    for mod in modules:\n        path = mod.__path__\n        prefix = '%s.' % mod.__name__\n\n        for loader_, name, is_pkg in iter_modules(path, prefix):\n            if not is_pkg:\n                yield name\n\n\n@pytest.fixture\ndef po_test_dir(request, tmpdir):\n    po_dir = str(tmpdir.mkdir(\"po\"))\n\n    def rm_po_dir():\n        if os.path.exists(po_dir):\n            shutil.rmtree(po_dir)\n\n    request.addfinalizer(rm_po_dir)\n    return po_dir\n\n\n@pytest.fixture\ndef po_directory(request, po_test_dir, settings):\n    \"\"\"Sets up a tmp directory for PO files.\"\"\"\n    from pootle_store.models import fs\n\n    translation_directory = settings.POOTLE_TRANSLATION_DIRECTORY\n\n    # Adjust locations\n    settings.POOTLE_TRANSLATION_DIRECTORY = po_test_dir\n    fs.location = po_test_dir\n\n    def _cleanup():\n        settings.POOTLE_TRANSLATION_DIRECTORY = translation_directory\n\n    request.addfinalizer(_cleanup)\n\n\n@pytest.fixture(scope='session')\ndef tests_use_db(request):\n    return bool(\n        [item for item in request.node.items\n         if item.get_marker('django_db')])\n\n\n@pytest.fixture(scope='session')\ndef tests_use_vfolders(request):\n    return bool(\n        [item for item in request.node.items\n         if item.get_marker('pootle_vfolders')])\n\n\n@pytest.fixture(scope='session')\ndef tests_use_migration(request, tests_use_db):\n    return bool(\n        tests_use_db\n        and [item for item in request.node.items\n             if item.get_marker('django_migration')])\n\n\n@pytest.fixture(autouse=True, scope='session')\ndef setup_db_if_needed(request, tests_use_db):\n    \"\"\"Sets up the site DB only if tests requested to use the DB (autouse).\"\"\"\n    if tests_use_db:\n        return request.getfuncargvalue('post_db_setup')\n\n\n@pytest.fixture(scope='session')\ndef post_db_setup(translations_directory, django_db_setup, django_db_blocker,\n                  tests_use_db, tests_use_vfolders, request):\n    \"\"\"Sets up the site DB for the test session.\"\"\"\n    if tests_use_db:\n        with django_db_blocker.unblock():\n            PootleTestEnv().setup(\n                vfolders=tests_use_vfolders)\n\n\n@pytest.fixture(scope='session')\ndef django_db_use_migrations(tests_use_migration):\n    return tests_use_migration\n\n\npytest_plugins = tuple(\n    _load_fixtures(\n        fixtures,\n        fixtures_core_management,\n        fixtures_core_utils,\n        fixtures_formats,\n        fixtures_models,\n        fixtures_fs))\n", "path": "pytest_pootle/plugin.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nimport os\nimport shutil\nfrom pkgutil import iter_modules\n\nimport pytest\n\nfrom . import fixtures\nfrom .env import PootleTestEnv\nfrom .fixtures import models as fixtures_models\nfrom .fixtures.core import management as fixtures_core_management\nfrom .fixtures.core import utils as fixtures_core_utils\nfrom .fixtures import formats as fixtures_formats\nfrom .fixtures import pootle_fs as fixtures_fs\n\n\ndef _load_fixtures(*modules):\n    for mod in modules:\n        path = mod.__path__\n        prefix = '%s.' % mod.__name__\n\n        for loader_, name, is_pkg in iter_modules(path, prefix):\n            if not is_pkg:\n                yield name\n\n\n@pytest.fixture\ndef po_test_dir(request, tmpdir):\n    po_dir = str(tmpdir.mkdir(\"po\"))\n\n    def rm_po_dir():\n        if os.path.exists(po_dir):\n            shutil.rmtree(po_dir)\n\n    request.addfinalizer(rm_po_dir)\n    return po_dir\n\n\n@pytest.fixture\ndef po_directory(request, po_test_dir, settings):\n    \"\"\"Sets up a tmp directory for PO files.\"\"\"\n    from pootle_store.models import fs\n\n    translation_directory = settings.POOTLE_TRANSLATION_DIRECTORY\n\n    # Adjust locations\n    settings.POOTLE_TRANSLATION_DIRECTORY = po_test_dir\n    fs.location = po_test_dir\n\n    def _cleanup():\n        settings.POOTLE_TRANSLATION_DIRECTORY = translation_directory\n\n    request.addfinalizer(_cleanup)\n\n\n@pytest.fixture(scope='session')\ndef tests_use_db(request):\n    return bool(\n        [item for item in request.node.items\n         if item.get_marker('django_db')])\n\n\n@pytest.fixture(scope='session')\ndef tests_use_vfolders(request):\n    return bool(\n        [item for item in request.node.items\n         if item.get_marker('pootle_vfolders')])\n\n\n@pytest.fixture(scope='session')\ndef tests_use_migration(request, tests_use_db):\n    return bool(\n        tests_use_db\n        and [item for item in request.node.items\n             if item.get_marker('django_migration')])\n\n\n@pytest.fixture(autouse=True, scope='session')\ndef setup_db_if_needed(request, tests_use_db):\n    \"\"\"Sets up the site DB only if tests requested to use the DB (autouse).\"\"\"\n    if tests_use_db and not request.config.getvalue('reuse_db'):\n        return request.getfuncargvalue('post_db_setup')\n\n\n@pytest.fixture(scope='session')\ndef post_db_setup(translations_directory, django_db_setup, django_db_blocker,\n                  tests_use_db, tests_use_vfolders, request):\n    \"\"\"Sets up the site DB for the test session.\"\"\"\n    if tests_use_db:\n        with django_db_blocker.unblock():\n            PootleTestEnv().setup(\n                vfolders=tests_use_vfolders)\n\n\n@pytest.fixture(scope='session')\ndef django_db_use_migrations(tests_use_migration):\n    return tests_use_migration\n\n\npytest_plugins = tuple(\n    _load_fixtures(\n        fixtures,\n        fixtures_core_management,\n        fixtures_core_utils,\n        fixtures_formats,\n        fixtures_models,\n        fixtures_fs))\n", "path": "pytest_pootle/plugin.py"}]}