"""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