diff options
Diffstat (limited to 'tests')
24 files changed, 0 insertions, 1125 deletions
diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/tests/__init__.py +++ /dev/null diff --git a/tests/conftest.py b/tests/conftest.py deleted file mode 100644 index dc92eb1..0000000 --- a/tests/conftest.py +++ /dev/null @@ -1,245 +0,0 @@ -import os -import sys -import pytest -import shutil -from pathlib import Path -from typing import Any, Callable -from pytest import MonkeyPatch -from argparse import ArgumentParser -from datetime import datetime, timezone -from importlib.metadata import version as v -from logging import Logger, getLogger, DEBUG -from copy import deepcopy - -from pyssg.arg_parser import get_parser -from pyssg.custom_logger import setup_logger -from pyssg.database_entry import DatabaseEntry -from pyssg.page import Page - - -@pytest.fixture(scope='session') -def version(): - return v('pyssg') - - -@pytest.fixture(scope='session') -def rss_date_fmt(): - return '%a, %d %b %Y %H:%M:%S GMT' - - -@pytest.fixture(scope='session') -def sitemap_date_fmt(): - return '%Y-%m-%d' - - -@pytest.fixture(scope='session') -def sample_files_path() -> str: - return f'{str(os.path.dirname(os.path.abspath(__file__)))}/sample_files' - - -@pytest.fixture(scope='session') -def config_resource() -> str: - return 'tests.sample_files.config' - - -@pytest.fixture(scope='session') -def default_yaml() -> str: - return 'default.yaml' - - -@pytest.fixture(scope='session') -def arg_parser() -> ArgumentParser: - return get_parser() - - -@pytest.fixture(scope='session') -def logger() -> Logger: - setup_logger(__name__, DEBUG) - return getLogger(__name__) - - -@pytest.fixture(scope='function') -def capture_stdout(monkeypatch: MonkeyPatch) -> dict[str, str | int]: - buffer: dict[str, str | int] = {'stdout': '', 'write_calls': 0} - - def fake_writer(s): - buffer['stdout'] += s - buffer['write_calls'] += 1 # type: ignore - - monkeypatch.setattr(sys.stdout, 'write', fake_writer) - return buffer - - -@pytest.fixture(scope='session') -def get_fmt_time() -> Callable[..., str]: - def fmt_time(fmt: str) -> str: - return datetime.now(tz=timezone.utc).strftime(fmt) - return fmt_time - - -@pytest.fixture(scope='function') -def default_config() -> dict[str, Any]: - return {'define': '$PYSSG_HOME/pyssg/site_example/', - 'title': 'Example site', - 'path': { - 'src': '/tmp/pyssg/pyssg/site_example/src', - 'dst': '/tmp/pyssg/pyssg/site_example/dst', - 'plt': '/tmp/pyssg/pyssg/site_example/plt', - 'db': '/tmp/pyssg/pyssg/site_example/.files'}, - 'url': { - 'main': 'https://example.com'}, - 'fmt': { - 'date': '%a, %b %d, %Y @ %H:%M %Z'}, - 'dirs': { - '/': { - 'cfg': { - 'plt': 'page.html', - 'tags': False, - 'index': False, - 'rss': False, - 'sitemap': False}}}} - - -@pytest.fixture(scope='function') -def root_dir_config() -> dict[str, Any]: - return {'plt': 'page.html', - 'tags': False, - 'index': False, - 'rss': False, - 'sitemap': False, - 'src': '/tmp/pyssg/pyssg/site_example/src', - 'dst': '/tmp/pyssg/pyssg/site_example/dst', - 'url': 'https://example.com'} - - -@pytest.fixture(scope='function') -def tmp_dir_structure(tmp_path: Path) -> Path: - root: Path = tmp_path/'dir_structure' - # order matters - dirs: list[Path] = [root, - root/'first', - root/'first/f1', - root/'first/f1/f2', - root/'second', - root/'second/s1'] - for i, d in enumerate(dirs): - d.mkdir() - for ext in ['txt', 'md', 'html']: - (d/f'f{i}.{ext}').write_text('sample') - return root - - -@pytest.fixture(scope='session') -def tmp_db_e1() -> DatabaseEntry: - return DatabaseEntry(('first.md', - 1671076311.823135, - 0.0, - '778bce781d95730cd1e872a10130e20d', - '-')) - - -@pytest.fixture(scope='session') -def tmp_db_e2() -> DatabaseEntry: - return DatabaseEntry(('a/second.md', - 1671077831.63301, - # 1671078892.892921, - 1677381461.8107588, - # '6092d6471d3a83135293e34ef6012939', - 'a61d0116844b6ebc02db62b4b1bf453d', - 'english,short,update')) - - -@pytest.fixture(scope='function') -def tmp_db(tmp_path: Path, - tmp_db_e1: DatabaseEntry, - tmp_db_e2: DatabaseEntry) -> Path: - root: Path = tmp_path/'db' - db_path: Path = tmp_path/'db/sample_db.psv' - root.mkdir() - e1: str = '|'.join(tmp_db_e1.get_raw_entry()) - e2: str = '|'.join(tmp_db_e2.get_raw_entry()) - db_path.write_text(f'{e1}\n{e2}\n') - return db_path - - -@pytest.fixture(scope='function') -def tmp_db_wrong_col_num(tmp_path: Path) -> Path: - root: Path = tmp_path/'db' - db_path: Path = tmp_path/'db/sample_db_wrong_col_num.psv' - root.mkdir() - # missing tags, could be anything though - db_path.write_text('name|0.0|0.0|cksm\n') - return db_path - - -@pytest.fixture(scope='function') -def tmp_src_dir(tmp_path: Path, - sample_files_path: str) -> Path: - src: Path = tmp_path/'src' - src_a: Path = src/'a' - src.mkdir() - src_a.mkdir() - src_test: str = f'{sample_files_path}/md' - - files: list[str] = ['first.md', 'new.md', 'a/second.md'] - for f in files: - shutil.copy2(f'{src_test}/{f}', f'{str(src)}/{f}') - return src - - -@pytest.fixture(scope='function') -def page_simple(default_config: dict[str, Any], - root_dir_config: dict[str, Any], - rss_date_fmt: str, - sitemap_date_fmt: str) -> Page: - # adding the fmt as it is added on the code before being passed to the page - config: dict[str, Any] = deepcopy(default_config) - config['fmt']['rss_date'] = rss_date_fmt - config['fmt']['sitemap_date'] = sitemap_date_fmt - - return Page( - name='simple.md', - ctime=1682418172.291993, - mtime=0.0, - html='<p>Simple converted md with nothing but this text.</p>', - toc='<div class="toc">\n<ul></ul>\n</div>\n', - toc_tokens=[], - meta=dict(), - config=config, - dir_config=root_dir_config - ) - - -# this is basically page_simple with extras -@pytest.fixture(scope='function') -def page_simple_modified(default_config: dict[str, Any], - root_dir_config: dict[str, Any], - rss_date_fmt: str, - sitemap_date_fmt: str) -> Page: - config: dict[str, Any] = deepcopy(default_config) - config['fmt']['rss_date'] = rss_date_fmt - config['fmt']['sitemap_date'] = sitemap_date_fmt - - dir_config: dict[str, Any] = deepcopy(root_dir_config) - dir_config['tags'] = True - tags: list[str] = ['blog', 'english', 'etc'] - - basic_meta: dict[str, Any] = { - 'title': 'Example title', - 'author': 'Single Author', - 'summary': 'Some summary.', - 'lang': 'es', - 'tags': tags - } - - return Page( - name='simple_modified.md', - ctime=1682418300.291993, - mtime=1682418350.291993, - html='<p>Simple converted md with nothing but this text, modified.</p>', - toc='<div class="toc">\n<ul></ul>\n</div>\n', - toc_tokens=[], - meta=basic_meta, - config=config, - dir_config=dir_config - ) diff --git a/tests/sample_files/__init__.py b/tests/sample_files/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/tests/sample_files/__init__.py +++ /dev/null diff --git a/tests/sample_files/checksum.txt b/tests/sample_files/checksum.txt deleted file mode 100644 index 025b879..0000000 --- a/tests/sample_files/checksum.txt +++ /dev/null @@ -1 +0,0 @@ -The content of this file is irrelevant as it is only to test the checksum function.
\ No newline at end of file diff --git a/tests/sample_files/config/__init__.py b/tests/sample_files/config/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/tests/sample_files/config/__init__.py +++ /dev/null diff --git a/tests/sample_files/config/default.yaml b/tests/sample_files/config/default.yaml deleted file mode 100644 index 08121a6..0000000 --- a/tests/sample_files/config/default.yaml +++ /dev/null @@ -1,23 +0,0 @@ -%YAML 1.2 ---- -define: &root "$PYSSG_HOME/pyssg/site_example/" - -title: "Example site" -path: - src: !join [*root, "src"] - dst: !join [*root, "dst"] - plt: !join [*root, "plt"] - db: !join [*root, ".files"] -url: - main: "https://example.com" -fmt: - date: "%a, %b %d, %Y @ %H:%M %Z" -dirs: - /: - cfg: - plt: "page.html" - tags: False - index: False - rss: False - sitemap: False -...
\ No newline at end of file diff --git a/tests/sample_files/config/default_missing_dirs.yaml b/tests/sample_files/config/default_missing_dirs.yaml deleted file mode 100644 index 03ee35a..0000000 --- a/tests/sample_files/config/default_missing_dirs.yaml +++ /dev/null @@ -1,17 +0,0 @@ -%YAML 1.2 ---- -define: &root "$PYSSG_HOME/pyssg/site_example/" - -title: "Example site" -path: - src: !join [*root, "src"] - dst: !join [*root, "dst"] - plt: !join [*root, "plt"] - db: !join [*root, ".files"] -url: - main: "https://example.com" -fmt: - date: "%a, %b %d, %Y @ %H:%M %Z" -dirs: -# test missing dirs (doesn't have any) -...
\ No newline at end of file diff --git a/tests/sample_files/config/default_missing_mandatory_key.yaml b/tests/sample_files/config/default_missing_mandatory_key.yaml deleted file mode 100644 index b5554f7..0000000 --- a/tests/sample_files/config/default_missing_mandatory_key.yaml +++ /dev/null @@ -1,24 +0,0 @@ -%YAML 1.2 ---- -define: &root "$PYSSG_HOME/pyssg/site_example/" - -# test missing mandatory key -# title: "Example site" -path: - src: !join [*root, "src"] - dst: !join [*root, "dst"] - plt: !join [*root, "plt"] - db: !join [*root, ".files"] -url: - main: "https://example.com" -fmt: - date: "%a, %b %d, %Y @ %H:%M %Z" -dirs: - /: - cfg: - plt: "page.html" - tags: False - index: False - rss: False - sitemap: False -...
\ No newline at end of file diff --git a/tests/sample_files/config/default_missing_root_dir.yaml b/tests/sample_files/config/default_missing_root_dir.yaml deleted file mode 100644 index 896e141..0000000 --- a/tests/sample_files/config/default_missing_root_dir.yaml +++ /dev/null @@ -1,18 +0,0 @@ -%YAML 1.2 ---- -define: &root "$PYSSG_HOME/pyssg/site_example/" - -title: "Example site" -path: - src: !join [*root, "src"] - dst: !join [*root, "dst"] - plt: !join [*root, "plt"] - db: !join [*root, ".files"] -url: - main: "https://example.com" -fmt: - date: "%a, %b %d, %Y @ %H:%M %Z" -dirs: -# test missing "/" dir in specific - something: -...
\ No newline at end of file diff --git a/tests/sample_files/config/multiple_default.yaml b/tests/sample_files/config/multiple_default.yaml deleted file mode 100644 index 54954b1..0000000 --- a/tests/sample_files/config/multiple_default.yaml +++ /dev/null @@ -1,45 +0,0 @@ -%YAML 1.2 ---- -define: &root "$PYSSG_HOME/pyssg/site_example/" - -title: "Example site" -path: - src: !join [*root, "src"] - dst: !join [*root, "dst"] - plt: !join [*root, "plt"] - db: !join [*root, ".files"] -url: - main: "https://example.com" -fmt: - date: "%a, %b %d, %Y @ %H:%M %Z" -dirs: - /: - cfg: - plt: "page.html" - tags: False - index: False - rss: False - sitemap: False -... ---- -define: &root "$PYSSG_HOME/pyssg/site_example/" - -title: "Example site" -path: - src: !join [*root, "src"] - dst: !join [*root, "dst"] - plt: !join [*root, "plt"] - db: !join [*root, ".files"] -url: - main: "https://example.com" -fmt: - date: "%a, %b %d, %Y @ %H:%M %Z" -dirs: - /: - cfg: - plt: "page.html" - tags: False - index: False - rss: False - sitemap: False -...
\ No newline at end of file diff --git a/tests/sample_files/config/multiple_default_one_doc_error.yaml b/tests/sample_files/config/multiple_default_one_doc_error.yaml deleted file mode 100644 index 44d9beb..0000000 --- a/tests/sample_files/config/multiple_default_one_doc_error.yaml +++ /dev/null @@ -1,39 +0,0 @@ -%YAML 1.2 ---- -define: &root "$PYSSG_HOME/pyssg/site_example/" - -title: "Example site" -path: - src: !join [*root, "src"] - dst: !join [*root, "dst"] - plt: !join [*root, "plt"] - db: !join [*root, ".files"] -url: - main: "https://example.com" -fmt: - date: "%a, %b %d, %Y @ %H:%M %Z" -dirs: - /: - cfg: - plt: "page.html" - tags: False - index: False - rss: False - sitemap: False -... ---- -define: &root "$PYSSG_HOME/pyssg/site_example/" - -title: "Example site" -path: - src: !join [*root, "src"] - dst: !join [*root, "dst"] - plt: !join [*root, "plt"] - db: !join [*root, ".files"] -url: - main: "https://example.com" -fmt: - date: "%a, %b %d, %Y @ %H:%M %Z" -dirs: - # just removing all paths as it will cause an error -...
\ No newline at end of file diff --git a/tests/sample_files/md/__init__.py b/tests/sample_files/md/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/tests/sample_files/md/__init__.py +++ /dev/null diff --git a/tests/sample_files/md/a/__init__.py b/tests/sample_files/md/a/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/tests/sample_files/md/a/__init__.py +++ /dev/null diff --git a/tests/sample_files/md/a/second.md b/tests/sample_files/md/a/second.md deleted file mode 100644 index cb4e333..0000000 --- a/tests/sample_files/md/a/second.md +++ /dev/null @@ -1,12 +0,0 @@ -title: Second blog post for testing purposes -author: David Luévano - Someone Else -lang: en -summary: This is just a post used for testing (the second). -tags: test - english - short - update - multiple-author - -A second "blog entry" for testing purposes which uses multiple authors and is inside a subdirectory. diff --git a/tests/sample_files/md/first.md b/tests/sample_files/md/first.md deleted file mode 100644 index 567ea3e..0000000 --- a/tests/sample_files/md/first.md +++ /dev/null @@ -1,10 +0,0 @@ -title: First blog post for testing purposes -author: David Luévano -lang: en -summary: This is just a post used for testing. -tags: test - english - short - update - -Even though I have this "blog" subdomain and page setup, doesn't mean I'll be blogging for pyssg, this is just to serve as an example for the types of sites that pyssg can be used for.
\ No newline at end of file diff --git a/tests/sample_files/md/new.md b/tests/sample_files/md/new.md deleted file mode 100644 index ce684a7..0000000 --- a/tests/sample_files/md/new.md +++ /dev/null @@ -1,10 +0,0 @@ -title: New file -author: David Luévano -lang: en -summary: New file used for testing the database. -tags: test - english - short - update - -This is a sample markdown file used for testing pyssg.
\ No newline at end of file diff --git a/tests/test_arg_parser.py b/tests/test_arg_parser.py deleted file mode 100644 index e233e93..0000000 --- a/tests/test_arg_parser.py +++ /dev/null @@ -1,42 +0,0 @@ -import sys -import pytest -from unittest.mock import patch -from argparse import ArgumentParser - - -# these tests don't really show any coverage, so not sure how useful they're -# but I'm including them as at least these should work -@pytest.mark.parametrize('args, arg_name, exp_result', [ - (['--version'], 'version', True), - (['-v'], 'version', True), - # config really just inputs a string - (['--config', 'value'], 'config', 'value'), - (['-c', 'value'], 'config', 'value'), - (['--copy-default-config'], 'copy_default_config', True), - (['--init'], 'init', True), - (['-i'], 'init', True), - (['--build'], 'build', True), - (['-b'], 'build', True), - (['--debug'], 'debug', True) -]) -def test_valid_args(args: list[str], - arg_name: str, - exp_result: str | bool, - arg_parser: ArgumentParser) -> None: - with patch.object(sys, 'argv', ['pyssg'] + args): - parsed_args: dict[str, str | bool] = vars(arg_parser.parse_args()) - assert parsed_args[arg_name] == exp_result - - -@pytest.mark.parametrize('args', [ - (['--something-random']), - (['-z']), - (['help']), - (['h']) -]) -def test_invalid_args(args: list[str], - arg_parser: ArgumentParser) -> None: - with pytest.raises(SystemExit) as system_exit: - arg_parser.parse_args(args) - assert system_exit.type == SystemExit - assert system_exit.value.code == 2 diff --git a/tests/test_configuration.py b/tests/test_configuration.py deleted file mode 100644 index 1b16441..0000000 --- a/tests/test_configuration.py +++ /dev/null @@ -1,96 +0,0 @@ -import pytest -from pytest import LogCaptureFixture -from typing import Any, Callable -from logging import ERROR -from pyssg.configuration import get_static_config, get_parsed_config - - -# this test is a bit sketchy, as the way the datetimes are calculated could vary -# by milliseconds or even have a difference in seconds -def test_static_config(rss_date_fmt: str, - sitemap_date_fmt: str, - get_fmt_time: Callable[..., str], - version: str) -> None: - rss_run_date: str = get_fmt_time(rss_date_fmt) - sitemap_run_date: str = get_fmt_time(sitemap_date_fmt) - sc_dict: dict[str, Any] = {'fmt': {'rss_date': rss_date_fmt, - 'sitemap_date': sitemap_date_fmt}, - 'info': {'rss_run_date': rss_run_date, - 'sitemap_run_date': sitemap_run_date, - 'version': version}} - static_config: dict[str, Any] = get_static_config() - assert static_config == sc_dict - - -def test_default_config(sample_files_path: str, - default_yaml: str, - default_config: dict[str, Any]) -> None: - yaml_path: str = f'{sample_files_path}/config/{default_yaml}' - yaml: list[dict[str, Any]] = get_parsed_config(yaml_path) - assert len(yaml) == 1 - assert yaml[0] == default_config - - -def test_default_config_mising_mandatory_key(sample_files_path: str, - caplog: LogCaptureFixture) -> None: - err: tuple[str, int, str] = ('pyssg.configuration', - ERROR, - 'config doesn\'t have "title"') - yaml_path: str = f'{sample_files_path}/config/default_missing_mandatory_key.yaml' - with pytest.raises(SystemExit) as system_exit: - get_parsed_config(yaml_path) - assert system_exit.type == SystemExit - assert system_exit.value.code == 1 - assert caplog.record_tuples[-1] == err - - -def test_default_config_mising_dirs(sample_files_path: str, - caplog: LogCaptureFixture) -> None: - err: tuple[str, int, str] = ('pyssg.configuration', - ERROR, - 'config doesn\'t have any dirs (dirs.*)') - yaml_path: str = f'{sample_files_path}/config/default_missing_dirs.yaml' - with pytest.raises(SystemExit) as system_exit: - get_parsed_config(yaml_path) - assert system_exit.type == SystemExit - assert system_exit.value.code == 1 - assert caplog.record_tuples[-1] == err - - -def test_default_config_root_dir(sample_files_path: str, - caplog: LogCaptureFixture) -> None: - err: tuple[str, int, str] = ('pyssg.configuration', - ERROR, - 'config doesn\'t have "dirs./"') - yaml_path: str = f'{sample_files_path}/config/default_missing_root_dir.yaml' - with pytest.raises(SystemExit) as system_exit: - get_parsed_config(yaml_path) - assert system_exit.type == SystemExit - assert system_exit.value.code == 1 - assert caplog.record_tuples[-1] == err - - -# this really just tests that both documents in the yaml file are read, -# both documents are the same (the default.yaml) -def test_multiple_default_config(sample_files_path: str, - default_config: dict[str, Any]) -> None: - yaml_path: str = f'{sample_files_path}/config/multiple_default.yaml' - yaml: list[dict[str, Any]] = get_parsed_config(yaml_path) - assert len(yaml) == 2 - assert yaml[0] == default_config - assert yaml[1] == default_config - - -# also, this just tests that the checks for a well formed config file are -# processed for all documents -def test_multiple_default_config_one_doc_error(sample_files_path: str, - caplog: LogCaptureFixture) -> None: - err: tuple[str, int, str] = ('pyssg.configuration', - ERROR, - 'config doesn\'t have any dirs (dirs.*)') - yaml_path: str = f'{sample_files_path}/config/multiple_default_one_doc_error.yaml' - with pytest.raises(SystemExit) as system_exit: - get_parsed_config(yaml_path) - assert system_exit.type == SystemExit - assert system_exit.value.code == 1 - assert caplog.record_tuples[-1] == err diff --git a/tests/test_custom_logger.py b/tests/test_custom_logger.py deleted file mode 100644 index 9102f52..0000000 --- a/tests/test_custom_logger.py +++ /dev/null @@ -1,19 +0,0 @@ -import pytest -from logging import Logger, DEBUG, INFO, WARNING, ERROR, CRITICAL - - -@pytest.mark.parametrize('log_level, starts_with, message', [ - (DEBUG, '[DEBUG]', 'first message'), - (INFO, 'second message', 'second message'), - (WARNING, '\x1b[33m[WARNING]', 'third message'), - (ERROR, '\x1b[31m[ERROR]', 'fourth message'), - (CRITICAL, '\x1b[31;1m[CRITICAL]', 'fifth message'), -]) -def test_log_levels(log_level: int, - starts_with: str, - message: str, - logger: Logger, - capture_stdout: dict[str, str | int]) -> None: - logger.log(log_level, message) - assert str(capture_stdout['stdout']).startswith(starts_with) - assert message in str(capture_stdout['stdout']) diff --git a/tests/test_database.py b/tests/test_database.py deleted file mode 100644 index c5957e4..0000000 --- a/tests/test_database.py +++ /dev/null @@ -1,189 +0,0 @@ -from pathlib import Path -import pytest -from logging import DEBUG, WARNING, ERROR, CRITICAL -from pytest import LogCaptureFixture -from pyssg.database import Database -from pyssg.database_entry import DatabaseEntry - - -def test_read_database_no_db(sample_files_path: str, - caplog: LogCaptureFixture) -> None: - path: str = f'{sample_files_path}/non_existent_db.psv' - war: tuple[str, int, str] = ('pyssg.database', - WARNING, - f'"{path}" doesn\'t exist, will be created ' - 'once process finishes, ignore if it\'s the ' - 'first run') - db: Database = Database(path) - db.read() - assert caplog.record_tuples[-1] == war - - -def test_read_database_not_a_file(sample_files_path: str, - caplog: LogCaptureFixture) -> None: - path: str = sample_files_path - err: tuple[str, int, str] = ('pyssg.database', - ERROR, - f'"{path}" is not a file') - db: Database = Database(path) - with pytest.raises(SystemExit) as system_exit: - db.read() - assert system_exit.type == SystemExit - assert system_exit.value.code == 1 - assert caplog.record_tuples[-1] == err - - -def test_read_database(tmp_db: Path, - tmp_db_e1: DatabaseEntry, - tmp_db_e2: DatabaseEntry) -> None: - db: Database = Database(str(tmp_db)) - db.read() - exp_db_e: dict[str, DatabaseEntry] = {tmp_db_e1.fname: tmp_db_e1, - tmp_db_e2.fname: tmp_db_e2} - for fname in db.e.keys(): - assert str(db.e[fname]) == str(exp_db_e[fname]) - - -def test_read_database_wrong_col_num(tmp_db_wrong_col_num: Path, - caplog: LogCaptureFixture) -> None: - cri: tuple[str, int, str] = ('pyssg.database', - CRITICAL, - 'row 1 doesn\'t contain 5 columns, ' - 'contains 4 columns: ' - '"[\'name\', \'0.0\', \'0.0\', \'cksm\']"') - db: Database = Database(str(tmp_db_wrong_col_num)) - with pytest.raises(SystemExit) as system_exit: - db.read() - assert system_exit.type == SystemExit - assert system_exit.value.code == 1 - assert caplog.record_tuples[-1] == cri - - -def test_update_entry_tags(tmp_db: Path, - tmp_db_e1: DatabaseEntry, - caplog: LogCaptureFixture) -> None: - caplog.set_level(DEBUG, logger='pyssg.database') - fname: str = tmp_db_e1.fname - new_tags: set[str] = {'tag1', 'tag2', 'tag3'} - deb: tuple[str, int, str] = ('pyssg.database', - DEBUG, - f'entry "{fname}" new tags: {new_tags}') - db: Database = Database(str(tmp_db)) - db.read() - db.update_tags(fname, new_tags) - assert db.e[fname].tags == new_tags - assert caplog.record_tuples[-1] == deb - - -def test_update_entry_tags_failure(tmp_db: Path, - caplog: LogCaptureFixture) -> None: - fname: str = 'non_existent_file.md' - new_tags: set[str] = {'tag1', 'tag2', 'tag3'} - err: tuple[str, int, str] = ('pyssg.database', - ERROR, - f'can\'t update tags for entry "{fname}",' - ' as it is not present in db') - db: Database = Database(str(tmp_db)) - db.read() - with pytest.raises(SystemExit) as system_exit: - db.update_tags(fname, new_tags) - assert system_exit.type == SystemExit - assert system_exit.value.code == 1 - assert caplog.record_tuples[-1] == err - - -def test_update_entry_new_entry_full_path(tmp_db: Path, - tmp_src_dir: Path, - caplog: LogCaptureFixture) -> None: - caplog.set_level(DEBUG, logger='pyssg.database') - fname: str = f'{tmp_src_dir}/new.md' - deb: tuple[str, int, str] = ('pyssg.database', - DEBUG, - f'entry "{fname}" didn\'t exist, adding with' - ' defaults') - db: Database = Database(str(tmp_db)) - db.read() - db.update(fname) - assert caplog.record_tuples[-1] == deb - - -def test_update_entry_new_entry_fname_only(tmp_db: Path, - tmp_src_dir: Path, - caplog: LogCaptureFixture) -> None: - caplog.set_level(DEBUG, logger='pyssg.database') - fname: str = f'{tmp_src_dir}/new.md' - deb: tuple[str, int, str] = ('pyssg.database', - DEBUG, - 'entry "new.md" didn\'t exist, adding with' - ' defaults') - db: Database = Database(str(tmp_db)) - db.read() - db.update(fname, f'{tmp_src_dir}/') - assert caplog.record_tuples[-1] == deb - - -def test_update_entry_no_mod(tmp_db: Path, - tmp_src_dir: Path, - caplog: LogCaptureFixture) -> None: - caplog.set_level(DEBUG, logger='pyssg.database') - fname: str = f'{tmp_src_dir}/first.md' - deb: tuple[str, int, str] = ('pyssg.database', - DEBUG, - 'entry "first.md" hasn\'t been modified') - db: Database = Database(str(tmp_db)) - db.read() - db.update(fname, f'{tmp_src_dir}/') - assert caplog.record_tuples[-1] == deb - - -def test_update_entry_modified(tmp_db: Path, - tmp_src_dir: Path, - caplog: LogCaptureFixture) -> None: - caplog.set_level(DEBUG, logger='pyssg.database') - fname: str = f'{tmp_src_dir}/a/second.md' - with open(fname, 'a') as f: - f.write('Added modification.\n') - deb: tuple[str, int, str] = ('pyssg.database', - DEBUG, - 'entry "a/second.md" new content: ') - db: Database = Database(str(tmp_db)) - db.read() - db.update(fname, f'{tmp_src_dir}/') - # instead of checking the whole deb tuple, check that the message starts - # with the "new content", as getting the same timestamp will be difficult - assert caplog.record_tuples[-1][1] == DEBUG - assert caplog.record_tuples[-1][2].startswith(deb[2]) - - -def test_write_database_no_change(tmp_db: Path, - tmp_db_e1: DatabaseEntry, - tmp_db_e2: DatabaseEntry) -> None: - db: Database = Database(str(tmp_db)) - db.read() - db.write() - exp_db_e: dict[str, DatabaseEntry] = {tmp_db_e1.fname: tmp_db_e1, - tmp_db_e2.fname: tmp_db_e2} - db2: Database = Database(str(tmp_db)) - db2.read() - for fname in db2.e.keys(): - assert str(db2.e[fname]) == str(exp_db_e[fname]) - - -def test_write_database_new_entry(tmp_db: Path, - tmp_src_dir: Path, - tmp_db_e1: DatabaseEntry, - tmp_db_e2: DatabaseEntry) -> None: - fname: str = 'new.md' - full_path: str = f'{tmp_src_dir}/{fname}' - db: Database = Database(str(tmp_db)) - db.read() - db.update(full_path, f'{tmp_src_dir}/') - db_e2: DatabaseEntry = db.e[fname] - db.write() - exp_db_e: dict[str, DatabaseEntry] = {tmp_db_e1.fname: tmp_db_e1, - tmp_db_e2.fname: tmp_db_e2, - fname: db_e2} - db2: Database = Database(str(tmp_db)) - db2.read() - for fname in db2.e.keys(): - assert str(db2.e[fname]) == str(exp_db_e[fname]) diff --git a/tests/test_database_entry.py b/tests/test_database_entry.py deleted file mode 100644 index 1320577..0000000 --- a/tests/test_database_entry.py +++ /dev/null @@ -1,81 +0,0 @@ -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_page.py b/tests/test_page.py deleted file mode 100644 index 0ffaff8..0000000 --- a/tests/test_page.py +++ /dev/null @@ -1,62 +0,0 @@ -from copy import deepcopy -from logging import WARNING -from typing import Any -from pytest import LogCaptureFixture -from pyssg.page import Page - - -# TODO: this probably needs more testing, but I'm doing the coverage for now - - -def test_page_basic(page_simple: Page) -> None: - page_simple.parse_metadata() - assert page_simple.title == '' - assert page_simple.author == [''] - assert page_simple.summary == '' - assert page_simple.lang == 'en' - assert page_simple.url == f"{page_simple.dir_config['url']}/{page_simple.name.replace('.md', '.html')}" - - -def test_page_no_mdate(page_simple: Page, - caplog: LogCaptureFixture) -> None: - page_simple.parse_metadata() - war: tuple[str, int, str] = ('pyssg.page', - WARNING, - 'no mdatetime found, can\'t return a formatted string') - assert page_simple.mdate('date') == '' - assert caplog.record_tuples[-1] == war - - -def test_page_no_fmt(page_simple: Page, - caplog: LogCaptureFixture) -> None: - page_simple.parse_metadata() - war: tuple[str, int, str] = ('pyssg.page', - WARNING, - 'format "something" not found in config, ' - 'returning empty string') - assert page_simple.cdate('something') == '' - assert caplog.record_tuples[-1] == war - - -def test_page_comparison(page_simple: Page, - page_simple_modified: Page) -> None: - assert not page_simple > page_simple_modified - assert page_simple < page_simple_modified - assert page_simple != page_simple_modified - - -def test_page_modified(page_simple_modified: Page) -> None: - page_simple_modified.parse_metadata() - meta: dict[str, Any] = deepcopy(page_simple_modified.meta) - assert page_simple_modified.title == meta['title'] - assert page_simple_modified.author == list(meta['author']) - assert page_simple_modified.summary == meta['summary'] - assert page_simple_modified.lang == meta['lang'] - assert page_simple_modified.url == f"{page_simple_modified.dir_config['url']}/{page_simple_modified.name.replace('.md', '.html')}" - - -def test_page_modified_no_tags(page_simple_modified: Page) -> None: - meta: dict[str, Any] = deepcopy(page_simple_modified.meta) - meta['tags'] = [] - page_simple_modified.meta = meta - page_simple_modified.parse_metadata() diff --git a/tests/test_utils.py b/tests/test_utils.py deleted file mode 100644 index 75b79c2..0000000 --- a/tests/test_utils.py +++ /dev/null @@ -1,169 +0,0 @@ -import pytest -from pytest import LogCaptureFixture -from pathlib import Path -from logging import INFO -from pyssg.utils import (get_expanded_path, get_checksum, copy_file, create_dir, - get_dir_structure, get_file_list) - - -# $PYSSG_HOME is the only env var set -# in the project settings that resemble a path -@pytest.mark.parametrize('path, expected_expanded', [ - ('$PYSSG_HOME', '/tmp/pyssg'), - ('$PYSSG_HOME/', '/tmp/pyssg'), - ('/test$PYSSG_HOME/', '/test/tmp/pyssg'), - ('/test/$PYSSG_HOME/', '/test/tmp/pyssg'), - ('/test/$PYSSG_HOME/test', '/test/tmp/pyssg/test') -]) -def test_path_expansion(path: str, expected_expanded: str) -> None: - expanded: str = get_expanded_path(path) - assert expanded == expected_expanded - - -@pytest.mark.parametrize('path', [ - ('$'), - ('$NON_EXISTENT_VARIABLE'), - ('/path/to/something/$'), - ('/path/to/something/$NON_EXISTENT_VARIABLE') -]) -def test_path_expansion_failure(path: str) -> None: - with pytest.raises(SystemExit) as system_exit: - get_expanded_path(path) - assert system_exit.type == SystemExit - assert system_exit.value.code == 1 - - -def test_checksum(sample_files_path: str) -> None: - path: str = f'{sample_files_path}/checksum.txt' - simple_yaml_checksum: str = '437b5a0e20d32fc14944c1c00d066303' - checksum: str = get_checksum(path) - assert checksum == simple_yaml_checksum - - -# TODO: actually check the existence of the files and not just the log -def test_copy_file(tmp_path: Path, caplog: LogCaptureFixture) -> None: - src: Path = tmp_path/'src' - dst: Path = tmp_path/'dst' - src.mkdir() - dst.mkdir() - src_file: Path = src/'tmp_file.txt' - dst_file: Path = dst/'tmp_file.txt' - src_file.write_text('something') - inf: tuple[str, int, str] = ('pyssg.utils', - INFO, - f'copied file "{src_file}" to "{dst_file}"') - copy_file(str(src_file), str(dst_file)) - assert caplog.record_tuples[-1] == inf - - -# TODO: actually check the existence of the files and not just the log -def test_copy_file_already_exists(tmp_path: Path, - caplog: LogCaptureFixture) -> None: - src: Path = tmp_path/'src' - dst: Path = tmp_path/'dst' - src.mkdir() - dst.mkdir() - src_file: Path = src/'tmp_file.txt' - dst_file: Path = dst/'tmp_file.txt' - src_file.write_text('something') - dst_file.write_text('something') - inf: tuple[str, int, str] = ('pyssg.utils', - INFO, - f'file "{dst_file}" already exists, ignoring') - copy_file(str(src_file), str(dst_file)) - assert caplog.record_tuples[-1] == inf - - -def test_create_dir(tmp_path: Path, caplog: LogCaptureFixture) -> None: - path: Path = tmp_path/'new_dir' - inf: tuple[str, int, str] = ('pyssg.utils', - INFO, - f'created directory "{path}"') - assert path.exists() is False - create_dir(str(path), False, False) - assert path.exists() is True - assert caplog.record_tuples[-1] == inf - - -# TODO: actually check the existence of the files and not just the log -def test_create_dir_already_exists(tmp_path: Path, - caplog: LogCaptureFixture) -> None: - path: Path = tmp_path/'new_dir' - inf: tuple[str, int, str] = ('pyssg.utils', - INFO, - f'directory "{path}" exists, ignoring') - path.mkdir() - create_dir(str(path), False, False) - assert caplog.record_tuples[-1] == inf - - -def test_create_dirs(tmp_path: Path, caplog: LogCaptureFixture) -> None: - path: Path = tmp_path/'new_dir' - sub_path: Path = path/'sub_dir' - inf: tuple[str, int, str] = ('pyssg.utils', - INFO, - f'created directory "{sub_path}"') - assert path.exists() is False - assert sub_path.exists() is False - create_dir(str(sub_path), True, False) - assert path.exists() is True - assert sub_path.exists() is True - assert caplog.record_tuples[-1] == inf - - -# TODO: actually check the existence of the files and not just the log -def test_create_dirs_already_exists(tmp_path: Path, - caplog: LogCaptureFixture) -> None: - path: Path = tmp_path/'new_dir' - sub_path: Path = path/'sub_dir' - inf: tuple[str, int, str] = ('pyssg.utils', - INFO, - f'directory "{sub_path}" exists, ignoring') - path.mkdir() - sub_path.mkdir() - create_dir(str(sub_path), True, False) - assert caplog.record_tuples[-1] == inf - - -@pytest.mark.parametrize('exclude, exp_dir_str', [ - ([], ['second/s1', 'first/f1/f2']), - (['f2'], ['second/s1', 'first/f1']), - (['f1'], ['second/s1', 'first']), - (['second'], ['first/f1/f2']), - (['s1', 'f2'], ['second', 'first/f1']), - (['s1', 'f1'], ['second', 'first']), - (['s1', 'first'], ['second']) -]) -def test_dir_structure(tmp_dir_structure: Path, - exclude: list[str], - exp_dir_str: list[str]) -> None: - dir_str: list[str] = get_dir_structure(str(tmp_dir_structure), exclude) - # order doesn't matter, only for checking that both lists contain the same - assert sorted(dir_str) == sorted(exp_dir_str) - - -@pytest.mark.parametrize('exts, exclude_dirs, exp_flist', [ - (('txt',), [], ['f0.txt', 'second/f4.txt', - 'second/s1/f5.txt', 'first/f1.txt', - 'first/f1/f2.txt', 'first/f1/f2/f3.txt']), - (('txt', 'html'), [], ['f0.html', 'f0.txt', - 'second/f4.txt', 'second/f4.html', - 'second/s1/f5.html', 'second/s1/f5.txt', - 'first/f1.html', 'first/f1.txt', - 'first/f1/f2.txt', 'first/f1/f2.html', - 'first/f1/f2/f3.txt', 'first/f1/f2/f3.html']), - (('md',), [], ['f0.md', 'second/f4.md', - 'second/s1/f5.md', 'first/f1.md', - 'first/f1/f2.md', 'first/f1/f2/f3.md']), - (('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',]) -]) -def test_file_list(tmp_dir_structure: Path, - exts: tuple[str], - exclude_dirs: list[str], - exp_flist: list[str]) -> None: - flist: list[str] = get_file_list(str(tmp_dir_structure), exts, exclude_dirs) - # order doesn't matter, only for checking that both lists contain the same - assert sorted(flist) == sorted(exp_flist) diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py deleted file mode 100644 index 0d8df96..0000000 --- a/tests/test_yaml_parser.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import Any -from pyssg.yaml_parser import get_parsed_yaml - -# the point of these tests is just to read yaml files -# and test the join functionality - - -def test_yaml_resource_read(default_yaml: str, config_resource: str) -> None: - yaml: list[dict[str, Any]] = get_parsed_yaml(default_yaml, config_resource) - assert len(yaml) == 1 - - -def test_yaml_path_read(sample_files_path: str, default_yaml: str) -> None: - yaml_path: str = f'{sample_files_path}/config/{default_yaml}' - yaml: list[dict[str, Any]] = get_parsed_yaml(yaml_path) - assert len(yaml) == 1 - - -def test_yaml_join(default_yaml: str, config_resource: str) -> None: - yaml: dict[str, Any] = get_parsed_yaml(default_yaml, config_resource)[0] - define_str: str = '$PYSSG_HOME/pyssg/site_example/' - assert yaml['define'] == define_str - assert yaml['path']['src'] == f'{define_str}src' |