about summary refs log tree commit diff
path: root/tests/unit/test_sqlite3.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi <fredmanglis@gmail.com>2026-10-28 15:06:07 -0500
committerFrederick Muriuki Muriithi <fredmanglis@gmail.com>2026-10-28 15:06:07 -0500
commitcf930d1335d894b468e8e0d8f600a50515ecee36 (patch)
treeaa9cf89a1ff7717a2169af7f3204b73db24061e0 /tests/unit/test_sqlite3.py
parent6c67a0ad5bb5fe18722556a2824921c9e6a28e87 (diff)
downloadgn-libs-cf930d1335d894b468e8e0d8f600a50515ecee36.tar.gz
Enable passing PRAGMA directives on connection string.
Enable passing of PRAGMA directives on the connection string to enable
setting different directives for different environments.
Diffstat (limited to 'tests/unit/test_sqlite3.py')
-rw-r--r--tests/unit/test_sqlite3.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/unit/test_sqlite3.py b/tests/unit/test_sqlite3.py
new file mode 100644
index 0000000..343f1e5
--- /dev/null
+++ b/tests/unit/test_sqlite3.py
@@ -0,0 +1,56 @@
+"""Test functions used with SQLite3 databases"""
+import pytest
+
+from gn_libs.sqlite3 import _process_query, _process_connection_string
+
+
+@pytest.mark.unit_test
+@pytest.mark.parametrize(
+    "query,expected",
+    (("", ({}, {}, {})),
+     ("cache=on",
+      ({"cache": "on"}, {}, {})),
+     ("cache=on&pragma_foreign_keys=on",
+      ({"cache": "on"}, {"foreign_keys": "on"}, {})),
+     ("cache=on&pragma_foreign_keys=on&unknown=1",
+      ({"cache": "on"}, {"foreign_keys": "on"}, {"unknown": "1"}))))
+def test__process_query_splits_query_parameters_correctly(query, expected):
+    """Test that _process_query splits the query string parameters correctly.
+
+    GIVEN: the query part of a valid url string,
+    WHEN: _process_query(...) is called with the query string
+    THEN: ensure the result is 3 mappings, VFS options, PRAGMA directives and
+          unknown options."""
+    assert _process_query(query) == expected
+
+
+@pytest.mark.unit_test
+@pytest.mark.parametrize(
+    "emptystr", ("", " ", "   "))
+def test__process_connection_string_raises_with_empty_string(emptystr):
+    """Test that _process_connection_string does not accept empty connection strings.
+
+    GIVEN: An empty string for a connection string
+    WHEN: that is passed to _process_connection_string
+    THEN: a ValueError is raised"""
+    with pytest.raises(ValueError):
+        _process_connection_string(emptystr)
+
+
+@pytest.mark.unit_test
+@pytest.mark.parametrize(
+    "configstr,expected",
+    ((":memory:", ("file::memory:", {})),
+     (":memory:?cache=private", ("file::memory:?cache=private", {})),
+     (":memory:?cache=private&pragma_temp_store=memory",
+      ("file::memory:?cache=private", {"temp_store": "memory"})),
+     ("/home/user/datastore.db?cache=private&pragma_temp_store=memory",
+      ("file:///home/user/datastore.db?cache=private", {"temp_store": "memory"}))))
+def test__process_connection_string_returns_correct_values(configstr, expected):
+    """Test that _process_connection_string parses the connection strings correctly.
+
+    GIVEN: A connection string to an SQLite3 database,
+    WHEN: _process_connection_string(...) is called with the connection string,
+    THEN: Ensure the connection string returned is valid, and the PRAGMA
+          directives are extracted ."""
+    assert _process_connection_string(configstr) == expected