Skip to content

test: Add unit tests for encrypt_pdf function#6

Open
ambicuity wants to merge 2 commits intoSUPAIDEAS:mainfrom
ambicuity:test/issue-2-encrypt-pdf-tests
Open

test: Add unit tests for encrypt_pdf function#6
ambicuity wants to merge 2 commits intoSUPAIDEAS:mainfrom
ambicuity:test/issue-2-encrypt-pdf-tests

Conversation

@ambicuity
Copy link
Copy Markdown
Contributor

Summary

This PR adds comprehensive unit tests for the encrypt_pdf() function to improve test coverage and ensure reliable PDF encryption functionality.

Linked Issue

Fixes #2

Changes Made

Added 5 unit tests in tests/unittests/test_encryptpdf.py:

  1. test_encrypt_pdf_creates_output_file - Verifies that an encrypted PDF file is created
  2. test_encrypt_pdf_output_is_encrypted - Confirms the output PDF is properly encrypted
  3. test_encrypt_pdf_with_correct_password - Tests decryption with the correct password
  4. test_encrypt_pdf_preserves_page_count - Ensures page count is preserved during encryption
  5. test_encrypt_pdf_with_invalid_input - Validates error handling for non-existent input files

Testing

All tests use:

  • unittest.TestCase framework (consistent with existing tests)
  • Temporary directories for output files (proper cleanup)
  • Sample PDF from tests/resources/Sample_PDF.pdf
  • Proper setup and teardown methods

Checklist

  • Tests cover the main functionality of encrypt_pdf()
  • Tests follow existing code style and structure
  • All tests include docstrings
  • Temporary files are cleaned up properly
  • Tests validate both success and error cases

Add unit tests for encrypt_pdf function to verify functionality, including output file creation, encryption status, decryption with correct password, page count preservation, and handling of invalid input.
@nirmalchandra
Copy link
Copy Markdown
Contributor

Thanks @ambicuity for the PR.
Waiting for this CI ticket to be picked up #7 .

Even otherwise, will review soon and merge if all looks good.

@nirmalchandra nirmalchandra self-requested a review October 24, 2025 10:40
@nirmalchandra
Copy link
Copy Markdown
Contributor

@ambicuity , can you please resolve the conflicts? Also, check if CI goes Green, then we can merge

Copilot AI review requested due to automatic review settings February 20, 2026 05:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive unit tests for the encrypt_pdf() function to improve test coverage. The tests verify that the function correctly encrypts PDF files, preserves content during encryption, and handles error cases appropriately.

Changes:

  • Added 5 new integration-style unit tests covering file creation, encryption verification, decryption, page count preservation, and error handling
  • Renamed test class from TestPdfUnitTests to TestEncryptPdf for better clarity
  • Added poetry.lock file with dependency specifications

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/unittests/test_encryptpdf.py Added 5 new unit tests for encrypt_pdf function with proper setup/teardown and docstrings
poetry.lock Auto-generated lock file specifying exact versions of all dependencies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +27 to +28
if os.path.exists(self.temp_dir):
os.rmdir(self.temp_dir)
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tearDown method uses os.rmdir() which will fail if the directory is not empty. If a test fails before removing the output_pdf file, or if multiple files are created during testing, the directory removal will raise an OSError. Consider using shutil.rmtree(self.temp_dir) instead, or wrap it in a try-except block for more robust cleanup.

Copilot uses AI. Check for mistakes.
Comment on lines +54 to +59
# Decrypt with correct password
decrypt_result = reader.decrypt(self.test_password)

self.assertNotEqual(
decrypt_result,
0,
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion pattern here is inconsistent with the integration test at tests/integrationtests/test_encryptpdf_integration.py:40, which uses self.assertTrue(reader.decrypt(password)). While both approaches work (decrypt returns 0 for failure and non-zero for success), consider using assertTrue for consistency with the existing test pattern.

Suggested change
# Decrypt with correct password
decrypt_result = reader.decrypt(self.test_password)
self.assertNotEqual(
decrypt_result,
0,
# Decrypt with correct password and assert success
self.assertTrue(
reader.decrypt(self.test_password),

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[UNIT TEST]Add couple of unit tests for the funtion encrypt_pdf()

3 participants