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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
from datetime import datetime
import os
import pytest
import shutil
from typing import Callable, Optional
import unittest
import warnings
from markdown_it.token import Token
from markdown_it.utils import EnvType, OptionsDict
import pypandoc
from iotas.attachment import Attachment
from iotas.attachment_helpers import (
write_attachment_to_disk,
)
from iotas.exporter import Exporter
from iotas.html_generator import HtmlGenerator
from iotas.note import Note
from iotas.pdf_exporter import PdfExporter
def get_output_dir() -> str:
file_dir = os.path.dirname(__file__)
return os.path.join(file_dir, os.pardir, "testing-tmp")
def get_attachments_dir() -> str:
return os.path.join(get_output_dir(), "user-attachments")
import iotas.attachment_helpers
iotas.attachment_helpers.get_attachments_dir = get_attachments_dir
warnings.filterwarnings("ignore", "version")
class _MockPdfExporter(PdfExporter):
note: Note
location: str
def set_callbacks(self, finished_callback: Callable, error_callback: Callable) -> None:
"""Set functions to be called upon export result.
:param Callable finished_callback: Finished callback
:param Callable error_callback: Error callback
"""
pass
def export(self, note: Note, location: str) -> None:
"""Export PDF of note.
:param Note note: Note to export
:param str location: Destination location
"""
self.note = note
self.location = location
class MockHtmlGenerator(HtmlGenerator):
"""HTML generator mocked."""
def generate(
self,
note: Note,
tokens: list[Token],
render_func: Callable[[list[Token], OptionsDict, EnvType], str],
parser_options: OptionsDict,
searching: bool,
export_format: Optional[str],
scroll_position: Optional[float] = None,
) -> str:
"""Generator HTML for note.
:param Note note: Note to render
:param list[Token] tokens: Parser tokens
:param Callable[[list[Token], OptionsDict, EnvType], str] render_fun: Render function
:param OptionsDict parser_options: Parser options
:param bool searching: Whether search CSS should be included
:param Optional[str] export_format: Export format, if using
:param Optional[float] scroll_position: Position to scroll to
:return: Generated HTML
:rtype: str
"""
return self.get_output(note)
def generate_user_stylesheet(self, searching: bool) -> str:
"""Generate part of stylesheet based on state (configuration etc).
:param bool searching: Whether searching
:return: stylesheet
:rtype: str
"""
return ""
def update_font_family(self, family: str) -> None:
"""Update the font family.
:param str family: New font family
"""
pass
def get_output(self, note: Note) -> str:
"""Get the mocked output.
:param Note note: The note
:return: Generated content:
:rtype: str
"""
return f"{note.title}|{note.content}"
class Test(unittest.TestCase):
exporter: Exporter
pdf_exporter: _MockPdfExporter
def test_export_md_attachments(self) -> None:
exporter = self.__reset()
note = Note(new_note=True)
note.id = 1
note.content = ""
note.title = "Test Note Title"
out_path = self.__prepare_output_dir()
self.__create_attachment(note, "image.png")
dir_path = os.path.join(out_path, "Test Note Title")
file_path = os.path.join(dir_path, "Test Note Title.md")
exporter.export(
note,
out_format="md",
file_extension="md",
supporting_tex=False,
allow_missing_images=False,
user_location=dir_path,
)
self.assertTrue(os.path.exists(file_path))
with open(file_path, "r") as f:
contents = f.read()
self.assertEqual(contents, "")
attachment_path = os.path.join(dir_path, "attachments", "image.png")
self.assertTrue(os.path.exists(attachment_path))
self.__clean_output_dir()
def test_export_md_no_attachments(self) -> None:
exporter = self.__reset()
note = Note(new_note=True)
note.id = 1
note.content = "content"
note.title = "Test Note Title"
out_path = self.__prepare_output_dir()
file_path = os.path.join(out_path, "Test Note Title.md")
exporter.export(
note,
out_format="md",
file_extension="md",
supporting_tex=False,
allow_missing_images=False,
user_location=file_path,
)
self.assertTrue(os.path.exists(file_path))
with open(file_path, "r") as f:
contents = f.read()
self.assertEqual(contents, note.content)
self.__clean_output_dir()
def test_export_mock_html(self) -> None:
exporter = self.__reset()
note = Note(new_note=True)
note.id = 1
note.content = "content\n\n"
note.title = "Test Note Title"
out_path = self.__prepare_output_dir()
self.__create_attachment(note, "image.png")
dir_path = os.path.join(out_path, "Test Note Title")
exporter.export(
note,
out_format="html",
file_extension="html",
supporting_tex=False,
allow_missing_images=False,
user_location=dir_path,
)
index_path = os.path.join(dir_path, "index.html")
self.assertTrue(os.path.exists(index_path))
with open(index_path, "r") as f:
contents = f.read()
self.assertTrue(f"{note.content}" in contents)
self.assertTrue(f"{note.title}" in contents)
attachment_path = os.path.join(dir_path, "attachments", "image.png")
self.assertTrue(os.path.exists(attachment_path))
self.__clean_output_dir()
def test_mock_export_pdf(self) -> None:
exporter = self.__reset()
note = Note(new_note=True)
note.content = "content"
note.title = "Test Note Title"
out_path = self.__prepare_output_dir()
file_path = os.path.join(out_path, "Test Note Title.pdf")
exporter.export(
note,
out_format="pdf",
file_extension="pdf",
supporting_tex=False,
allow_missing_images=False,
user_location=file_path,
)
self.assertEqual(self.pdf_exporter.note, note)
self.assertEqual(self.pdf_exporter.location, file_path)
self.__clean_output_dir()
def test_export_odt(self) -> None:
# TODO Skipped during CI for https://gitlab.gnome.org/World/iotas/-/issues/260
if "DISPLAY" not in os.environ:
return pytest.skip("DISPLAY not set")
exporter = self.__reset()
note = Note(new_note=True)
note.content = "content"
note.title = "Test Note Title"
out_path = self.__prepare_output_dir()
file_path = os.path.join(out_path, "Test Note Title.odt")
exporter.export(
note,
out_format="odt",
file_extension="odt",
supporting_tex=False,
user_location=file_path,
)
self.assertTrue(os.path.exists(file_path))
with open(file_path, "rb") as f:
contents = f.read()
md = pypandoc.convert_text(contents, to="md", format="odt")
self.assertTrue(note.content == md.strip())
self.__clean_output_dir()
def test_build_default_filename(self) -> None:
exporter = self.__reset()
note = Note(new_note=True)
note.content = "content"
note.title = "Test Note Title"
filename = exporter.build_default_filename(
note, out_format="md", file_extension="md", add_timestamp=False
)
self.assertEqual(filename, "Test Note Title.md")
filename = exporter.build_default_filename(
note, out_format="odt", file_extension="odt", add_timestamp=False
)
self.assertEqual(filename, "Test Note Title.odt")
filename = exporter.build_default_filename(
note, out_format="pdf", file_extension="pdf", add_timestamp=False
)
self.assertEqual(filename, "Test Note Title.pdf")
filename = exporter.build_default_filename(
note, out_format="html", file_extension="html", add_timestamp=False
)
self.assertEqual(filename, "Test Note Title")
filename = exporter.build_default_filename(
note, out_format="md", file_extension="md", add_timestamp=True
)
parts = filename.split(" ")
self.assertEqual(len(parts), 4)
timestamp = None
try:
timestamp = datetime.strptime(parts[0], "%Y-%m-%dT%H:%M:%S")
except ValueError:
pass
self.assertIsNotNone(timestamp)
self.assertEqual(" ".join(parts[1:]), "Test Note Title.md")
def __reset(self) -> Exporter:
self.pdf_exporter = _MockPdfExporter()
html_generator = MockHtmlGenerator()
data_path = "/mock/data/path"
self.exporter = Exporter(self.pdf_exporter, html_generator, data_path)
return self.exporter
def __prepare_output_dir(self) -> str:
out_path = get_output_dir()
if os.path.exists(out_path):
shutil.rmtree(out_path)
os.makedirs(out_path)
return out_path
def __clean_output_dir(self) -> None:
out_path = get_output_dir()
if os.path.exists(out_path):
shutil.rmtree(out_path)
def __create_attachment(self, note: Note, path: str) -> bool:
os.makedirs(get_attachments_dir())
attachment = Attachment()
attachment.note_id = note.id
attachment.path = path
return write_attachment_to_disk(attachment, b"bytes")
|