CenterForTheBuiltEnvironment / pythermalcomfort

Package to calculate several thermal comfort indices (e.g. PMV, PPD, SET, adaptive) and convert physical variables.
https://pythermalcomfort.readthedocs.io/en/latest/
MIT License
147 stars 52 forks source link

Write tests for JOS3 model #56

Closed FedericoTartarini closed 1 year ago

FedericoTartarini commented 1 year ago

We should create some tests to validate the output of the JOS3 model. We can create a new file called test_jos3.py inside tests and then write inside there all the tests for the JOS3 functions.

AkihisaNomoto commented 1 year ago

Do you want to test all the functions that JOS3 model uses? Let's discuss this later.

FedericoTartarini commented 1 year ago

You can use ChatGPT to write some tests. They will not be perfect but it will save a lot of time so we can test all the functions.

My prompt to ChatGPT:

write some tests using pytest for this python function:

def bsa_rate(
    height=1.72,
    weight=74.43,
    formula="dubois",
):
    """Calculate the rate of bsa to standard body.

    Parameters
    ----------
    height : float, optional
        Body height [m]. The default is 1.72.
    weight : float, optional
        Body weight [kg]. The default is 74.43.
    formula : str, optional
        The equation name (str) of bsa calculation. Choose a name from "dubois",
        "takahira", "fujimoto", or "kurazumi". The default is "dubois".

    Returns
    -------
    bsa_rate : float
        The ratio of bsa to the standard body [-].
    """
    bsa_all = body_surface_area(
        height=height,
        weight=weight,
        formula=formula,
    )
    return bsa_all / _BSAst.sum()  # The bsa ratio to the standard body (1.87m2)

Its answer:

import pytest
from my_module import bsa_rate

def test_bsa_rate_with_defaults():
    assert bsa_rate() == pytest.approx(1.0)

def test_bsa_rate_with_custom_height_weight():
    assert bsa_rate(height=1.80, weight=80.0) == pytest.approx(1.1904, rel=1e-4)

def test_bsa_rate_with_invalid_formula():
    with pytest.raises(ValueError):
        bsa_rate(formula="invalid_formula_name")

Actually the last test would be very useful and now it is not working since this function body_surface_area has a bit of a problem since it does not return anything if the wrong model is passed. I have then fixed the body_surface_area function`.

I have pushed the changes so you can see the test I have implemented.

This tool is very useful to see which functions of pythermalcomfort have been tested.

AkihisaNomoto commented 1 year ago

Thanks you for navigating me to write a right way to test_function. I haven't used pytest but it is very nice. Let me work on it for a while, and I will get back to you later.