Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Add tests for XMP information #996

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions tests/test_xmp.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
from datetime import datetime
from io import BytesIO

import pytest

import PyPDF2.generic
import PyPDF2.xmp
from PyPDF2 import PdfReader

from . import get_pdf_from_url

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
RESOURCE_ROOT = os.path.join(PROJECT_ROOT, "resources")
Expand Down Expand Up @@ -83,6 +86,88 @@ def test_identity(x):
assert PyPDF2.xmp._identity(x) == x


@pytest.mark.parametrize(
("url", "name", "xmpmm_instance_id"),
[
(
"https://corpora.tika.apache.org/base/docs/govdocs1/955/955562.pdf",
"tika-955562.pdf",
"uuid:ca96e032-c2af-49bd-a71c-95889bafbf1d",
)
],
)
def test_xmpmm(url, name, xmpmm_instance_id):
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
xmp_metadata = reader.xmp_metadata
assert xmp_metadata.xmpmm_instanceId == xmpmm_instance_id
# cache hit:
assert xmp_metadata.xmpmm_instanceId == xmpmm_instance_id


def test_dc_description():
url = "https://corpora.tika.apache.org/base/docs/govdocs1/953/953770.pdf"
name = "tika-953770.pdf"
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
xmp_metadata = reader.xmp_metadata
assert xmp_metadata.dc_description == {
"x-default": "U.S. Title 50 Certification Form"
}
# cache hit:
assert xmp_metadata.dc_description == {
"x-default": "U.S. Title 50 Certification Form"
}


def test_dc_creator():
url = "https://corpora.tika.apache.org/base/docs/govdocs1/953/953770.pdf"
name = "tika-953770.pdf"
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
xmp_metadata = reader.xmp_metadata
assert xmp_metadata.dc_creator == ["U.S. Fish and Wildlife Service"]
# cache hit:
assert xmp_metadata.dc_creator == ["U.S. Fish and Wildlife Service"]


def test_custom_properties():
url = "https://corpora.tika.apache.org/base/docs/govdocs1/986/986065.pdf"
name = "tika-986065.pdf"
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
xmp_metadata = reader.xmp_metadata
assert xmp_metadata.custom_properties == {"Style": "Searchable Image (Exact)"}
# cache hit:
assert xmp_metadata.custom_properties == {"Style": "Searchable Image (Exact)"}


def test_dc_subject():
url = "https://corpora.tika.apache.org/base/docs/govdocs1/959/959519.pdf"
name = "tika-959519.pdf"
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
xmp_metadata = reader.xmp_metadata
assert xmp_metadata.dc_subject == [
"P&P",
"manual",
"1240.2325",
"CVM",
"PROCEDURES ON MEDIA INQUIRIES",
"animal",
"media",
"procedures",
"inquiries",
]
# Cache hit:
assert xmp_metadata.dc_subject == [
"P&P",
"manual",
"1240.2325",
"CVM",
"PROCEDURES ON MEDIA INQUIRIES",
"animal",
"media",
"procedures",
"inquiries",
]


# def test_getter_bag():
# f = PyPDF2.xmp._getter_bag("namespace", "name")
# class Tst: # to replace pdf
Expand Down