mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
feat(db): document and test Database.memory and Database.memory_name
Refs #590
This commit is contained in:
parent
8d74ffc932
commit
36a34e95bf
2 changed files with 39 additions and 0 deletions
|
|
@ -105,6 +105,14 @@ You can also create a named in-memory database. Unlike regular memory databases
|
|||
|
||||
db = Database(memory_name="my_shared_database")
|
||||
|
||||
After creating a ``Database`` you can use ``db.memory`` and ``db.memory_name`` to tell whether it is backed by an in-memory database and to read the shared cache name. ``db.memory`` is ``True`` for any in-memory database and ``db.memory_name`` holds the name passed to ``memory_name=``, or ``None`` otherwise.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
db = Database(memory_name="shared")
|
||||
db.memory # True
|
||||
db.memory_name # "shared"
|
||||
|
||||
Connections use ``PRAGMA recursive_triggers=on`` by default. If you don't want to use `recursive triggers <https://www.sqlite.org/pragma.html#pragma_recursive_triggers>`__ you can turn them off using:
|
||||
|
||||
.. code-block:: python
|
||||
|
|
|
|||
|
|
@ -39,3 +39,34 @@ def test_database_close(tmpdir, memory):
|
|||
db.close()
|
||||
with pytest.raises(sqlite3.ProgrammingError):
|
||||
db.execute("select 1 + 1")
|
||||
|
||||
|
||||
def test_memory_attribute_for_memory_true():
|
||||
db = Database(memory=True)
|
||||
assert db.memory is True
|
||||
assert db.memory_name is None
|
||||
|
||||
|
||||
def test_memory_attribute_for_memory_name():
|
||||
db = Database(memory_name="shared_attr")
|
||||
assert db.memory is True
|
||||
assert db.memory_name == "shared_attr"
|
||||
|
||||
|
||||
def test_memory_attribute_for_memory_string_path():
|
||||
db = Database(":memory:")
|
||||
assert db.memory is True
|
||||
assert db.memory_name is None
|
||||
|
||||
|
||||
def test_memory_attribute_for_file_path(tmpdir):
|
||||
db = Database(str(tmpdir / "file.db"))
|
||||
assert db.memory is False
|
||||
assert db.memory_name is None
|
||||
|
||||
|
||||
def test_memory_attribute_for_existing_connection():
|
||||
conn = sqlite3.connect(":memory:")
|
||||
db = Database(conn)
|
||||
assert db.memory is False
|
||||
assert db.memory_name is None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue