Skip to content

Commit

Permalink
WIP: adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 31, 2023
1 parent 16efc1c commit e611685
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
20 changes: 16 additions & 4 deletions papermerge/core/page_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ def apply_pages_op(items: List[PageAndRotOp]) -> List[PyDocVer]:
page_count=len(items)
)

transform_pdf_pages(
copy_pdf_pages(
src=old_version.file_path,
dst=new_version.file_path,
items=items
)

reuse_text_field(
old_version=old_version,
new_version=new_version,
page_numbers=[p.number for p in pages]
)

notify_index_update(
remove_page_ids=[str(p.id) for p in old_version.pages.all()],
add_page_ids=[str(p.id) for p in new_version.pages.all()]
Expand All @@ -47,7 +53,7 @@ def apply_pages_op(items: List[PageAndRotOp]) -> List[PyDocVer]:
return doc.versions.all()


def transform_pdf_pages(
def copy_pdf_pages(
src: Path,
dst: Path,
items: List[PageAndRotOp]
Expand Down Expand Up @@ -167,12 +173,17 @@ def reuse_ocr_data(
def reuse_text_field(
old_version: PyDocVer,
new_version: PyDocVer,
page_map: list
page_numbers: list[int]
) -> None:
logger.debug(
f"Reuse text field for page numbers={page_numbers}"
f" old_version={old_version}"
f" new_version={new_version}"
)
streams = collect_text_streams(
version=old_version,
# list of old_version page numbers
page_numbers=[item[1] for item in page_map]
page_numbers=page_numbers
)

# updates page.text fields and document_version.text field
Expand Down Expand Up @@ -517,4 +528,5 @@ def notify_index_update(
add_page_ids: List[str],
remove_page_ids: List[str]
):
"""Sends tasks to the index to remove/add pages"""
current_app.send_task(INDEX_UPDATE, (add_page_ids, remove_page_ids))
3 changes: 3 additions & 0 deletions papermerge/search/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ def update_index(add_page_ids: List[str], remove_page_ids: List[str]):
Removes `remove_page_ids` and adds `add_page_ids` from index in
one "transaction".
"""
logger.debug(
f"Index Update: add={add_page_ids}, remove={remove_page_ids}"
)
remove_folder_or_page_from_index(remove_page_ids)
add_pages_to_index(add_page_ids)

Expand Down
24 changes: 23 additions & 1 deletion tests/core/test_page_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@
import pytest

from papermerge.core.models import Document
from papermerge.core.page_ops import extract_pages, move_pages
from papermerge.core.page_ops import apply_pages_op, extract_pages, move_pages
from papermerge.core.pathlib import abs_page_path
from papermerge.core.schemas.pages import ExtractStrategy, MoveStrategy
from papermerge.core.schemas.pages import Page as PyPage
from papermerge.core.schemas.pages import PageAndRotOp
from papermerge.test import maker
from papermerge.test.baker_recipes import folder_recipe, user_recipe


@pytest.mark.django_db
@patch('papermerge.core.signals.ocr_document_task')
@patch('papermerge.core.signals.generate_page_previews_task')
def test_apply_pages_op(_, __):
user = user_recipe.make()
src = maker.document(
resource='living-things.pdf',
user=user,
include_ocr_data=True
)
assert src.versions.last().pages.count() == 2
page = src.versions.last().pages.first()
pypage = PyPage(id=page.pk, number=page.number)
items = [PageAndRotOp(page=pypage, angle=0)]
versions = apply_pages_op(items)
newly_created_version = versions.last()

assert newly_created_version.pages.count() == 1


@pytest.mark.django_db
@patch('papermerge.core.signals.ocr_document_task')
@patch('papermerge.core.signals.generate_page_previews_task')
Expand Down

0 comments on commit e611685

Please sign in to comment.