test: Fix formatting and annotation tests

This commit is contained in:
Marty Oehme 2023-09-22 20:04:39 +02:00
parent ee4690f52b
commit 1e29642cba
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
3 changed files with 77 additions and 52 deletions

View file

@ -10,8 +10,8 @@ def format_markdown(
) -> str:
template = (
"{{#tag}}#{{tag}}\n{{/tag}}"
"{{#quote}}> {{quote}}{{/quote}} {{#page}}[p. {{page}}]{{/page}}"
"\n{{#note}} NOTE: {{note}}{{/note}}"
"{{#quote}}> {{quote}}{{/quote}}{{#page}} [p. {{page}}]{{/page}}"
"{{#note}}\n NOTE: {{note}}{{/note}}"
)
output = ""
for entry in docs:
@ -30,11 +30,11 @@ def format_markdown(
for a in entry.annotations:
output += a.format(template)
output += "\n"
output += "\n\n"
output += "\n\n\n"
return output
return output.rstrip()
def format_markdown_atx(docs: list[AnnotatedDocument] = []) -> str:
@ -63,7 +63,7 @@ def format_count(docs: list[AnnotatedDocument] = []) -> str:
f"{count}\n"
)
return output
return output.rstrip()
def format_csv(docs: list[AnnotatedDocument] = []) -> str:
@ -82,7 +82,7 @@ def format_csv(docs: list[AnnotatedDocument] = []) -> str:
output += a.format(template, doc=d)
output += "\n"
return output
return output.rstrip()
formatters: dict[str, Formatter] = {

View file

@ -1,25 +1,23 @@
from papis.document import Document
import pytest
from papis_extract.annotation_data import Annotation
from papis_extract.templating import Custom
from papis_extract.annotation import Annotation
@pytest.mark.parametrize(
"fmt_string,expected",
[
(Custom(string="{{quote}}"), "I am the text value"),
("{{quote}}", "I am the text value"),
(
Custom(string="> {{quote}}\n{{#note}}Note: {{note}}{{/note}}"),
"> {{quote}}\n{{#note}}Note: {{note}}{{/note}}",
"> I am the text value\nNote: Whereas I represent the note",
),
(
Custom(
string="{{#note}}Note: {{note}}{{/note}}{{#page}}, p. {{page}}{{/page}}"
),
"{{#note}}Note: {{note}}{{/note}}{{#page}}, p. {{page}}{{/page}}",
"Note: Whereas I represent the note",
),
],
)
def test_formatting(fmt_string, expected):
def test_formatting_replacements(fmt_string, expected):
sut = Annotation(
"myfile",
text="I am the text value",
@ -28,6 +26,23 @@ def test_formatting(fmt_string, expected):
assert sut.format(fmt_string) == expected
@pytest.mark.parametrize(
"fmt_string,expected",
[
("{{doc.title}}", "document-title"),
("{{doc.title}}-{{doc.author}}", "document-title-document-author"),
("{{quote}} ({{doc.author}})", "I am the text value (document-author)"),
]
)
def test_formatting_document_access(fmt_string, expected):
sut = Annotation(
"myfile",
text="I am the text value",
content="Whereas I represent the note",
)
doc = Document(data= {"title": "document-title", "author": "document-author"})
assert sut.format(fmt_string, doc=doc) == expected
def test_colorname_matches_exact():
sut = Annotation("testfile", colors=(1.0, 0.0, 0.0), minimum_similarity_color=1.0)

View file

@ -1,49 +1,59 @@
import chevron
from papis.document import Document
from papis_extract.annotation import AnnotatedDocument, Annotation
from papis_extract.templating import Markdown, Csv
from papis_extract.formatter import (
format_count,
format_csv,
format_markdown,
format_markdown_atx,
)
def test_template_markers():
...
an_doc: AnnotatedDocument = AnnotatedDocument(
Document(data={"author": "document-author", "title": "document-title"}),
[
Annotation("myfile.pdf", text="my lovely text"),
Annotation("myfile.pdf", text="my second text", content="with note"),
],
)
def test_markdown_default():
fmt = Markdown()
assert (
chevron.render(
fmt.string,
{
"file": "somefile/somewhere.pdf",
"quote": "I am quote",
"note": "and including note.",
"page": 46,
"tag": "important",
"type": "highlight",
},
)
== "#important\n> I am quote [p. 46]\n NOTE: and including note."
fmt = format_markdown
assert fmt([an_doc]) == (
"""============== ---------------
document-title - document-author
============== ---------------
> my lovely text
> my second text
NOTE: with note"""
)
def test_csv_string():
fmt = Csv()
assert (
chevron.render(
fmt.string,
{
"file": "somefile/somewhere.pdf",
"quote": "I am quote",
"note": "and including note.",
"page": 46,
"tag": "important",
"type": "highlight",
},
)
== "highlight, important, 46, "
"I am quote, and including note., somefile/somewhere.pdf"
def test_markdown_atx():
fmt = format_markdown_atx
assert fmt([an_doc]) == (
"""# document-title - document-author
> my lovely text
> my second text
NOTE: with note"""
)
def test_csv_header():
fmt = Csv()
assert chevron.render(fmt.header, {}) == "type, tag, page, quote, note, file"
def test_count_default():
fmt = format_count
assert fmt([an_doc]) == ("""document-author - document-title: 2""")
def test_csv_default():
fmt = format_csv
assert fmt([an_doc]) == (
"type,tag,page,quote,note,author,title,ref,file\n"
'Highlight,,0,"my lovely text","","document-author",'
'"document-title","","myfile.pdf"\n'
'Highlight,,0,"my second text","with note","document-author",'
'"document-title","","myfile.pdf"'
)