Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Optionally use an on-disk sqlite db in tests #11702

Merged
merged 7 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11702.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the option to write sqlite test dbs to disk when running tests.
11 changes: 10 additions & 1 deletion tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import hashlib
import json
import logging
import os
import time
import uuid
import warnings
Expand Down Expand Up @@ -71,6 +72,7 @@
POSTGRES_HOST,
POSTGRES_PASSWORD,
POSTGRES_USER,
SQLITE_TEST_DB_LOCATION,
USE_POSTGRES_FOR_TESTS,
MockClock,
default_config,
Expand Down Expand Up @@ -739,9 +741,16 @@ def setup_test_homeserver(
},
}
else:
if SQLITE_TEST_DB_LOCATION != ":memory:":
# nuke the DB on disk
try:
os.remove(SQLITE_TEST_DB_LOCATION)
except FileNotFoundError:
pass
clokep marked this conversation as resolved.
Show resolved Hide resolved

database_config = {
"name": "sqlite3",
"args": {"database": ":memory:", "cp_min": 1, "cp_max": 1},
"args": {"database": SQLITE_TEST_DB_LOCATION, "cp_min": 1, "cp_max": 1},
}

if "db_txn_limit" in kwargs:
Expand Down
4 changes: 4 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
POSTGRES_PASSWORD = os.environ.get("SYNAPSE_POSTGRES_PASSWORD", None)
POSTGRES_BASE_DB = "_synapse_unit_tests_base_%s" % (os.getpid(),)

# When debugging a specific test, it's occasionally useful to write the
# DB to /tmp and query it with the sqlite CLI.
SQLITE_TEST_DB_LOCATION = os.environ.get("SYNAPSE_TEST_SQLITE_DB_LOCATION", ":memory:")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I debated if this should default to None so we could encapsulate the magic ":memory:" string in a single spot, what do you think?


# the dbname we will connect to in order to create the base database.
POSTGRES_DBNAME_FOR_INITIAL_CREATE = "postgres"

Expand Down