tenkan/tests/files_test.py
2022-01-12 22:00:37 +01:00

71 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
from datetime import datetime, timezone
from pathlib import Path
from tenkan.files import (
_rebuild_atom_file,
delete_folder,
path_exists,
write_article,
)
data: dict = {
'title': 'bla',
'url': 'bla',
'fetched_content': 'bla',
'last_update': None,
'gmi_url': 'bla',
'articles': [],
}
article_data = {
'article_title': 'article_title',
'article_formatted_title': 'article_formatted_title',
'article_content': {'summary': 'article_content'},
'article_date': datetime(2022, 1, 7, 15, 25, 0, tzinfo=timezone.utc),
'http_url': 'article_link',
'updated': 'Fri, 07 Jan 2022 15:25:00 +0000',
'updated_parsed': datetime(
2022, 1, 7, 15, 25, 0, tzinfo=timezone.utc
).timetuple(),
}
def test_path_exists(tmp_path):
d = tmp_path / 'sub'
d.mkdir()
assert path_exists(d) is True
def test_path_doesnt_exist(tmp_path):
d = tmp_path / 'sub'
assert path_exists(d) is False
def test_article_written(tmp_path):
path = tmp_path / 'sub'
path.mkdir()
date = article_data['article_date']
file_date = date.strftime('%Y-%m-%d_%H-%M-%S')
file_title = article_data['article_formatted_title']
res = write_article(article=article_data, data=data, path=path)
assert res['new_file'] is True
assert (
res['url']
== f"{data['gmi_url']}{data['title']}/{file_date}_{file_title}.gmi"
)
def test_folder_deleted(tmp_path):
subpath = tmp_path / 'sub2'
delete_folder(path=tmp_path, feed_name='sub2')
assert not subpath.exists()
def test_atomfile_built(tmp_path):
data['articles'].append(article_data)
_rebuild_atom_file(path=tmp_path, data=data, urls=['bla'])
assert Path(f'{tmp_path}/atom.xml').is_file()