diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2023-02-24 21:37:37 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2023-02-24 21:37:37 -0600 |
commit | eae1a3a5f602e9c79e07f4b6c1b133dfd12e1d5c (patch) | |
tree | 5ac9e4e20524b83b2020f985ca415ddbb341f097 /tests | |
parent | a609b1cb2b43fd17e03efa62314f679b47ae6cb5 (diff) |
add database_entry tests, change type for tags attr
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_database_entry.py | 81 | ||||
-rw-r--r-- | tests/test_utils.py | 2 |
2 files changed, 82 insertions, 1 deletions
diff --git a/tests/test_database_entry.py b/tests/test_database_entry.py new file mode 100644 index 0000000..1320577 --- /dev/null +++ b/tests/test_database_entry.py @@ -0,0 +1,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 diff --git a/tests/test_utils.py b/tests/test_utils.py index b7c9754..86242c2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -151,7 +151,7 @@ def test_dir_structure(tmp_dir_structure: Path, (('md',), ['first'], ['f0.md', 'second/f4.md', 'second/s1/f5.md']), (('md',), ['first', 's1'], ['f0.md', 'second/f4.md']), (('md',), ['f2', 's1'], ['f0.md', 'second/f4.md', - 'first/f1.md', 'first/f1/f2.md',]), + 'first/f1.md', 'first/f1/f2.md',]) ]) def test_file_list(tmp_dir_structure: Path, exts: tuple[str], |