dotimplement / HealthChain

Simplify testing and validating AI and NLP applications in a healthcare context 💫 🏥
https://dotimplement.github.io/HealthChain/
Apache License 2.0
18 stars 13 forks source link

Add api decorator unit test #74

Open jenniferjiangkells opened 2 weeks ago

jenniferjiangkells commented 2 weeks ago

Description

We need to implement unit tests for the API decorator in the test_decorators.py file. Currently, there are no tests specifically covering the functionality of this decorator.

Context

The API decorator is used to mark and configure API endpoints. Testing this decorator is important to ensure it behaves correctly under various scenarios and integrates properly with the rest of the system.

The api decorator does two things that the new test function needs to cover:

  1. It sets the function attribute is_service_route
  2. It wraps the function in the class APIMethod

decorators.py

apimethod.py

Possible Implementation

Add a new test function in test_decorators.py, for example:

def test_api_decorator():
    # Test basic functionality
    @api
    def sample_api_function():
        return "Hello, API!"

    assert hasattr(sample_api_function, 'is_service_route')
    assert sample_api_function() == "Hello, API!"
Prototype4988 commented 2 weeks ago

Hi @jenniferjiangkells. I would like to work on this issue. Can you assign it to me

rajatkriplani commented 2 weeks ago

implemented the follwoing function, can you please give me feedback for it?

import unittest
from typing import Callable, Dict, Optional, Union
from decorators import api, APIMethod

class TestAPIDecorator(unittest.TestCase):
    def test_api_decorator_basic_functionality(self):
        @api
        def sample_api_function():
            return "Hello, API!"

        # Test if function has 'is_service_route'
        self.assertTrue(hasattr(sample_api_function, 'is_service_route'))

        # Test if attribute 'is_service_route' is True
        self.assertTrue(sample_api_function.is_service_route)

        # Test the return value of function
        self.assertEqual(sample_api_function(), "Hello, API!")

        # Test if the function is wrapped in APIMethod
        wrapped_function = APIMethod(sample_api_function)
        self.assertIsInstance(wrapped_function, APIMethod)
        self.assertEqual(wrapped_function.func(), "Hello, API!")

if __name__ == "__main__":
    unittest.main()
rajatkriplani commented 2 weeks ago

@jenniferjiangkells should I just directly push the above function?

jenniferjiangkells commented 2 weeks ago

@rajatkriplani This is great but can you please make sure to create a fork and open a Pull Request for this - you can follow the steps in our contributing guideline 🙂 Also mind that we use Pytest for the project.

rajatkriplani commented 2 weeks ago

@rajatkriplani This is great but can you please make sure to create a fork and open a Pull Request for this - you can follow the steps in our contributing guideline 🙂 Also mind that we use Pytest for the project.

on it!

DDibyajyot commented 12 hours ago

Hey, I'd like to work on this if it is still open. Do let me know if this issue has not been resolved :)