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

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

View file

@ -1,25 +1,23 @@
from papis.document import Document
import pytest import pytest
from papis_extract.annotation_data import Annotation from papis_extract.annotation import Annotation
from papis_extract.templating import Custom
@pytest.mark.parametrize( @pytest.mark.parametrize(
"fmt_string,expected", "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", "> I am the text value\nNote: Whereas I represent the note",
), ),
( (
Custom( "{{#note}}Note: {{note}}{{/note}}{{#page}}, p. {{page}}{{/page}}",
string="{{#note}}Note: {{note}}{{/note}}{{#page}}, p. {{page}}{{/page}}"
),
"Note: Whereas I represent the note", "Note: Whereas I represent the note",
), ),
], ],
) )
def test_formatting(fmt_string, expected): def test_formatting_replacements(fmt_string, expected):
sut = Annotation( sut = Annotation(
"myfile", "myfile",
text="I am the text value", text="I am the text value",
@ -28,6 +26,23 @@ def test_formatting(fmt_string, expected):
assert sut.format(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(): def test_colorname_matches_exact():
sut = Annotation("testfile", colors=(1.0, 0.0, 0.0), minimum_similarity_color=1.0) 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,
)
an_doc: AnnotatedDocument = AnnotatedDocument(
def test_template_markers(): 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(): def test_markdown_default():
fmt = Markdown() fmt = format_markdown
assert ( assert fmt([an_doc]) == (
chevron.render( """============== ---------------
fmt.string, document-title - document-author
{ ============== ---------------
"file": "somefile/somewhere.pdf",
"quote": "I am quote", > my lovely text
"note": "and including note.",
"page": 46, > my second text
"tag": "important", NOTE: with note"""
"type": "highlight",
},
)
== "#important\n> I am quote [p. 46]\n NOTE: and including note."
) )
def test_csv_string(): def test_markdown_atx():
fmt = Csv() fmt = format_markdown_atx
assert ( assert fmt([an_doc]) == (
chevron.render( """# document-title - document-author
fmt.string,
{ > my lovely text
"file": "somefile/somewhere.pdf",
"quote": "I am quote", > my second text
"note": "and including note.", NOTE: with note"""
"page": 46,
"tag": "important",
"type": "highlight",
},
)
== "highlight, important, 46, "
"I am quote, and including note., somefile/somewhere.pdf"
) )
def test_csv_header(): def test_count_default():
fmt = Csv() fmt = format_count
assert chevron.render(fmt.header, {}) == "type, tag, page, quote, note, file" 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"'
)