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

test: adding unit tests to separate branch #15

Closed
wants to merge 31 commits into from

Conversation

Karanjot786
Copy link
Member

@Karanjot786 Karanjot786 commented Jul 16, 2024

Description:

This PR separates the unit tests from the main implementation and moves them to a dedicated branch. This is done to ensure focused testing and validation of individual components without merging the tests prematurely.

Files Included:

  • tests/unit/test_tes_converter.py
  • tests/unit/test_wes_converter.py

Details:

Unit Tests for TES Converter:

  • Tests the conversion from TES to WRROC.
  • Tests the conversion from WRROC to TES.

Unit Tests for WES Converter:

  • Tests the conversion from WES to WRROC.
  • Tests the conversion from WRROC to WES.

Instructions for Running the Tests:

  1. Ensure all dependencies are installed:

    poetry install
  2. Run the unit tests:

    poetry run pytest tests/unit

Sample TES Input (tes_example.json):

{
    "id": "task-id",
    "name": "test-task",
    "description": "test-description",
    "executors": [{"image": "executor-image"}],
    "inputs": [{"url": "input-url", "path": "input-path"}],
    "outputs": [{"url": "output-url", "path": "output-path"}],
    "creation_time": "2023-07-10T14:30:00Z",
    "end_time": "2023-07-10T15:30:00Z"
}

Sample WES Input (wes_example.json):

{
    "run_id": "run-id",
    "run_log": {
        "name": "test-run",
        "start_time": "2023-07-10T14:30:00Z",
        "end_time": "2023-07-10T15:30:00Z"
    },
    "state": "COMPLETED",
    "outputs": [{"location": "output-location", "name": "output-name"}]
}

Summary by Sourcery

This pull request introduces new conversion functionalities for TES and WES formats to WRROC and vice versa. It also includes a CLI for performing these conversions, updates the project name to 'CrateGen', and adjusts the CI configuration accordingly. Additionally, unit tests for the new conversion functionalities have been added.

  • New Features:
    • Introduced TES to WRROC and WRROC to TES conversion functionality.
    • Introduced WES to WRROC and WRROC to WES conversion functionality.
    • Added a CLI for converting TES/WES to WRROC.
  • Enhancements:
    • Renamed project from 'WRROC <> GA4GH API Conversion Tool' to 'CrateGen'.
  • CI:
    • Updated CI configuration to reflect the new project structure and naming.
  • Tests:
    • Added unit tests for TES to WRROC and WRROC to TES conversion.
    • Added unit tests for WES to WRROC and WRROC to WES conversion.

Karanjot786 and others added 30 commits June 21, 2024 09:56
Copy link

sourcery-ai bot commented Jul 16, 2024

Reviewer's Guide by Sourcery

This pull request introduces unit tests for the TES and WES converters, moving them to a dedicated branch to ensure focused testing. Additionally, it updates the CI workflow and README to reflect the new project structure and naming conventions.

File-Level Changes

Files Changes
tests/unit/test_tes_converter.py
tests/unit/test_wes_converter.py
Added unit tests for TES and WES converters.
crategen/converters/tes_converter.py
crategen/converters/wes_converter.py
Implemented conversion logic for TES and WES to WRROC and vice versa.
tests/data/output/tes_to_wrroc_output.json
tests/data/output/wes_to_wrroc_output.json
tests/data/input/tes_example.json
tests/data/input/wes_example.json
Added sample input and output JSON files for TES and WES conversions.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

@Karanjot786 Karanjot786 deleted the add-tests branch July 16, 2024 07:36
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Karanjot786 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 6 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 9 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@@ -25,24 +25,20 @@ jobs:

- name: Lint with Ruff
run: |
poetry run ruff check src/ tests/
poetry run ruff check crategen/ tests/
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Consider adding a specific file path for tests.

To ensure that only the relevant test files are checked, consider specifying the exact path to the test files instead of using a general 'tests/' directory.

Suggested change
poetry run ruff check crategen/ tests/
poetry run ruff check crategen/ tests/specific_test_file.py

inputs = tes_data.get("inputs", [])
outputs = tes_data.get("outputs", [])
creation_time = tes_data.get("creation_time", "")
end_time = tes_data.get("logs", [{}])[0].get("end_time", "") # Corrected to fetch from logs
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Check for potential IndexError.

If 'logs' is an empty list, this line will raise an IndexError. Consider adding a check to ensure 'logs' is not empty before accessing the first element.

"@id": id,
"name": name,
"description": description,
"instrument": executors[0].get("image", None) if executors else None,
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Check for potential IndexError.

If 'executors' is an empty list, this line will raise an IndexError. Consider adding a check to ensure 'executors' is not empty before accessing the first element.

"status": state,
"startTime": convert_to_iso8601(start_time),
"endTime": convert_to_iso8601(end_time),
"result": [{"@id": output.get("location", ""), "name": output.get("name", "")} for output in outputs],
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Check for potential KeyError.

Ensure that 'outputs' is a list of dictionaries and that each dictionary contains the keys 'location' and 'name'. If not, this line may raise a KeyError.

except ValueError:
continue
# Handle incorrect format or other issues
return None
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider logging incorrect timestamp formats.

Instead of silently returning None for incorrect timestamp formats, consider logging a warning or error message to help with debugging.

Suggested change
return None
import logging
logging.basicConfig(level=logging.WARNING)
# Handle incorrect format or other issues
logging.warning("Incorrect timestamp format encountered.")
return None

result = self.converter.convert_to_wrroc(wes_data)
self.assertEqual(result, expected_wrroc_data)

def test_convert_from_wrroc(self):
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Add tests for invalid data formats

It would be beneficial to add tests that check how the converter handles invalid data formats, such as incorrect date formats or unexpected data types.

Suggested change
def test_convert_from_wrroc(self):
def test_convert_from_wrroc_invalid_data(self):
invalid_wrroc_data = {
"@id": 123, # Invalid type, should be a string
"date": "invalid-date-format" # Invalid date format
}
with self.assertRaises(ValueError):
self.converter.convert_from_wrroc(invalid_wrroc_data)

@@ -0,0 +1,62 @@
import unittest
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Consider using pytest for more concise tests

Using pytest can make the tests more concise and easier to read. For example, you can use fixtures for setup and teardown, and avoid boilerplate code.

Suggested change
import unittest
import pytest

@@ -0,0 +1,58 @@
import unittest
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Consider using pytest for more concise tests

Using pytest can make the tests more concise and easier to read. For example, you can use fixtures for setup and teardown, and avoid boilerplate code.

Suggested change
import unittest
import pytest

}

result = self.converter.convert_to_wrroc(tes_data)
self.assertEqual(result, expected_wrroc_data)
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Add assertion messages for better debugging

Adding custom messages to assertions can help with debugging when a test fails. For example: self.assertEqual(result, expected_wrroc_data, 'Conversion to WRROC failed').

Suggested change
self.assertEqual(result, expected_wrroc_data)
self.assertEqual(result, expected_wrroc_data, 'Conversion to WRROC failed')

}

result = self.converter.convert_to_wrroc(wes_data)
self.assertEqual(result, expected_wrroc_data)
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Add assertion messages for better debugging

Adding custom messages to assertions can help with debugging when a test fails. For example: self.assertEqual(result, expected_wrroc_data, 'Conversion to WRROC failed').

Suggested change
self.assertEqual(result, expected_wrroc_data)
self.assertEqual(result, expected_wrroc_data, 'Conversion to WRROC failed')

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.

1 participant