-
Notifications
You must be signed in to change notification settings - Fork 1
chore: Add pytest to requirements.txt #3
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
Open
daler91
wants to merge
12
commits into
rdale-dev:master
Choose a base branch
from
daler91:jules-fix-requirements-2966955879941166032
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0d61c12
chore: Add pytest to requirements.txt
google-labs-jules[bot] bcd4cf1
🧪 Add test for untestable exception block in xml-validator.py
google-labs-jules[bot] f56a001
🧪 Add tests for BaseConverter to verify ABC behavior
google-labs-jules[bot] 6b4c987
🧪 Add unit tests for Data Validation module
google-labs-jules[bot] 1e1b587
🧪 Add Error Path Tests for Date Formatting in src/data_cleaning.py
google-labs-jules[bot] 32b2068
🔒 Fix XXE vulnerability in xml-validator
google-labs-jules[bot] 083304d
Merge pull request #6 from daler91/fix-xxe-vulnerability-367821129734…
daler91 0b52d81
Merge pull request #5 from daler91/jules-testing-improvement-date-for…
daler91 1af67b4
Merge pull request #4 from daler91/jules-add-data-validation-tests-12…
daler91 383417e
Merge pull request #3 from daler91/fix-base-converter-tests-318024770…
daler91 325d649
Merge pull request #2 from daler91/test-xml-validator-exception-78858…
daler91 d7926bb
Merge branch 'master' into jules-fix-requirements-2966955879941166032
daler91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| pandas | ||
| pytest | ||
| defusedxml | ||
| lxml | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import unittest | ||
| import os | ||
| import sys | ||
|
|
||
| # Add the project root to the Python path | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) | ||
|
|
||
| from src.converters.base_converter import BaseConverter | ||
| from src.logging_util import ConversionLogger | ||
| from src.validation_report import ValidationTracker | ||
|
|
||
| class TestBaseConverter(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.logger = ConversionLogger("test_base", log_level="DEBUG", log_to_file=False).logger | ||
| self.validator = ValidationTracker() | ||
|
|
||
| def test_cannot_instantiate_abc(self): | ||
| """ | ||
| Tests that BaseConverter cannot be instantiated directly because it's an ABC. | ||
| """ | ||
| with self.assertRaisesRegex(TypeError, "Can't instantiate abstract class BaseConverter"): | ||
| BaseConverter(self.logger, self.validator) | ||
|
|
||
| def test_subclass_must_implement_convert(self): | ||
| """ | ||
| Tests that a subclass must implement the 'convert' method. | ||
| """ | ||
| class IncompleteConverter(BaseConverter): | ||
| pass | ||
|
|
||
| with self.assertRaisesRegex(TypeError, "Can't instantiate abstract class IncompleteConverter"): | ||
| IncompleteConverter(self.logger, self.validator) | ||
|
|
||
| def test_subclass_with_convert_can_be_instantiated(self): | ||
| """ | ||
| Tests that a subclass that implements 'convert' can be instantiated. | ||
| """ | ||
| class CompleteConverter(BaseConverter): | ||
| def convert(self, input_path: str, output_path: str): | ||
| pass | ||
|
|
||
| converter = CompleteConverter(self.logger, self.validator) | ||
| self.assertIsInstance(converter, CompleteConverter) | ||
| self.assertIsInstance(converter, BaseConverter) | ||
| self.assertEqual(converter.logger, self.logger) | ||
| self.assertEqual(converter.validator, self.validator) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import unittest | ||
| from unittest.mock import MagicMock | ||
| import sys | ||
| import os | ||
|
|
||
| # Add the project root to the Python path | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) | ||
|
|
||
| from src.data_validation import ( | ||
| validate_counseling_record, | ||
| validate_training_record, | ||
| analyze_counseling_csv, | ||
| analyze_training_csv | ||
| ) | ||
| from src.config import ValidationCategory as VC, CounselingConfig, TrainingConfig | ||
|
|
||
| class TestDataValidation(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.validator = MagicMock() | ||
|
|
||
| def test_validate_counseling_record_success(self): | ||
| row = { | ||
| CounselingConfig.REQUIRED_FIELDS[0]: "C-123", | ||
| 'Last Name': 'Doe', | ||
| 'First Name': 'John', | ||
| 'Date': '2023-10-15' | ||
| } | ||
|
|
||
| result = validate_counseling_record(row, 1, self.validator) | ||
|
|
||
| self.assertTrue(result) | ||
| self.validator.set_current_record_id.assert_called_once_with("C-123") | ||
| self.validator.add_issue.assert_not_called() | ||
|
|
||
| def test_validate_counseling_record_missing_id(self): | ||
| row = { | ||
| 'Last Name': 'Doe', | ||
| 'First Name': 'John', | ||
| 'Date': '2023-10-15' | ||
| } | ||
|
|
||
| result = validate_counseling_record(row, 2, self.validator) | ||
|
|
||
| self.assertFalse(result) | ||
| self.validator.set_current_record_id.assert_not_called() | ||
| self.validator.add_issue.assert_called_once_with( | ||
| "Row_2", "error", VC.MISSING_REQUIRED, CounselingConfig.REQUIRED_FIELDS[0], "Missing required Contact ID." | ||
| ) | ||
|
|
||
| def test_validate_counseling_record_missing_last_name(self): | ||
| row = { | ||
| CounselingConfig.REQUIRED_FIELDS[0]: "C-124", | ||
| 'First Name': 'John', | ||
| 'Date': '2023-10-15' | ||
| } | ||
|
|
||
| result = validate_counseling_record(row, 3, self.validator) | ||
|
|
||
| self.assertTrue(result) | ||
| self.validator.set_current_record_id.assert_called_once_with("C-124") | ||
| self.validator.add_issue.assert_called_once_with( | ||
| "C-124", "warning", VC.MISSING_FIELD, "Last Name", "Missing Last Name." | ||
| ) | ||
|
|
||
| def test_validate_counseling_record_invalid_date_format(self): | ||
| row = { | ||
| CounselingConfig.REQUIRED_FIELDS[0]: "C-125", | ||
| 'Last Name': 'Doe', | ||
| 'Date': 'invalid-date' | ||
| } | ||
|
|
||
| result = validate_counseling_record(row, 4, self.validator) | ||
|
|
||
| self.assertTrue(result) | ||
| self.validator.set_current_record_id.assert_called_once_with("C-125") | ||
| self.validator.add_issue.assert_called_once_with( | ||
| "C-125", "warning", VC.INVALID_FORMAT, "Date Counseled", "Invalid date format: invalid-date" | ||
| ) | ||
|
|
||
| def test_validate_counseling_record_early_date(self): | ||
| row = { | ||
| CounselingConfig.REQUIRED_FIELDS[0]: "C-126", | ||
| 'Last Name': 'Doe', | ||
| 'Date': '2020-01-01' | ||
| } | ||
|
|
||
| result = validate_counseling_record(row, 5, self.validator) | ||
|
|
||
| self.assertTrue(result) | ||
| self.validator.set_current_record_id.assert_called_once_with("C-126") | ||
| self.validator.add_issue.assert_called_once_with( | ||
| "C-126", "warning", VC.INVALID_DATE, "Date Counseled", f"Date 2020-01-01 is before minimum of {CounselingConfig.MIN_COUNSELING_DATE}" | ||
| ) | ||
|
|
||
| def test_validate_training_record_success(self): | ||
| event_id_col = TrainingConfig.COLUMN_MAPPING['event_id'] | ||
| row = { | ||
| event_id_col: "T-999", | ||
| 'Other': 'Data' | ||
| } | ||
|
|
||
| result = validate_training_record(row, 1, self.validator) | ||
|
|
||
| self.assertTrue(result) | ||
| self.validator.set_current_record_id.assert_called_once_with("T-999") | ||
| self.validator.add_issue.assert_not_called() | ||
|
|
||
| def test_validate_training_record_missing_id(self): | ||
| event_id_col = TrainingConfig.COLUMN_MAPPING['event_id'] | ||
| row = { | ||
| 'Other': 'Data' | ||
| } | ||
|
|
||
| result = validate_training_record(row, 2, self.validator) | ||
|
|
||
| self.assertFalse(result) | ||
| self.validator.set_current_record_id.assert_not_called() | ||
| self.validator.add_issue.assert_called_once_with( | ||
| "Row_2", "error", VC.MISSING_REQUIRED, event_id_col, "Missing required Class/Event ID." | ||
| ) | ||
|
|
||
| def test_analyze_counseling_csv(self): | ||
| rows = [ | ||
| {CounselingConfig.REQUIRED_FIELDS[0]: "C-1", 'Last Name': 'Doe', 'First Name': 'John', 'Date': '2023-10-15'}, | ||
| {'Last Name': 'Smith', 'First Name': 'Alice'}, # missing id | ||
| {CounselingConfig.REQUIRED_FIELDS[0]: "C-3", 'First Name': 'Bob'}, # missing last name | ||
| {CounselingConfig.REQUIRED_FIELDS[0]: "C-4", 'Last Name': 'Brown', 'Date': 'invalid'}, # invalid date, missing first name | ||
| ] | ||
|
|
||
| analysis = analyze_counseling_csv(rows) | ||
|
|
||
| self.assertEqual(analysis['row_count'], 4) | ||
| self.assertEqual(analysis['missing_contact_id'], 1) | ||
| self.assertEqual(analysis['missing_names'], 2) | ||
| self.assertEqual(analysis['invalid_dates'], 1) | ||
|
|
||
| def test_analyze_training_csv(self): | ||
| event_id_col = TrainingConfig.COLUMN_MAPPING['event_id'] | ||
| rows = [ | ||
| {event_id_col: "T-1"}, | ||
| {}, # missing event id | ||
| {event_id_col: "T-3"} | ||
| ] | ||
|
|
||
| analysis = analyze_training_csv(rows) | ||
|
|
||
| self.assertEqual(analysis['row_count'], 3) | ||
| self.assertEqual(analysis['missing_event_id'], 1) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import unittest | ||
| from unittest.mock import patch | ||
| import sys | ||
| import os | ||
| import importlib | ||
|
|
||
| # Add the project root to the Python path | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src'))) | ||
|
|
||
| # Import using importlib because of the dash in the filename | ||
| xml_validator = importlib.import_module("xml-validator") | ||
|
|
||
| class TestValidateAgainstXsd(unittest.TestCase): | ||
|
|
||
| def test_validate_against_xsd_exception(self): | ||
| # We can patch using getattr since the module has a dash | ||
| with patch.object(xml_validator.etree, 'parse') as mock_parse: | ||
| # Setup mock to raise an exception | ||
| mock_parse.side_effect = Exception("Test exception") | ||
|
|
||
| # Call the function | ||
| is_valid, errors = xml_validator.validate_against_xsd("dummy.xml", "dummy.xsd") | ||
|
|
||
| # Verify the exception was caught and returned correctly | ||
| self.assertFalse(is_valid) | ||
| self.assertEqual(errors, ["Validation error: Test exception"]) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Development dependencies like
pytestshould be kept separate from production dependencies to avoid installing them in production environments. Please consider moving this to a separate file for development/test dependencies, such asrequirements-dev.txt.