# Path: notifications_utils/pdf.py
# def extract_page_from_pdf(src_pdf, page_number):
#     """
#     Retrieves a new PDF document with the page extracted from the source PDF file.
# 
#     :param src_pdf: File object or an object that supports the standard read and seek methods similar to a File object.
#     :param page_number: The page number to retrieve (pages begin at zero)
#     """
#     pdf = PyPDF2.PdfFileReader(src_pdf)
# 
#     if pdf.numPages < page_number:
#         raise PdfReadError("Page number requested: {} of {} does not exist in document".format(
#             str(page_number),
#             str(pdf.numPages)
#         ))
# 
#     writer = PdfFileWriter()
#     writer.addPage(pdf.getPage(page_number))
# 
#     pdf_bytes = io.BytesIO()
#     writer.write(pdf_bytes)
#     pdf_bytes.seek(0)
# 
#     return pdf_bytes.read()
# 
# def is_letter_too_long(page_count):
#     """
#     Returns True if page count above the limit
#     :param page_count: number of pages in a document or None
#     """
#     if not page_count:
#         return False
#     return page_count > LETTER_MAX_PAGE_COUNT
# 
# def pdf_page_count(src_pdf):
#     """
#     Returns number of pages in a pdf file
# 
#     :param PyPDF2.PdfFileReader src_pdf: A File object or an object that supports the standard read and seek methods
#     """
#     try:
#         pdf = PyPDF2.PdfFileReader(src_pdf)
#     except AttributeError as e:
#         raise PdfReadError("Could not open PDF file, stream is null", e)
# 
#     return pdf.numPages


import base64
import PyPDF2
import pytest
from io import BytesIO
from PyPDF2.utils import PdfReadError
from notifications_utils.pdf import (
    extract_page_from_pdf,
    is_letter_too_long,
    pdf_page_count,
)
from tests.pdf_consts import multi_page_pdf, not_pdf, one_page_pdf



def test_pdf_page_count_src_pdf_is_null():
    with pytest.raises(PdfReadError):
        pdf_page_count(None)


def test_pdf_page_count_src_pdf_has_one_page():
    file_data = base64.b64decode(one_page_pdf)
    num = pdf_page_count(BytesIO(file_data))
    assert num == 1


def test_pdf_page_count_src_pdf_has_multiple_pages():
    file_data = base64.b64decode(multi_page_pdf)
    num = pdf_page_count(BytesIO(file_data))
    assert num == 10


def test_pdf_page_count_src_pdf_not_a_pdf():
    with pytest.raises(PdfReadError):
        file_data = base64.b64decode(not_pdf)
        pdf_page_count(BytesIO(file_data))


@pytest.mark.parametrize("page_count, expected_result", [
    (None, False), (10, False), (11, True)
])
def test_is_letter_too_long(page_count, expected_result):

# Path: notifications_utils/pdf.py
# def extract_page_from_pdf(src_pdf, page_number):
#     """
#     Retrieves a new PDF document with the page extracted from the source PDF file.
# 
#     :param src_pdf: File object or an object that supports the standard read and seek methods similar to a File object.
#     :param page_number: The page number to retrieve (pages begin at zero)
#     """
#     pdf = PyPDF2.PdfFileReader(src_pdf)
# 
#     if pdf.numPages < page_number:
#         raise PdfReadError("Page number requested: {} of {} does not exist in document".format(
#             str(page_number),
#             str(pdf.numPages)
#         ))
# 
#     writer = PdfFileWriter()
#     writer.addPage(pdf.getPage(page_number))
# 
#     pdf_bytes = io.BytesIO()
#     writer.write(pdf_bytes)
#     pdf_bytes.seek(0)
# 
#     return pdf_bytes.read()
# 
# def is_letter_too_long(page_count):
#     """
#     Returns True if page count above the limit
#     :param page_count: number of pages in a document or None
#     """
#     if not page_count:
#         return False
#     return page_count > LETTER_MAX_PAGE_COUNT
# 
# def pdf_page_count(src_pdf):
#     """
#     Returns number of pages in a pdf file
# 
#     :param PyPDF2.PdfFileReader src_pdf: A File object or an object that supports the standard read and seek methods
#     """
#     try:
#         pdf = PyPDF2.PdfFileReader(src_pdf)
#     except AttributeError as e:
#         raise PdfReadError("Could not open PDF file, stream is null", e)
# 
#     return pdf.numPages


# Path: tests/test_pdf.py
import base64
import PyPDF2
import pytest
from io import BytesIO
from PyPDF2.utils import PdfReadError
from notifications_utils.pdf import (
    extract_page_from_pdf,
    is_letter_too_long,
    pdf_page_count,
)
from tests.pdf_consts import multi_page_pdf, not_pdf, one_page_pdf




def test_pdf_page_count_src_pdf_is_null():
    with pytest.raises(PdfReadError):
        pdf_page_count(None)


def test_pdf_page_count_src_pdf_has_one_page():
    file_data = base64.b64decode(one_page_pdf)
    num = pdf_page_count(BytesIO(file_data))
    assert num == 1


def test_pdf_page_count_src_pdf_has_multiple_pages():
    file_data = base64.b64decode(multi_page_pdf)
    num = pdf_page_count(BytesIO(file_data))
    assert num == 10


def test_pdf_page_count_src_pdf_not_a_pdf():
    with pytest.raises(PdfReadError):
        file_data = base64.b64decode(not_pdf)
        pdf_page_count(BytesIO(file_data))


@pytest.mark.parametrize("page_count, expected_result", [
    (None, False), (10, False), (11, True)
])
def test_is_letter_too_long(page_count, expected_result):

    assert is_letter_too_long(page_count) == expected_result