From 7688cb4094eaf4ae106d031012b0b582525a86cb Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Wed, 25 Dec 2024 12:05:26 -0500 Subject: [PATCH] Use compact test comment style for each case under @pytest.mark.parametrize --- news/test-func-format-compact.rst | 23 ++++++++++++ tests/test_diffraction_objects.py | 59 +++++++++++-------------------- tests/test_transforms.py | 46 ++++++++++-------------- 3 files changed, 61 insertions(+), 67 deletions(-) create mode 100644 news/test-func-format-compact.rst diff --git a/news/test-func-format-compact.rst b/news/test-func-format-compact.rst new file mode 100644 index 00000000..ecb92ac0 --- /dev/null +++ b/news/test-func-format-compact.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* test comment format with compact style without extra line for each comment + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index 6fa843d8..9dee757d 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -15,8 +15,7 @@ "do_args_1, do_args_2, expected_equality, wavelength_warning_expected", [ # Test when __eq__ returns True and False - # C1: Identical args, expect equality - ( + ( # C1: Identical args, expect equality { "name": "same", "scat_quantity": "x-ray", @@ -38,8 +37,7 @@ True, False, ), - # Different names, expect inequality - ( + ( # C2: Different names, expect inequality { "name": "something", "xtype": "tth", @@ -57,8 +55,7 @@ False, True, ), - # C2: One without wavelength, expect inequality - ( + ( # C3: One without wavelength, expect inequality { "wavelength": 0.71, "xtype": "tth", @@ -75,8 +72,7 @@ False, True, ), - # C3: Different wavelength values, expect inequality - ( + ( # C4: Different wavelength values, expect inequality { "wavelength": 0.71, "xtype": "tth", @@ -94,8 +90,7 @@ False, False, ), - # C4: Different scat_quantity, expect inequality - ( + ( # C5: Different scat_quantity, expect inequality { "scat_quantity": "x-ray", "xtype": "tth", @@ -113,8 +108,7 @@ False, True, ), - # C5: Different q xarray values, expect inequality - ( + ( # C6: Different q xarray values, expect inequality { "xtype": "q", "xarray": np.array([1.0, 2.0]), @@ -129,8 +123,7 @@ False, True, ), - # C6: Different metadata, expect inequality - ( + ( # C7: Different metadata, expect inequality { "xtype": "q", "xarray": np.empty(0), @@ -167,8 +160,8 @@ def test_diffraction_objects_equality( # Test whether on_xtype returns the correct xarray values. # C1: tth to tth, expect no change in xarray value # 1. "tth" provided, expect tth - # 2. "2theta" provided, expect tth ("tth", np.array([30, 60])), + # 2. "2theta" provided, expect tth ("2theta", np.array([30, 60])), # C2: "q" provided, expect q converted from tth ("q", np.array([0.51764, 1])), @@ -198,8 +191,7 @@ def test_init_invalid_xtype(): "org_do_args, target_do_args, scale_inputs, expected", [ # Test whether scale_to() scales to the expected values - # C1: Same x-array and y-array with 2.1 offset, expect yarray shifted by 2.1 offset - ( + ( # C1: Same x-array and y-array with 2.1 offset, expect yarray shifted by 2.1 offset { "xarray": np.array([10, 15, 25, 30, 60, 140]), "yarray": np.array([2, 3, 4, 5, 6, 7]), @@ -220,8 +212,7 @@ def test_init_invalid_xtype(): }, {"xtype": "tth", "yarray": np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])}, ), - # C2: Same length x-arrays with exact x-value match - ( + ( # C2: 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]), @@ -242,8 +233,7 @@ def test_init_invalid_xtype(): }, {"xtype": "tth", "yarray": np.array([1, 2, 2.5, 3, 6, 10])}, ), - # C3: Same length x-arrays with approximate x-value match - ( + ( # C3: 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]), @@ -264,8 +254,7 @@ def test_init_invalid_xtype(): }, {"xtype": "q", "yarray": np.array([1, 2, 4, 6])}, ), - # C4: Different x-array lengths with approximate x-value match - ( + ( # C4: 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]), @@ -303,8 +292,7 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected): "org_do_args, target_do_args, scale_inputs", [ # Test expected errors produced from scale_to() with invalid inputs - # C1: none of q, tth, d, provided, expect ValueError - ( + ( # C1: none of q, tth, d, provided, expect ValueError { "xarray": np.array([0.1, 0.2, 0.3]), "yarray": np.array([1, 2, 3]), @@ -324,8 +312,7 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected): "offset": 0, }, ), - # C2: tth and d both provided, expect ValueErrort - ( + ( # C2: tth and d both provided, expect ValueErrort { "xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]), "yarray": np.array([10, 20, 30, 40, 50, 60, 100]), @@ -366,8 +353,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs): "do_args, get_array_index_inputs, expected_index", [ # Test get_array_index() returns the expected index given xtype and value - # C1: Target value is in the xarray and xtype is identical, expect exact index match - ( + ( # C1: Target value is in the xarray and xtype is identical, expect exact index match { "wavelength": 4 * np.pi, "xarray": np.array([30.005, 60]), @@ -380,8 +366,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs): }, [0], ), - # C2: Target value lies in the array, expect the (first) closest index - ( + ( # C2: Target value lies in the array, expect the (first) closest index { "wavelength": 4 * np.pi, "xarray": np.array([30, 60]), @@ -408,8 +393,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs): [0], ), # C3: Target value out of the range, expect the closest index - # 1. Test with xtype of "q" - ( + ( # 1. Test with xtype of "q" { "wavelength": 4 * np.pi, "xarray": np.array([0.25, 0.5, 0.71]), @@ -422,8 +406,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs): }, [0], ), - # 2. Test with xtype of "tth" - ( + ( # 2. Test with xtype of "tth" { "wavelength": 4 * np.pi, "xarray": np.array([30, 60]), @@ -488,8 +471,7 @@ def test_dump(tmp_path, mocker): "do_init_args, expected_do_dict, divide_by_zero_warning_expected", [ # Test __dict__ of DiffractionObject instance initialized with valid arguments - ( - # C1: Minimum arguments provided for init, expect all attributes set without None + ( # C1: Minimum arguments provided for init, expect all attributes set without None { "xarray": np.array([0.0, 90.0, 180.0]), "yarray": np.array([1.0, 2.0, 3.0]), @@ -518,8 +500,7 @@ def test_dump(tmp_path, mocker): }, True, ), - # C2: Initialize with an optional scat_quantity argument, expect non-empty string for scat_quantity - ( + ( # C2: Initialize with an optional scat_quantity argument, expect non-empty string for scat_quantity { "xarray": np.array([np.inf, 2 * np.sqrt(2) * np.pi, 2 * np.pi]), "yarray": np.array([1.0, 2.0, 3.0]), diff --git a/tests/test_transforms.py b/tests/test_transforms.py index ff202384..60f87c2e 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -16,14 +16,16 @@ # 2. No wavelength provided, expected empty array of tth and wavelength UserWarning (None, np.empty((0)), np.empty((0))), # C2: Use non-empty q values to compute tth with or without wavelength - # 1. No wavelength provided, expect valid tth values in degrees with wavelength UserWarning - ( + ( # 1. No wavelength provided, expect valid tth values in degrees with wavelength UserWarning None, np.array([0, 0.2, 0.4, 0.6, 0.8, 1]), np.array([0, 1, 2, 3, 4, 5]), ), - # 2. Wavelength provided, expect tth values of 2*arcsin(q) in degrees - (4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0]), np.array([0, 90.0, 180.0])), + ( # 2. Wavelength provided, expect tth values of 2*arcsin(q) in degrees + 4 * np.pi, + np.array([0, 1 / np.sqrt(2), 1.0]), + np.array([0, 90.0, 180.0]), + ), ], ) def test_q_to_tth(wavelength, q, expected_tth, wavelength_warning_msg): @@ -39,14 +41,12 @@ def test_q_to_tth(wavelength, q, expected_tth, wavelength_warning_msg): "wavelength, q, expected_error_type", [ # Test ValeuError in q to tth conversion with invalid two-theta values. - # C1: Invalid q values that result in tth > 180 degrees, expect ValueError - ( + ( # C1: Invalid q values that result in tth > 180 degrees, expect ValueError 4 * np.pi, np.array([0.2, 0.4, 0.6, 0.8, 1, 1.2]), ValueError, ), - # C2: Wrong wavelength that results in tth > 180 degrees, expect ValueError - ( + ( # C2: Wrong wavelength that results in tth > 180 degrees, expect ValueError 100, np.array([0, 0.2, 0.4, 0.6, 0.8, 1]), ValueError, @@ -69,14 +69,12 @@ def test_q_to_tth_bad(wavelength, q, expected_error_type, invalid_q_or_d_or_wave # 2. No wavelength provided, expected empty array of q and wavelength UserWarning (4 * np.pi, np.array([]), np.array([])), # C2: Use non-empty tth values between 0-180 degrees to compute q, with or without wavelength - # 1. No wavelength provided, expect valid q values between 0-1 - ( + ( # 1. No wavelength provided, expect valid q values between 0-1 None, np.array([0, 30, 60, 90, 120, 180]), np.array([0, 1, 2, 3, 4, 5]), ), - # 2. Wavelength provided, expect expected q values are sin15, sin30, sin45, sin60, sin90 - ( + ( # 2. Wavelength provided, expect expected q values are sin15, sin30, sin45, sin60, sin90 4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0]), np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]), @@ -97,15 +95,13 @@ def test_tth_to_q(wavelength, tth, expected_q, wavelength_warning_msg): "wavelength, tth, expected_error_type, expected_error_msg", [ # C1: Invalid tth value of > 180 degrees provided, with or without wavelength - # 1. No wavelength provided, expect two theta ValueError - ( + ( # 1. No wavelength provided, expect two theta ValueError None, np.array([0, 30, 60, 90, 120, 181]), ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors.", ), - # 2. Wavelength provided, expect two theta ValueError - ( + ( # 2. Wavelength provided, expect two theta ValueError 4 * np.pi, np.array([0, 30, 60, 90, 120, 181]), ValueError, @@ -125,14 +121,12 @@ def test_tth_to_q_bad(wavelength, tth, expected_error_type, expected_error_msg): # C1: Empty q values, expect empty d values (np.array([]), np.array([]), False), # C2: - # 1. Valid q values, expect d values without warning - ( + ( # 1. Valid q values, expect d values without warning np.array([0.1, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi]), np.array([62.83185307, 2, 1, 0.66667, 0.5, 0.4]), False, ), - # 2. Valid q values containing 0, expect d values with divide by zero warning - ( + ( # 2. Valid q values containing 0, expect d values with divide by zero warning np.array([0, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi]), np.array([np.inf, 2, 1, 0.66667, 0.5, 0.4]), True, @@ -180,8 +174,7 @@ def test_d_to_q(d, expected_q, zero_divide_error_expected): (4 * np.pi, np.array([]), np.array([]), False), # C3: User specified valid tth values between 0-180 degrees (without wavelength) (None, np.array([0, 30, 60, 90, 120, 180]), np.array([0, 1, 2, 3, 4, 5]), False), - # C4: User specified valid tth values between 0-180 degrees (with wavelength) - ( + ( # C4: User specified valid tth values between 0-180 degrees (with wavelength) 4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0]), np.array([np.inf, 24.27636, 12.56637, 8.88577, 7.25520, 6.28319]), @@ -204,15 +197,13 @@ def test_tth_to_d(wavelength, tth, expected_d, divide_by_zero_warning_expected, @pytest.mark.parametrize( "wavelength, tth, expected_error_type, expected_error_msg", [ - # C1: Invalid tth value of > 180 degrees, no wavelength, expect two theta ValueError - ( + ( # C1: Invalid tth value of > 180 degrees, no wavelength, expect two theta ValueError None, np.array([0, 30, 60, 90, 120, 181]), ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors.", ), - # C2: Invalid tth value of > 180 degrees with wavelength, expect two theta ValueError - ( + ( # C2: Invalid tth value of > 180 degrees with wavelength, expect two theta ValueError 4 * np.pi, np.array([0, 30, 60, 90, 120, 181]), ValueError, @@ -234,8 +225,7 @@ def test_tth_to_d_invalid(wavelength, tth, expected_error_type, expected_error_m (4 * np.pi, np.empty((0)), np.empty(0), False), # C3: Valid d values, no wavelength, expect valid and non-empty tth values (None, np.array([1, 0.8, 0.6, 0.4, 0.2, 0]), np.array([0, 1, 2, 3, 4, 5]), True), - # C4: Valid d values with wavelength, expect valid and non-empty thh values - ( + ( # C4: Valid d values with wavelength, expect valid and non-empty thh values 4 * np.pi, np.array([4 * np.pi, 4 / np.sqrt(2) * np.pi, 4 / np.sqrt(3) * np.pi]), np.array([60.0, 90.0, 120.0]),