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

[PR] Unit Testing for Core Components #45

Merged
merged 18 commits into from
Oct 27, 2024
Merged

[PR] Unit Testing for Core Components #45

merged 18 commits into from
Oct 27, 2024

Conversation

J1Loop
Copy link
Contributor

@J1Loop J1Loop commented Oct 27, 2024

[PR] Unit Testing for Core Components

Description

This PR adds unit tests for several components and models in the project. Each component has been tested to ensure that it meets functional requirements and properly handles potential errors. Tests include type validations, attribute initialization, and expected behavior of key methods.

Summary of Tested Components:

  • SMTPConfig: Verification of SMTP server configuration and initialization.
  • EmailMessageBuilder: Validation of email message construction.
  • EmailSender: Tests for email sending and exception handling.
  • SFTPDelivery: Validation of file delivery processes via SFTP.
  • AWSConfig: Configuration and credential validation for AWS.
  • S3Delivery: Testing file uploads and handling in Amazon S3.
  • CSVDataSource and XLSXDataSource: Data reading from CSV and Excel files.
  • PostgreSQLDataSource: Connections, queries, and data handling in PostgreSQL.
  • Certificate and CertificateFactory: Generation and validation of certificate instances from structured data.
  • Template: Initialization, HTML/CSS rendering, and value substitution in HTML templates.

Test Objectives:

  1. Initialization Coverage: Each component has been tested to ensure proper initialization with required parameters.
  2. Attribute Validation: Setters and getters are checked to handle attribute types and values appropriately.
  3. Realistic Environment Simulation: Using tmp_path for temporary files and mocks for configurations and external dependencies.
  4. Error Handling: Tests cover common exceptions and errors, such as invalid values, missing files, and failed queries.

NOTE: Some components, such as template_manager.py, still require testing. Adding that test suite once the component won't change too much with new implementations.

@J1Loop J1Loop added the tests Test suite for a component label Oct 27, 2024
@J1Loop J1Loop requested a review from pandasoncode October 27, 2024 10:30
@J1Loop J1Loop self-assigned this Oct 27, 2024
@J1Loop J1Loop linked an issue Oct 27, 2024 that may be closed by this pull request
Copy link
Contributor

Choose a reason for hiding this comment

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

CertificateFactory and Certificate should have been deleted xd

Copy link
Contributor

Choose a reason for hiding this comment

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

Certificate should have been deleted xd

mock_s3_client = MockS3Client()

def mock_boto3_client(*args, **kwargs):
return mock_s3_client
Copy link
Contributor

Choose a reason for hiding this comment

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

assert args[0] == "s3"

s3_delivery.upload_file("path", "bucket", "")


def test_s3_delivery_upload_file_no_credentials(monkeypatch, s3_delivery, tmp_path):
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary test. NoCredentialsError is an exception from boto3 🥇

)


def test_sftp_delivery_connect(monkeypatch, sftp_delivery):
Copy link
Contributor

Choose a reason for hiding this comment

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

This test should check that __establish_ssh_connection and open_sftp methods have been called.

assert sftp_delivery.sftp_client is not None


def test_sftp_delivery_connect_invalid_credentials(monkeypatch, sftp_delivery):
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary test. AuthenticationException is an exception from paramiko 🥇

sftp_delivery.connect()


def test_sftp_delivery_upload_file(monkeypatch, sftp_delivery, tmp_path):
Copy link
Contributor

Choose a reason for hiding this comment

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

This test should check that put has been called with local_file and remote_file args.

assert sftp_delivery.sftp_client.files[remote_file] == b"Test content."


def test_sftp_delivery_download_file(monkeypatch, sftp_delivery, tmp_path):
Copy link
Contributor

Choose a reason for hiding this comment

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

This test should check that get has been called with remote_file and local_file args.

assert local_file.read_bytes() == b"Test content."


def test_sftp_delivery_upload_with_verification(monkeypatch, sftp_delivery, tmp_path):
Copy link
Contributor

Choose a reason for hiding this comment

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

This test should check that upload_file and __verify_upload methods has been called with local_file, remote_file and algorithm args. It should mock __verify_upload and check if upload returns the same.

assert result is True


def test_sftp_delivery_upload_with_verification_failure(
Copy link
Contributor

Choose a reason for hiding this comment

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

This test should check that upload_file and __verify_upload methods has been called with local_file, remote_file and algorithm args. It should mock __verify_upload and check if upload returns the same.

sftp_delivery.upload_file(str(local_file), remote_file)


def test_sftp_delivery_connect_invalid_private_key(monkeypatch):
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary test. SSHException is an exception from paramiko 🥇

sftp_delivery.upload(str(local_file), remote_file, algorithm="invalid_algo")


def test_sftp_delivery_calculate_checksum(sftp_delivery, tmp_path):
Copy link
Contributor

Choose a reason for hiding this comment

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

Rework this test 👍

assert email_builder.custom_headers == {}


def test_add_recipient(email_builder):
Copy link
Contributor

Choose a reason for hiding this comment

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

Assertions missing 👍

Copy link
Contributor

@pandasoncode pandasoncode left a comment

Choose a reason for hiding this comment

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

OK!

@J1Loop J1Loop merged commit e20e493 into main Oct 27, 2024
1 check passed
@J1Loop J1Loop deleted the test/core-components branch October 27, 2024 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Test suite for a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Develop Unit Tests for Core Components
2 participants