Skip to content

Commit 6a9419a

Browse files
authored
Merge pull request #284 from bobleesj/invalid-tests
Add two test cases for init DiffractionObject with empty `xarray` and `yarray` values
2 parents 5a31716 + b865a8e commit 6a9419a

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

news/valid-empty-do.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* unit tests for initializing DiffractionObject with empty array in xarray and yarray
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

tests/test_diffraction_objects.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,61 @@ def test_dump(tmp_path, mocker):
468468

469469

470470
@pytest.mark.parametrize(
471-
"do_init_args, expected_do_dict, divide_by_zero_warning_expected",
471+
"do_init_args, expected_do_dict, divide_by_zero_warning_expected, wavelength_warning_expected",
472472
[
473473
# Test __dict__ of DiffractionObject instance initialized with valid arguments
474-
( # C1: Minimum arguments provided for init, expect all attributes set without None
474+
( # C1: Instantiate DO with empty arrays, expect it to be a valid DO, but with everything empty
475+
{
476+
"xarray": np.empty(0),
477+
"yarray": np.empty(0),
478+
"xtype": "tth",
479+
},
480+
{
481+
"_all_arrays": np.array([]),
482+
"_input_xtype": "tth",
483+
"metadata": {},
484+
"name": "",
485+
"scat_quantity": "",
486+
"qmin": np.float64(np.inf),
487+
"qmax": np.float64(0.0),
488+
"tthmin": np.float64(np.inf),
489+
"tthmax": np.float64(0.0),
490+
"dmin": np.float64(np.inf),
491+
"dmax": np.float64(0.0),
492+
"wavelength": None,
493+
},
494+
False,
495+
True,
496+
),
497+
( # C2: Instantiate just DO with empty array like in C1 but with wavelength, xtype, name, and metadata
498+
# expect a valid DO with empty arrays, but with some non-array attributes
499+
{
500+
"xarray": np.empty(0),
501+
"yarray": np.empty(0),
502+
"xtype": "tth",
503+
"name": "test_name",
504+
"wavelength": 1.54,
505+
"metadata": {"item_1": "1", "item_2": "2"},
506+
},
507+
{
508+
"_all_arrays": np.array([]),
509+
"_input_xtype": "tth",
510+
"metadata": {"item_1": "1", "item_2": "2"},
511+
"name": "test_name",
512+
"scat_quantity": "",
513+
"qmin": np.float64(np.inf),
514+
"qmax": np.float64(0.0),
515+
"tthmin": np.float64(np.inf),
516+
"tthmax": np.float64(0.0),
517+
"dmin": np.float64(np.inf),
518+
"dmax": np.float64(0.0),
519+
"wavelength": 1.54,
520+
},
521+
False,
522+
False,
523+
),
524+
( # C3: Minimum arguments provided for init with non-empty values for xarray and yarray and wavelength
525+
# expect all attributes set without None
475526
{
476527
"xarray": np.array([0.0, 90.0, 180.0]),
477528
"yarray": np.array([1.0, 2.0, 3.0]),
@@ -499,8 +550,9 @@ def test_dump(tmp_path, mocker):
499550
"wavelength": 4.0 * np.pi,
500551
},
501552
True,
553+
False,
502554
),
503-
( # C2: Initialize with an optional scat_quantity argument, expect non-empty string for scat_quantity
555+
( # C4: Same as C3, but with an optional scat_quantity argument, expect non-empty string for scat_quantity
504556
{
505557
"xarray": np.array([np.inf, 2 * np.sqrt(2) * np.pi, 2 * np.pi]),
506558
"yarray": np.array([1.0, 2.0, 3.0]),
@@ -529,13 +581,23 @@ def test_dump(tmp_path, mocker):
529581
"wavelength": 4.0 * np.pi,
530582
},
531583
False,
584+
False,
532585
),
533586
],
534587
)
535-
def test_init_valid(do_init_args, expected_do_dict, divide_by_zero_warning_expected):
588+
def test_init_valid(
589+
do_init_args,
590+
expected_do_dict,
591+
divide_by_zero_warning_expected,
592+
wavelength_warning_expected,
593+
wavelength_warning_msg,
594+
):
536595
if divide_by_zero_warning_expected:
537596
with pytest.warns(RuntimeWarning, match="divide by zero encountered in divide"):
538597
actual_do_dict = DiffractionObject(**do_init_args).__dict__
598+
elif wavelength_warning_expected:
599+
with pytest.warns(UserWarning, match=re.escape(wavelength_warning_msg)):
600+
actual_do_dict = DiffractionObject(**do_init_args).__dict__
539601
else:
540602
actual_do_dict = DiffractionObject(**do_init_args).__dict__
541603
diff = DeepDiff(

0 commit comments

Comments
 (0)