Skip to content

Commit cbf5e8c

Browse files
committed
Clear the functests search index when clearing the DB
For speed h does not normally clear the DB between functests unless the test has `@pytest.mark.parametrize("with_clean_db")`, see: #6845 At the time when this change was made the _search index_ was still being cleared before each functest which makes no sense: the search API will not return an annotation if it's not in the search index, even if the annotation is in the DB. If you aren't going to clear the DB before each functest, you shouldn't clear the search index either. A later PR accidentally broke the `always_delete_all_elasticsearch_documents` fixture so that the search index wasn't actually being cleared between functests anyway, see: #9636 This commit: 1. Removes the broken `always_delete_all_elasticsearch_documents` fixture: it doesn't do anything, and since we don't clear the DB between tests it doesn't make sense to clear the search index either. 2. Replaces the `with_clean_db` fixture with `with_clean_db_and_search_index`: it doesn't make sense to clear the DB without also clearing the search index. Longer-term I think we probably want to find a fast way to clear both the DB and search index before each functional test.
1 parent c615572 commit cbf5e8c

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

tests/functional/api/bulk/action_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pytestmark = pytest.mark.usefixtures("init_elasticsearch")
1111

1212

13-
@pytest.mark.usefixtures("with_clean_db")
13+
@pytest.mark.usefixtures("with_clean_db_and_search_index")
1414
class TestBulk:
1515
AUTHORITY = "lms.hypothes.is"
1616

tests/functional/api/bulk/annotation_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from h.models import GroupMembership
66

77

8-
@pytest.mark.usefixtures("with_clean_db")
8+
@pytest.mark.usefixtures("with_clean_db_and_search_index")
99
class TestBulkAnnotation:
1010
def test_it_requires_authentication(self, make_request):
1111
response = make_request(headers={"bad_auth": "BAD"}, expect_errors=True)

tests/functional/bin/run_data_task_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class TestRunSQLTask:
1313
# We use "clean DB" here to ensure the schema is created
14-
@pytest.mark.usefixtures("with_clean_db")
14+
@pytest.mark.usefixtures("with_clean_db_and_search_index")
1515
def test_reporting_tasks(self, environ):
1616
for task_name in (
1717
# Run all the jobs in one line in the order they are expected to

tests/functional/conftest.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,24 @@ def reset_app(app):
5757

5858

5959
@pytest.fixture
60-
def with_clean_db(db_engine):
60+
def with_clean_db_and_search_index(
61+
db_engine,
62+
# The es_client fixture clears the search index as part of its fixture
63+
# teardown, so this apparently unused dependency is what actually
64+
# causes this fixture to clear the search index.
65+
es_client, # noqa: F811
66+
):
67+
"""Empty the DB and search index before running the test.
68+
69+
h doesn't normally reset the DB or search index before each functest
70+
because doing so was taking too long, see:
71+
https://github.com/hypothesis/h/pull/6845
72+
73+
This does mean that functests need to be robust against data from previous
74+
tests still being in the DB and search index. Alternatively, a test can use
75+
@pytest.mark.usefixtures("with_clean_db_and_search_index") to request have
76+
the DB and search index be emptied before running the test.
77+
"""
6178
tables = reversed(db.Base.metadata.sorted_tables)
6279
with contextlib.closing(db_engine.connect()) as conn:
6380
tx = conn.begin()
@@ -90,10 +107,3 @@ def factories(db_session):
90107
@pytest.fixture(scope="session")
91108
def pyramid_app():
92109
return create_app(None, **TEST_SETTINGS)
93-
94-
95-
# Always unconditionally wipe the Elasticsearch index after every functional
96-
# test.
97-
@pytest.fixture(autouse=True)
98-
def always_delete_all_elasticsearch_documents():
99-
pass

0 commit comments

Comments
 (0)