summaryrefslogtreecommitdiff
path: root/tests/test_database_entry.py
blob: 1320577dc9972c0399c682c06958f35b93b5b1b1 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import pytest
from typing import Any
from logging import ERROR
from pytest import LogCaptureFixture
from pyssg.database_entry import DatabaseEntry


@pytest.mark.parametrize('entry, exp_str', [
    (('t', 0.0, 0.0, '1', set()), "['t', 0.0, 0.0, '1', []]"),
    (('t', 0, 1, '1', set()), "['t', 0.0, 1.0, '1', []]"),
    (('t', 0.0, 0.0, '1', '-'), "['t', 0.0, 0.0, '1', []]"),
    (('t', 0.0, 0.0, 1, '-'), "['t', 0.0, 0.0, '1', []]"),
    (('t', 0.0, 0.0, '1', {'-', 'tag'}), "['t', 0.0, 0.0, '1', ['tag']]"),
    (('t', 0.0, 0.0, '1', '-,tag'), "['t', 0.0, 0.0, '1', ['tag']]"),
    (('t', 0.0, 0.0, '1', 'tag,-,-'), "['t', 0.0, 0.0, '1', ['tag']]"),
    (('t', 0.0, 0.0, '1', 'tag1,tag2'), "['t', 0.0, 0.0, '1', ['tag1', 'tag2']]"),
    (('t', 0.0, 0.0, '1', {'tag1', 'tag2'}), "['t', 0.0, 0.0, '1', ['tag1', 'tag2']]"),
    (('t', 0.0, 0.0, '1', ' tag1 , tag2,tag3'), "['t', 0.0, 0.0, '1', ['tag1', 'tag2', 'tag3']]"),
    (('t', 0.0, 0.0, '1', 'tag3,tag2,tag1'), "['t', 0.0, 0.0, '1', ['tag1', 'tag2', 'tag3']]"),
    (('t', 0.0, 0.0, '1', 'tag2,tag3,tag1'), "['t', 0.0, 0.0, '1', ['tag1', 'tag2', 'tag3']]")
])
def test_db_entry_obj(entry: tuple[str, float, float, str, str | set[str]],
                      exp_str: str) -> None:
    db_entry: DatabaseEntry = DatabaseEntry(entry)
    assert str(db_entry) == exp_str


@pytest.mark.parametrize('entry, exp_str', [
    (('t', 0.0, 0.0, '1', set()), ['t', '0.0', '0.0', '1', '-']),
    (('t', 0, 1, '1', set()), ['t', '0.0', '1.0', '1', '-']),
    (('t', 0.0, 0.0, '1', '-'), ['t', '0.0', '0.0', '1', '-']),
    (('t', 0.0, 0.0, 1, '-'), ['t', '0.0', '0.0', '1', '-']),
    (('t', 0.0, 0.0, '1', '-,tag'), ['t', '0.0', '0.0', '1', 'tag']),
    (('t', 0.0, 0.0, '1', {'-', 'tag'}), ['t', '0.0', '0.0', '1', 'tag']),
    (('t', 0.0, 0.0, '1', 'tag,-,-'), ['t', '0.0', '0.0', '1', 'tag']),
    (('t', 0.0, 0.0, '1', 'tag1,tag2'), ['t', '0.0', '0.0', '1', 'tag1,tag2']),
    (('t', 0.0, 0.0, '1', {'tag1', 'tag2'}), ['t', '0.0', '0.0', '1', 'tag1,tag2']),
    (('t', 0.0, 0.0, '1', ' tag1 , tag2,tag3'), ['t', '0.0', '0.0', '1', 'tag1,tag2,tag3']),
    (('t', 0.0, 0.0, '1', 'tag3,tag2,tag1'), ['t', '0.0', '0.0', '1', 'tag1,tag2,tag3']),
    (('t', 0.0, 0.0, '1', 'tag2,tag3,tag1'), ['t', '0.0', '0.0', '1', 'tag1,tag2,tag3'])
])
def test_db_entry_get_raw(entry: tuple[str, float, float, str, str | set[str]],
                          exp_str: list[str]) -> None:
    db_entry: DatabaseEntry = DatabaseEntry(entry)
    db_entry_raw: list[str] = db_entry.get_raw_entry()
    assert db_entry_raw == exp_str


# not sure if this is enough to test tag updating,
#   it's a bit redundant as the set functionality is doing all the work
@pytest.mark.parametrize('new_tags', [
    ({'tag'}),
    ({'tag1', 'tag2'}),
    ({'tag1', 'tag2', 'tag3'}),
    ({'-'}),
    ({'-', '-'}),
    (set()),
    ({'-', '-'}),
])
def test_db_entry_update_tags(new_tags: set[str]) -> None:
    db_entry: DatabaseEntry = DatabaseEntry(('t', 0.0, 0.0, '1', {'just', 'something'}))
    db_entry.update_tags(new_tags)
    assert db_entry.tags == new_tags


# just a few random tests for things that are not str or set
@pytest.mark.parametrize('tags', [
    ({}),
    (tuple()),
    (1),
    (1.0),
])
def test_db_entry_bad_tags(tags: Any, caplog: LogCaptureFixture) -> None:
    err: tuple[str, int, str] = ('pyssg.database_entry',
                                 ERROR,
                                 'tags has to be either a set or string (comma separated)')
    with pytest.raises(SystemExit) as system_exit:
        DatabaseEntry(('t', 0.0, 0.0, '1', tags))
    assert system_exit.type == SystemExit
    assert system_exit.value.code == 1
    assert caplog.record_tuples[-1] == err