aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_db.py
blob: d1b269abb695f25cd816a1607919514b2f25b4da (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
import pytest

from paste import db


@pytest.fixture
def unmigrated_conn():
    with db.connect(":memory:", migrations=None) as conn:
        yield conn


@pytest.fixture
def conn():
    with db.connect(":memory:") as conn:
        yield conn


def test_connect_succeeds(conn):
    _ = conn
    pass


def test_migration_succeeds(unmigrated_conn):
    conn = unmigrated_conn
    assert db.get_version(conn) == 0
    (schema_count,) = conn.execute("SELECT COUNT(*) FROM sqlite_schema").fetchone()
    assert schema_count == 0
    db.migrate(conn, db.migrations)
    assert db.get_version(conn) == 1
    (schema_count,) = conn.execute("SELECT COUNT(*) FROM sqlite_schema").fetchone()
    assert schema_count == 7


hash_testdata = [
    ("", "", True),
    (b"", b"", True),
    ("a", "a", True),
    (b"a", b"a", True),
    ("a", "", False),
    ("a", "A", False),
    ("a", "b", False),
    ("a", "aa", False),
    (b"a", "a", True),
    (b"a", "A", False),
]


@pytest.mark.parametrize("a,b,expected", hash_testdata)
def test_hash_behaves_normally(unmigrated_conn, a, b, expected):
    (res,) = unmigrated_conn.execute(
        "SELECT DATA_HASH(?) == DATA_HASH(?)", (a, b)
    ).fetchone()
    assert res == (1 if expected else 0)