about summary refs log tree commit diff
path: root/tests/unit/test_sqlite3.py
blob: 343f1e5d25079d3d2bfdf4aed343b3d1654f68d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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