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

testing framework #1

Open
larsbuntemeyer opened this issue May 2, 2024 · 4 comments
Open

testing framework #1

larsbuntemeyer opened this issue May 2, 2024 · 4 comments

Comments

@larsbuntemeyer
Copy link
Contributor

larsbuntemeyer commented May 2, 2024

Just some ideas, could we maybe use a python test framework for data validation? That would be nice, e.g., to generate reports. If we don't want to stop on the first assertion, we could use, e.g.

@larsbuntemeyer
Copy link
Contributor Author

larsbuntemeyer commented May 2, 2024

Example to use subtests:

import unittest

class Car(object):
  def __init__(self, make, model):
    self.make = make
    self.model = make  # Copy and paste error: should be model.
    self.has_seats = True
    self.wheel_count = 3  # Typo: should be 4.

class CarTest(unittest.TestCase):
  def test_init(self):
    make = "Ford"
    model = "Model T"
    car = Car(make=make, model=model)
    with self.subTest(msg='Car.make check'):
        self.assertEqual(car.make, make)
    with self.subTest(msg='Car.model check'):
        self.assertEqual(car.model, model)
    with self.subTest(msg='Car.has_seats check'):
        self.assertTrue(car.has_seats)
    with self.subTest(msg='Car.wheel_count check'):
        self.assertEqual(car.wheel_count, 4)

if __name__ == "__main__":
    unittest.main()

@larsbuntemeyer
Copy link
Contributor Author

examples using pydantic:

from pydantic import BaseModel, field_validator
class User(BaseModel):
    username: str
    password: str
    age: int
    @field_validator('password')
    def password_must_be_strong(cls, v):
        if len(v) < 16:
            raise ValueError('Password must be at least 16 characters long.')
            return v
    @field_validator('username')
    def username_must_be_strong(cls, v):
        if len(v) < 4:
            raise ValueError('Username must be at least 4 characters long.')
            return v

# Validate incoming user_data
user_data = {'username': 'App', 'password': 'password', 'age': 25}
user = User(**user_data)

@larsbuntemeyer
Copy link
Contributor Author

it seems that data validation for n-d array/xarray is not really implemented yet. I guess for now, i will simply work with a logger and logging levels...

@larsbuntemeyer
Copy link
Contributor Author

larsbuntemeyer commented May 29, 2024

using conftest for input parameters with pytest:

import glob


def pytest_addoption(parser):
    parser.addoption(
        "--filename",
        action="store",
        type=str,
        help="list of files to pass to test functions",
    )
    parser.addoption("--cv", action="store", default="default name")


def pytest_generate_tests(metafunc):
    if "filename" in metafunc.fixturenames:
        metafunc.parametrize(
            "filename", glob.glob(str(metafunc.config.getoption("filename")))
        )

run with pytest, e.g.

pytest -s -v cmor_check/tests/test_cmor.py --filename "/work/bb1203/g300046_CMOR/_CMOR/NUKLEUS/output/EUR-11/GERICS/ECMWF-ERA5/evaluation/r1i1p1f1/GERICS-REMO2020/v1/day/tas/v20240402/tas_E    UR-11_ECMWF-ERA5_evaluation_r1i1p1f1_GERICS-REMO2020_v1_day_*" --cv /work/bb1203/g300046_CMOR/cmor-tables/Tables/CORDEX-CMIP6_CV.json --html=report.html --self-contained-html

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

No branches or pull requests

1 participant