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

Add docstrings for constraints.py and implement unit test framework for Constraints class #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

pingluuu
Copy link

@pingluuu pingluuu commented Mar 3, 2025

Add Docstrings for constraints.py and Implement Unit Test Framework for Constraints Class

Description

This pull request introduces the following changes to the src/constraint.py file and test/test_quadratic_program.py:

  • Docstrings: Added detailed docstrings to the Constraints class and its methods to improve code readability and make it easier to use.
  • Unit Test Framework: Set up a framework TestConstraintsMethods() for unit tests for the Constraints class.
  • Implemented test_add_constaints_init() and test_add_constraints_init_error(): This method validates the correct initialization of the Constraints class.

These improvements aim to help developers better understand the functionality and usage of the Constraints class, and the unit test framework will ensure to reach 100% coverage in the Constraint class in src/constraint.py.

Changes:

1. Added Docstrings

  • Detailed docstrings were added to all methods in the Constraints class to describe their functionality, parameters, return values, and potential exceptions.
  • Example:
    def add_budget(self, rhs=1, sense='=') -> None:
        """
       Adds a budget constraint to the object.
    
       Args:
           rhs (int or float): The right-hand side value of the budget constraint. Default is 1.
           sense (str): The type of inequality ('=' or other). Default is '='.
    
       Raises:
           Warning: If the budget is being overwritten.
       """

2. Unit Test Framework

  • Created a basic unit test structure using the unittest framework.

  • Implemented the test_init() method to test the initialization logic of the Constraints class, verifying that the selection argument is processed correctly and that the class attributes are initialized properly.

  • Example of test_add_constaints_init(self) method:

     def test_add_constaints_init(self):
       """
       Test the successful initialization and basic functionality of the Constraints class.
       """
       # Test with valid input
       selection = ["A", "B", "C"]
       constraints = Constraints(selection=selection)
       
       # Test if selection is set correctly
       assert constraints.selection == selection, "Selection should be correctly initialized"
       
       # Test initial values for budget, box, linear, and l1
       assert constraints.budget == {'Amat': None, 'sense': None, 'rhs': None}, "Initial budget should be empty"
       assert constraints.box == {'box_type': 'NA', 'lower': None, 'upper': None}, "Initial box should be empty"
       assert constraints.linear == {'Amat': None, 'sense': None, 'rhs': None}, "Initial linear constraints should be empty"
       assert constraints.l1 == {}, "Initial l1 should be empty"

3. Updated Import Paths:

I changed the import statements in the code to reflect the updated folder structure. Specifically:

  • From:
    from helper_functions import to_numpy
    from data_loader import load_data_msci
    from constraints import Constraints
    from covariance import Covariance
    from optimization import *
    from optimization_data import OptimizationData
  • To:
    from src.helper_functions import to_numpy
    from src.data_loader import load_data_msci
    from src.constraints import Constraints
    from src.covariance import Covariance
    from src.optimization import *
    from src.optimization_data import OptimizationData

This change was necessary because the test file is not directly under the root folder. With the modified folder structure, imports need to be adjusted to access the correct modules from the src/ directory.

Suggestion to Add requirements.txt:
I suggest to add the requirements.txt file in the next commit to specify the required dependencies for the project. This will help ensure a consistent environment across all development setups, especially since there may be differences in the environment configuration. The requirements.txt file will allow anyone working on the project to install the exact versions of dependencies necessary for the code to run smoothly, reducing the risk of issues caused by environment differences or missing packages.

Next Steps:

  • I will implement the rest of unit tests in TestConstraintsMethods Class in the next few commits.
  • Please review the aforementioned changes, and feel free to let me know if there is any misunderstanding or improvement I can do.

Thanks!

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