Skip to content

Review test functions and group test cases #257

Closed
@bobleesj

Description

@bobleesj

Problem

Originally posted by @sbillinge in #255 (comment)

The majority of code has been listing "cases", but we can further scale our tests by grouping them:

From:

@pytest.mark.parametrize(
    "org_do_args, target_do_args, scale_inputs, expected",
    [
        # Case 1: same x-array and y-array, check offset
        (
            {
                "xarray": np.array([10, 15, 25, 30, 60, 140]),
                "yarray": np.array([2, 3, 4, 5, 6, 7]),
                "xtype": "tth",
                "wavelength": 2 * np.pi,
            },
            {
                "xarray": np.array([10, 15, 25, 30, 60, 140]),
                "yarray": np.array([2, 3, 4, 5, 6, 7]),
                "xtype": "tth",
                "wavelength": 2 * np.pi,
            },
            {
                "q": None,
                "tth": 60,
                "d": None,
                "offset": 2.1,
            },
            {"xtype": "tth", "yarray": np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])},
        ),
        # Case 2: same length x-arrays with exact x-value match
        (
            {
                "xarray": np.array([10, 15, 25, 30, 60, 140]),
                "yarray": np.array([10, 20, 25, 30, 60, 100]),
                "xtype": "tth",
                "wavelength": 2 * np.pi,
            },
            {
                "xarray": np.array([10, 20, 25, 30, 60, 140]),
                "yarray": np.array([2, 3, 4, 5, 6, 7]),
                "xtype": "tth",
                "wavelength": 2 * np.pi,
            },
            {
                "q": None,
                "tth": 60,
                "d": None,
                "offset": 0,
            },
            {"xtype": "tth", "yarray": np.array([1, 2, 2.5, 3, 6, 10])},
        ),
        # Case 3: same length x-arrays with approximate x-value match
        (
            {
                "xarray": np.array([0.12, 0.24, 0.31, 0.4]),
                "yarray": np.array([10, 20, 40, 60]),
                "xtype": "q",
                "wavelength": 2 * np.pi,
            },
            {
                "xarray": np.array([0.14, 0.24, 0.31, 0.4]),
                "yarray": np.array([1, 3, 4, 5]),
                "xtype": "q",
                "wavelength": 2 * np.pi,
            },
            {
                "q": 0.1,
                "tth": None,
                "d": None,
                "offset": 0,
            },
            {"xtype": "q", "yarray": np.array([1, 2, 4, 6])},
        ),
        # Case 4: different x-array lengths with approximate x-value match
        (
            {
                "xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
                "yarray": np.array([10, 20, 30, 40, 50, 60, 100]),
                "xtype": "tth",
                "wavelength": 2 * np.pi,
            },
            {
                "xarray": np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
                "yarray": np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
                "xtype": "tth",
                "wavelength": 2 * np.pi,
            },
            {
                "q": None,
                "tth": 60,
                "d": None,
                "offset": 0,
            },
            # Case 5 Scaling factor is calculated at index = 4 (tth=61) for self and index = 5 for target (tth=62)
            {"xtype": "tth", "yarray": np.array([1, 2, 3, 4, 5, 6, 10])},
        ),
    ],
)
...

To:

@pytest.mark.parametrize(
    "wavelength, q, expected_tth",
    [
        # Case 1: Allow empty arrays for q
        # 1. Empty q values, no wavelength, return empty arrays
        (None, np.empty((0)), np.empty((0))),
        # 2. Empty q values, wavelength specified, return empty arrays
        (4 * np.pi, np.empty((0)), np.empty(0)),
       
        # Case 2: Allow wavelength to be missing.
        # Valid q values, no wavelength, return index array
        (
            None,
            np.array([0, 0.2, 0.4, 0.6, 0.8, 1]),
            np.array([0, 1, 2, 3, 4, 5]),
        ),

        # Case 3: Correctly specified q and wavelength
        # Expected tth values are 2*arcsin(q) in degrees
        (4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0]), np.array([0, 90.0, 180.0])),
    ],
)

Proposed solution

For all test functions diffpy.utils, review each test function and group test cases.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions